1
1
#include " led-matrix.addon.h"
2
2
3
- #define BILLION 1000000000L ;
4
- #define MILLION 1000000000L ;
3
+ #define BILLION 1000000000L ;
4
+ #define MILLION 1000000000L ;
5
5
6
6
inline double get_now_ms () {
7
7
struct timespec t;
8
- if (clock_gettime (CLOCK_MONOTONIC_RAW, &t) < 0 ) {
9
- throw " Failed to get the current time." ;
10
- }
8
+ if (clock_gettime (CLOCK_MONOTONIC_RAW, &t) < 0 ) { throw " Failed to get the current time." ; }
11
9
12
10
return (t.tv_sec * 1000 ) + (t.tv_nsec / 1000000 );
13
11
}
@@ -71,7 +69,7 @@ LedMatrixAddon::LedMatrixAddon(const Napi::CallbackInfo& info)
71
69
if (!info[1 ].IsObject ()) {
72
70
throw Napi::Error::New (env, " Constructor expects its first parameter to be an object of runtime options!" );
73
71
}
74
- auto matrixOpts = create_matrix_options (env, info[0 ].As <Napi::Object>());
72
+ auto matrixOpts = create_matrix_options (env, info[0 ].As <Napi::Object>());
75
73
auto runtimeOpts = create_runtime_options (env, info[1 ].As <Napi::Object>());
76
74
77
75
this ->matrix_ = CreateMatrixFromOptions (matrixOpts, runtimeOpts);
@@ -98,20 +96,16 @@ Napi::Value LedMatrixAddon::sync(const Napi::CallbackInfo& info) {
98
96
auto env = info.Env ();
99
97
100
98
if (!after_sync_cb_.IsEmpty ()) {
101
- auto now = get_now_ms ();
102
- auto now_ms = now - t_start_;
103
- t_dsync_ms_ = now_ms - t_sync_ms_;
104
- t_sync_ms_ = now_ms;
105
-
106
- auto resync = after_sync_cb_.Call (info.This (), {
107
- info.This (),
108
- Napi::Number::New (env, t_dsync_ms_),
109
- Napi::Number::New (env, t_sync_ms_)
110
- });
111
-
112
- if (resync.ToBoolean () == true ) {
113
- sync (info);
114
- }
99
+ auto now = get_now_ms ();
100
+ auto now_ms = now - t_start_;
101
+ t_dsync_ms_ = now_ms - t_sync_ms_;
102
+ t_sync_ms_ = now_ms;
103
+
104
+ auto resync
105
+ = after_sync_cb_
106
+ .Call (info.This (), { info.This (), Napi::Number::New (env, t_dsync_ms_), Napi::Number::New (env, t_sync_ms_) });
107
+
108
+ if (resync.ToBoolean () == true ) { sync (info); }
115
109
}
116
110
117
111
return Napi::Number::New (env, 0 );
@@ -133,35 +127,32 @@ Napi::Value LedMatrixAddon::map(const Napi::CallbackInfo& info) {
133
127
134
128
assert (cb.IsFunction ());
135
129
136
- auto env = info.Env ();
137
- auto now = get_now_ms ();
130
+ auto env = info.Env ();
131
+ auto now = get_now_ms ();
138
132
auto now_ms = Napi::Number::New (env, now - t_start_);
139
133
140
- Napi::Array coord_array = Napi::Array::New (env, 3 );
141
- uint32_t zero = 0 ; // The compiler can't match the overloaded signature if given 0 explicitly
142
- uint32_t one = 1 ;
143
- uint32_t two = 2 ;
134
+ Napi::Array coord_array = Napi::Array::New (env, 3 );
135
+ uint32_t zero = 0 ; // The compiler can't match the overloaded signature if given 0 explicitly
136
+ uint32_t one = 1 ;
137
+ uint32_t two = 2 ;
144
138
145
- auto i = 0 ;
139
+ auto i = 0 ;
146
140
147
- for (int x = 0 ; x < this ->matrix_ ->width (); x++) {
148
- coord_array.Set (zero, x);
141
+ for (int x = 0 ; x < this ->matrix_ ->width (); x++) {
142
+ coord_array.Set (zero, x);
149
143
150
- for (int y = 0 ; y < this ->matrix_ ->height (); y++) {
151
- coord_array.Set (one, y);
152
- coord_array.Set (two, i++);
144
+ for (int y = 0 ; y < this ->matrix_ ->height (); y++) {
145
+ coord_array.Set (one, y);
146
+ coord_array.Set (two, i++);
153
147
154
- auto color = cb.Call (info.This (), {
155
- coord_array,
156
- now_ms
157
- });
148
+ auto color = cb.Call (info.This (), { coord_array, now_ms });
158
149
159
- assert (color.IsNumber ());
150
+ assert (color.IsNumber ());
160
151
161
- const auto hex = color.As <Napi::Number>().Uint32Value ();
162
- this ->matrix_ ->SetPixel (x, y, 0xFF & (hex >> 16 ), 0xFF & (hex >> 8 ), 0xFF & hex);
163
- }
164
- }
152
+ const auto hex = color.As <Napi::Number>().Uint32Value ();
153
+ this ->matrix_ ->SetPixel (x, y, 0xFF & (hex >> 16 ), 0xFF & (hex >> 8 ), 0xFF & hex);
154
+ }
155
+ }
165
156
166
157
return info.This ();
167
158
}
@@ -179,10 +170,10 @@ Napi::Value LedMatrixAddon::brightness(const Napi::CallbackInfo& info) {
179
170
180
171
Napi::Value LedMatrixAddon::clear (const Napi::CallbackInfo& info) {
181
172
if (info.Length () > 0 ) {
182
- const auto x0 = info[0 ].As <Napi::Number>().Uint32Value ();
183
- const auto y0 = info[1 ].As <Napi::Number>().Uint32Value ();
184
- const auto x1 = info[2 ].As <Napi::Number>().Uint32Value ();
185
- const auto y1 = info[3 ].As <Napi::Number>().Uint32Value ();
173
+ const auto x0 = info[0 ].As <Napi::Number>().Uint32Value ();
174
+ const auto y0 = info[1 ].As <Napi::Number>().Uint32Value ();
175
+ const auto x1 = info[2 ].As <Napi::Number>().Uint32Value ();
176
+ const auto y1 = info[3 ].As <Napi::Number>().Uint32Value ();
186
177
const auto black = Color (0 , 0 , 0 );
187
178
for (auto y = y0; y <= y1; y++) { DrawLine (this ->canvas_ , x0, y, x1, y, black); }
188
179
}
@@ -196,12 +187,12 @@ Napi::Value LedMatrixAddon::draw_buffer(const Napi::CallbackInfo& info) {
196
187
const auto buffer = info[0 ].As <Napi::Buffer<uint8_t > >();
197
188
const auto w = info[1 ].IsNumber () ? info[1 ].As <Napi::Number>().Uint32Value () : this ->matrix_ ->width ();
198
189
const auto h = info[2 ].IsNumber () ? info[2 ].As <Napi::Number>().Uint32Value () : this ->matrix_ ->height ();
199
- const auto data = buffer.Data ();
200
- const auto len = buffer.Length ();
190
+ const auto data = buffer.Data ();
191
+ const auto len = buffer.Length ();
201
192
202
193
assert (len == w * h * 3 );
203
194
204
- Image* img = new Image ();
195
+ Image* img = new Image ();
205
196
Pixel* pixels = (Pixel*) malloc (sizeof (Pixel) * w * h);
206
197
for (unsigned int i = 0 ; i < w * h; i++) {
207
198
auto j = i * 3 ;
@@ -351,9 +342,9 @@ Napi::Value LedMatrixAddon::bg_color(const Napi::CallbackInfo& info) {
351
342
352
343
Napi::Value LedMatrixAddon::font (const Napi::CallbackInfo& info) {
353
344
if (info.Length () > 0 ) {
354
- auto font = Napi::ObjectWrap<FontAddon>::Unwrap (info[0 ].As <Napi::Object>());
345
+ auto font = Napi::ObjectWrap<FontAddon>::Unwrap (info[0 ].As <Napi::Object>());
355
346
this ->font_ = &(font->font );
356
- font_name_ = font->name (info).ToString ();
347
+ font_name_ = font->name (info).ToString ();
357
348
return info.This ();
358
349
}
359
350
else {
@@ -362,18 +353,15 @@ Napi::Value LedMatrixAddon::font(const Napi::CallbackInfo& info) {
362
353
}
363
354
364
355
Napi::Value LedMatrixAddon::get_available_pixel_mappers (const Napi::CallbackInfo& info) {
365
- auto env = info.Env ();
366
- auto mappers = GetAvailablePixelMappers ();
367
- Napi::Array mapper_name_array = Napi::Array::New (env, mappers.size ());
356
+ auto env = info.Env ();
357
+ auto mappers = GetAvailablePixelMappers ();
358
+ Napi::Array mapper_name_array = Napi::Array::New (env, mappers.size ());
368
359
369
- for (uint8_t i = 0 ; i < mappers.size (); i++) {
370
- mapper_name_array.Set (i, Napi::String::New (env, mappers.at (i)));
371
- }
360
+ for (uint8_t i = 0 ; i < mappers.size (); i++) { mapper_name_array.Set (i, Napi::String::New (env, mappers.at (i))); }
372
361
373
- return mapper_name_array;
362
+ return mapper_name_array;
374
363
}
375
364
376
-
377
365
/* *
378
366
* Create an instance of Options from a JS object.
379
367
*/
@@ -389,6 +377,7 @@ RGBMatrix::Options LedMatrixAddon::create_matrix_options(const Napi::Env& env, c
389
377
options.inverse_colors = obj.Get (" inverseColors" ).As <Napi::Boolean>();
390
378
auto led_rgb_sequence = std::string (obj.Get (" ledRgbSequence" ).As <Napi::String>());
391
379
options.led_rgb_sequence = strcpy (new char [led_rgb_sequence.size ()], led_rgb_sequence.c_str ());
380
+ options.limit_refresh_rate_hz = obj.Get (" limitRefreshRateHz" ).As <Napi::Number>();
392
381
auto pixel_mapper_config = std::string (obj.Get (" pixelMapperConfig" ).As <Napi::String>());
393
382
options.pixel_mapper_config = strcpy (new char [pixel_mapper_config.size ()], pixel_mapper_config.c_str ());
394
383
options.multiplexing = obj.Get (" multiplexing" ).As <Napi::Number>();
@@ -413,7 +402,7 @@ RGBMatrix::Options LedMatrixAddon::create_matrix_options(const Napi::Env& env, c
413
402
RuntimeOptions LedMatrixAddon::create_runtime_options (const Napi::Env& env, const Napi::Object& obj) {
414
403
RuntimeOptions options = RuntimeOptions ();
415
404
416
- options.gpio_slowdown = obj.Get (" gpioSlowdown" ).As <Napi::Number>();
405
+ options.gpio_slowdown = obj.Get (" gpioSlowdown" ).As <Napi::Number>();
417
406
options.daemon = obj.Get (" daemon" ).As <Napi::Number>();
418
407
options.drop_privileges = obj.Get (" dropPrivileges" ).As <Napi::Number>();
419
408
options.do_gpio_init = obj.Get (" doGpioInit" ).As <Napi::Boolean>();
@@ -441,6 +430,7 @@ Napi::Object LedMatrixAddon::matrix_options_to_obj(const Napi::Env& env, const R
441
430
obj.Set (" hardwareMapping" , Napi::String::New (env, hardware_mapping));
442
431
obj.Set (" inverseColors" , Napi::Boolean::New (env, options.inverse_colors ));
443
432
obj.Set (" ledRgbSequence" , Napi::String::New (env, led_rgb_sequence));
433
+ obj.Set (" limitRefreshRateHz" , Napi::Number::New (env, options.limit_refresh_rate_hz ));
444
434
obj.Set (" multiplexing" , Napi::Number::New (env, options.multiplexing ));
445
435
obj.Set (" parallel" , Napi::Number::New (env, options.parallel ));
446
436
obj.Set (" pixelMapperConfig" , Napi::String::New (env, pixel_mapper_config));
@@ -460,11 +450,10 @@ Napi::Object LedMatrixAddon::matrix_options_to_obj(const Napi::Env& env, const R
460
450
*/
461
451
Napi::Object LedMatrixAddon::runtime_options_to_obj (const Napi::Env& env, const RuntimeOptions& options) {
462
452
auto obj = Napi::Object::New (env);
463
-
464
- obj.Set (" gpioSlowdown" , Napi::Number::New (env, options.gpio_slowdown ));
465
453
obj.Set (" daemon" , Napi::Number::New (env, options.daemon ));
466
- obj.Set (" dropPrivileges" , Napi::Number::New (env, options.drop_privileges ));
467
454
obj.Set (" doGpioInit" , Napi::Boolean::New (env, options.do_gpio_init ));
455
+ obj.Set (" dropPrivileges" , Napi::Number::New (env, options.drop_privileges ));
456
+ obj.Set (" gpioSlowdown" , Napi::Number::New (env, options.gpio_slowdown ));
468
457
469
458
return obj;
470
459
}
@@ -498,9 +487,9 @@ Color LedMatrixAddon::color_from_callback_info(const Napi::CallbackInfo& info) {
498
487
}
499
488
else if (info[0 ].IsObject ()) {
500
489
const auto obj = info[0 ].As <Napi::Object>();
501
- uint8_t r = obj.Get (" r" ).As <Napi::Number>().Uint32Value ();
502
- uint8_t g = obj.Get (" g" ).As <Napi::Number>().Uint32Value ();
503
- uint8_t b = obj.Get (" b" ).As <Napi::Number>().Uint32Value ();
490
+ uint8_t r = obj.Get (" r" ).As <Napi::Number>().Uint32Value ();
491
+ uint8_t g = obj.Get (" g" ).As <Napi::Number>().Uint32Value ();
492
+ uint8_t b = obj.Get (" b" ).As <Napi::Number>().Uint32Value ();
504
493
return Color (r, g, b);
505
494
}
506
495
else if (info[0 ].IsNumber ()) {
0 commit comments