├── .gitignore ├── LICENSE.txt ├── README.md ├── config.js ├── index.js ├── lib ├── agent.js ├── armband │ └── armband.js ├── communicator │ ├── ConcurrencyTest.js │ └── communicator.js ├── myoProtocol.js └── util │ ├── deserialise.js │ ├── eulerAngle.js │ └── serialise.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/* 2 | /www/* 3 | /out/* 4 | /.idea/* 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Manuel Overdijk 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MYO Bluetooth communication library 2 | 3 | A bluetooth communication library for the myo armband, using Node and Noble and written in Javascript. **It doesn't require the Myo Connect software or the Myo SDK**, but communicates directly over bluetooth by using the released bluetooth spec. 4 | 5 | For a demo build with this library, see https://www.youtube.com/watch?v=R19rKXpttjY The demo application will be published later. 6 | 7 | ## Install 8 | This library uses [Noble](https://github.com/sandeepmistry/noble) v0.3.8, which currently only supports Mac OS X and Linux. Checkout Noble to find out it's prerequisites and installation guidance. 9 | 10 | I have not yet published this repository on npm, but will do eventually. Install via npm: 11 | 12 | ``` npm install git://github.com/manueloverdijk/myonodebluetooth.git ``` 13 | 14 | As the project is written with some of the new javascript features of ES6, compile the project with traceur or use [io.js](https://github.com/iojs/io.js) with the following flags: 15 | 16 | ``` --use_strict --es_staging --harmony_classes --harmony_arrow_functions``` 17 | 18 | ## Usage 19 | 20 | The following is an example which starts discovering myo armbands and connects to the first available armband. Wait for the ```ready event``` after calling ```initStart()``` before reading and writing to characteristics of the Myo. 21 | 22 | ``` javascript 23 | var MyoBluetooth = require('MyoNodeBluetooth'); 24 | var MyoAgent = new MyoBluetooth(); 25 | 26 | 27 | MyoAgent.on('discovered', function(armband){ 28 | armband.on('connect', function(connected){ 29 | 30 | // armband connected succesfully 31 | if(connected){ 32 | // discover all services/characteristics and enable emg/imu/classifier chars 33 | this.initStart(); 34 | } else { 35 | // armband disconnected 36 | } 37 | 38 | }); 39 | 40 | // Armband receives the ready event when all services/characteristics are discovered and emg/imu/classifier mode is enabled 41 | armband.on('ready', function(){ 42 | 43 | // register for events 44 | armband.on('batteryInfo',function(data){ 45 | console.log('BatteryInfo: ', data.batteryLevel); 46 | }); 47 | 48 | // read or write data from/to the Myo 49 | armband.readBatteryInfo(); 50 | }); 51 | 52 | armband.connect(); 53 | }); 54 | ``` 55 | 56 | ## Supported features 57 | ### Agent 58 | 59 | Get notified of newly discovered Myo armbands. 60 | 61 | ``` javascript 62 | Agent.on('discovered', function(armband){ 63 | ... 64 | }); 65 | ``` 66 | 67 | Get notified of bluetooth adapter state changes, see Noble for more information. 68 | 69 | ``` javascript 70 | Agent.on('stateChange', function(state){ 71 | ... 72 | }); 73 | ``` 74 | 75 | 76 | ### Armband 77 | 78 | Discover all services/characteristics and enable the EMG, IMU and Classifier Characteristics to notify/indicate the communicator of new events. The Myo will also be set in no sleep mode and unlocked until disconnection. 79 | 80 | ``` javascript 81 | Armband.initStart(); 82 | ``` 83 | 84 | Wait for the ready event after calling initStart(), after which the Myo is ready to read/write and notify characteristics. 85 | 86 | ```javascript 87 | Armband.on('ready', function(){ 88 | ... 89 | }) 90 | ``` 91 | #### Reading characteristics 92 | ##### EMG data 93 | Get notified of EMG data (which the Myo sends after calling ```initStart()```) 94 | 95 | ```javascript 96 | /* 97 | data = { 98 | sample1: Array[8], 99 | sample2: Array[8] 100 | } 101 | */ 102 | Armband.on('emg', function(data){ 103 | 104 | ... 105 | }); 106 | ``` 107 | 108 | ##### Orientation data 109 | 110 | Get notified of orientation data (which the Myo sends after calling ```initStart()```) 111 | 112 | ```javascript 113 | /* 114 | data = { 115 | w, x, y, z 116 | } 117 | */ 118 | Armband.on('orientation', function(data){ 119 | ... 120 | }); 121 | ``` 122 | 123 | ##### Accelerometer data 124 | 125 | Get notified of accelerometer data (which the Myo sends after calling ```initStart()```) 126 | 127 | ```javascript 128 | //data = Array[3] 129 | Armband.on('accelerometer', function(data){ 130 | ... 131 | }); 132 | ``` 133 | 134 | ##### Gyroscope data 135 | 136 | Get notified of gyroscope data (which the Myo sends after calling ```initStart()```) 137 | 138 | ```javascript 139 | //data = Array[3] 140 | Armband.on('gyroscope', function(data){ 141 | ... 142 | }); 143 | ``` 144 | 145 | ##### Pose events 146 | 147 | Get notified of pose events (which the Myo sends after calling ```initStart()```) 148 | 149 | ```javascript 150 | /* data = { 151 | type: rest | fist | waveIn | waveOut | spread | tap | unkown 152 | } */ 153 | Armband.on('pose', function(data){ 154 | ... 155 | }); 156 | ``` 157 | 158 | ##### Sync events 159 | 160 | Get notified of sync events (which the Myo sends after calling ```initStart()```) 161 | 162 | ```javascript 163 | // Synced true | false | failed 164 | Armband.on('sync', function(Boolean){ 165 | ... 166 | }); 167 | ``` 168 | 169 | ##### Unlock events 170 | 171 | Get notified of unlock events (which the Myo sends after calling ```initStart()```) 172 | 173 | ```javascript 174 | // Unlocked true | false 175 | Armband.on('unlocked', function(Boolean){ 176 | ... 177 | }); 178 | ``` 179 | 180 | ##### Read info 181 | 182 | Read the Myo info characteristic 183 | 184 | ```javascript 185 | Armband.on('info', function(data){ 186 | ... 187 | }); 188 | Armband.readInfo(); 189 | ``` 190 | 191 | ##### Read version 192 | 193 | Read the Myo version characteristic 194 | 195 | ```javascript 196 | Armband.on('version', function(data){ 197 | ... 198 | }); 199 | Armband.readVersion(); 200 | ``` 201 | 202 | ##### Read batteryInfo 203 | 204 | Read the Myo batteryinfo characteristic 205 | 206 | ```javascript 207 | // data: { batteryLevel } 208 | Armband.on('batteryInfo', function(data){ 209 | ... 210 | }); 211 | Armband.readBatteryInfo(); 212 | ``` 213 | 214 | #### Writing characteristics 215 | 216 | Listen for the command event after executing a write command to the Myo to be notified on results/success 217 | 218 | ```javascript 219 | // data = {type, data} 220 | Armband.on('command', function(data){ 221 | ... 222 | }); 223 | ``` 224 | 225 | Set the sleep mode 226 | 227 | ```javascript 228 | Armband.setSleepMode(Boolean); // Never sleep: true | false 229 | ``` 230 | 231 | Set the unlock mode 232 | 233 | ```javascript 234 | 235 | // mode: 0 -> unlock now and relock after lock command is recieved 236 | // 1 -> re-lock immediatly 237 | // 2 -> unlock now and relock after a fixed timeout 238 | 239 | Armband.setUnlockMode(mode); 240 | ``` 241 | 242 | Set the user action mode, to notify the user that an action has been recognized / confirmed. 243 | ```javascript 244 | Armband.setUserAction(); 245 | ``` 246 | 247 | Set the IMU/EMG/Classifier modes to notify/indicate the communicator for data events 248 | ```javascript 249 | Armband.setMode(); 250 | ``` 251 | 252 | Set the vibrate command 253 | ```javascript 254 | // mode: 0 -> Do not vibrate. 255 | // 1 -> Vibrate for a short amount of time 256 | // 2 -> Vibrate for a medium amount of time 257 | // 3 -> Vibrate for a long amount of time 258 | 259 | Armband.vibrate(mode) 260 | ``` 261 | 262 | ## TODO 263 | * Normalize the gyroscope/accelerometer data, calculate Euler Angles 264 | * Implement extended vibrate command 265 | * Refactor initStart service/characteristic discovery 266 | * Refactor some functions of the deserialisation class 267 | * Fix the unresponsive-classifier state which Myo enters after unsyncing while emitting Classifier events over Bluetooth 268 | 269 | -------------------------------------------------------------------------------- /config.js: -------------------------------------------------------------------------------- 1 | System.config({ 2 | "baseURL": "/", 3 | "paths": { 4 | "*": "*.js", 5 | "github:*": "jspm_packages/github/*.js", 6 | "npm:*": "jspm_packages/npm/*.js" 7 | } 8 | }); 9 | 10 | System.config({ 11 | "map": { 12 | "bluebird": "npm:bluebird@2.9.25", 13 | "debug": "npm:debug@2.2.0", 14 | "events": "npm:events@1.0.2", 15 | "noble": "npm:noble@0.3.14", 16 | "promiseq": "npm:promiseq@0.1.0", 17 | "ws": "npm:ws@0.7.1", 18 | "github:jspm/nodelibs-assert@0.1.0": { 19 | "assert": "npm:assert@1.3.0" 20 | }, 21 | "github:jspm/nodelibs-buffer@0.1.0": { 22 | "buffer": "npm:buffer@3.2.2" 23 | }, 24 | "github:jspm/nodelibs-constants@0.1.0": { 25 | "constants-browserify": "npm:constants-browserify@0.0.1" 26 | }, 27 | "github:jspm/nodelibs-crypto@0.1.0": { 28 | "crypto-browserify": "npm:crypto-browserify@3.9.14" 29 | }, 30 | "github:jspm/nodelibs-events@0.1.0": { 31 | "events-browserify": "npm:events-browserify@0.0.1" 32 | }, 33 | "github:jspm/nodelibs-http@1.7.1": { 34 | "Base64": "npm:Base64@0.2.1", 35 | "events": "github:jspm/nodelibs-events@0.1.0", 36 | "inherits": "npm:inherits@2.0.1", 37 | "stream": "github:jspm/nodelibs-stream@0.1.0", 38 | "url": "github:jspm/nodelibs-url@0.1.0", 39 | "util": "github:jspm/nodelibs-util@0.1.0" 40 | }, 41 | "github:jspm/nodelibs-https@0.1.0": { 42 | "https-browserify": "npm:https-browserify@0.0.0" 43 | }, 44 | "github:jspm/nodelibs-net@0.1.2": { 45 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 46 | "crypto": "github:jspm/nodelibs-crypto@0.1.0", 47 | "http": "github:jspm/nodelibs-http@1.7.1", 48 | "net": "github:jspm/nodelibs-net@0.1.2", 49 | "process": "github:jspm/nodelibs-process@0.1.1", 50 | "stream": "github:jspm/nodelibs-stream@0.1.0", 51 | "timers": "github:jspm/nodelibs-timers@0.1.0", 52 | "util": "github:jspm/nodelibs-util@0.1.0" 53 | }, 54 | "github:jspm/nodelibs-os@0.1.0": { 55 | "os-browserify": "npm:os-browserify@0.1.2" 56 | }, 57 | "github:jspm/nodelibs-path@0.1.0": { 58 | "path-browserify": "npm:path-browserify@0.0.0" 59 | }, 60 | "github:jspm/nodelibs-process@0.1.1": { 61 | "process": "npm:process@0.10.1" 62 | }, 63 | "github:jspm/nodelibs-stream@0.1.0": { 64 | "stream-browserify": "npm:stream-browserify@1.0.0" 65 | }, 66 | "github:jspm/nodelibs-timers@0.1.0": { 67 | "timers-browserify": "npm:timers-browserify@1.4.1" 68 | }, 69 | "github:jspm/nodelibs-tty@0.1.0": { 70 | "tty-browserify": "npm:tty-browserify@0.0.0" 71 | }, 72 | "github:jspm/nodelibs-url@0.1.0": { 73 | "url": "npm:url@0.10.3" 74 | }, 75 | "github:jspm/nodelibs-util@0.1.0": { 76 | "util": "npm:util@0.10.3" 77 | }, 78 | "github:jspm/nodelibs-vm@0.1.0": { 79 | "vm-browserify": "npm:vm-browserify@0.0.4" 80 | }, 81 | "github:jspm/nodelibs-zlib@0.1.0": { 82 | "browserify-zlib": "npm:browserify-zlib@0.1.4" 83 | }, 84 | "npm:asn1.js@1.0.4": { 85 | "assert": "github:jspm/nodelibs-assert@0.1.0", 86 | "bn.js": "npm:bn.js@1.3.0", 87 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 88 | "inherits": "npm:inherits@2.0.1", 89 | "minimalistic-assert": "npm:minimalistic-assert@1.0.0", 90 | "vm": "github:jspm/nodelibs-vm@0.1.0" 91 | }, 92 | "npm:assert@1.3.0": { 93 | "util": "npm:util@0.10.3" 94 | }, 95 | "npm:async@0.9.0": { 96 | "process": "github:jspm/nodelibs-process@0.1.1" 97 | }, 98 | "npm:bindings@1.2.1": { 99 | "fs": "github:jspm/nodelibs-fs@0.1.2", 100 | "path": "github:jspm/nodelibs-path@0.1.0", 101 | "process": "github:jspm/nodelibs-process@0.1.1" 102 | }, 103 | "npm:bluebird@2.3.11": { 104 | "process": "github:jspm/nodelibs-process@0.1.1" 105 | }, 106 | "npm:bluebird@2.9.25": { 107 | "events": "github:jspm/nodelibs-events@0.1.0", 108 | "process": "github:jspm/nodelibs-process@0.1.1" 109 | }, 110 | "npm:bplist-parser@0.0.6": { 111 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 112 | "fs": "github:jspm/nodelibs-fs@0.1.2" 113 | }, 114 | "npm:browserify-aes@1.0.0": { 115 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 116 | "create-hash": "npm:create-hash@1.1.1", 117 | "crypto": "github:jspm/nodelibs-crypto@0.1.0", 118 | "fs": "github:jspm/nodelibs-fs@0.1.2", 119 | "inherits": "npm:inherits@2.0.1", 120 | "stream": "github:jspm/nodelibs-stream@0.1.0", 121 | "systemjs-json": "github:systemjs/plugin-json@0.1.0" 122 | }, 123 | "npm:browserify-rsa@2.0.0": { 124 | "bn.js": "npm:bn.js@1.3.0", 125 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 126 | "constants": "github:jspm/nodelibs-constants@0.1.0", 127 | "crypto": "github:jspm/nodelibs-crypto@0.1.0", 128 | "randombytes": "npm:randombytes@2.0.1" 129 | }, 130 | "npm:browserify-sign@3.0.1": { 131 | "bn.js": "npm:bn.js@1.3.0", 132 | "browserify-rsa": "npm:browserify-rsa@2.0.0", 133 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 134 | "create-hash": "npm:create-hash@1.1.1", 135 | "create-hmac": "npm:create-hmac@1.1.3", 136 | "crypto": "github:jspm/nodelibs-crypto@0.1.0", 137 | "elliptic": "npm:elliptic@1.0.1", 138 | "inherits": "npm:inherits@2.0.1", 139 | "parse-asn1": "npm:parse-asn1@3.0.0", 140 | "stream": "github:jspm/nodelibs-stream@0.1.0", 141 | "systemjs-json": "github:systemjs/plugin-json@0.1.0" 142 | }, 143 | "npm:browserify-zlib@0.1.4": { 144 | "assert": "github:jspm/nodelibs-assert@0.1.0", 145 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 146 | "pako": "npm:pako@0.2.6", 147 | "process": "github:jspm/nodelibs-process@0.1.1", 148 | "readable-stream": "npm:readable-stream@1.1.13", 149 | "util": "github:jspm/nodelibs-util@0.1.0" 150 | }, 151 | "npm:buffer@3.2.2": { 152 | "base64-js": "npm:base64-js@0.0.8", 153 | "ieee754": "npm:ieee754@1.1.5", 154 | "is-array": "npm:is-array@1.0.1" 155 | }, 156 | "npm:bufferutil@1.0.1": { 157 | "bindings": "npm:bindings@1.2.1", 158 | "nan": "npm:nan@1.6.2" 159 | }, 160 | "npm:constants-browserify@0.0.1": { 161 | "systemjs-json": "github:systemjs/plugin-json@0.1.0" 162 | }, 163 | "npm:core-util-is@1.0.1": { 164 | "buffer": "github:jspm/nodelibs-buffer@0.1.0" 165 | }, 166 | "npm:create-ecdh@2.0.0": { 167 | "bn.js": "npm:bn.js@1.3.0", 168 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 169 | "crypto": "github:jspm/nodelibs-crypto@0.1.0", 170 | "elliptic": "npm:elliptic@1.0.1" 171 | }, 172 | "npm:create-hash@1.1.1": { 173 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 174 | "crypto": "github:jspm/nodelibs-crypto@0.1.0", 175 | "fs": "github:jspm/nodelibs-fs@0.1.2", 176 | "inherits": "npm:inherits@2.0.1", 177 | "ripemd160": "npm:ripemd160@1.0.1", 178 | "sha.js": "npm:sha.js@2.4.0", 179 | "stream": "github:jspm/nodelibs-stream@0.1.0" 180 | }, 181 | "npm:create-hmac@1.1.3": { 182 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 183 | "create-hash": "npm:create-hash@1.1.1", 184 | "crypto": "github:jspm/nodelibs-crypto@0.1.0", 185 | "inherits": "npm:inherits@2.0.1", 186 | "stream": "github:jspm/nodelibs-stream@0.1.0" 187 | }, 188 | "npm:crypto-browserify@3.9.14": { 189 | "browserify-aes": "npm:browserify-aes@1.0.0", 190 | "browserify-sign": "npm:browserify-sign@3.0.1", 191 | "create-ecdh": "npm:create-ecdh@2.0.0", 192 | "create-hash": "npm:create-hash@1.1.1", 193 | "create-hmac": "npm:create-hmac@1.1.3", 194 | "diffie-hellman": "npm:diffie-hellman@3.0.1", 195 | "inherits": "npm:inherits@2.0.1", 196 | "pbkdf2": "npm:pbkdf2@3.0.4", 197 | "public-encrypt": "npm:public-encrypt@2.0.0", 198 | "randombytes": "npm:randombytes@2.0.1" 199 | }, 200 | "npm:debug@0.7.4": { 201 | "process": "github:jspm/nodelibs-process@0.1.1", 202 | "tty": "github:jspm/nodelibs-tty@0.1.0" 203 | }, 204 | "npm:debug@2.2.0": { 205 | "fs": "github:jspm/nodelibs-fs@0.1.2", 206 | "ms": "npm:ms@0.7.1", 207 | "net": "github:jspm/nodelibs-net@0.1.2", 208 | "process": "github:jspm/nodelibs-process@0.1.1", 209 | "tty": "github:jspm/nodelibs-tty@0.1.0", 210 | "util": "github:jspm/nodelibs-util@0.1.0" 211 | }, 212 | "npm:diffie-hellman@3.0.1": { 213 | "bn.js": "npm:bn.js@1.3.0", 214 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 215 | "crypto": "github:jspm/nodelibs-crypto@0.1.0", 216 | "miller-rabin": "npm:miller-rabin@1.1.5", 217 | "process": "github:jspm/nodelibs-process@0.1.1", 218 | "randombytes": "npm:randombytes@2.0.1", 219 | "systemjs-json": "github:systemjs/plugin-json@0.1.0" 220 | }, 221 | "npm:elliptic@1.0.1": { 222 | "bn.js": "npm:bn.js@1.3.0", 223 | "brorand": "npm:brorand@1.0.5", 224 | "hash.js": "npm:hash.js@1.0.2", 225 | "inherits": "npm:inherits@2.0.1", 226 | "systemjs-json": "github:systemjs/plugin-json@0.1.0" 227 | }, 228 | "npm:events-browserify@0.0.1": { 229 | "process": "github:jspm/nodelibs-process@0.1.1" 230 | }, 231 | "npm:hash.js@1.0.2": { 232 | "inherits": "npm:inherits@2.0.1" 233 | }, 234 | "npm:https-browserify@0.0.0": { 235 | "http": "github:jspm/nodelibs-http@1.7.1" 236 | }, 237 | "npm:inherits@2.0.1": { 238 | "util": "github:jspm/nodelibs-util@0.1.0" 239 | }, 240 | "npm:miller-rabin@1.1.5": { 241 | "bn.js": "npm:bn.js@1.3.0", 242 | "brorand": "npm:brorand@1.0.5" 243 | }, 244 | "npm:nan@1.6.2": { 245 | "path": "github:jspm/nodelibs-path@0.1.0" 246 | }, 247 | "npm:nan@1.8.4": { 248 | "path": "github:jspm/nodelibs-path@0.1.0" 249 | }, 250 | "npm:noble@0.3.14": { 251 | "bplist-parser": "npm:bplist-parser@0.0.6", 252 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 253 | "child_process": "github:jspm/nodelibs-child_process@0.1.0", 254 | "debug": "npm:debug@0.7.4", 255 | "events": "github:jspm/nodelibs-events@0.1.0", 256 | "os": "github:jspm/nodelibs-os@0.1.0", 257 | "process": "github:jspm/nodelibs-process@0.1.1", 258 | "systemjs-json": "github:systemjs/plugin-json@0.1.0", 259 | "util": "github:jspm/nodelibs-util@0.1.0", 260 | "xpc-connection": "npm:xpc-connection@0.1.3" 261 | }, 262 | "npm:options@0.0.6": { 263 | "fs": "github:jspm/nodelibs-fs@0.1.2" 264 | }, 265 | "npm:os-browserify@0.1.2": { 266 | "os": "github:jspm/nodelibs-os@0.1.0" 267 | }, 268 | "npm:pako@0.2.6": { 269 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 270 | "process": "github:jspm/nodelibs-process@0.1.1" 271 | }, 272 | "npm:parse-asn1@3.0.0": { 273 | "asn1.js": "npm:asn1.js@1.0.4", 274 | "browserify-aes": "npm:browserify-aes@1.0.0", 275 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 276 | "create-hash": "npm:create-hash@1.1.1", 277 | "pbkdf2-compat": "npm:pbkdf2-compat@3.0.2", 278 | "systemjs-json": "github:systemjs/plugin-json@0.1.0" 279 | }, 280 | "npm:path-browserify@0.0.0": { 281 | "process": "github:jspm/nodelibs-process@0.1.1" 282 | }, 283 | "npm:pbkdf2-compat@3.0.2": { 284 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 285 | "child_process": "github:jspm/nodelibs-child_process@0.1.0", 286 | "create-hmac": "npm:create-hmac@1.1.3", 287 | "crypto": "github:jspm/nodelibs-crypto@0.1.0", 288 | "path": "github:jspm/nodelibs-path@0.1.0", 289 | "process": "github:jspm/nodelibs-process@0.1.1", 290 | "systemjs-json": "github:systemjs/plugin-json@0.1.0" 291 | }, 292 | "npm:pbkdf2@3.0.4": { 293 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 294 | "child_process": "github:jspm/nodelibs-child_process@0.1.0", 295 | "create-hmac": "npm:create-hmac@1.1.3", 296 | "crypto": "github:jspm/nodelibs-crypto@0.1.0", 297 | "path": "github:jspm/nodelibs-path@0.1.0", 298 | "process": "github:jspm/nodelibs-process@0.1.1", 299 | "systemjs-json": "github:systemjs/plugin-json@0.1.0" 300 | }, 301 | "npm:process@0.11.0": { 302 | "assert": "github:jspm/nodelibs-assert@0.1.0" 303 | }, 304 | "npm:promiseq@0.1.0": { 305 | "async": "npm:async@0.9.0", 306 | "bluebird": "npm:bluebird@2.3.11", 307 | "os": "github:jspm/nodelibs-os@0.1.0", 308 | "process": "github:jspm/nodelibs-process@0.1.1" 309 | }, 310 | "npm:public-encrypt@2.0.0": { 311 | "bn.js": "npm:bn.js@1.3.0", 312 | "browserify-rsa": "npm:browserify-rsa@2.0.0", 313 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 314 | "create-hash": "npm:create-hash@1.1.1", 315 | "crypto": "github:jspm/nodelibs-crypto@0.1.0", 316 | "parse-asn1": "npm:parse-asn1@3.0.0", 317 | "randombytes": "npm:randombytes@2.0.1" 318 | }, 319 | "npm:punycode@1.3.2": { 320 | "process": "github:jspm/nodelibs-process@0.1.1" 321 | }, 322 | "npm:randombytes@2.0.1": { 323 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 324 | "crypto": "github:jspm/nodelibs-crypto@0.1.0", 325 | "process": "github:jspm/nodelibs-process@0.1.1" 326 | }, 327 | "npm:readable-stream@1.1.13": { 328 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 329 | "core-util-is": "npm:core-util-is@1.0.1", 330 | "events": "github:jspm/nodelibs-events@0.1.0", 331 | "inherits": "npm:inherits@2.0.1", 332 | "isarray": "npm:isarray@0.0.1", 333 | "process": "github:jspm/nodelibs-process@0.1.1", 334 | "stream": "github:jspm/nodelibs-stream@0.1.0", 335 | "stream-browserify": "npm:stream-browserify@1.0.0", 336 | "string_decoder": "npm:string_decoder@0.10.31", 337 | "util": "github:jspm/nodelibs-util@0.1.0" 338 | }, 339 | "npm:ripemd160@1.0.1": { 340 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 341 | "process": "github:jspm/nodelibs-process@0.1.1" 342 | }, 343 | "npm:sha.js@2.4.0": { 344 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 345 | "fs": "github:jspm/nodelibs-fs@0.1.2", 346 | "inherits": "npm:inherits@2.0.1", 347 | "process": "github:jspm/nodelibs-process@0.1.1" 348 | }, 349 | "npm:stream-browserify@1.0.0": { 350 | "events": "github:jspm/nodelibs-events@0.1.0", 351 | "inherits": "npm:inherits@2.0.1", 352 | "readable-stream": "npm:readable-stream@1.1.13" 353 | }, 354 | "npm:string_decoder@0.10.31": { 355 | "buffer": "github:jspm/nodelibs-buffer@0.1.0" 356 | }, 357 | "npm:timers-browserify@1.4.1": { 358 | "process": "npm:process@0.11.0" 359 | }, 360 | "npm:ultron@1.0.1": { 361 | "events": "github:jspm/nodelibs-events@0.1.0" 362 | }, 363 | "npm:url@0.10.3": { 364 | "assert": "github:jspm/nodelibs-assert@0.1.0", 365 | "punycode": "npm:punycode@1.3.2", 366 | "querystring": "npm:querystring@0.2.0", 367 | "util": "github:jspm/nodelibs-util@0.1.0" 368 | }, 369 | "npm:utf-8-validate@1.0.1": { 370 | "bindings": "npm:bindings@1.2.1", 371 | "nan": "npm:nan@1.6.2" 372 | }, 373 | "npm:util@0.10.3": { 374 | "inherits": "npm:inherits@2.0.1", 375 | "process": "github:jspm/nodelibs-process@0.1.1" 376 | }, 377 | "npm:vm-browserify@0.0.4": { 378 | "indexof": "npm:indexof@0.0.1" 379 | }, 380 | "npm:ws@0.7.1": { 381 | "buffer": "github:jspm/nodelibs-buffer@0.1.0", 382 | "bufferutil": "npm:bufferutil@1.0.1", 383 | "crypto": "github:jspm/nodelibs-crypto@0.1.0", 384 | "events": "github:jspm/nodelibs-events@0.1.0", 385 | "http": "github:jspm/nodelibs-http@1.7.1", 386 | "https": "github:jspm/nodelibs-https@0.1.0", 387 | "options": "npm:options@0.0.6", 388 | "process": "github:jspm/nodelibs-process@0.1.1", 389 | "stream": "github:jspm/nodelibs-stream@0.1.0", 390 | "tls": "github:jspm/nodelibs-tls@0.1.0", 391 | "ultron": "npm:ultron@1.0.1", 392 | "url": "github:jspm/nodelibs-url@0.1.0", 393 | "utf-8-validate": "npm:utf-8-validate@1.0.1", 394 | "util": "github:jspm/nodelibs-util@0.1.0", 395 | "zlib": "github:jspm/nodelibs-zlib@0.1.0" 396 | }, 397 | "npm:xpc-connection@0.1.3": { 398 | "events": "github:jspm/nodelibs-events@0.1.0", 399 | "nan": "npm:nan@1.8.4" 400 | } 401 | } 402 | }); 403 | 404 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | This Source Code is licensed under the MIT license. If a copy of the 3 | MIT-license was not distributed with this file, You can obtain one at: 4 | http://opensource.org/licenses/mit-license.html. 5 | @author: Manuel Overdijk (manueloverdijk) 6 | @license MIT 7 | @copyright Manuel Overdijk, 2015 8 | */ 9 | "use strict" 10 | 11 | var MyoNodeBluetooth = require('./lib/agent'); 12 | module.exports = MyoNodeBluetooth; 13 | 14 | -------------------------------------------------------------------------------- /lib/agent.js: -------------------------------------------------------------------------------- 1 | /** 2 | This Source Code is licensed under the MIT license. If a copy of the 3 | MIT-license was not distributed with this file, You can obtain one at: 4 | http://opensource.org/licenses/mit-license.html. 5 | @author: Manuel Overdijk (manueloverdijk) 6 | @license MIT 7 | @copyright Manuel Overdijk, 2015 8 | */ 9 | "use strict" 10 | 11 | var MyoProtocol = require('./myoProtocol'); 12 | var noble = require('noble'); 13 | var Armband = require('./armband/armband'); 14 | var EventEmitter = require("events").EventEmitter; 15 | 16 | class Agent extends EventEmitter{ 17 | 18 | get armbands(){return this._armbands} 19 | set armbands(value){this._armbands = value} 20 | 21 | constructor(){ 22 | super(); 23 | this.myoProtocol = new MyoProtocol(); 24 | this._armbands = []; 25 | this.startDiscover(); 26 | } 27 | 28 | /** 29 | * startDiscover 30 | * Start discovering MYO peripherals 31 | */ 32 | 33 | startDiscover(){ 34 | noble.on('stateChange', function(state){ 35 | this.emit('stateChange', state); 36 | 37 | /* Start discovering Myo peripherals */ 38 | if (state === 'poweredOn') { 39 | this.discover(this.myoProtocol.services.control.id); 40 | } else { 41 | 42 | /* Set all armbands to not connected */ 43 | for(let armband of this.armbands){ 44 | armband.setConnected(false); 45 | } 46 | throw new Error('Bluetooth Adapter not found'); 47 | 48 | } 49 | }.bind(this)); 50 | } 51 | 52 | /** 53 | * Discovers peripherals which advertise given UUID 54 | * @param UUID 55 | */ 56 | discover(UUID){ 57 | console.log('start scanning for MYO devices'); 58 | 59 | noble.startScanning([UUID],false); 60 | 61 | noble.on('discover', function (peripheral) { 62 | let armband = new Armband(peripheral); 63 | this._armbands.push(armband); 64 | this.emit('discovered', armband); 65 | }.bind(this)); 66 | } 67 | 68 | /** 69 | * stopScanning 70 | */ 71 | stopScanning(){ 72 | noble.stopScanning(); 73 | } 74 | } 75 | 76 | module.exports = Agent; 77 | -------------------------------------------------------------------------------- /lib/armband/armband.js: -------------------------------------------------------------------------------- 1 | /** 2 | This Source Code is licensed under the MIT license. If a copy of the 3 | MIT-license was not distributed with this file, You can obtain one at: 4 | http://opensource.org/licenses/mit-license.html. 5 | @author: Manuel Overdijk (manueloverdijk) 6 | @license MIT 7 | @copyright Manuel Overdijk, 2015 8 | */ 9 | 10 | "use strict" 11 | 12 | var Communicator = require('../communicator/communicator'); 13 | var EventEmitter = require("events").EventEmitter; 14 | var EulerAngle = require('../util/eulerAngle'); 15 | 16 | class Armband extends EventEmitter { 17 | 18 | get peripheral(){return this._peripheral} 19 | set peripheral(value){this._peripheral = value} 20 | 21 | get UUID(){return this.uuid;} 22 | set UUID(value){this.uuid = UUID} 23 | 24 | get communicator(){return this._communicator} 25 | set communicator(value){this._communicator = value} 26 | 27 | constructor(peripheral){ 28 | super(); 29 | this._peripheral = peripheral; 30 | this._communicator = new Communicator(peripheral); 31 | } 32 | 33 | /** 34 | * 35 | */ 36 | connect(){ 37 | if (this.peripheral.state === 'connected') { 38 | this.setConnected(true); 39 | } else { 40 | this.peripheral.connect(); 41 | this.setConnected(false); 42 | 43 | this.peripheral.on('connect', function(){ 44 | 45 | this.uuid = this.peripheral.uuid; 46 | this.setConnected(true); 47 | 48 | }.bind(this)); 49 | } 50 | 51 | } 52 | 53 | /** 54 | * Start the communication and handle EventData/IMUData 55 | */ 56 | initStart(){ 57 | if(this.isConnected()){ 58 | this.communicator.initStart(function(eventData){ 59 | if(eventData.ready != undefined){ 60 | this.setReady(eventData.ready); 61 | } 62 | 63 | if(eventData.emgData){ 64 | this.emit('emg', eventData); 65 | } 66 | 67 | // IMU data 68 | if(eventData.orientation){ 69 | this.emit('orientation',eventData.orientation); 70 | 71 | let eulerAngle = new EulerAngle(eventData.orientation.x, eventData.orientation.y, eventData.orientation.z, eventData.orientation.w); 72 | 73 | this.emit('eulerangle', eulerAngle.getAngles()); 74 | } 75 | if(eventData.accelerometer){ 76 | // TODO: Sample EventData 77 | this.emit('accelerometer',eventData.accelerometer) 78 | } 79 | if(eventData.gyroscope){ 80 | // TODO: Sample EventData 81 | this.emit('gyroscope',eventData.gyroscope) 82 | } 83 | // Classifier data 84 | else if (eventData.type) { 85 | if (eventData.type == 3) { 86 | switch(eventData.pose){ 87 | case 0: 88 | this.emit('pose',{type:'rest'}); 89 | break; 90 | case 1: 91 | this.emit('pose',{type:'fist'}); 92 | break; 93 | case 2: 94 | this.emit('pose', {type:'waveIn'}); 95 | break; 96 | case 3: 97 | this.emit('pose', {type:'waveOut'}); 98 | break; 99 | case 4: 100 | this.emit('pose', {type:'spread'}); 101 | break; 102 | case 5: 103 | this.emit('pose', {type:'tap'}); 104 | break; 105 | case 255: 106 | this.emit('pose',{type:'unkown'}); 107 | } 108 | } 109 | // TODO: SWitch case 110 | else if (eventData.type == 1) { 111 | this.emit('sync', true); 112 | } 113 | // arm unsynced 114 | else if (eventData.type == 2) { 115 | this.emit('sync', false); 116 | } else if(eventData.type == 6) { 117 | this.emit('sync', eventData.sync_result); 118 | } else if(eventData.type == 5) { 119 | this.emit('unlocked', false); 120 | } else if(eventData.type == 4) { 121 | this.emit('unlocked', true); 122 | } 123 | } 124 | }.bind(this)); 125 | } 126 | } 127 | 128 | /** 129 | * readInfo 130 | */ 131 | readInfo(){ 132 | if(this.isConnected()){ 133 | this.communicator.readInfo(function(data){ 134 | this.emit('info',data); 135 | }.bind(this)); 136 | } 137 | } 138 | 139 | /** 140 | * readAdvertisement 141 | * @returns {advertisement|*|{localName, txPowerLevel, serviceUuids, manufacturerData, serviceData}} 142 | */ 143 | readAdvertisement(){ 144 | if(this.isConnected()){ 145 | return this.peripheral.advertisement; 146 | } 147 | } 148 | 149 | /** 150 | * readVerison 151 | */ 152 | readVersion(){ 153 | if(this.isConnected()){ 154 | this.communicator.readVersion(function(data){ 155 | this.emit('version', data); 156 | }.bind(this)); 157 | } 158 | } 159 | 160 | /** 161 | * readPose 162 | */ 163 | 164 | readPose(){ 165 | if(this.isConnected()){ 166 | this.communicator.readClassifier(function(data){ 167 | this.emit('classifier', data); 168 | }.bind(this)); 169 | } 170 | } 171 | 172 | /** 173 | * readIMU 174 | */ 175 | 176 | readIMU(){ 177 | if(this.isConnected()){ 178 | this.communicator.readIMU(function(data){ 179 | // needs to be split in different data sections here 180 | this.emit('imu', data); 181 | }.bind(this)); 182 | } 183 | } 184 | 185 | /** 186 | * write vibrate command to Myo 187 | * @param mode 188 | */ 189 | vibrate(mode){ 190 | if(this.isConnected()){ 191 | this.communicator.vibrate(mode,function(data){ 192 | this.emit('command', {type: 'vibrate', data: data}); 193 | }.bind(this)); 194 | } 195 | } 196 | 197 | /** 198 | * setSleepMode 199 | * @param neverSleep {Boolean} 200 | */ 201 | 202 | setSleepMode(neverSleep){ 203 | if(this.isConnected()){ 204 | this.communicator.commandSetSleepMode(neverSleep,function(data){ 205 | this.emit('command', {type: 'sleepMode', data: data}); 206 | }.bind(this)); 207 | } 208 | } 209 | 210 | /** 211 | * setUnlockMode 212 | * @param mode 213 | */ 214 | 215 | setUnlockMode(mode){ 216 | if(this.isConnected()){ 217 | this.communicator.commandSetUnlockMode(mode,function(data){ 218 | this.emit('command', {type: 'unlockMode', data: data}); 219 | }.bind(this)); 220 | } 221 | } 222 | 223 | /** 224 | * setUserAction 225 | */ 226 | 227 | setUserAction(){ 228 | if(this.isConnected()){ 229 | this.communicator.commandSetUserAction(function(data){ 230 | this.emit('command', {type: 'userAction', data: data}); 231 | }.bind(this)); 232 | } 233 | } 234 | 235 | /** 236 | * setMode 237 | */ 238 | // TODO: Implement modes 239 | setMode(){ 240 | if(this.isConnected()){ 241 | this.communicator.commandSetMode(function(data){ 242 | this.emit('command', {type: 'setMode', data: data}); 243 | }.bind(this)); 244 | } 245 | } 246 | 247 | /** 248 | * readBatteryInfo 249 | */ 250 | 251 | readBatteryInfo(){ 252 | if(this.isConnected()){ 253 | this.communicator.batteryInfo(function(data){ 254 | this.emit('batteryInfo', data); 255 | }.bind(this)); 256 | } 257 | } 258 | 259 | isConnected(){ 260 | return this.connected; 261 | } 262 | 263 | setConnected(boolean){ 264 | this.connected = boolean; 265 | this.emit('connect',boolean); 266 | } 267 | 268 | setReady(boolean){ 269 | this.ready = boolean; 270 | this.emit('ready', boolean); 271 | } 272 | 273 | } 274 | 275 | module.exports = Armband; 276 | -------------------------------------------------------------------------------- /lib/communicator/ConcurrencyTest.js: -------------------------------------------------------------------------------- 1 | /** 2 | This Source Code is licensed under the MIT license. If a copy of the 3 | MIT-license was not distributed with this file, You can obtain one at: 4 | http://opensource.org/licenses/mit-license.html. 5 | @author: Manuel Overdijk (manueloverdijk) 6 | @license MIT 7 | @copyright Manuel Overdijk, 2015 8 | */ 9 | 10 | var Deserialise = require('../util/deserialise'); 11 | var Serialise = require('../util/serialise'); 12 | var MyoProtocol = require('../myoProtocol'); 13 | 14 | class ConcurrencyTest { 15 | get peripheral() { 16 | return this._peripheral 17 | } 18 | 19 | set peripheral(value) { 20 | this._peripheral = value 21 | } 22 | 23 | constructor(peripheral) { 24 | this._peripheral = peripheral; 25 | this.deserialise = new Deserialise(); 26 | this.serialise = new Serialise(); 27 | this.protocol = new MyoProtocol(); 28 | 29 | } 30 | 31 | 32 | /* Test Concurrency of notifying characteristics when connected */ 33 | testConcurrency(){ 34 | 35 | let commandPayload = this.serialise.command_set_mode(); 36 | 37 | this.peripheral.discoverServices([this.protocol.services.control.id,this.protocol.services.classifier.id, this.protocol.services.imuData.id], function(error, services) { 38 | console.log('Services discovered: ', services); 39 | var commandService = services[0]; 40 | var classifierService = services[2]; 41 | var imuService = services[1]; 42 | 43 | 44 | /* Write CommandService to enable IMU/Classifier events */ 45 | commandService.discoverCharacteristics([this.protocol.services.control.COMMAND], function(error, characteristics) { 46 | console.log('discovered Command characteristic', characteristics); 47 | var commandChar = characteristics[0]; 48 | 49 | commandChar.write(commandPayload, true, function(error) { 50 | console.log('set command_set_mode'); 51 | console.log('error == ',error); 52 | }); 53 | }.bind(this)); 54 | 55 | /* Get notified of IMU events */ 56 | 57 | setTimeout(function(){ 58 | imuService.discoverCharacteristics([], function(error, characteristics) { 59 | console.log('discovered characteristic readIMU', characteristics); 60 | var imuChar = characteristics[0]; 61 | 62 | imuChar.notify(true, function(error){ 63 | console.log('error happend', error); 64 | }); 65 | 66 | imuChar.on('notify', function(data, isNotification) { 67 | //console.log('notify',data); 68 | }.bind(this)); 69 | 70 | imuChar.on('read', function(data, isNotification){ 71 | console.log('got data', this.deserialise.imu_data_t(data)); 72 | //console.log('isNotification', isNotification); 73 | }.bind(this)); // 74 | }.bind(this)); 75 | }.bind(this),2000); 76 | 77 | /* Get notified of Classifier events */ 78 | 79 | setTimeout(function(){ 80 | classifierService.discoverCharacteristics([this.protocol.services.classifier.classifierEvent], function(error, characteristics) { 81 | console.log('discovered characteristic classifier', characteristics); 82 | var classifierChar = characteristics[0]; 83 | 84 | classifierChar.notify(true, function(error){ 85 | console.log('error happend classifier', error); 86 | }); 87 | 88 | classifierChar.on('notify', function(data, isNotification) { 89 | console.log('notify classifier',data); 90 | console.log('is notification',isNotification); 91 | }.bind(this)); 92 | 93 | classifierChar.on('read', function(data, isNotification){ 94 | let eventData = this.deserialise.classifier_event_t(data); 95 | console.log('eventData', eventData); 96 | if(eventData.type == 3){ 97 | if(eventData.pose == 0){ 98 | console.log('rest!'); 99 | } 100 | if(eventData.pose == 1){ 101 | console.log('fist!'); 102 | } 103 | if(eventData.pose == 2){ 104 | console.log('waveIn!'); 105 | } 106 | if(eventData.pose == 3){ 107 | console.log('waveOut!'); 108 | } 109 | if(eventData.pose == 4){ 110 | console.log('fingersspread!'); 111 | } 112 | if(eventData.pose == 5){ 113 | console.log('doubleTap!'); 114 | } 115 | if(eventData.pose == 255){ 116 | console.log('unkown!'); 117 | } 118 | } 119 | else if(eventData.type == 1){ 120 | console.log('arm synced!'); 121 | } 122 | // arm unsynced 123 | else if(eventData.type == 2){ 124 | console.log('arm unsynced'); 125 | } 126 | }.bind(this)); 127 | }.bind(this)); 128 | 129 | }.bind(this),5000); 130 | 131 | 132 | }.bind(this)); 133 | } 134 | } 135 | 136 | module.exports = ConcurrencyTest; -------------------------------------------------------------------------------- /lib/communicator/communicator.js: -------------------------------------------------------------------------------- 1 | /** 2 | This Source Code is licensed under the MIT license. If a copy of the 3 | MIT-license was not distributed with this file, You can obtain one at: 4 | http://opensource.org/licenses/mit-license.html. 5 | @author: Manuel Overdijk (manueloverdijk) 6 | @license MIT 7 | @copyright Manuel Overdijk, 2015 8 | */ 9 | 10 | "use strict" 11 | 12 | var Deserialise = require('../util/deserialise'); 13 | var Serialise = require('../util/serialise'); 14 | var MyoProtocol = require('../myoProtocol'); 15 | var PromiseQueue = require('promiseq/index'); 16 | var Promise = require('bluebird'); 17 | 18 | class Communicator{ 19 | get peripheral(){return this._peripheral} 20 | set peripheral(value){this._peripheral = value} 21 | 22 | constructor(peripheral) { 23 | this._peripheral = peripheral; 24 | this.deserialise = new Deserialise(); 25 | this.serialise = new Serialise(); 26 | this.protocol = new MyoProtocol(); 27 | 28 | // Create a new queue for reading/writing Characteristics sequentially 29 | var workerCount = 1; 30 | this.queue = new PromiseQueue(workerCount); 31 | } 32 | 33 | /** 34 | * initStart 35 | * Because of a limitation in Noble, being notified of two characteristics only works if you discover the Services 36 | * concurrently. Thus, upon connection the Communicator should write the ControlService to enable IMU & Classifier events 37 | * and notify the ClassifierEvent Characteristic and IMUDatacharacteristic to send events. 38 | */ 39 | initStart(callback){ 40 | // payload to set IMU/Classifier events 41 | let commandPayload = this.serialise.command_set_mode(); 42 | // payload to set the Myo unlocked 43 | let unlockPayload = this.serialise.set_unlock_mode(0); 44 | // payload to set the Myo to never sleep 45 | let sleepModepayload = this.serialise.set_sleep_mode_t(true); 46 | 47 | this.peripheral.discoverServices([this.protocol.services.control.id,this.protocol.services.classifier.id, this.protocol.services.imuData.id, this.protocol.services.emgData.id, this.protocol.services.battery.id], function(error, services) { 48 | 49 | // TODO: Check for UUID's 50 | this.batteryService = services[0]; 51 | this.commandService = services[1]; 52 | this.imuService = services[2]; 53 | this.classifierService = services[3]; 54 | this.emgService = services[4]; 55 | 56 | 57 | 58 | /* Write CommandService to enable IMU/Classifier events */ 59 | this.commandService.discoverCharacteristics([this.protocol.services.control.COMMAND, this.protocol.services.control.FIRMWARE_VERSION, this.protocol.services.control.MYO_INFO], function (error, characteristics) { 60 | if(characteristics.length == 3){ 61 | this.myoInfoChar = characteristics[0]; 62 | this.firmwareChar = characteristics[1]; 63 | this.commandChar = characteristics[2]; 64 | 65 | 66 | 67 | this.commandChar.write(commandPayload, true, function (error) { 68 | if(!error){ 69 | console.log('commandChar set'); 70 | this.commandChar.write(unlockPayload,true, function(error) { 71 | if(!error){ 72 | console.log('unlock mode set'); 73 | callback('unlock mode set'); 74 | this.commandChar.write(sleepModepayload,true, function(error){ 75 | if(!error){ 76 | console.log('sleep mode set'); 77 | callback('sleep mode set'); // TODO: info message 78 | } else { 79 | throw new Error('SleepModePayLoad: ', error); 80 | } 81 | }) 82 | } else { 83 | throw new Error('UnlockPayload:', error); 84 | } 85 | }.bind(this)); 86 | callback('commandChar set'); 87 | } else { 88 | throw new Error('CommandChar: ', error); 89 | } 90 | }.bind(this)); 91 | } else { 92 | throw new Error('CommandService: too few characteristics discovered'); 93 | } 94 | 95 | }.bind(this)); 96 | 97 | setTimeout(function(){ 98 | this.batteryService.discoverCharacteristics([this.protocol.services.battery.BATTERY_LEVEL],function(error, characteristics){ 99 | if(characteristics.length > 0){ 100 | this.batteryChar = characteristics[0]; 101 | } else { 102 | throw new Error('BatteryService: too few characteristics discovered'); 103 | } 104 | }.bind(this)); 105 | }.bind(this),1000); 106 | 107 | /* Get notified of IMU events */ 108 | setTimeout(function () { 109 | this.imuService.discoverCharacteristics([this.protocol.services.imuData.IMU_DATA], function (error, characteristics) { 110 | if(characteristics.length > 0) { 111 | 112 | this.imuChar = characteristics[0]; 113 | 114 | // Notifiy Characteristic to send events 115 | this.imuChar.notify(true, function (error) { 116 | if(error){ 117 | throw new Error('imuChar: ', error); 118 | } 119 | }); 120 | 121 | this.imuChar.on('read', function (data, isNotification) { 122 | data = this.deserialise.imu_data_t(data); 123 | callback(data); 124 | }.bind(this)); 125 | 126 | } else { 127 | throw new Error('imuService: too few characteristics discovered'); 128 | } 129 | }.bind(this)); 130 | }.bind(this), 2000); 131 | 132 | /* Get notified of Classifier events */ 133 | 134 | setTimeout(function () { 135 | this.classifierService.discoverCharacteristics([this.protocol.services.classifier.classifierEvent], function (error, characteristics) { 136 | if(characteristics.length > 0) { 137 | this.classifierChar = characteristics[0]; 138 | 139 | // Notifiy Characteristic to send events 140 | this.classifierChar.notify(true, function (error) { 141 | if(error){ 142 | throw new Error('classifierChar: ', error); 143 | } else { 144 | // change Myo state to ready 145 | callback({'ready':true}); 146 | } 147 | }); 148 | this.classifierChar.on('read', function (data, isNotification) { 149 | let eventData = this.deserialise.classifier_event_t(data); 150 | callback(eventData); 151 | }.bind(this)); 152 | 153 | } else { 154 | throw new Error('classifierService: too few characteristics discovered'); 155 | } 156 | }.bind(this)); 157 | }.bind(this), 4000); 158 | 159 | setTimeout(function () { 160 | this.emgService.discoverCharacteristics([], function (error, characteristics) { 161 | if(characteristics.length == 4) { 162 | // TODO check UUID's 163 | for(let index in characteristics){ 164 | let char = characteristics[index]; 165 | char.notify(true, function (error) { 166 | if(error){ 167 | throw new Error('emgChar: ', error); 168 | } 169 | }); 170 | char.on('read', function (data, isNotification) { 171 | let emgData = this.deserialise.emg_data_t(data); 172 | callback({id: index, emgData: emgData}); 173 | }.bind(this)); 174 | } 175 | } else { 176 | throw new Error('emgService: too few characteristics discovered'); 177 | } 178 | }.bind(this)); 179 | }.bind(this), 3000); 180 | 181 | }.bind(this)); 182 | } 183 | 184 | readInfo(callback){ 185 | if(!callback) throw new Error('ReadInfo: callback undefined'); 186 | 187 | let readInfoPromise = function(){ 188 | return new Promise(function(resolve, reject) { 189 | if(this.myoInfoChar){ 190 | this.myoInfoChar.read(function (error, data) {}); 191 | this.myoInfoChar.on('read', function (data, isNotification) { 192 | data = this.deserialise.info_t(data); 193 | resolve(data); 194 | }.bind(this)); 195 | } else { 196 | throw new Error('ReadInfo: myInfoChar undefined'); 197 | } 198 | }.bind(this)); 199 | }; 200 | 201 | this.queue.push(readInfoPromise.bind(this)).then(function(results){ 202 | console.log('Job complete', results); 203 | callback(results) 204 | }.bind(this)); 205 | } 206 | 207 | readVersion(callback){ 208 | if(!callback) throw new Error('ReadVersion: callback undefined'); 209 | let readVersionPromise = function() { 210 | return new Promise(function (resolve, reject) { 211 | if(this.firmwareChar){ 212 | this.firmwareChar.read(function (error, data) {}); 213 | this.firmwareChar.once('read', function (data, isNotification) { 214 | console.log('data found readVersion', data); 215 | data = this.deserialise.version_t(data); 216 | resolve(data); 217 | }.bind(this)); 218 | } else { 219 | throw new Error('ReadVersion: firmwareChar undefined'); 220 | } 221 | }.bind(this)); 222 | }; 223 | 224 | this.queue.push(readVersionPromise.bind(this)).then(function(results){ 225 | console.log('Job complete', results); 226 | callback(results) 227 | }); 228 | }; 229 | 230 | /** 231 | * 232 | */ 233 | readClassifier(callback){ 234 | if(!callback) throw new Error('readClassifier: callback undefined'); 235 | 236 | this.peripheral.discoverServices([this.protocol.services.classifier.id], function(error, services) { 237 | var service = services[0]; 238 | console.log('discovered service Classifier', services); 239 | 240 | service.discoverCharacteristics([this.protocol.services.classifier.classifierEvent], function(error, characteristics) { 241 | console.log('discovered characteristic classifier', characteristics); 242 | var classifierChar = characteristics[0]; 243 | 244 | classifierChar.notify(true, function(error){ 245 | console.log('error happend classifier', error); 246 | }); 247 | 248 | classifierChar.on('notify', function(data, isNotification) { 249 | //data = this.deserialise.version_t(data); 250 | console.log('notify classifier',data); 251 | console.log('is notification',isNotification); 252 | //callback(data); 253 | }.bind(this)); 254 | 255 | classifierChar.on('read', function(data, isNotification){ 256 | let eventData = this.deserialise.classifier_event_t(data); 257 | console.log('eventData', eventData); 258 | if(eventData.type == 3){ 259 | if(eventData.pose == 0){ 260 | console.log('rest!'); 261 | callback('rest'); 262 | } 263 | if(eventData.pose == 1){ 264 | console.log('fist!'); 265 | callback('fist'); 266 | } 267 | if(eventData.pose == 2){ 268 | console.log('waveIn!'); 269 | callback('waveIn'); 270 | } 271 | if(eventData.pose == 3){ 272 | console.log('waveOut!'); 273 | callback('waveOut'); 274 | } 275 | if(eventData.pose == 4){ 276 | console.log('fingersspread!'); 277 | callback('spread'); 278 | } 279 | if(eventData.pose == 5){ 280 | console.log('doubleTap!'); 281 | callback('tap'); 282 | } 283 | if(eventData.pose == 255){ 284 | console.log('unkown!'); 285 | callback('unkown'); 286 | } 287 | } 288 | else if(eventData.type == 1){ 289 | console.log('arm synced!'); 290 | callback('synced'); 291 | } 292 | // arm unsynced 293 | else if(eventData.type == 2){ 294 | console.log('arm unsynced'); 295 | callback('unsynced'); 296 | } 297 | 298 | 299 | console.log('isNotification', isNotification); 300 | }.bind(this)); // 301 | }.bind(this)); 302 | }.bind(this)); 303 | } 304 | 305 | /** 306 | * 307 | * @param callback 308 | */ 309 | readIMU(callback){ 310 | this.peripheral.discoverServices([this.protocol.services.imuData.id], function(error, services) { 311 | var service = services[0]; 312 | console.log('discovered service readIMU', services); 313 | 314 | service.discoverCharacteristics([this.protocol.services.imuData.IMU_DATA], function(error, characteristics) { 315 | console.log('discovered characteristic readIMU', characteristics); 316 | var batteryLevelCharacteristic = characteristics[0]; 317 | 318 | batteryLevelCharacteristic.notify(true, function(error){ 319 | console.log('error happend', error); 320 | }); 321 | 322 | batteryLevelCharacteristic.on('notify', function(data, isNotification) { 323 | //console.log('notify',data); 324 | callback(data); 325 | }.bind(this)); 326 | 327 | batteryLevelCharacteristic.on('read', function(data, isNotification){ 328 | console.log('p') 329 | //console.log('got data', this.deserialise.imu_data_t(data)); 330 | //console.log('isNotification', isNotification); 331 | }.bind(this)); // 332 | }.bind(this)); 333 | }.bind(this)); 334 | } 335 | 336 | /** 337 | * 338 | */ 339 | vibrate(time, callback){ 340 | if(!callback) throw new Error('Vibrate: callback undefined'); 341 | let payload = this.serialise.vibrate_t(time); 342 | 343 | let writeVibratePromise = function(payload) { 344 | return new Promise(function (resolve, reject) { 345 | if(this.commandChar) { 346 | this.commandChar.write(payload, false, function (error) { 347 | if (!error) { 348 | // TODO 349 | resolve('succes'); 350 | } else { 351 | reject(); 352 | } 353 | }.bind(this)); 354 | } else throw new Error('vibrateChar: commandChar undefined'); 355 | }.bind(this)); 356 | }.bind(this); 357 | 358 | this.queue.push(writeVibratePromise.bind(this, payload)).then(function(results){ 359 | console.log('Job complete', results); 360 | callback(results) 361 | }); 362 | } 363 | 364 | /** 365 | * 366 | */ 367 | commandSetMode(callback){ 368 | if(!callback) throw new Error('CommandSetMode: callback undefined'); 369 | let payload = this.serialise.command_set_mode(); 370 | 371 | let writeCommandPromise = function(payload) { 372 | return new Promise(function (resolve, reject) { 373 | if(this.commandChar){ 374 | this.commandChar.write(payload, true, function(error) { 375 | if(!error){ 376 | // TODO 377 | resolve('success'); 378 | } else { 379 | reject(); 380 | } 381 | }); 382 | } else { 383 | throw new Error('CommandSetMode: commandChar undefined'); 384 | } 385 | }.bind(this)); 386 | }.bind(this); 387 | 388 | this.queue.push(writeCommandPromise.bind(this, payload)).then(function(results){ 389 | console.log('Job complete', results); 390 | callback(results) 391 | }); 392 | } 393 | 394 | /** 395 | * 396 | * @param neverSleep 397 | * @param callback 398 | */ 399 | 400 | commandSetSleepMode(neverSleep,callback){ 401 | if(!callback) throw new Error('SleepMode: callback undefined'); 402 | let payload = this.serialise.set_sleep_mode_t(neverSleep); 403 | 404 | let writeSleepModePromise = function(payload) { 405 | return new Promise(function (resolve, reject) { 406 | if (this.commandChar) { 407 | this.commandChar.write(payload, true, function (error) { 408 | if (!error) { 409 | // TODO 410 | resolve('success'); 411 | } else { 412 | reject(); 413 | } 414 | }); 415 | } else { 416 | throw new Error('CommandSetSleepMode: commandChar undefined'); 417 | } 418 | }.bind(this)); 419 | }.bind(this); 420 | 421 | this.queue.push(writeSleepModePromise.bind(this, payload)).then(function(results){ 422 | console.log('Job complete', results); 423 | callback(results) 424 | }); 425 | } 426 | 427 | commandSetUnlockMode(mode,callback){ 428 | if(!callback) throw new Error('UnlockMode: callback undefined'); 429 | let payload = this.serialise.set_unlock_mode(mode); 430 | 431 | let writeUnlockModePromise = function(payload) { 432 | return new Promise(function (resolve, reject) { 433 | if (this.commandChar) { 434 | this.commandChar.write(payload, true, function (error) { 435 | if (!error) { 436 | // TODO 437 | resolve('success'); 438 | } else { 439 | reject(); 440 | } 441 | }); 442 | } else { 443 | throw new Error('commandSetUnlockMode: commandChar undefined'); 444 | } 445 | }.bind(this)); 446 | }.bind(this); 447 | 448 | this.queue.push(writeUnlockModePromise.bind(this, payload)).then(function(results){ 449 | console.log('Job complete', results); 450 | callback(results) 451 | }); 452 | } 453 | 454 | commandSetUserAction(callback){ 455 | if(!callback) throw new Error('UserAction: callback undefined'); 456 | 457 | let payload = this.serialise.user_action(); 458 | 459 | let writeUserActionPromise = function(payload) { 460 | return new Promise(function (resolve, reject) { 461 | if (this.commandChar) { 462 | this.commandChar.write(payload, true, function (error) { 463 | if (!error) { 464 | // TODO 465 | resolve('success'); 466 | } else { 467 | reject(); 468 | } 469 | }); 470 | } else { 471 | throw new Error('commandSetUserAction: commandChar undefined'); 472 | } 473 | }.bind(this)); 474 | }.bind(this); 475 | 476 | this.queue.push(writeUserActionPromise.bind(this, payload)).then(function(results){ 477 | console.log('Job complete', results); 478 | callback(results) 479 | }); 480 | } 481 | 482 | batteryInfo(callback){ 483 | if(!callback) throw new Error('BatteryInfo: callback undefined'); 484 | let readBatteryInfo = function(payload) { 485 | return new Promise(function (resolve, reject) { 486 | if(this.batteryChar){ 487 | this.batteryChar.read(function(error, data) { 488 | if(error){ 489 | reject(); 490 | } 491 | }); 492 | this.batteryChar.on('read', function(data, isNotification) { 493 | data = this.deserialise.battery_t(data); 494 | resolve(data); 495 | }.bind(this)); 496 | } else { 497 | throw new Error('BatteryInfo: too few characteristics discovered'); 498 | } 499 | }.bind(this)); 500 | }.bind(this); 501 | 502 | this.queue.push(readBatteryInfo.bind(this)).then(function(results){ 503 | console.log('Job complete', results); 504 | callback(results) 505 | }); 506 | } 507 | 508 | } 509 | 510 | module.exports = Communicator; -------------------------------------------------------------------------------- /lib/myoProtocol.js: -------------------------------------------------------------------------------- 1 | /** 2 | This Source Code is licensed under the MIT license. If a copy of the 3 | MIT-license was not distributed with this file, You can obtain one at: 4 | http://opensource.org/licenses/mit-license.html. 5 | @author: Manuel Overdijk (manueloverdijk) 6 | @license MIT 7 | @copyright Manuel Overdijk, 2015 8 | */ 9 | "use strict" 10 | 11 | class MyoProtocol { 12 | 13 | get services(){ return this._services} 14 | set serivices(services){this._services = services}; 15 | 16 | constructor(){ 17 | 18 | this._services = { 19 | control: { 20 | id: "D5060001A904DEB947482C7F4A124842", 21 | MYO_INFO: "d5060101a904deb947482c7f4a124842", 22 | FIRMWARE_VERSION: "d5060201a904deb947482c7f4a124842", 23 | COMMAND: "d5060401a904deb947482c7f4a124842" 24 | }, 25 | imuData: { 26 | id: "D5060002A904DEB947482C7F4A124842", 27 | IMU_DATA : "D5060402A904DEB947482C7F4A124842" 28 | }, 29 | classifier: { 30 | id: "D5060003A904DEB947482C7F4A124842", 31 | classifierEvent: "D5060103A904DEB947482C7F4A124842" 32 | }, 33 | emgData: { 34 | id: "d5060005a904deb947482c7f4a124842", 35 | EMG_DATA_0: 'd5060105a904deb947482c7f4a124842', 36 | EMG_DATA_1: 'd5060205a904deb947482c7f4a124842', 37 | EMG_DATA_2: 'd5060305a904deb947482c7f4a124842', 38 | EMG_DATA_3: 'd5060405a904deb947482c7f4a124842' 39 | }, 40 | battery: { 41 | id: "180f", 42 | BATTERY_LEVEL: "2a19" 43 | }, 44 | genericAccess: { 45 | id: "1800", 46 | DEVICE_NAME: "2a00" 47 | } 48 | } 49 | 50 | } 51 | } 52 | 53 | module.exports = MyoProtocol; -------------------------------------------------------------------------------- /lib/util/deserialise.js: -------------------------------------------------------------------------------- 1 | /** 2 | This Source Code is licensed under the MIT license. If a copy of the 3 | MIT-license was not distributed with this file, You can obtain one at: 4 | http://opensource.org/licenses/mit-license.html. 5 | @author: Manuel Overdijk (manueloverdijk) 6 | @license MIT 7 | @copyright Manuel Overdijk, 2015 8 | */ 9 | 10 | "use strict" 11 | 12 | class Deserialise { 13 | constructor() { 14 | 15 | this.scaling = { 16 | 'ORIENTATION_SCALE': 16384.0, ///< See myohw_imu_data_t::orientation 17 | 'ACCELEROMETER_SCALE': 2048.0, ///< See myohw_imu_data_t::accelerometer 18 | 'GYROSCOPE_SCALE': 16.0 ///< See myohw_imu_data_t::gyroscope 19 | }; 20 | 21 | this.poses = { 22 | 'pose_rest': 0x0000, 23 | 'pose_fist': 0x0001, 24 | 'pose_wave_in': 0x0002, 25 | 'pose_wave_out': 0x0003, 26 | 'pose_fingers_spread': 0x0004, 27 | 'pose_double_tap': 0x0005, 28 | 'pose_unknown': 0xffff 29 | }; 30 | 31 | 32 | // identifying the myo armband on the right or left hand 33 | this.arm = { 34 | 'arm_right': 0x01, 35 | 'arm_left': 0x02, 36 | 'arm_unknown': 0xff 37 | }; 38 | 39 | 40 | // direction of the Myo armband on the forarm 41 | this.arm_direction = { 42 | 'x_direction_toward_wrist': 0x01, 43 | 'x_direction_toward_elbow': 0x02, 44 | 'x_direction_unknown': 0xff 45 | }; 46 | 47 | // result of a sync gesture 48 | this.sync_result = { 49 | 'sync_failed_too_hard': 0x01 ///< Sync gesture was performed too hard. 50 | } 51 | 52 | 53 | 54 | } 55 | 56 | /** 57 | * Info data 58 | * @param Buffer 59 | * @returns {{serial_number: Buffer, unlock_pose: Number, active_classifier_type: *, active_classifier_index: *, has_custom_classifier: *, stream_indicating: *, sku: *, reserved: Buffer}} 60 | */ 61 | 62 | info_t(Buffer) { 63 | console.log('got Buffer', Buffer); 64 | if (Buffer.length < 20) { 65 | console.log('error!'); 66 | return null; 67 | } 68 | return { 69 | serial_number: Buffer.slice(0, 6), 70 | unlock_pose: Buffer.readUInt16LE(6), 71 | active_classifier_type: Buffer.readUInt8(8), 72 | active_classifier_index: Buffer.readUInt8(9), 73 | has_custom_classifier: Buffer.readUInt8(10), 74 | stream_indicating: Buffer.readUInt8(11), 75 | sku: Buffer.readUInt8(12), 76 | reserved: Buffer.slice(13, 7) 77 | } 78 | 79 | } 80 | 81 | /** 82 | * Version data 83 | * @param Buffer 84 | * @returns {{major: Number, minor: Number, patch: Number, hardware_rev: Number}} 85 | */ 86 | version_t(Buffer) { 87 | if (Buffer.length < 8) { 88 | //error 89 | return null; 90 | } 91 | return { 92 | major: Buffer.readUInt16LE(0), 93 | minor: Buffer.readUInt16LE(2), 94 | patch: Buffer.readUInt16LE(4), 95 | hardware_rev: Buffer.readUInt16LE(6) 96 | } 97 | } 98 | 99 | /** 100 | * IMU data 101 | * @param Buffer 102 | * @returns {{orientation: {w: *, x: *, y: *, z: *}, accelerometer: Array, gyroscope: Array}} 103 | */ 104 | imu_data_t(Buffer) { 105 | if (Buffer.length < 20) { 106 | //error 107 | return null; 108 | } 109 | return { 110 | orientation: { 111 | w: Buffer.readInt16LE(0)/this.scaling.ORIENTATION_SCALE, 112 | x: Buffer.readInt16LE(2)/this.scaling.ORIENTATION_SCALE, 113 | y: Buffer.readInt16LE(4)/this.scaling.ORIENTATION_SCALE, 114 | z: Buffer.readInt16LE(6)/this.scaling.ORIENTATION_SCALE 115 | }, 116 | accelerometer:[ 117 | Buffer.readInt16LE(8)/this.scaling.ACCELEROMETER_SCALE, 118 | Buffer.readInt16LE(10)/this.scaling.ACCELEROMETER_SCALE, 119 | Buffer.readInt16LE(12)/this.scaling.ACCELEROMETER_SCALE 120 | ], 121 | gyroscope: [ 122 | Buffer.readInt16LE(14)/this.scaling.GYROSCOPE_SCALE, 123 | Buffer.readInt16LE(16)/this.scaling.GYROSCOPE_SCALE, 124 | Buffer.readInt16LE(18)/this.scaling.GYROSCOPE_SCALE 125 | ] 126 | } 127 | 128 | } 129 | 130 | /** 131 | * Not yet tested 132 | * @param Buffer 133 | * @returns {*} 134 | */ 135 | motion_event_t(Buffer){ 136 | if(Buffer.length <3){ 137 | //error 138 | return null; 139 | } 140 | return { 141 | type: Buffer.readUInt8(0), // type should be 0x00 142 | tap_direction: Buffer.readUInt8(1), 143 | tap_count: Buffer.readUInt8(2) 144 | } 145 | } 146 | 147 | /** 148 | * Classifier event 149 | * @param Buffer 150 | * @returns {*} 151 | */ 152 | classifier_event_t(Buffer){ 153 | //event pose recieved 154 | if(Buffer.readUInt8(0) == 3){ 155 | return { 156 | type: Buffer.readUInt8(0), 157 | pose: Buffer.readUInt16LE(1) 158 | }; 159 | // arm synced TODO: Not tested 160 | } else if (Buffer.readUInt8(0) == 1){ 161 | return { 162 | type: Buffer.readUInt8(0), 163 | arm: Buffer.readUInt8(1), 164 | x_direction: Buffer.readUInt8(2) 165 | }; 166 | // syc failed TODO: Not tested 167 | } else if(Buffer.readUInt8(0) == 6){ 168 | return { 169 | type: Buffer.readUInt8(0), 170 | sync_result: Buffer.readUint8(1) 171 | }; 172 | } 173 | return { 174 | type: Buffer.readUInt8(0), 175 | pose: Buffer.readUInt16LE(1) 176 | }; 177 | 178 | } 179 | 180 | 181 | /** 182 | * Emg data, 4 streams 183 | * @param Buffer 184 | * @returns {*} 185 | */ 186 | emg_data_t(Buffer){ 187 | if(Buffer.length < 16){ 188 | //error 189 | return null; 190 | } 191 | return { 192 | sample1: [ 193 | Buffer.readInt8(0), 194 | Buffer.readInt8(1), 195 | Buffer.readInt8(2), 196 | Buffer.readInt8(3), 197 | Buffer.readInt8(4), 198 | Buffer.readInt8(5), 199 | Buffer.readInt8(6), 200 | Buffer.readInt8(7) 201 | ], 202 | sample2: [ 203 | Buffer.readInt8(8), 204 | Buffer.readInt8(9), 205 | Buffer.readInt8(10), 206 | Buffer.readInt8(11), 207 | Buffer.readInt8(12), 208 | Buffer.readInt8(13), 209 | Buffer.readInt8(14), 210 | Buffer.readInt8(15) 211 | ] 212 | } 213 | } 214 | 215 | /** 216 | * Battery info 217 | * @param Buffer 218 | * @returns {{batteryLevel: Number}} 219 | */ 220 | 221 | battery_t(Buffer){ 222 | return { 223 | batteryLevel: Buffer.readUInt8() 224 | } 225 | } 226 | 227 | } 228 | 229 | module.exports = Deserialise; -------------------------------------------------------------------------------- /lib/util/eulerAngle.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by manuel on 18-05-15. 3 | */ 4 | 5 | "use strict" 6 | class eulerAngle { 7 | constructor(x, y, z, w){ 8 | this.x = x; 9 | this.y = y; 10 | this.z = z; 11 | this.w = w; 12 | 13 | this.pitch = this.setPitch(); 14 | this.roll = this.setRoll(); 15 | this.yaw = this.setYaw(); 16 | } 17 | 18 | getAngles(){ 19 | return { 20 | pitch: this.pitch, 21 | yaw: this.yaw, 22 | roll: this.roll 23 | } 24 | } 25 | 26 | setPitch(){ 27 | return Math.asin(2.0 * (this.w * this.y - this.z * this.x)); 28 | } 29 | 30 | setYaw(){ 31 | return Math.atan2(2.0 * (this.w * this.z + this.x * this.y), 32 | 1.0 - 2.0 * (this.y * this.y + this.z * this.z)); 33 | } 34 | 35 | setRoll(){ 36 | return Math.atan2(2.0 * (this.w * this.x + this.y * this.z), 37 | 1.0 - 2.0 * (this.x * this.x + this.y * this.y)); 38 | } 39 | 40 | 41 | } 42 | 43 | module.exports = eulerAngle; -------------------------------------------------------------------------------- /lib/util/serialise.js: -------------------------------------------------------------------------------- 1 | /** 2 | This Source Code is licensed under the MIT license. If a copy of the 3 | MIT-license was not distributed with this file, You can obtain one at: 4 | http://opensource.org/licenses/mit-license.html. 5 | @author: Manuel Overdijk (manueloverdijk) 6 | @license MIT 7 | @copyright Manuel Overdijk, 2015 8 | */ 9 | 10 | "use strict" 11 | 12 | class Serialise { 13 | constructor() { 14 | 15 | this.emg_mode_t = { 16 | 'emg_mode_none' : 0x00, ///< Do not send EMG data. 17 | 'emg_mode_send_emg' : 0x02, ///< Send filtered EMG data. 18 | 'emg_mode_send_emg_raw': 0x03 ///< Send raw (unfiltered) EMG data. 19 | }; 20 | 21 | this.imu_mode_t = { 22 | 'imu_mode_none': 0x00, ///< Do not send IMU data or events. 23 | 'imu_mode_send_data': 0x01, ///< Send IMU data streams (accelerometer, gyroscope, and orientation). 24 | 'imu_mode_send_events': 0x02, ///< Send motion events detected by the IMU (e.g. taps). 25 | 'imu_mode_send_all': 0x03, ///< Send both IMU data streams and motion events. 26 | 'imu_mode_send_raw': 0x04 ///< Send raw IMU data streams. 27 | }; 28 | 29 | this.classifier_mode_t = { 30 | 'classifier_mode_disabled': 0x00, ///< Disable and reset the internal state of the onboard classifier. 31 | 'classifier_mode_enabled': 0x01 ///< Send classifier events (poses and arm events). 32 | }; 33 | 34 | this.unlock_type_t = { 35 | 'unlock_lock': 0x00, ///< Re-lock immediately. 36 | 'unlock_timed': 0x01, ///< Unlock now and re-lock after a fixed timeout. 37 | 'unlock_hold': 0x02 ///< Unlock now and remain unlocked until a lock command is received. 38 | 39 | }; 40 | 41 | } 42 | 43 | /** 44 | * Command for turning on EMG/IMU/Classifier characteristics 45 | * @returns {Buffer} 46 | */ 47 | command_set_mode(){ 48 | // TODO: Let dev choose 49 | let emg_mode = this.emg_mode_t.emg_mode_send_emg; 50 | let imu_mode = this.imu_mode_t.imu_mode_send_all; 51 | let classifier_mode = this.classifier_mode_t.classifier_mode_enabled; 52 | 53 | return new Buffer([0x01,3,emg_mode,imu_mode,classifier_mode]); 54 | } 55 | 56 | /** 57 | * Vibrate command 58 | * @param time 59 | * @returns {Buffer} 60 | */ 61 | vibrate_t(time){ 62 | if(time == 1) return new Buffer([0x03,1,0x01]); 63 | if(time == 2) return new Buffer([0x03,1,0x02]); 64 | if(time == 3) return new Buffer([0x03,1,0x03]); 65 | return new Buffer([0x03,1,0x00]); 66 | 67 | } 68 | 69 | /** 70 | * Set the sleep mode of the Myo 71 | * @param never_sleep 72 | * @returns {Buffer} 73 | */ 74 | 75 | set_sleep_mode_t(never_sleep) { 76 | if (never_sleep) return new Buffer([0x09, 1, 0x01]); 77 | return new Buffer([0x09, 1, 0x00]); 78 | 79 | } 80 | 81 | /** 82 | * Set unlock mode of the Myo 83 | * @param mode 84 | * @returns {Buffer} 85 | */ 86 | 87 | set_unlock_mode(mode){ 88 | if(mode == 1) return new Buffer([0x0a,1,this.unlock_type_t.unlock_lock]); // re-lock immediatly 89 | if(mode == 2) return new Buffer([0x0a,1,this.unlock_type_t.unlock_timed]); // unlock now and relock after a fixed timeout 90 | return new Buffer([0x0a,1,this.unlock_type_t.unlock_hold]); // unlock now and relock after lock command is recieved 91 | } 92 | 93 | /** 94 | * User action 95 | * @returns {Buffer} 96 | */ 97 | 98 | user_action(){ 99 | return new Buffer([0x0b, 1, 0x00]); 100 | } 101 | 102 | command_header_t(){ 103 | 104 | } 105 | 106 | } 107 | module.exports = Serialise; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "MyoNodeBluetooth", 3 | "version": "0.1.0", 4 | "description": "Myo bluetooth library for Node.JS using Noble", 5 | "main": "index.js", 6 | "dependencies": { 7 | "bluebird": "^2.9.25", 8 | "debug": "^2.1.3", 9 | "noble": "0.3.8", 10 | "promiseq": "^0.1.0" 11 | }, 12 | "repository": { 13 | "type": "git", 14 | "url": "https://github.com/ManuelOverdijk/MyoNodeBluetooth.git" 15 | }, 16 | "author": "Manuel Overdijk", 17 | "license": "ISC", 18 | "maintainers": [ 19 | { 20 | "name": "Manuel Overdijk", 21 | "email": "manuel.overdijk@gmail.com" 22 | } 23 | ] 24 | } 25 | --------------------------------------------------------------------------------