Author Topic: Control Digispark via nodejs?  (Read 6302 times)

dougal

  • Sr. Member
  • ****
  • Posts: 289
Control Digispark via nodejs?
« on: February 06, 2013, 11:39:43 am »
Has anybody gotten communications with the Digispark working using nodejs?


I've just started looking at some libraries (duino, node-usb), but the differences between DS and "full" Arduino are stumping me. The duino library wants you to load a program on the DS that uses the Servo library, so that won't work out-of-the-box.


Any pointers? I'd just like a really basic example that will let me, for example, turn the on-board LED on and off using USB communications, based on events (keyboard input or whatever) on the nodejs side.


Hizzle

  • Newbie
  • *
  • Posts: 4
Re: Control Digispark via nodejs?
« Reply #1 on: February 06, 2013, 12:55:32 pm »


One simple solution would be using
  var exec = require('child_process').exec;
Of node to send a command to a terminal script. For example a ruby script using the ruby gem DigiUSB written by bluebie.

dougal

  • Sr. Member
  • ****
  • Posts: 289
Re: Control Digispark via nodejs?
« Reply #2 on: February 06, 2013, 01:17:24 pm »


One simple solution would be using
  var exec = require('child_process').exec;
Of node to send a command to a terminal script. For example a ruby script using the ruby gem DigiUSB written by bluebie.


I'd rather not do that. If I was going to go that route, I might as well just do the whole thing in Ruby and leave nodejs out of the picture. I want to use native node programming.


Thanks for the suggestion, though.


Bluebie

  • Sr. Member
  • ****
  • Posts: 486
Re: Control Digispark via nodejs?
« Reply #3 on: February 07, 2013, 10:25:42 pm »
arduino-related node modules aren't going to help you much, as they are designed for devices which expose serial ports - the digispark does not. The node-usb module sounds like the way to go, but you'll need to write a simple class to handle communicating with the device. Thankfully this isn't too complicated - digiusb has a really simple protocol designed around sending and receiving individual bytes. You should be able to hook a node stream up to it without too much fuss. It is however polling based, so you might want to use setInterval or something like that to schedule a function to poll for new bytes.


There aren't really any fantastic documents explaining the digiusb protocol yet as far as I know - I had to reverse it from the device-side code, which is a bit obtuse. Hopefully my ruby library will provide a relatively clear example of what's going on: https://github.com/Bluebie/digiusb.rb/blob/master/lib/digiusb.rb the business really happens in self.sparks, getc, putc, io, and control_transfer - most of the rest of that library is recreating the memes of IO-type objects in ruby.


If you get lost at all feel free to pester me. I'm more responsive to email than this forum generally. My email is linked on http://creativepony.com

dougal

  • Sr. Member
  • ****
  • Posts: 289
Re: Control Digispark via nodejs?
« Reply #4 on: February 08, 2013, 01:11:16 pm »
Bluebie,


Once I can at least get some basic proof-of-concept communications going in nodejs, I'll probably use your Ruby class as a template for my own JavaScript version. So thanks for that!


And as for the duino module, I'll probably fork that and try to make a Digispark compatible version. The basic idea for dynamically controlling the hardware on-the-fly is the same, I'll mainly just need to replace the communication method.


dougal

  • Sr. Member
  • ****
  • Posts: 289
Re: Control Digispark via nodejs?
« Reply #5 on: February 12, 2013, 06:15:15 am »
I haven't gotten the node-hid stuff working yet, but I did make a stripped-down version of the 'du.ino' avr-side code from the node duino module. I ripped out the Servo stuff, changed all the Serial calls to DigiUSB, and took out the ping routines for the sake of memory. I'm thinking I can save a little more memory by reducing the 'cmd' and 'pin' parameters to just one byte, which will reduce the logic required to support 2-byte data chunks.


With my Digispark-tuned du.ino code, I was able to dynamically set pin states (toggling the status LED with both digital and analog writes) via the python 'write.py' script. For example, sending '!01p101.' to turn the LED on. The first '01' is the 'digitalWrite' command, the 'p1' is Pin 1, and the last '01' is the value to write. The '!' and '.' characters are the 'start' and 'end' sentinels. I could probably dispense with those, as well, reducing the memory requirements further.


I'll update here again when I have something