Browse Source

Initial commit

Thomas Dy 7 years ago
commit
c770c72f42
3 changed files with 75 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 59 0
      index.js
  3. 15 0
      package.json

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+node_modules

+ 59 - 0
index.js

@@ -0,0 +1,59 @@
+let noble = require('noble');
+
+const ACCEL_OFF = Buffer.from([0x00]);
+const ACCEL_ON = Buffer.from([0x01]);
+
+// MAGIC CONSTANTS FOLLOW
+// these might be different for other devices, I wouldn't know
+const IDENTIFIER = 'CHSLEEV_00';
+const DEVICE_INFO_UUID = '180a';
+const ACCEL_UUID = 'ffa0';
+const TARE = 41690;
+const RATIO = 14;
+// MAGIC CONSTANTS END
+
+noble.on('stateChange', function(state) {
+  if(state === 'poweredOn') {
+    noble.startScanning();
+  }
+  else {
+    noble.stopScanning();
+  }
+});
+
+noble.on('discover', function(peripheral) {
+  if(peripheral.advertisement.localName === 'CHSLEEV_00') {
+    noble.stopScanning();
+
+    console.log('Prep Pad found with address '+peripheral.address);
+
+    peripheral.on('disconnect', function() {
+      console.log('Disconnecting from Prep Pad '+peripheral.address);
+      process.exit(0);
+    });
+
+    peripheral.connect(function(error) {
+      console.log('Connected to Prep Pad');
+      peripheral.discoverSomeServicesAndCharacteristics([DEVICE_INFO_UUID, ACCEL_UUID], [], function(error, services) {
+        if(services.length != 2) {
+          console.log('Could not find the relevant services. This might not actually be a Prep Pad');
+          peripheral.disconnect();
+        }
+        else {
+          let information = services[0];
+          let accelerometer = services[1];
+          information.characteristics[2].read(function(error, data) {
+            console.log('Serial Number: '+data);
+          });
+          accelerometer.characteristics[0].write(ACCEL_ON);
+          accelerometer.characteristics[2].subscribe();
+          accelerometer.characteristics[2].on('data', function(data, isNotification) {
+            let value = data[1] * 256 + data[0];
+            let grams = Math.floor((TARE - value) / RATIO);
+            console.log(grams+'g');
+          });
+        }
+      });
+    });
+  }
+});

+ 15 - 0
package.json

@@ -0,0 +1,15 @@
+{
+  "name": "openpreppad",
+  "version": "0.0.1",
+  "description": "TUI for Prep Pad",
+  "main": "index.js",
+  "dependencies": {
+    "noble": "^1.7.0",
+  ,
+  "devDependencies": {},
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "author": "Thomas Dy <thatsmydoing@gmail.com>",
+  "license": "ISC"
+}