Created
October 12, 2016 00:52
-
-
Save jamesbulpin/70081281ccee3e47e25789eccff6ed5a to your computer and use it in GitHub Desktop.
*Really* rough prototype Octoblu connector to drive a USBMicro U421 device connected to a novelty button
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var meshblu = require('meshblu'); | |
var meshbluJSON = require("./meshblu.json"); | |
// meshblu.json must contain the UUID and token for a generic device created | |
// within Octoblu: {"server":"meshblu.octoblu.com","port":80,"uuid":"56<redacted>c0","token":"93<redacted>42"} | |
// Specifies how you want your message payload to be passed | |
// from Octoblu to your device | |
var MESSAGE_SCHEMA = { | |
type: 'object', | |
properties: { | |
} | |
}; | |
// Not specifying a UUID/Token auto-registers a new device | |
var conn = meshblu.createConnection({ | |
"uuid": meshbluJSON.uuid, | |
"token": meshbluJSON.token, | |
"server": "meshblu.octoblu.com", | |
"port": 80 | |
}); | |
conn.on('notReady', function(data){ | |
console.log('Octoblu UUID FAILED AUTHENTICATION!'); | |
console.log(data); | |
}); | |
var usb = require('usb') | |
// Find the U421 device by VID/PID, assume there is only one of them | |
var device = usb.findByIds(0x0de7, 0x01a5) | |
device.open() | |
var intf = device.interface(0) | |
if (intf.isKernelDriverActive()) { | |
intf.detachKernelDriver() | |
} | |
intf.claim() | |
var ep = intf.endpoint(129) | |
ep.on("error", function(err) { | |
console.log("Endpoint error: " + err); | |
}); | |
ep.on("data", function(data) { | |
console.log("Rx: " + data.toJSON()); | |
}); | |
ep.on("end", function() { | |
console.log("end"); | |
}); | |
console.log(ep.transferType); | |
console.log(ep); | |
//ep.startPoll(); | |
// XXX still not happy with the receive handling here | |
function ctCallback(err,data){ | |
if (err) {console.log("err " + err);} | |
if (data) {console.log("data " + data.toJSON());} | |
} | |
// Reset the U421 | |
device.controlTransfer(0x21, 9, 0x0200, 0, new Buffer([0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]), ctCallback); | |
ep.transfer(8, ctCallback); | |
// Direction (input/output) for the U421 ports | |
device.controlTransfer(0x21, 9, 0x0200, 0, new Buffer([0x09,0xfe,0xfe,0x00,0x00,0x00,0x00,0x00]), ctCallback); | |
ep.transfer(8, ctCallback); | |
device.controlTransfer(0x21, 9, 0x0200, 0, new Buffer([0x0a,0xff,0xff,0x00,0x00,0x00,0x00,0x00]), ctCallback); | |
ep.transfer(8, ctCallback); | |
// Set the output pin (A1) high to start with | |
device.controlTransfer(0x21, 9, 0x0200, 0, new Buffer([0x03,0xff,0x02,0x00,0x00,0x00,0x00,0x00]), ctCallback); | |
ep.transfer(8, ctCallback); | |
device.controlTransfer(0x21, 9, 0x0200, 0, new Buffer([0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00]), ctCallback); | |
ep.transfer(8, ctCallback); | |
device.controlTransfer(0x21, 9, 0x0200, 0, new Buffer([0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00]), ctCallback); | |
ep.transfer(8, ctCallback); | |
conn.on('ready', function(rdata){ | |
console.log('UUID AUTHENTICATED!'); | |
console.log(rdata); | |
conn.update({ | |
"uuid": meshbluJSON.uuid, | |
"token": meshbluJSON.token, | |
"messageSchema": MESSAGE_SCHEMA, | |
}); | |
conn.on('message', function(data){ | |
console.log('Octoblu message received'); | |
console.log(data); | |
// Lower and then raise A1 to simluate a button push to trigger the device | |
device.controlTransfer(0x21, 9, 0x0200, 0, new Buffer([0x03,0xfd,0x00,0x00,0x00,0x00,0x00,0x00]), ctCallback); | |
setTimeout(function(){ | |
device.controlTransfer(0x21, 9, 0x0200, 0, new Buffer([0x03,0xff,0x02,0x00,0x00,0x00,0x00,0x00]), ctCallback); | |
}, 100); | |
}); | |
}); | |
var lastButton = 0; | |
function rdCallback(err,data){ | |
if (err) { | |
console.log("err " + err); | |
} | |
else if (data) { | |
//console.log("data " + data.toJSON()); | |
var button = (data[1]&0x01)?1:0; | |
if (button != lastButton) { | |
if (button) { | |
console.log("Button pushed"); | |
conn.message({ | |
"devices": "*" | |
}); | |
} | |
} | |
lastButton = button; | |
} | |
} | |
// Polling loop for the input pin (A0) | |
setInterval(function() { | |
device.controlTransfer(0x21, 9, 0x0200, 0, new Buffer([0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00]), ctCallback); | |
ep.transfer(8, rdCallback); | |
}, 100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment