Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to use Nan and multiple versions of Node.js up to 5 #5

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"target_name": "node-rpi-rgb-led-matrix",
"sources": [ "src/base.cc", "src/ledmatrix.cc" ],
"dependencies": [ "./binding.gyp:rpi-rgb-led-matrix" ],
"include_dirs": [ "./external/matrix/include", "./include/" ]
"include_dirs": [ "./external/matrix/include", "./include/", "<!(node -e \"require('nan')\")" ]
},
{
"target_name": "rpi-rgb-led-matrix",
Expand Down
38 changes: 19 additions & 19 deletions include/ledmatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,35 @@
#define NODE_LED_MATRIX_H

#include <node.h>
#include <node_object_wrap.h>
#include <nan.h>

#include <led-matrix.h>

using namespace v8;
using namespace node;
using namespace rgb_matrix;
using rgb_matrix::GPIO;


/**
* Class: LedMatrix
* Wrapper for rpi-rgb-led-matrix.
*/
class LedMatrix : public ObjectWrap {
class LedMatrix : public node::ObjectWrap {
public:
/**
* Variable: constructor_template
* Variable: constructor
* Used to create nodejs constructor.
*/
static v8::Persistent<v8::FunctionTemplate> constructor_template;
static Nan::Persistent<v8::Function> constructor;

/**
* Function: Initialize
* Used to intialize the EventEmitter from Node.js
*
* Parameters:
* target - v8::Object the Node.js global module object
*/
static void Initialize(v8::Handle<v8::Object> target);
* Function: Initialize
* Used to intialize the EventEmitter from Node.js
*
* Parameters:
* target - v8::Object the Node.js global module object
*/
static void Init(v8::Local<v8::Object> exports);

int GetWidth();
int GetHeight();
Expand All @@ -46,16 +47,15 @@ class LedMatrix : public ObjectWrap {

virtual ~LedMatrix();

static v8::Handle<v8::Value> New(const v8::Arguments& args);
static void New(const Nan::FunctionCallbackInfo<v8::Value>& args);

static v8::Handle<v8::Value> GetWidth(const v8::Arguments& args);
static v8::Handle<v8::Value> GetHeight(const v8::Arguments& args);
static v8::Handle<v8::Value> SetPixel(const v8::Arguments& args);
static v8::Handle<v8::Value> Clear(const v8::Arguments& args);
static v8::Handle<v8::Value> Fill(const v8::Arguments& args);
static void GetWidth(const Nan::FunctionCallbackInfo<v8::Value>& args);
static void GetHeight(const Nan::FunctionCallbackInfo<v8::Value>& args);
static void SetPixel(const Nan::FunctionCallbackInfo<v8::Value>& args);
static void Clear(const Nan::FunctionCallbackInfo<v8::Value>& args);
static void Fill(const Nan::FunctionCallbackInfo<v8::Value>& args);

private:
v8::Handle<v8::Object> self;

GPIO io;
RGBMatrix* matrix;
Expand Down
30 changes: 20 additions & 10 deletions package.json
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,33 @@
"version": "0.0.2",
"description": "control led matrix with nodejs on raspberry pi. Nodejs binding of https://github.com/hzeller/rpi-rgb-led-matrix",
"keywords": [
"raspberry", "raspberrypi", "pi", "rpi", "ledmatrix",
"rpi-rgb-led-matrix", "led-matrix"
"raspberry",
"raspberrypi",
"pi",
"rpi",
"ledmatrix",
"rpi-rgb-led-matrix",
"led-matrix"
],
"homepage": "https://github.com/zeitungen/node-rpi-rgb-led-matrix/",
"bugs": {"url": "https://github.com/zeitungen/node-rpi-rgb-led-matrix/issues"},
"bugs": {
"url": "https://github.com/zeitungen/node-rpi-rgb-led-matrix/issues"
},
"author": {
"name": "Maxime Journaux",
"email": "journaux.maxime@gmail.com",
"url": "https://github.com/zeitungen/"
"name": "Maxime Journaux",
"email": "journaux.maxime@gmail.com",
"url": "https://github.com/zeitungen/"
},
"repository": {
"type": "git",
"url": "https://github.com/zeitungen/node-rpi-rgb-led-matrix/"
"type": "git",
"url": "https://github.com/zeitungen/node-rpi-rgb-led-matrix/"
},
"main": "index.js",
"license": "WTFPL",
"engines" : {
"node" : ">=0.10.3 <0.12"
"engines": {
"node": ">=0.10.3"
},
"dependencies": {
"nan": "^2.2.0"
}
}
7 changes: 3 additions & 4 deletions src/base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
* Copyright 2015, Maxime Journaux <journaux.maxime@gmail.com>
*/

#include <node.h>
#include <v8.h>
#include <nan.h>

#include <ledmatrix.h>

void init(Handle<v8::Object> target) {
LedMatrix::Initialize(target);
void init(v8::Local<v8::Object> exports) {
LedMatrix::Init(exports);
}

NODE_MODULE(node_rpi_rgb_led_matrix, init);
114 changes: 54 additions & 60 deletions src/ledmatrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ using namespace node;
using namespace rgb_matrix;
using rgb_matrix::GPIO;

Nan::Persistent<v8::Function> LedMatrix::constructor;

LedMatrix::LedMatrix(int rows, int chained_displays, int parallel_displays) {
assert(io.Init());
matrix = new RGBMatrix(&io, rows, chained_displays, parallel_displays);
Expand All @@ -25,22 +27,24 @@ LedMatrix::~LedMatrix() {
delete matrix;
}

void LedMatrix::Initialize(Handle<v8::Object> target) {
HandleScope handle;

Local<FunctionTemplate> t = FunctionTemplate::New(LedMatrix::New);
void LedMatrix::Init(v8::Local<v8::Object> exports) {

constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
constructor_template->SetClassName(String::NewSymbol("LedMatrix"));
Nan::HandleScope scope;

v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);

tpl->SetClassName(Nan::New("LedMatrix").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);

NODE_SET_PROTOTYPE_METHOD(constructor_template, "getWidth", GetWidth);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "getHeight", GetHeight);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "setPixel", SetPixel);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "clear", Clear);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "fill", Fill);
Nan::SetPrototypeMethod(tpl, "getWidth", GetWidth);
Nan::SetPrototypeMethod(tpl, "getHeight", GetHeight);
Nan::SetPrototypeMethod(tpl, "setPixel", SetPixel);
Nan::SetPrototypeMethod(tpl, "clear", Clear);
Nan::SetPrototypeMethod(tpl, "fill", Fill);

constructor.Reset(tpl->GetFunction());

target->Set(String::NewSymbol("LedMatrix"), constructor_template->GetFunction());
exports->Set(Nan::New("LedMatrix").ToLocalChecked(), tpl->GetFunction());
}

int LedMatrix::GetWidth() {
Expand All @@ -63,60 +67,57 @@ void LedMatrix::Fill(uint8_t r, uint8_t g, uint8_t b) {
matrix->Fill(r, g, b);
}

Handle<Value> LedMatrix::New(const Arguments& args) {
HandleScope scope;
void LedMatrix::New(const Nan::FunctionCallbackInfo<Value>& args) {

assert(args.IsConstructCall());
// throw an error if it's not a constructor
if (!args.IsConstructCall()) {
Nan::ThrowError("LedMatrix::must be called as a constructor with 'new' keyword");
}

int rows = 32;
int chained = 1;
int parallel = 1;
// grab parameters
int rows = 32;
int chained = 1;
int parallel = 1;

if(args.Length() > 0 && args[0]->IsNumber()) {
rows = args[0]->ToInteger()->Value();
}

if(args.Length() > 1 && args[1]->IsNumber()) {
chained = args[1]->ToInteger()->Value();
}

if(args.Length() > 2 && args[2]->IsNumber()) {
parallel = args[2]->ToInteger()->Value();
}


rows = args[0]->ToInteger()->Value();
}
if(args.Length() > 1 && args[1]->IsNumber()) {
chained = args[1]->ToInteger()->Value();
}
if(args.Length() > 2 && args[2]->IsNumber()) {
parallel = args[2]->ToInteger()->Value();
}

// make the matrix
LedMatrix* matrix = new LedMatrix(rows, chained, parallel);
matrix->Wrap(args.This());

matrix->self = Persistent<Object>::New(args.This());

return scope.Close(args.This());
// return this object
args.GetReturnValue().Set(args.This());
}

Handle<Value> LedMatrix::GetWidth(const v8::Arguments& args) {
HandleScope scope;

LedMatrix* matrix = ObjectWrap::Unwrap<LedMatrix>(args.This());
void LedMatrix::GetWidth(const Nan::FunctionCallbackInfo<v8::Value>& args) {

return Integer::New(matrix->GetWidth());
LedMatrix* matrix = ObjectWrap::Unwrap<LedMatrix>(args.Holder());

args.GetReturnValue().Set(Nan::New<v8::Number>(matrix->GetWidth()));
}

Handle<Value> LedMatrix::GetHeight(const Arguments& args) {
HandleScope scope;
void LedMatrix::GetHeight(const Nan::FunctionCallbackInfo<v8::Value>& args) {

LedMatrix* matrix = ObjectWrap::Unwrap<LedMatrix>(args.This());
LedMatrix* matrix = ObjectWrap::Unwrap<LedMatrix>(args.Holder());

return Integer::New(matrix->GetHeight());
args.GetReturnValue().Set(Nan::New<v8::Number>(matrix->GetHeight()));
}

Handle<Value> LedMatrix::SetPixel(const Arguments& args) {
HandleScope scope;
void LedMatrix::SetPixel(const Nan::FunctionCallbackInfo<v8::Value>& args) {

LedMatrix* matrix = ObjectWrap::Unwrap<LedMatrix>(args.This());
LedMatrix* matrix = ObjectWrap::Unwrap<LedMatrix>(args.Holder());

if(!args.Length() == 5 || !args[0]->IsNumber() || !args[1]->IsNumber() || !args[2]->IsNumber()
|| !args[3]->IsNumber() || !args[4]->IsNumber()) {
return ThrowException(Exception::Error(String::New("Parameters error")));
Nan::ThrowTypeError("Wrong parameters! Expects 5 numbers");
}

int x = args[0]->ToInteger()->Value();
Expand All @@ -127,34 +128,27 @@ Handle<Value> LedMatrix::SetPixel(const Arguments& args) {

matrix->SetPixel(x, y, r, g, b);

return Undefined();
}

Handle<Value> LedMatrix::Clear(const Arguments& args) {
HandleScope scope;
void LedMatrix::Clear(const Nan::FunctionCallbackInfo<v8::Value>& args) {

LedMatrix* matrix = ObjectWrap::Unwrap<LedMatrix>(args.This());
LedMatrix* matrix = ObjectWrap::Unwrap<LedMatrix>(args.Holder());

matrix->Clear();

return Undefined();
}

Handle<Value> LedMatrix::Fill(const Arguments& args) {
HandleScope scope;
void LedMatrix::Fill(const Nan::FunctionCallbackInfo<v8::Value>& args) {

LedMatrix* matrix = ObjectWrap::Unwrap<LedMatrix>(args.This());
LedMatrix* matrix = ObjectWrap::Unwrap<LedMatrix>(args.Holder());

if(!args.Length() == 3 || !args[0]->IsNumber() || !args[1]->IsNumber() || !args[2]->IsNumber()) {
return ThrowException(Exception::Error(String::New("Parameters error")));
}
Nan::ThrowTypeError("Wrong parameters! Expects 3 numbers");
}

int r = args[0]->ToInteger()->Value();
int g = args[1]->ToInteger()->Value();
int b = args[2]->ToInteger()->Value();
matrix->Fill(r, g, b);

return Undefined();
}

Persistent<FunctionTemplate> LedMatrix::constructor_template;