Added simple game controller is Node.JS using Bleno

This commit is contained in:
Adam Greenwood-Byrne 2020-08-12 18:58:10 +01:00
parent 21681400ae
commit 6fe0e6682d
2 changed files with 105 additions and 0 deletions

View file

@ -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;

View file

@ -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();