Categories
Electronics

Upgrading a node module that wraps C code

Today I wanted to get this ultra-thin LED matrix working with Raspberry Pi in NodeJS as part of my work on my new ST4i workshop.

Ultrathin RGB Matrix_02

The SeeedStudio Ultrathin Red LED matrix.

It’s such a nice bright matrix, I really love it, but they’re a little weird to work with. They use the HUB75 protocol which scans across the screen doing a few pixels at a time, so they tend to look super flickery on a standard slow Arduino. But there’s this C++ library for Raspberry Pi that is way faster and some nice person has wrapped it in a Node.js module.

Although I got the C++ code working, I couldn’t install the Node module – the compilation process failed. After some exploration, I realised that the module was designed for older versions of Node (I think a maximum of 0.12.0) and I’m running 5.4.1.

I tried to revert my Node version with n but that seemed temperamental on the Raspberry Pi ARM versions of node. I couldn’t get it working, and it didn’t feel right to switch Node versions if I didn’t have to.

So plan B – I set to work updating the node module. There’s lots of documentation on how to wrap C++ code into Node addons and I have to admit, it’s pretty intense! At least for me, as I haven’t really got much experience of the V8 engine.

But I figured it out – the main issues were how objects are passed back and forth between the JavaScript and C++. It works! But sadly I think it’s probably broken in older versions.

I really should use the Native Abstractions which is a way to wrap C++ code that will remain compatible with past and future versions of Node.js, but one thing at a time. I’ll take a look at that next.

[UPDATE here’s a link to my fork of the repo]

[UPDATE 2 Now rewritten to work with Nan! So is hopefully good for Node.js versions 0.8, 0.10, 0.12, 4 and 5. here’s a link to my fork of the repo, pull request pending.]

[UPDATE 3 My pull request has been accepted and the main repo is now updated.]

In the meantime, enjoy the particles!