-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathidisplay.cpp
More file actions
196 lines (164 loc) · 7.28 KB
/
idisplay.cpp
File metadata and controls
196 lines (164 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* cppsrc/main.cpp */
#include <napi.h>
#include <string>
#include "iDisplay_ButtonDisplay.h"
Napi::Number DeviceOpenWrapper(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 2 || !info[0].IsNumber() || !info[1].IsNumber()) {
Napi::TypeError::New(env, "Number expected").ThrowAsJavaScriptException();
return Napi::Number::New(env, -1);
}
int vid = (info[0].As<Napi::Number>()).Int32Value();
int pid = (info[1].As<Napi::Number>()).Int32Value();
int returnValue = iD_USB_DeviceOpen(vid, pid);
return Napi::Number::New(env, returnValue);
}
Napi::Number DeviceCloseWrapper(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() > 0) {
Napi::TypeError::New(env, "Number expected").ThrowAsJavaScriptException();
return Napi::Number::New(env, -1);
}
int returnValue = iD_USB_DeviceClose();
return Napi::Number::New(env, returnValue);
}
Napi::Number ShowImageWrapper(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 2 || !info[0].IsNumber() || !info[1].IsString()) {
Napi::TypeError::New(env, "Incorrect arguments.").ThrowAsJavaScriptException();
return Napi::Number::New(env, -1);
}
int idx = (info[0].As<Napi::Number>()).Int32Value();
std::string path = (info[1].As<Napi::String>()).Utf8Value();
FILE* fd = fopen(path.c_str(), "rb");
if (fd==NULL) {
Napi::Error::New(env, "Unable to Open file.").ThrowAsJavaScriptException();
return Napi::Number::New(env, -1);
}
fseek(fd, 0, SEEK_END);
int dataLength = ftell(fd);
fseek(fd, 0, SEEK_SET);
uint8_t *imgBuf = (uint8_t*)malloc(dataLength);
if (imgBuf==NULL) {
Napi::Error::New(env, "Alloc memory fail!").ThrowAsJavaScriptException();
return Napi::Number::New(env, -1);
}
int retCnt = fread(imgBuf, sizeof(uint8_t), dataLength, fd);
if (retCnt != dataLength) {
Napi::Error::New(env, "Read image fail!\n").ThrowAsJavaScriptException();
return Napi::Number::New(env, -1);
}
fclose(fd);
int returnValue = iD_USB_ShowImageOnButton(idx, imgBuf);
free(imgBuf);
return Napi::Number::New(env, returnValue);
}
Napi::Number FirmwareUpdateWrapper(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 1 || !info[0].IsString()) {
Napi::TypeError::New(env, "Incorrect arguments.").ThrowAsJavaScriptException();
}
std::string path = (info[0].As<Napi::String>()).Utf8Value();
FILE* fd = fopen(path.c_str(), "rb");
if (fd==NULL) {
Napi::Error::New(env, "Unable to Open file.").ThrowAsJavaScriptException();
return Napi::Number::New(env, -1);
}
fseek(fd, 0, SEEK_END);
uint32_t dataLength = ftell(fd);
fseek(fd, 0, SEEK_SET);
uint8_t *firBuf = (uint8_t*)malloc(dataLength);
if (firBuf==NULL) {
Napi::Error::New(env, "Alloc memory fail!").ThrowAsJavaScriptException();
return Napi::Number::New(env, -1);
}
int retCnt = fread(firBuf, sizeof(uint8_t), dataLength, fd);
if (retCnt != dataLength) {
Napi::Error::New(env, "Read image fail!\n").ThrowAsJavaScriptException();
return Napi::Number::New(env, -1);
}
fclose(fd);
int returnValue = iD_USB_FirmwareUpdate(firBuf, dataLength);
free(firBuf);
return Napi::Number::New(env, returnValue);
}
Napi::Number CleanScreenWrapper(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 3 || !info[0].IsNumber() || !info[1].IsNumber() || !info[2].IsNumber()) {
Napi::TypeError::New(env, "Incorrect arguments.").ThrowAsJavaScriptException();
return Napi::Number::New(env, -1);
}
int r = (info[0].As<Napi::Number>()).Int32Value();
int g = (info[1].As<Napi::Number>()).Int32Value();
int b = (info[2].As<Napi::Number>()).Int32Value();
int returnValue = iD_USB_CleanScreen(r, g, b);
return Napi::Number::New(env, returnValue);
}
Napi::Number SetBrightnessWrapper(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 1 || !info[0].IsNumber()) {
Napi::TypeError::New(env, "Incorrect arguments.").ThrowAsJavaScriptException();
return Napi::Number::New(env, -1);
}
int per = (info[0].As<Napi::Number>()).Int32Value();
int returnValue = iD_USB_SetBrightness(per);
return Napi::Number::New(env, returnValue);
}
Napi::ArrayBuffer FirmwareVersionWrapper(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
uint8_t out_verStr[32];
int returnValue = iD_USB_GetFirmwareVersion(out_verStr);
if(returnValue){ // 0 => no error
Napi::Error::New(env, "Get FirmwareVersion failed !\n").ThrowAsJavaScriptException();
return Napi::ArrayBuffer::New(env, 0);
}
return Napi::ArrayBuffer::New(env, out_verStr, sizeof(out_verStr)/sizeof(out_verStr[0]));
}
Napi::ArrayBuffer SerialNumberWrapper(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
uint8_t out_sn[32];
int returnValue = iD_USB_GetCustomSerialNumber(out_sn);
if(returnValue){ // 0 => no error
Napi::Error::New(env, "Get SerialNumber failed !\n").ThrowAsJavaScriptException();
return Napi::ArrayBuffer::New(env, 0);
}
return Napi::ArrayBuffer::New(env, out_sn, sizeof(out_sn)/sizeof(out_sn[0]));
}
Napi::ArrayBuffer LibraryVersionWrapper(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
uint8_t out_ver[32];
int returnValue = iD_USB_GetCustomSerialNumber(out_ver);
if(returnValue){ // 0 => no error
Napi::Error::New(env, "Get LibaryVersion failed !\n").ThrowAsJavaScriptException();
return Napi::ArrayBuffer::New(env, 0);
}
return Napi::ArrayBuffer::New(env, out_ver, sizeof(out_ver)/sizeof(out_ver[0]));
}
Napi::ArrayBuffer ButtonEventWrapper(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 1 || !info[0].IsNumber() ) {
Napi::TypeError::New(env, "Incorrect arguments.").ThrowAsJavaScriptException();
}
int timeout = (info[0].As<Napi::Number>()).Int32Value();
uint8_t arr[256];
int returnValue = iD_USB_WaitEvent(arr, timeout);
if(returnValue){ // 0 => no error
Napi::Error::New(env, "No Data Button Event!\n").ThrowAsJavaScriptException();
return Napi::ArrayBuffer::New(env, 0);
}
return Napi::ArrayBuffer::New(env, arr, sizeof(arr)/sizeof(arr[0]));
}
Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
exports.Set("deviceOpen", Napi::Function::New(env, DeviceOpenWrapper));
exports.Set("deviceClose", Napi::Function::New(env, DeviceCloseWrapper));
exports.Set("showImage", Napi::Function::New(env, ShowImageWrapper));
exports.Set("cleanScreen", Napi::Function::New(env, CleanScreenWrapper));
exports.Set("brightness", Napi::Function::New(env, SetBrightnessWrapper));
exports.Set("firmwareVersion", Napi::Function::New(env, FirmwareVersionWrapper));
exports.Set("serialNumber", Napi::Function::New(env, SerialNumberWrapper));
exports.Set("libraryVersion", Napi::Function::New(env, LibraryVersionWrapper));
exports.Set("updateFirmware", Napi::Function::New(env, FirmwareUpdateWrapper));
exports.Set("getButtonData", Napi::Function::New(env, ButtonEventWrapper));
return exports;
}
NODE_API_MODULE(idisplay, InitAll)