From 6fe0e6682db1abe8ff6eecc3475f6250dafe7e3d Mon Sep 17 00:00:00 2001 From: Adam Greenwood-Byrne Date: Wed, 12 Aug 2020 18:58:10 +0100 Subject: [PATCH] Added simple game controller is Node.JS using Bleno --- .../controller/characteristic.js | 52 ++++++++++++++++++ part8-breakout-ble/controller/main.js | 53 +++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 part8-breakout-ble/controller/characteristic.js create mode 100644 part8-breakout-ble/controller/main.js diff --git a/part8-breakout-ble/controller/characteristic.js b/part8-breakout-ble/controller/characteristic.js new file mode 100644 index 0000000..d898536 --- /dev/null +++ b/part8-breakout-ble/controller/characteristic.js @@ -0,0 +1,52 @@ +var util = require('util'); + +var bleno = require('bleno-mac'); + +var BlenoCharacteristic = bleno.Characteristic; + +var EchoCharacteristic = function() { + EchoCharacteristic.super_.call(this, { + uuid: 'ec0e', + properties: ['read', 'write', 'notify'], + value: null + }); + + this._value = new Buffer(0); + this._updateValueCallback = null; +}; + +util.inherits(EchoCharacteristic, BlenoCharacteristic); + +EchoCharacteristic.prototype.onReadRequest = function(offset, callback) { + console.log('EchoCharacteristic - onReadRequest: value = ' + this._value.toString('hex')); + + callback(this.RESULT_SUCCESS, this._value); +}; + +EchoCharacteristic.prototype.onWriteRequest = function(data, offset, withoutResponse, callback) { + this._value = data; + + console.log('EchoCharacteristic - onWriteRequest: value = ' + this._value.toString('hex')); + + if (this._updateValueCallback) { + console.log('EchoCharacteristic - onWriteRequest: notifying'); + + this._updateValueCallback(this._value); + } + + callback(this.RESULT_SUCCESS); +}; + +EchoCharacteristic.prototype.onSubscribe = function(maxValueSize, updateValueCallback) { + console.log('EchoCharacteristic - onSubscribe'); + + this._updateValueCallback = updateValueCallback; +}; + +EchoCharacteristic.prototype.onUnsubscribe = function() { + console.log('EchoCharacteristic - onUnsubscribe'); + + this._updateValueCallback = null; +}; + +module.exports = EchoCharacteristic; diff --git a/part8-breakout-ble/controller/main.js b/part8-breakout-ble/controller/main.js new file mode 100644 index 0000000..22fa8b9 --- /dev/null +++ b/part8-breakout-ble/controller/main.js @@ -0,0 +1,53 @@ +var bleno = require('bleno-mac'); + +var BlenoPrimaryService = bleno.PrimaryService; + +var EchoCharacteristic = require('./characteristic'); + +console.log('bleno - echo'); + +var e = new EchoCharacteristic(); + +bleno.on('stateChange', function(state) { + console.log('on -> stateChange: ' + state); + + if (state === 'poweredOn') { + bleno.startAdvertising('echo', ['ec00']); + } else { + bleno.stopAdvertising(); + } +}); + +bleno.on('advertisingStart', function(error) { + console.log('on -> advertisingStart: ' + (error ? 'error ' + error : 'success')); + + if (!error) { + bleno.setServices([ + new BlenoPrimaryService({ + uuid: 'ec00', + characteristics: [ + e + ] + }) + ]); + } +}); + +var ioHook = require('iohook'); + +var buf = Buffer.allocUnsafe(1); +var obuf = Buffer.allocUnsafe(1); +const scrwidth = 1680; +const divisor = scrwidth / 100; + +ioHook.on( 'mousemove', event => { + buf.writeUInt8(Math.round(event.x / divisor), 0); + + if (Buffer.compare(buf, obuf)) { + e._value = buf; + if (e._updateValueCallback) e._updateValueCallback(e._value); + buf.copy(obuf); + } +}); + +ioHook.start();