C++ Node JS Module for SPI Bus LED Signs

In the past couple of posts, I have documented how I built a LED Message board using some modules from Sure Electronics and then wrote a C program to scroll messages across it. This of course cool, but to really make it insteresting you need interesting messages to scroll. To get interesting messages you have to get connected. Since the Raspberry Pi is Unix based, you could easily start piping programs togther. I wanted someting a little more complicated though so I decided to write a Node JS program. In order to write to hardware devices like the Message Board you need to put together a Module and most of the time it will have to have some C++ code to do it, and that is a exactly what I did.

Node JS is an Ascrynous Programming langauge. This means that there is generally only one thing happening at a time and you wait for it to finish before doing the next. If you need to something that might take a long time, (Blocking in Comp Sci terms), you pass it a function to call when it is done and then go onto the next item.

For my module I started with the C program I had written and converted it into a C++ class. Node JS is based on the Google V8 Javascript engine, which is written in C++. Once I the C++ class written, I wrapped it so that it could be used in Node. This was actually pretty tricky to do. The V8 engine has a lot of specialized Macros and structure to help with memory management. My module also had to support running in background so that it could scroll a message without stopping the other things the main program needed to do. Again, this was a bit tricky because you had to make sure you working with only items that would be available in the background context. I don’t have any advice to offer other than Google around a bit.

The final tid-bit is that I wanted to add in support for Events. For me Events made it easier to write a program around instead of using Call Back functions. However for this to work, I couldn’t create a pure C++ module because the Node JS events are Javascript based. In order to get this I had to create a small Javascript wrapper around my custom C++ code that inherited the Event Emitter class.

The basic structure for the overall program is:

  1. Create an array of message you wish to display
  2. Create a function that is tied to the Scrolling Complete event from your Message Board, and will start scrolling the next message in the array. Also update the Array with any new messages.
  3. Start scrolling the First message.

Of course the whole point of doing this was to scroll interesting messages from the Internet. To get those “interesting messages”, I built a couple of tiny Node Module to pull NPR and ESPN Headlines.

So there you have it! A simple way to scroll messages off the Internet and impress (annoy) your co-workers.

Here is the code:

  1. Pi-Led: GithubNPM
  2. espn-headlines: GithubNPM
  3. npr-news: GithubNPM

Posted

in

by

Tags:

Comments

3 responses to “C++ Node JS Module for SPI Bus LED Signs”

  1. Dan Avatar
    Dan

    Sorry to ask an obvious question, but after installing nodejs and npm, how do you kick off, say, the npr-news module? “nodejs npr-news.js” doesn’t do anything, and I get the following when trying to run pi-led.js:

    Error: Cannot find module ‘/home/pi/python/pi-led-master/build/Release/PiLed.node’

  2. Vidar S. Ramdal Avatar
    Vidar S. Ramdal

    I’m trying to follow your steps here, but I’m stuck at installing Pi-Led.

    When running npm install, I get:

    ../pi-led.cpp: In static member function 'static v8::Handle {anonymous}::PiLed::WriteMessage(const v8::Arguments&)':
    ../pi-led.cpp:508:77: error: invalid conversion from 'void (*)(uv_work_t*) {aka void (*)(uv_work_s*)}' to 'uv_after_work_cb {aka void (*)(uv_work_s*, int)}' [-fpermissive]
    /usr/local/include/node/uv.h:1438:15: error: initializing argument 4 of 'int uv_queue_work(uv_loop_t*, uv_work_t*, uv_work_cb, uv_after_work_cb)' [-fpermissive]
    PiLed.target.mk:83: recipe for target 'Release/obj.target/PiLed/pi-led.o' failed
    make: *** [Release/obj.target/PiLed/pi-led.o] Error 1

    Would be grateful for any hints on how to work around this.

  3. Vidar S. Ramdal Avatar
    Vidar S. Ramdal

    Never mind, I found the issue at GitHub, and editing the line #509 as suggested by ptitb in the comment did the trick.

    I forked your repository and created a pull request for the issue.