forked from jheising/node.pcduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_pcduino_analog.cc
executable file
·60 lines (43 loc) · 1.47 KB
/
node_pcduino_analog.cc
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
#define BUILDING_NODE_EXTENSION
#include <node.h>
#include "Arduino.h"
using namespace v8;
Handle<Value> analogWrite(const Arguments& args) {
HandleScope scope;
if (args.Length() < 2) {
ThrowException(Exception::TypeError(v8::String::New("Wrong number of arguments")));
return scope.Close(Undefined());
}
if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
ThrowException(Exception::TypeError(v8::String::New("Wrong arguments")));
return scope.Close(Undefined());
}
int pin = args[0]->NumberValue();
int value = args[1]->NumberValue();
analogWrite(pin, value);
return scope.Close(Undefined());
}
Handle<Value> analogRead(const Arguments& args) {
HandleScope scope;
if (args.Length() < 1) {
ThrowException(Exception::TypeError(v8::String::New("Wrong number of arguments")));
return scope.Close(Undefined());
}
if (!args[0]->IsNumber()) {
ThrowException(Exception::TypeError(v8::String::New("Wrong arguments")));
return scope.Close(Undefined());
}
int pin = args[0]->NumberValue();
int value = analogRead(pin);
Local<Number> num = Number::New(value);
return scope.Close(num);
}
void Init(Handle<Object> exports)
{
init();
exports->Set(v8::String::NewSymbol("analogWrite"),
FunctionTemplate::New(analogWrite)->GetFunction());
exports->Set(v8::String::NewSymbol("analogRead"),
FunctionTemplate::New(analogRead)->GetFunction());
}
NODE_MODULE(node_pcduino_analog, Init)