Following on from last weekends post about adding a pause node to Node-Red this weekend I’ve been trying to create a node to support my Digispark RGB USB LED.
The RGB shield for the Digispark came with a sketch and a some python code to send colours to the device using the HID protocol. I had a bit of a poke round and I found node-hid and thought things where going to be easy.
Unfortunately just trying to duplicate the python code in nodejs didn’t seam to work no matter what I tried.
After a bit more digging I found this blog post by Dougal Campbell who looked to have been pursuing a similar idea a little earlier in the year. I couldn’t see any follow up post on his blog but a quick comment later and I had pointers to what he’d managed to get working. I was hoping to leave the default sketch on the digiSpark but it needed a little change to the USB library to make things work properly, you can find the changes here.
88-digiRGB.js
// Require main module var RED = require("../../red/red"); var HID = require('node-hid'); var device; var node; // The main node definition - most things happen in here function DigiRGBNode(n) { // Create a RED node RED.nodes.createNode(this,n); node=this; //look up the matching devices var devices = HID.devices(0x16c0,0x05df); for (var i=0; i< devices.length; i++) { if (devices[i].product == 'DigiUSB') { path = devices[i].path; node.log("found: " + path); try { device = new HID.HID(devices[i].path); //only work with the first one found break; } catch (e) { node.log(e) } } } if (device) { this.on("input", function(msg) { if (msg != null) { var args = msg.payload.split(','); if (args.length == 3) { device.sendFeatureReport([115,parseInt(args[0]),parseInt(args[1]),parseInt(args[2])]); } } }); } else { node.warn("no digispark RGB found"); } } // Register the node by name. This must be called before overriding any of the // Node functions. RED.nodes.registerType("digiRGB",DigiRGBNode); DigiRGBNode.prototype.close = function() { // Called when the node is shutdown - eg on redeploy. // Allows ports to be closed, connections dropped etc. // eg: this.client.disconnect(); device.close(); }
If you want to have a play unzip 78-digiRGB.zip in the node-red directory to unpack the whole node into the nodes/hardware directory. I have now saved this a Github GIST which you can check out directly into /node-red/nodes/hardware directory as follows:
[user@node node-red]$ git clone https://gist.github.com/6573158.git
You will also need to run npm import node-hid in the node-red directory to add the dependency.
(Remember you will need to modify the DigiUSB.cpp and the usbconfig.h in the DigisparkUSB example as mentioned in the link above).
This is unlikely to make it into core Node-RED as the hardware is a little specialist, but if Nick and Dave decide to host a collection of hardware nodes it may end up in there.