2015-01-02
Getting NodeJS to draw and interact with the PiTFT on the Raspberry Pi

I’m trying to get a NodeJS (not my favorite environment) environment working on the rPi. I want an interactive graphic library that let’s me test on MacOSX and on the RaspberryPi using the PiTFT. Pygame WILL work (more on that later), but I’d like a NodeJS equivalent. Looked into SDL (Simple Direct Media Layer, or something like that). Found a NodeJS binding and figured out how to install it on MacOSX (Mavericks via homebrew and npm). I’m going to write down the magic before I forget.

I may end up abandoning a pure-NodeJS strategy if it is too much trouble and may revert to building a Pygame-based server for the UI, and developing a protocol between that server and a NodeJS program running on the rPi.

Building SDL2 for the Mac

I’m trying to get a NodeJS (not my favorite environment) environment working on the rPi. I want an interactive graphic library that let’s me test on MacOSX and on the RaspberryPi using the PiTFT. Pygame WILL work (more on that later), but I’d like a NodeJS equivalent. Looked into SDL (Simple Direct Media Layer, or something like that). Found a NodeJS binding and figured out how to install it on MacOSX (Mavericks via homebrew and npm). Going to write down the magic before I forget (haven’t test on the pi yet):

  • brew install sdl2
  • brew install sdl2_image
  • brew install sdl2_ttf
  • npm install –save node-sdl-runtime

Create a file test.js:

var SDL = require('node-sdl-runtime');

SDL.init(SDL.INIT.EVERYTHING);

var window = new SDL.Window("Hello World Window", SDL.WINDOWPOS.CENTERED, SDL.WINDOWPOS.CENTERED, 640, 480);

setTimeout(function() {
    delete window; // Clears reference of the SDL.Window instance
    SDL.quit();
}, 2000);

Run it with:

node test.js

Building SDL2 for the RaspberryPi

Useful build instructions from Stu’s Rusty Bucket

Older build instructions from Raspberry PI Site

Setting CPATH

The SDL2 headers are installed in /usr/local/include/SDL2, which needs to be included during npm install of SDL-related software. I did the following to help:

export CPATH=/usr/local/include/SDL2

This is a HACK and there must be a nicer way.

References

Now gotta do the same for the rPi.