-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgpio.cpp
More file actions
109 lines (80 loc) · 2.49 KB
/
gpio.cpp
File metadata and controls
109 lines (80 loc) · 2.49 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
//Cria string
//Local<String> str = args[0]->ToString();
/*
if(obj->IsArray()){
v8::Local<v8::Array> arr= v8::Local<v8::Array>::Cast(args[0]);
v8::String::Utf8Value key(arr->Get(0));
v8::String::Utf8Value value(arr->Get(1));
}
*/
#include <node.h>
#include <string.h>
extern "C"{
#include "gpio_lib.h"
}
using namespace v8;
void init(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
sunxi_gpio_init();
sunxi_gpio_init_R_PIO();
args.GetReturnValue().Set(SETUP_OK);
}
void pin(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
int32_t gpio = args[1]->ToInteger()->Value();
int32_t result_pin = 0;
v8::String::Utf8Value arg2(args[0]->ToString());
if(strcmp(*arg2, "PA") == 0){
result_pin = SUNXI_GPA(gpio);
}
if(strcmp(*arg2, "PC") == 0){
result_pin = SUNXI_GPC(gpio);
}
if(strcmp(*arg2, "PG") == 0){
result_pin = SUNXI_GPG(gpio);
}
args.GetReturnValue().Set(result_pin);
}
void dir(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
int32_t gpio = args[0]->ToInteger()->Value();
v8::String::Utf8Value arg3(args[1]->ToString());
if(strcmp(*arg3, "IN") == 0){
sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_INPUT);
}
if(strcmp(*arg3, "OUT") == 0){
sunxi_gpio_set_cfgpin(gpio, SUNXI_GPIO_OUTPUT);
}
}
void write(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
int32_t gpio = args[0]->ToInteger()->Value();
int32_t state = args[1]->ToInteger()->Value();
sunxi_gpio_output(gpio, state);
args.GetReturnValue().Set(state);
}
void read(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
int32_t result;
int32_t gpio = args[0]->ToInteger()->Value();
result = sunxi_gpio_input(gpio);
if(result == -1){
isolate->ThrowException(
Exception::TypeError(String::NewFromUtf8(isolate,"Reading pin failed"))
);
}
args.GetReturnValue().Set(result);
}
void Init(Handle<Object> exports) {
NODE_SET_METHOD(exports, "init", init);
NODE_SET_METHOD(exports, "write", write);
NODE_SET_METHOD(exports, "read", read);
NODE_SET_METHOD(exports, "pin", pin);
NODE_SET_METHOD(exports, "dir", dir);
}
NODE_MODULE(addon, Init)