Browse Source

Show raw accel data

Thomas Dy 7 years ago
parent
commit
c38c999b67
2 changed files with 51 additions and 4 deletions
  1. 21 4
      device.js
  2. 30 0
      index.js

+ 21 - 4
device.js

@@ -74,17 +74,33 @@ let connStatus$ = connection$.combineLatest(serial$, (conn, serial) => {
   }
 }).share()
 
-let xAccel$ = services$
+function makeAccelStream(characteristic) {
+  return Rx.Observable.fromEvent(characteristic, 'data')
+    .map(d => d[3] * 256 * 256 * 256 + d[2] * 256 * 256 + d[1] * 256 + d[0])
+    .startWith(-1)
+}
+
+let accel$ = services$
   .do(s => {
     s[1].characteristics[0].write(ACCEL_ON);
     s[1].characteristics[2].subscribe();
+    s[1].characteristics[3].subscribe();
+    s[1].characteristics[4].subscribe();
   })
   .flatMap(s => {
-    return Rx.Observable.fromEvent(s[1].characteristics[2], 'data');
+    let characteristics = s[1].characteristics;
+    return Rx.Observable.combineLatest(
+      makeAccelStream(characteristics[2]),
+      makeAccelStream(characteristics[3]),
+      makeAccelStream(characteristics[4])
+    )
   })
-  .map(d => d[1] * 256 + d[0])
   .share()
 
+let xAccel$ = accel$
+  .map(r => r[0])
+  .filter(v => v >= 0)
+
 function item(type, value) {
   return { type, value }
 }
@@ -114,5 +130,6 @@ let weight$ = xAccel$
 module.exports = {
   button$,
   connStatus$,
-  weight$
+  weight$,
+  accel$
 }

+ 30 - 0
index.js

@@ -57,6 +57,25 @@ let helpBox = blessed.box({
 
 screen.append(helpBox);
 
+let dataTable = blessed.Table({
+  bottom: 1,
+  right: 0,
+  width: 19,
+  height: 'shrink',
+  border: {
+    type: 'line'
+  },
+  data: [
+    [ 'Accel', 'Value' ],
+    [ 'X', '-1' ],
+    [ 'Y', '-1' ],
+    [ 'Z', '-1' ]
+  ]
+
+});
+
+screen.append(dataTable);
+
 screen.key(['escape', 'q', 'C-c'], function(ch, key) {
   process.exit(0);
 });
@@ -84,3 +103,14 @@ device.connStatus$.subscribe(s => {
   }
   screen.render();
 })
+
+device.accel$.subscribe(s => {
+  let data = [
+    [ 'Accel', 'Value' ],
+    [ 'X', ''+s[0] ],
+    [ 'Y', ''+s[1] ],
+    [ 'Z', ''+s[2] ]
+  ]
+  dataTable.setData(data);
+  screen.render();
+})