diff --git a/binding.gyp b/binding.gyp index c5258d2..8a3c3dd 100755 --- a/binding.gyp +++ b/binding.gyp @@ -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/", " +#include +#include #include -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 constructor_template; + static Nan::Persistent 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 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 exports); int GetWidth(); int GetHeight(); @@ -46,16 +47,15 @@ class LedMatrix : public ObjectWrap { virtual ~LedMatrix(); - static v8::Handle New(const v8::Arguments& args); + static void New(const Nan::FunctionCallbackInfo& args); - static v8::Handle GetWidth(const v8::Arguments& args); - static v8::Handle GetHeight(const v8::Arguments& args); - static v8::Handle SetPixel(const v8::Arguments& args); - static v8::Handle Clear(const v8::Arguments& args); - static v8::Handle Fill(const v8::Arguments& args); + static void GetWidth(const Nan::FunctionCallbackInfo& args); + static void GetHeight(const Nan::FunctionCallbackInfo& args); + static void SetPixel(const Nan::FunctionCallbackInfo& args); + static void Clear(const Nan::FunctionCallbackInfo& args); + static void Fill(const Nan::FunctionCallbackInfo& args); private: - v8::Handle self; GPIO io; RGBMatrix* matrix; diff --git a/package.json b/package.json old mode 100755 new mode 100644 index 748ac10..887128d --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/base.cc b/src/base.cc index eede9cb..f129157 100644 --- a/src/base.cc +++ b/src/base.cc @@ -2,13 +2,12 @@ * Copyright 2015, Maxime Journaux */ -#include -#include +#include #include -void init(Handle target) { - LedMatrix::Initialize(target); +void init(v8::Local exports) { + LedMatrix::Init(exports); } NODE_MODULE(node_rpi_rgb_led_matrix, init); \ No newline at end of file diff --git a/src/ledmatrix.cc b/src/ledmatrix.cc index 0a2c3cd..c7711cb 100755 --- a/src/ledmatrix.cc +++ b/src/ledmatrix.cc @@ -15,6 +15,8 @@ using namespace node; using namespace rgb_matrix; using rgb_matrix::GPIO; +Nan::Persistent LedMatrix::constructor; + LedMatrix::LedMatrix(int rows, int chained_displays, int parallel_displays) { assert(io.Init()); matrix = new RGBMatrix(&io, rows, chained_displays, parallel_displays); @@ -25,22 +27,24 @@ LedMatrix::~LedMatrix() { delete matrix; } -void LedMatrix::Initialize(Handle target) { - HandleScope handle; - - Local t = FunctionTemplate::New(LedMatrix::New); +void LedMatrix::Init(v8::Local exports) { - constructor_template = Persistent::New(t); - constructor_template->InstanceTemplate()->SetInternalFieldCount(1); - constructor_template->SetClassName(String::NewSymbol("LedMatrix")); + Nan::HandleScope scope; + + v8::Local tpl = Nan::New(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() { @@ -63,60 +67,57 @@ void LedMatrix::Fill(uint8_t r, uint8_t g, uint8_t b) { matrix->Fill(r, g, b); } -Handle LedMatrix::New(const Arguments& args) { - HandleScope scope; +void LedMatrix::New(const Nan::FunctionCallbackInfo& 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::New(args.This()); - - return scope.Close(args.This()); + // return this object + args.GetReturnValue().Set(args.This()); } -Handle LedMatrix::GetWidth(const v8::Arguments& args) { - HandleScope scope; - - LedMatrix* matrix = ObjectWrap::Unwrap(args.This()); +void LedMatrix::GetWidth(const Nan::FunctionCallbackInfo& args) { - return Integer::New(matrix->GetWidth()); + LedMatrix* matrix = ObjectWrap::Unwrap(args.Holder()); + + args.GetReturnValue().Set(Nan::New(matrix->GetWidth())); } -Handle LedMatrix::GetHeight(const Arguments& args) { - HandleScope scope; +void LedMatrix::GetHeight(const Nan::FunctionCallbackInfo& args) { - LedMatrix* matrix = ObjectWrap::Unwrap(args.This()); + LedMatrix* matrix = ObjectWrap::Unwrap(args.Holder()); - return Integer::New(matrix->GetHeight()); + args.GetReturnValue().Set(Nan::New(matrix->GetHeight())); } -Handle LedMatrix::SetPixel(const Arguments& args) { - HandleScope scope; +void LedMatrix::SetPixel(const Nan::FunctionCallbackInfo& args) { - LedMatrix* matrix = ObjectWrap::Unwrap(args.This()); + LedMatrix* matrix = ObjectWrap::Unwrap(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(); @@ -127,34 +128,27 @@ Handle LedMatrix::SetPixel(const Arguments& args) { matrix->SetPixel(x, y, r, g, b); - return Undefined(); } -Handle LedMatrix::Clear(const Arguments& args) { - HandleScope scope; +void LedMatrix::Clear(const Nan::FunctionCallbackInfo& args) { - LedMatrix* matrix = ObjectWrap::Unwrap(args.This()); + LedMatrix* matrix = ObjectWrap::Unwrap(args.Holder()); matrix->Clear(); - - return Undefined(); } -Handle LedMatrix::Fill(const Arguments& args) { - HandleScope scope; +void LedMatrix::Fill(const Nan::FunctionCallbackInfo& args) { - LedMatrix* matrix = ObjectWrap::Unwrap(args.This()); + LedMatrix* matrix = ObjectWrap::Unwrap(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 LedMatrix::constructor_template; \ No newline at end of file