forked from dangrie158/opencushion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binding.cpp
183 lines (133 loc) · 5.89 KB
/
binding.cpp
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
#include "binding.h"
using v8::Exception;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Null;
using v8::Object;
using v8::String;
using v8::Value;
using v8::Persistent;
Persistent<Function> HX711Wrapper::constructor;
HX711Wrapper::HX711Wrapper(uint8_t clockPin, uint8_t dataPin, uint8_t skipSetup) :
mSensor(new HX711(clockPin, dataPin, skipSetup))
{
}
HX711Wrapper::~HX711Wrapper(){
delete mSensor;
}
void HX711Wrapper::InitModule(Local<Object> exports) {
Isolate* isolate = exports->GetIsolate();
// Prepare constructor template
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
tpl->SetClassName(String::NewFromUtf8(isolate, "HX711"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
NODE_SET_PROTOTYPE_METHOD(tpl, "read", read);
NODE_SET_PROTOTYPE_METHOD(tpl, "setScale", setScale);
NODE_SET_PROTOTYPE_METHOD(tpl, "setOffset", setOffset);
NODE_SET_PROTOTYPE_METHOD(tpl, "tare", tare);
NODE_SET_PROTOTYPE_METHOD(tpl, "getUnits", getUnits);
NODE_SET_PROTOTYPE_METHOD(tpl, "setGain", setGain);
NODE_SET_PROTOTYPE_METHOD(tpl, "getOffset", getOffset);
NODE_SET_PROTOTYPE_METHOD(tpl, "getScale", getScale);
NODE_SET_PROTOTYPE_METHOD(tpl, "powerDown", powerDown);
NODE_SET_PROTOTYPE_METHOD(tpl, "powerUp", powerUp);
constructor.Reset(isolate, tpl->GetFunction());
exports->Set(String::NewFromUtf8(isolate, "HX711"), tpl->GetFunction());
}
void HX711Wrapper::read(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HX711Wrapper* obj = ObjectWrap::Unwrap<HX711Wrapper>(args.Holder());
int32_t value = obj->mSensor->getLatestData();
args.GetReturnValue().Set(Number::New(isolate, value));
}
void HX711Wrapper::setScale(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
float scaleArg = args[0]->IsUndefined() ? 1.0f : args[0]->NumberValue();
HX711Wrapper* obj = ObjectWrap::Unwrap<HX711Wrapper>(args.Holder());
obj->mSensor->setScale(scaleArg);
args.GetReturnValue().Set(Null(isolate));
}
void HX711Wrapper::setOffset(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
int32_t offsetArg = args[0]->IsUndefined() ? 0 : args[0]->NumberValue();
HX711Wrapper* obj = ObjectWrap::Unwrap<HX711Wrapper>(args.Holder());
obj->mSensor->setOffset(offsetArg);
args.GetReturnValue().Set(Null(isolate));
}
void HX711Wrapper::tare(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
uint8_t timesArg = args[0]->IsUndefined() ? 10 : args[0]->NumberValue();
HX711Wrapper* obj = ObjectWrap::Unwrap<HX711Wrapper>(args.Holder());
obj->mSensor->tare(timesArg);
args.GetReturnValue().Set(Null(isolate));
}
void HX711Wrapper::getUnits(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
uint8_t timesArg = args[0]->IsUndefined() ? 10 : args[0]->NumberValue();
HX711Wrapper* obj = ObjectWrap::Unwrap<HX711Wrapper>(args.Holder());
float value = obj->mSensor->getUnits(timesArg);
args.GetReturnValue().Set(Number::New(isolate, value));
}
void HX711Wrapper::setGain(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
uint8_t gainArg = args[0]->IsUndefined() ? 128 : args[0]->NumberValue();
HX711Wrapper* obj = ObjectWrap::Unwrap<HX711Wrapper>(args.Holder());
obj->mSensor->setGain(gainArg);
args.GetReturnValue().Set(Null(isolate));
}
void HX711Wrapper::getOffset(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HX711Wrapper* obj = ObjectWrap::Unwrap<HX711Wrapper>(args.Holder());
int32_t value = obj->mSensor->getOffset();
args.GetReturnValue().Set(Number::New(isolate, value));
}
void HX711Wrapper::getScale(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HX711Wrapper* obj = ObjectWrap::Unwrap<HX711Wrapper>(args.Holder());
float value = obj->mSensor->getScale();
args.GetReturnValue().Set(Number::New(isolate, value));
}
void HX711Wrapper::powerDown(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HX711Wrapper* obj = ObjectWrap::Unwrap<HX711Wrapper>(args.Holder());
obj->mSensor->powerDown();
args.GetReturnValue().Set(Null(isolate));
}
void HX711Wrapper::powerUp(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HX711Wrapper* obj = ObjectWrap::Unwrap<HX711Wrapper>(args.Holder());
obj->mSensor->powerUp();
args.GetReturnValue().Set(Null(isolate));
}
void HX711Wrapper::New(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
if (args.IsConstructCall()) {
// Invoked as constructor: `new HX711(...)`
if (args.Length() < 2) {
// Throw an Error that is passed back to JavaScript
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "You need to pass 2 arguments")));
return;
}
uint8_t clockPinArg = (uint8_t)args[0]->NumberValue();
uint8_t dataPinArg = (uint8_t)args[1]->NumberValue();
uint8_t skipSetup = 0;
if (args.Length() >= 3 )
skipSetup = (uint8_t)args[2]->BooleanValue();
HX711Wrapper* obj = new HX711Wrapper(clockPinArg, dataPinArg, skipSetup);
obj->Wrap(args.This());
args.GetReturnValue().Set(args.This());
} else {
// Invoked as plain function `HX711(...)`, turn into construct call.
const int argc = 2;
Local<Value> argv[argc] = { args[0], args[1] };
Local<Function> cons = Local<Function>::New(isolate, constructor);
args.GetReturnValue().Set(cons->NewInstance(argc, argv));
}
}
NODE_MODULE(hx711, HX711Wrapper::InitModule)