This repository has been archived by the owner on Mar 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addon.cc
137 lines (116 loc) · 3.53 KB
/
addon.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
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
#define BUILDING_NODE_EXTENSION
#include <node.h>
#include<fcntl.h>
//#include<stdio.h> //for debug only
#include<string>
extern "C" {
#include "spi.h"
}
using namespace v8;
/*
function name: configSPI
inputs:
args[0]: spi mode
args[1]: max clock speed
args[2]: device name (eg '/dev/spidev0.1')
returns:
undefined
*/
Handle<Value> configSPI(const Arguments& args) {
HandleScope scope;
//Check inputs
if (args.Length() < 2) {
ThrowException(
Exception::TypeError(String::New("Wrong number of arguments")));
return scope.Close(Undefined());
}
if (!args[0]->IsNumber() || !args[1]->IsNumber()) {
ThrowException(Exception::TypeError(String::New("Wrong arguments")));
return scope.Close(Undefined());
}
//unpack args
int spi_mode = args[0]->Int32Value();
int clk_speed = args[1]->Int32Value();
if (spi_mode<0 || spi_mode>4){
ThrowException(Exception::Error(String::New("invalid spi mode")));
return scope.Close(Undefined());
}
if (clk_speed<0 ){
ThrowException(Exception::Error(String::New("invalid clock speed")));
return scope.Close(Undefined());
}
v8::String::Utf8Value inputString(args[2]->ToString());
std::string devicePath = std::string(*inputString);
int fd = open(devicePath.c_str(), O_RDWR);
if (fd <0) {
ThrowException(Exception::Error(String::New("Can't open SPI device")));
return scope.Close(Undefined());
}
if (!initSPI(fd, (uint8_t)spi_mode, (int32_t)clk_speed)) {
close(fd);
ThrowException(
Exception::Error(String::New("unable to configure SPI")));
return scope.Close(Undefined());
}
close(fd);
return scope.Close(Undefined());
}
/*
function name: readwriteSPI
inputs:
args[0]: js array of numbers to transmit over SPI
args[1]: js string of the spi device to use (eg '/dev/spidev0.1' )
returns:
js array of bytes received (as Numbers).
*/
Handle<Value> readwriteSPI(const Arguments& args) {
HandleScope scope;
//Check inputs
if (args.Length() < 2) {
ThrowException(
Exception::TypeError(String::New("Wrong number of arguments")));
return scope.Close(Undefined());
}
if (!args[0]->IsArray() ) {
ThrowException(
Exception::TypeError(String::New("Wrong arguments")));
return scope.Close(Undefined());
}
//unwrap inputs
Local<Array> inputArray = Local<Array>::Cast(args[0]);
v8::String::Utf8Value inputString(args[1]->ToString());
std::string devicePath = std::string(*inputString);
///////////////////ACTUAL WORK///////////////////
unsigned int message_length = inputArray->Length();
uint8_t tx_buf[message_length];
uint8_t rx_buf[message_length];
int fd = open(devicePath.c_str(), O_RDWR);
if (fd <0) {
ThrowException(Exception::Error(String::New("Can't open SPI device")));
return scope.Close(Undefined());
}
//fill TX buffer with data from user
for (unsigned int i = 0; i<message_length ; i++){
tx_buf[i] = (uint8_t)inputArray->Get(Integer::New(i))->NumberValue();
}
//do the spi RW
if(spiRW(fd,message_length,tx_buf,rx_buf)) {
close(fd);
ThrowException(Exception::Error(String::New("SPI read/write failed.")));
return scope.Close(Undefined());
}
close(fd);
//////////////////PACK UP OUTPUTS/////////////////////
Local<Array> outputArray = Array::New(message_length);
for (unsigned int i=0; i<message_length ; i++){
outputArray->Set(i,Number::New(rx_buf[i]));
}
return scope.Close(outputArray);
}
void Init(Handle<Object> target) {
target->Set(String::NewSymbol("readwriteSPI"),
FunctionTemplate::New(readwriteSPI)->GetFunction());
target->Set(String::NewSymbol("configSPI"),
FunctionTemplate::New(configSPI)->GetFunction());
}
NODE_MODULE(rSPI, Init)