i2c 0.2.4 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/examples/accelerometer/accel.js +72 -0
- package/examples/blinkm/blinkm.js +69 -0
- package/lib/i2c.js +146 -0
- package/main.js +2 -4
- package/package.json +5 -8
- package/src/i2c.cc +68 -56
- package/examples/accelerometer/accel.coffee +0 -55
- package/examples/blinkm/blinkm.coffee +0 -57
- package/lib/i2c.coffee +0 -100
package/README.md
CHANGED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const Wire = require('../../main');
|
|
2
|
+
|
|
3
|
+
// for AK8975
|
|
4
|
+
// info: https://github.com/jrowberg/i2cdevlib/blob/master/Arduino/AK8975/AK8975.cpp
|
|
5
|
+
// http://stackoverflow.com/questions/4768933/read-two-bytes-into-an-integer
|
|
6
|
+
|
|
7
|
+
const RANGE_BWIDTH = 0x14;
|
|
8
|
+
const RANGE_BIT = 0x04;
|
|
9
|
+
const RANGE_LENGTH = 0x02;
|
|
10
|
+
const RANGE_2G = 0x00;
|
|
11
|
+
const BANDWIDTH_BIT = 0x02;
|
|
12
|
+
const BANDWIDTH_LENGTH = 0x03;
|
|
13
|
+
const BW_25HZ = 0x00;
|
|
14
|
+
const GET_ID = 0x00;
|
|
15
|
+
|
|
16
|
+
class Accelerometer {
|
|
17
|
+
|
|
18
|
+
constructor(address) {
|
|
19
|
+
this.address = address;
|
|
20
|
+
this.wire = new Wire(this.address);
|
|
21
|
+
|
|
22
|
+
this.setRange();
|
|
23
|
+
this.setBandwidth();
|
|
24
|
+
|
|
25
|
+
this.wire.on('data', (data) => {
|
|
26
|
+
console.log(data);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
setRange() {
|
|
31
|
+
this.wire.writeBytes(RANGE_BWIDTH, [RANGE_BIT, RANGE_LENGTH, RANGE_2G], null);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
testConnection(callback) {
|
|
35
|
+
this.getDeviceID((err, data) => {
|
|
36
|
+
callback(data[0] === 0b010);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
getDeviceID(callback) {
|
|
41
|
+
this.wire.readBytes(GET_ID, 1, callback);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
setBandwidth() {
|
|
45
|
+
this.wire.writeBytes(RANGE_BWIDTH, [BANDWIDTH_BIT, BANDWIDTH_LENGTH, BW_25HZ], null);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
getHeading() {
|
|
49
|
+
this.wire.writeBytes(0x0A, [0x1], (err) => {
|
|
50
|
+
if (err) console.error(err);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
setTimeout(() => {
|
|
54
|
+
this.wire.readBytes(0x03, 6, (err, buffer) => {
|
|
55
|
+
if (err) return console.error(err);
|
|
56
|
+
const pos = {
|
|
57
|
+
x: ((buffer[1]) << 8) | buffer[0],
|
|
58
|
+
y: ((buffer[3]) << 8) | buffer[2],
|
|
59
|
+
z: ((buffer[5]) << 8) | buffer[4]
|
|
60
|
+
};
|
|
61
|
+
console.log(pos);
|
|
62
|
+
});
|
|
63
|
+
}, 10);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
getMotion() {
|
|
67
|
+
this.wire.stream(0x02, 6, 100);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const accel = new Accelerometer(56);
|
|
72
|
+
accel.getHeading();
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
const Wire = require('../../main');
|
|
2
|
+
|
|
3
|
+
// BlinkM http://thingm.com/products/blinkm
|
|
4
|
+
// firmware http://code.google.com/p/codalyze/wiki/CyzRgb
|
|
5
|
+
|
|
6
|
+
const TO_RGB = 0x6e;
|
|
7
|
+
const GET_RGB = 0x67;
|
|
8
|
+
const FADE_TO_RGB = 0x63;
|
|
9
|
+
const FADE_TO_HSB = 0x68;
|
|
10
|
+
const GET_ADDRESS = 0x61;
|
|
11
|
+
const SET_ADDRESS = 0x41;
|
|
12
|
+
const SET_FADE = 0x66;
|
|
13
|
+
const GET_VERSION = 0x5a;
|
|
14
|
+
const WRITE_SCRIPT = 0x57;
|
|
15
|
+
const READ_SCRIPT = 0x52;
|
|
16
|
+
const PLAY_SCRIPT = 0x70;
|
|
17
|
+
const STOP_SCRIPT = 0x0f;
|
|
18
|
+
|
|
19
|
+
class Pixel {
|
|
20
|
+
|
|
21
|
+
constructor(address) {
|
|
22
|
+
this.address = address || 0x01;
|
|
23
|
+
this.wire = new Wire(this.address);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
off() {
|
|
27
|
+
this.setRGB(0, 0, 0);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
getAddress(callback) {
|
|
31
|
+
this._read(GET_ADDRESS, 1, callback);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
getVersion(callback) {
|
|
35
|
+
this._read(GET_VERSION, 1, callback);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
setFadeSpeed(speed) {
|
|
39
|
+
this._send(SET_FADE, [speed]);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
setRGB(r, g, b) {
|
|
43
|
+
this._send(TO_RGB, [r, g, b]);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
getRGB(callback) {
|
|
47
|
+
setTimeout(() => {
|
|
48
|
+
this._read(GET_RGB, 3, callback);
|
|
49
|
+
}, 200);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
fadeToRGB(r, g, b) {
|
|
53
|
+
this._send(FADE_TO_RGB, [r, g, b]);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
fadeToHSB(h, s, b) {
|
|
57
|
+
this._send(FADE_TO_HSB, [h, s, b]);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
_send(cmd, values) {
|
|
61
|
+
this.wire.writeBytes(cmd, values);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
_read(cmd, length, callback) {
|
|
65
|
+
this.wire.readBytes(cmd, length, callback);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
module.exports = Pixel;
|
package/lib/i2c.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
const wire = require('../build/Release/i2c');
|
|
2
|
+
const { EventEmitter } = require('events');
|
|
3
|
+
const tick = setImmediate || process.nextTick;
|
|
4
|
+
|
|
5
|
+
class i2c extends EventEmitter {
|
|
6
|
+
|
|
7
|
+
constructor(address, options) {
|
|
8
|
+
super();
|
|
9
|
+
this.address = address;
|
|
10
|
+
this.options = {
|
|
11
|
+
debug: false,
|
|
12
|
+
device: "/dev/i2c-1",
|
|
13
|
+
...(options || {})
|
|
14
|
+
};
|
|
15
|
+
this.history = [];
|
|
16
|
+
|
|
17
|
+
if (this.options.debug) {
|
|
18
|
+
require('repl').start({
|
|
19
|
+
prompt: "i2c > "
|
|
20
|
+
}).context.wire = this;
|
|
21
|
+
process.stdin.emit('data', ''); // trigger repl
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
process.on('exit', () => this.close());
|
|
25
|
+
|
|
26
|
+
this.on('data', (data) => {
|
|
27
|
+
this.history.push(data);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
this.on('error', (err) => {
|
|
31
|
+
console.log(`Error: ${err}`);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
this.open(this.options.device, (err) => {
|
|
35
|
+
if (!err) {
|
|
36
|
+
this.setAddress(this.address);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
scan(callback) {
|
|
42
|
+
wire.scan((err, data) => {
|
|
43
|
+
tick(() => {
|
|
44
|
+
// data is an Array from C++
|
|
45
|
+
callback(err, data.filter((num) => num >= 0));
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
setAddress(address) {
|
|
51
|
+
wire.setAddress(address);
|
|
52
|
+
this.address = address;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
open(device, callback) {
|
|
56
|
+
wire.open(device, (err) => {
|
|
57
|
+
tick(() => {
|
|
58
|
+
if (callback) callback(err);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
close() {
|
|
64
|
+
wire.close();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
write(buf, callback) {
|
|
68
|
+
this.setAddress(this.address);
|
|
69
|
+
if (!Buffer.isBuffer(buf)) {
|
|
70
|
+
buf = Buffer.from(buf);
|
|
71
|
+
}
|
|
72
|
+
wire.write(buf, (err) => {
|
|
73
|
+
tick(() => {
|
|
74
|
+
if (callback) callback(err);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
writeByte(byte, callback) {
|
|
80
|
+
this.setAddress(this.address);
|
|
81
|
+
wire.writeByte(byte, (err) => {
|
|
82
|
+
tick(() => {
|
|
83
|
+
if (callback) callback(err);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
writeBytes(cmd, buf, callback) {
|
|
89
|
+
this.setAddress(this.address);
|
|
90
|
+
if (!Buffer.isBuffer(buf)) {
|
|
91
|
+
buf = Buffer.from(buf);
|
|
92
|
+
}
|
|
93
|
+
wire.writeBlock(cmd, buf, (err) => {
|
|
94
|
+
tick(() => {
|
|
95
|
+
if (callback) callback(err);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
read(len, callback) {
|
|
101
|
+
this.setAddress(this.address);
|
|
102
|
+
wire.read(len, (err, data) => {
|
|
103
|
+
tick(() => {
|
|
104
|
+
if (callback) callback(err, data);
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
readByte(callback) {
|
|
110
|
+
this.setAddress(this.address);
|
|
111
|
+
wire.readByte((err, data) => {
|
|
112
|
+
tick(() => {
|
|
113
|
+
if (callback) callback(err, data);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
readBytes(cmd, len, callback) {
|
|
119
|
+
this.setAddress(this.address);
|
|
120
|
+
wire.readBlock(cmd, len, null, (err, actualBuffer) => {
|
|
121
|
+
tick(() => {
|
|
122
|
+
if (callback) callback(err, actualBuffer);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
stream(cmd, len, delay) {
|
|
128
|
+
if (delay == null) delay = 100;
|
|
129
|
+
this.setAddress(this.address);
|
|
130
|
+
wire.readBlock(cmd, len, delay, (err, data) => {
|
|
131
|
+
if (err) {
|
|
132
|
+
this.emit('error', err);
|
|
133
|
+
} else {
|
|
134
|
+
this.emit('data', {
|
|
135
|
+
address: this.address,
|
|
136
|
+
data: data,
|
|
137
|
+
cmd: cmd,
|
|
138
|
+
length: len,
|
|
139
|
+
timestamp: Date.now()
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
module.exports = i2c;
|
package/main.js
CHANGED
package/package.json
CHANGED
|
@@ -1,20 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "i2c",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Native bindings for i2c-dev. Plays well with Raspberry Pi and BeagleBone.",
|
|
5
5
|
"main": "main.js",
|
|
6
6
|
"author": "Kelly Korevec",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
|
-
"url": "
|
|
9
|
+
"url": "git+ssh://git@github.com/korevec/node-i2c.git"
|
|
10
10
|
},
|
|
11
11
|
"license": "BSD-3-Clause-Attribution",
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"bindings": "1.5.0",
|
|
14
|
-
"
|
|
15
|
-
"coffeescript": "2.5.1",
|
|
16
|
-
"repl":"0.1.3",
|
|
17
|
-
"nan": "~2.14.0"
|
|
13
|
+
"bindings": "^1.5.0",
|
|
14
|
+
"nan": "^2.24.0"
|
|
18
15
|
},
|
|
19
|
-
"engine": "node >= 0.
|
|
16
|
+
"engine": "node >= 18.0.0"
|
|
20
17
|
}
|
package/src/i2c.cc
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
#include <stdio.h>
|
|
9
9
|
#include <unistd.h>
|
|
10
10
|
#include <string.h>
|
|
11
|
+
#include <vector>
|
|
11
12
|
#include "i2c-dev.h"
|
|
12
13
|
|
|
13
14
|
|
|
@@ -35,7 +36,7 @@ void SetAddress(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
35
36
|
Nan::ThrowTypeError("addr must be an int");
|
|
36
37
|
return;
|
|
37
38
|
}
|
|
38
|
-
addr = info[0]->Int32Value();
|
|
39
|
+
addr = info[0]->Int32Value(Nan::GetCurrentContext()).FromJust();
|
|
39
40
|
setAddress(addr);
|
|
40
41
|
}
|
|
41
42
|
|
|
@@ -57,7 +58,7 @@ void Scan(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
57
58
|
if (res >= 0) {
|
|
58
59
|
res = i;
|
|
59
60
|
}
|
|
60
|
-
|
|
61
|
+
Nan::Set(results, i, Nan::New<Integer>(res));
|
|
61
62
|
}
|
|
62
63
|
|
|
63
64
|
setAddress(addr);
|
|
@@ -65,7 +66,7 @@ void Scan(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
65
66
|
const unsigned argc = 2;
|
|
66
67
|
Local<Value> argv[argc] = { err, results };
|
|
67
68
|
|
|
68
|
-
Nan::
|
|
69
|
+
Nan::Call(callback, Nan::GetCurrentContext()->Global(), argc, argv);
|
|
69
70
|
|
|
70
71
|
info.GetReturnValue().Set(results);
|
|
71
72
|
}
|
|
@@ -81,7 +82,7 @@ void Close(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
81
82
|
void Open(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
82
83
|
Nan::HandleScope scope;
|
|
83
84
|
|
|
84
|
-
|
|
85
|
+
Nan::Utf8String device(info[0]);
|
|
85
86
|
Local<Value> err = Nan::New<Value>(Nan::Null());
|
|
86
87
|
|
|
87
88
|
fd = open(*device, O_RDWR);
|
|
@@ -93,14 +94,14 @@ void Open(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
93
94
|
const unsigned argc = 1;
|
|
94
95
|
Local<Function> callback = Local<Function>::Cast(info[1]);
|
|
95
96
|
Local<Value> argv[argc] = { err };
|
|
96
|
-
Nan::
|
|
97
|
+
Nan::Call(callback, Nan::GetCurrentContext()->Global(), argc, argv);
|
|
97
98
|
}
|
|
98
99
|
}
|
|
99
100
|
|
|
100
101
|
void Read(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
101
102
|
Nan::HandleScope scope;
|
|
102
103
|
|
|
103
|
-
int len = info[0]->Int32Value();
|
|
104
|
+
int len = info[0]->Int32Value(Nan::GetCurrentContext()).FromJust();
|
|
104
105
|
|
|
105
106
|
Local<Array> data = Nan::New<Array>();
|
|
106
107
|
|
|
@@ -111,7 +112,7 @@ void Read(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
111
112
|
err = Nan::Error(Nan::New("Cannot read from device").ToLocalChecked());
|
|
112
113
|
} else {
|
|
113
114
|
for (int i = 0; i < len; ++i) {
|
|
114
|
-
|
|
115
|
+
Nan::Set(data, i, Nan::New<Integer>(buf[i]));
|
|
115
116
|
}
|
|
116
117
|
}
|
|
117
118
|
delete[] buf;
|
|
@@ -120,7 +121,7 @@ void Read(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
120
121
|
const unsigned argc = 2;
|
|
121
122
|
Local<Function> callback = Local<Function>::Cast(info[1]);
|
|
122
123
|
Local<Value> argv[argc] = { err, data };
|
|
123
|
-
Nan::
|
|
124
|
+
Nan::Call(callback, Nan::GetCurrentContext()->Global(), argc, argv);
|
|
124
125
|
}
|
|
125
126
|
}
|
|
126
127
|
|
|
@@ -142,7 +143,7 @@ void ReadByte(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
142
143
|
const unsigned argc = 2;
|
|
143
144
|
Local<Function> callback = Local<Function>::Cast(info[0]);
|
|
144
145
|
Local<Value> argv[argc] = { err, data };
|
|
145
|
-
Nan::
|
|
146
|
+
Nan::Call(callback, Nan::GetCurrentContext()->Global(), argc, argv);
|
|
146
147
|
}
|
|
147
148
|
|
|
148
149
|
info.GetReturnValue().Set(data);
|
|
@@ -151,31 +152,31 @@ void ReadByte(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
151
152
|
void ReadBlock(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
152
153
|
Nan::HandleScope scope;
|
|
153
154
|
|
|
154
|
-
int8_t cmd = info[0]->Int32Value();
|
|
155
|
-
int32_t len = info[1]->Int32Value();
|
|
156
|
-
|
|
155
|
+
int8_t cmd = info[0]->Int32Value(Nan::GetCurrentContext()).FromJust();
|
|
156
|
+
int32_t len = info[1]->Int32Value(Nan::GetCurrentContext()).FromJust();
|
|
157
|
+
|
|
158
|
+
std::vector<uint8_t> data(len);
|
|
157
159
|
Local<Value> err = Nan::New<Value>(Nan::Null());
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
Local<Object> buffer = Nan::NewBuffer(len).ToLocalChecked(); //todo - some error checking here as the devs intended?
|
|
160
|
+
|
|
161
|
+
Local<Object> buffer = Nan::NewBuffer(len).ToLocalChecked();
|
|
161
162
|
|
|
162
163
|
|
|
163
164
|
while (fd > 0) {
|
|
164
|
-
if (i2c_smbus_read_i2c_block_data(fd, cmd, len, data) != len) {
|
|
165
|
+
if (i2c_smbus_read_i2c_block_data(fd, cmd, len, data.data()) != len) {
|
|
165
166
|
err = Nan::Error(Nan::New("Error reading length of bytes").ToLocalChecked());
|
|
166
167
|
}
|
|
167
168
|
|
|
168
|
-
memcpy(node::Buffer::Data(buffer), data, len);
|
|
169
|
+
memcpy(node::Buffer::Data(buffer), data.data(), len);
|
|
169
170
|
|
|
170
171
|
if (info[3]->IsFunction()) {
|
|
171
172
|
const unsigned argc = 2;
|
|
172
173
|
Local<Function> callback = Local<Function>::Cast(info[3]);
|
|
173
174
|
Local<Value> argv[argc] = { err, buffer };
|
|
174
|
-
Nan::
|
|
175
|
+
Nan::Call(callback, Nan::GetCurrentContext()->Global(), argc, argv);
|
|
175
176
|
}
|
|
176
177
|
|
|
177
178
|
if (info[2]->IsNumber()) {
|
|
178
|
-
int32_t delay = info[2]->Int32Value();
|
|
179
|
+
int32_t delay = info[2]->Int32Value(Nan::GetCurrentContext()).FromJust();
|
|
179
180
|
usleep(delay * 1000);
|
|
180
181
|
} else {
|
|
181
182
|
break;
|
|
@@ -188,10 +189,10 @@ void ReadBlock(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
188
189
|
void Write(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
189
190
|
Nan::HandleScope scope;
|
|
190
191
|
|
|
191
|
-
|
|
192
|
+
auto bufferObj = info[0]->ToObject(Nan::GetCurrentContext()).ToLocalChecked();
|
|
192
193
|
|
|
193
|
-
int len = node::Buffer::Length(
|
|
194
|
-
char* data = node::Buffer::Data(
|
|
194
|
+
int len = node::Buffer::Length(bufferObj);
|
|
195
|
+
char* data = node::Buffer::Data(bufferObj);
|
|
195
196
|
|
|
196
197
|
Local<Value> err = Nan::New<Value>(Nan::Null());
|
|
197
198
|
|
|
@@ -203,14 +204,14 @@ void Write(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
203
204
|
const unsigned argc = 1;
|
|
204
205
|
Local<Function> callback = Local<Function>::Cast(info[1]);
|
|
205
206
|
Local<Value> argv[argc] = { err };
|
|
206
|
-
Nan::
|
|
207
|
+
Nan::Call(callback, Nan::GetCurrentContext()->Global(), argc, argv);
|
|
207
208
|
}
|
|
208
209
|
}
|
|
209
210
|
|
|
210
211
|
void WriteByte(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
211
212
|
Nan::HandleScope scope;
|
|
212
213
|
|
|
213
|
-
int8_t byte = info[0]->Int32Value();
|
|
214
|
+
int8_t byte = info[0]->Int32Value(Nan::GetCurrentContext()).FromJust();
|
|
214
215
|
Local<Value> err = Nan::New<Value>(Nan::Null());
|
|
215
216
|
|
|
216
217
|
if (i2c_smbus_write_byte(fd, byte) == -1) {
|
|
@@ -221,17 +222,19 @@ void WriteByte(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
221
222
|
const unsigned argc = 1;
|
|
222
223
|
Local<Function> callback = Local<Function>::Cast(info[1]);
|
|
223
224
|
Local<Value> argv[argc] = { err };
|
|
224
|
-
Nan::
|
|
225
|
+
Nan::Call(callback, Nan::GetCurrentContext()->Global(), argc, argv);
|
|
225
226
|
}
|
|
226
227
|
}
|
|
227
228
|
|
|
228
229
|
void WriteBlock(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
229
230
|
Nan::HandleScope scope;
|
|
230
231
|
|
|
231
|
-
Local<
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
232
|
+
v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
|
|
233
|
+
auto bufferObj = info[1]->ToObject(context).ToLocalChecked();
|
|
234
|
+
|
|
235
|
+
int8_t cmd = info[0]->Int32Value(Nan::GetCurrentContext()).FromJust();
|
|
236
|
+
int len = node::Buffer::Length(bufferObj);
|
|
237
|
+
char* data = node::Buffer::Data(bufferObj);
|
|
235
238
|
|
|
236
239
|
Local<Value> err = Nan::New<Value>(Nan::Null());
|
|
237
240
|
|
|
@@ -243,15 +246,15 @@ void WriteBlock(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
243
246
|
const unsigned argc = 1;
|
|
244
247
|
Local<Function> callback = Local<Function>::Cast(info[2]);
|
|
245
248
|
Local<Value> argv[argc] = { err };
|
|
246
|
-
Nan::
|
|
249
|
+
Nan::Call(callback, Nan::GetCurrentContext()->Global(), argc, argv);
|
|
247
250
|
}
|
|
248
251
|
}
|
|
249
252
|
|
|
250
253
|
void WriteWord(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
251
254
|
Nan::HandleScope scope;
|
|
252
255
|
|
|
253
|
-
int8_t cmd = info[0]->Int32Value();
|
|
254
|
-
int16_t word = info[1]->Int32Value();
|
|
256
|
+
int8_t cmd = info[0]->Int32Value(Nan::GetCurrentContext()).FromJust();
|
|
257
|
+
int16_t word = info[1]->Int32Value(Nan::GetCurrentContext()).FromJust();
|
|
255
258
|
|
|
256
259
|
Local<Value> err = Nan::New<Value>(Nan::Null());
|
|
257
260
|
|
|
@@ -263,33 +266,42 @@ void WriteWord(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
263
266
|
const unsigned argc = 1;
|
|
264
267
|
Local<Function> callback = Local<Function>::Cast(info[2]);
|
|
265
268
|
Local<Value> argv[argc] = { err };
|
|
266
|
-
Nan::
|
|
269
|
+
Nan::Call(callback, Nan::GetCurrentContext()->Global(), argc, argv);
|
|
267
270
|
}
|
|
268
271
|
}
|
|
269
272
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
273
|
+
NAN_MODULE_INIT(Init) {
|
|
274
|
+
|
|
275
|
+
Nan::Set(target, Nan::New("setAddress").ToLocalChecked(),
|
|
276
|
+
Nan::GetFunction(Nan::New<FunctionTemplate>(SetAddress)).ToLocalChecked());
|
|
277
|
+
|
|
278
|
+
Nan::Set(target, Nan::New("scan").ToLocalChecked(),
|
|
279
|
+
Nan::GetFunction(Nan::New<FunctionTemplate>(Scan)).ToLocalChecked());
|
|
280
|
+
|
|
281
|
+
Nan::Set(target, Nan::New("open").ToLocalChecked(),
|
|
282
|
+
Nan::GetFunction(Nan::New<FunctionTemplate>(Open)).ToLocalChecked());
|
|
283
|
+
|
|
284
|
+
Nan::Set(target, Nan::New("close").ToLocalChecked(),
|
|
285
|
+
Nan::GetFunction(Nan::New<FunctionTemplate>(Close)).ToLocalChecked());
|
|
286
|
+
|
|
287
|
+
Nan::Set(target, Nan::New("write").ToLocalChecked(),
|
|
288
|
+
Nan::GetFunction(Nan::New<FunctionTemplate>(Write)).ToLocalChecked());
|
|
289
|
+
|
|
290
|
+
Nan::Set(target, Nan::New("writeByte").ToLocalChecked(),
|
|
291
|
+
Nan::GetFunction(Nan::New<FunctionTemplate>(WriteByte)).ToLocalChecked());
|
|
292
|
+
|
|
293
|
+
Nan::Set(target, Nan::New("writeBlock").ToLocalChecked(),
|
|
294
|
+
Nan::GetFunction(Nan::New<FunctionTemplate>(WriteBlock)).ToLocalChecked());
|
|
295
|
+
|
|
296
|
+
Nan::Set(target, Nan::New("read").ToLocalChecked(),
|
|
297
|
+
Nan::GetFunction(Nan::New<FunctionTemplate>(Read)).ToLocalChecked());
|
|
298
|
+
|
|
299
|
+
Nan::Set(target, Nan::New("readByte").ToLocalChecked(),
|
|
300
|
+
Nan::GetFunction(Nan::New<FunctionTemplate>(ReadByte)).ToLocalChecked());
|
|
301
|
+
|
|
302
|
+
Nan::Set(target, Nan::New("readBlock").ToLocalChecked(),
|
|
303
|
+
Nan::GetFunction(Nan::New<FunctionTemplate>(ReadBlock)).ToLocalChecked());
|
|
292
304
|
|
|
293
305
|
}
|
|
294
306
|
|
|
295
|
-
NODE_MODULE(i2c, Init)
|
|
307
|
+
NODE_MODULE(i2c, Init)
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
Wire = require '../../main'
|
|
2
|
-
|
|
3
|
-
# for AK8975
|
|
4
|
-
# info: https://github.com/jrowberg/i2cdevlib/blob/master/Arduino/AK8975/AK8975.cpp
|
|
5
|
-
# http://stackoverflow.com/questions/4768933/read-two-bytes-into-an-integer
|
|
6
|
-
|
|
7
|
-
RANGE_BWIDTH = 0x14
|
|
8
|
-
RANGE_BIT = 0x04
|
|
9
|
-
RANGE_LENGTH = 0x02
|
|
10
|
-
RANGE_2G = 0x00
|
|
11
|
-
BANDWIDTH_BIT = 0x02
|
|
12
|
-
BANDWIDTH_LENGTH = 0x03
|
|
13
|
-
BW_25HZ = 0x00
|
|
14
|
-
GET_ID = 0x00
|
|
15
|
-
|
|
16
|
-
class Accelerometer
|
|
17
|
-
|
|
18
|
-
constructor: (@address) ->
|
|
19
|
-
@wire = new Wire @address
|
|
20
|
-
|
|
21
|
-
@setRange()
|
|
22
|
-
@setBandwidth()
|
|
23
|
-
|
|
24
|
-
@wire.on 'data', (data) ->
|
|
25
|
-
console.log data
|
|
26
|
-
|
|
27
|
-
setRange: ->
|
|
28
|
-
@wire.write(RANGE_BWIDTH, [RANGE_BIT, RANGE_LENGTH, RANGE_2G], null)
|
|
29
|
-
|
|
30
|
-
testConnection: (callback) ->
|
|
31
|
-
@getDeviceID (err, data) ->
|
|
32
|
-
data[0] == 0b010
|
|
33
|
-
|
|
34
|
-
getDeviceID: (callback) ->
|
|
35
|
-
@wire.read GET_ID, 1, callback
|
|
36
|
-
|
|
37
|
-
setBandwidth: ->
|
|
38
|
-
@wire.write(RANGE_BWIDTH, [BANDWIDTH_BIT, BANDWIDTH_LENGTH, BW_25HZ], null)
|
|
39
|
-
|
|
40
|
-
getHeading: ->
|
|
41
|
-
@wire.write(0x0A, 0x1);
|
|
42
|
-
setTimeout =>
|
|
43
|
-
@wire.read 0x03, 6, (err, buffer) ->
|
|
44
|
-
pos =
|
|
45
|
-
x: ((buffer[1]) << 8) | buffer[0]
|
|
46
|
-
y: ((buffer[3]) << 8) | buffer[2]
|
|
47
|
-
z: ((buffer[5]) << 8) | buffer[4]
|
|
48
|
-
console.log pos
|
|
49
|
-
, 10
|
|
50
|
-
getMotion: ->
|
|
51
|
-
@wire.stream 0x02, 6, 100
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
accel = new Accelerometer(56)
|
|
55
|
-
accel.getHeading()
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
Wire = require 'i2c'
|
|
2
|
-
_ = require 'underscore'
|
|
3
|
-
|
|
4
|
-
# BlinkM http://thingm.com/products/blinkm
|
|
5
|
-
# firmware http://code.google.com/p/codalyze/wiki/CyzRgb
|
|
6
|
-
|
|
7
|
-
TO_RGB = 0x6e
|
|
8
|
-
GET_RGB = 0x67
|
|
9
|
-
FADE_TO_RGB = 0x63
|
|
10
|
-
FADE_TO_HSB = 0x68
|
|
11
|
-
GET_ADDRESS = 0x61
|
|
12
|
-
SET_ADDRESS = 0x41
|
|
13
|
-
SET_FADE = 0x66
|
|
14
|
-
GET_VERSION = 0x5a
|
|
15
|
-
WRITE_SCRIPT = 0x57
|
|
16
|
-
READ_SCRIPT = 0x52
|
|
17
|
-
PLAY_SCRIPT = 0x70
|
|
18
|
-
STOP_SCRIPT = 0x0f
|
|
19
|
-
|
|
20
|
-
class Pixel
|
|
21
|
-
|
|
22
|
-
address: 0x01
|
|
23
|
-
|
|
24
|
-
constructor: (@address) ->
|
|
25
|
-
@wire = new Wire(@address);
|
|
26
|
-
|
|
27
|
-
off: ->
|
|
28
|
-
@setRGB(0, 0, 0)
|
|
29
|
-
|
|
30
|
-
getAddress: (callback) ->
|
|
31
|
-
@_read GET_ADDRESS, 1, callback
|
|
32
|
-
|
|
33
|
-
getVersion: (callback) ->
|
|
34
|
-
@_read GET_VERSION, 1, callback
|
|
35
|
-
|
|
36
|
-
setFadeSpeed: (speed) ->
|
|
37
|
-
@_send SET_FADE, speed
|
|
38
|
-
|
|
39
|
-
setRGB: (r, g, b) ->
|
|
40
|
-
@_send TO_RGB, [r, g, b]
|
|
41
|
-
|
|
42
|
-
getRGB: (callback) ->
|
|
43
|
-
setTimeout =>
|
|
44
|
-
@_read GET_RGB, 3, callback
|
|
45
|
-
, 200
|
|
46
|
-
|
|
47
|
-
fadeToRGB: (r, g, b) ->
|
|
48
|
-
@_send FADE_TO_RGB, [r, g, b]
|
|
49
|
-
|
|
50
|
-
fadeToHSB: (h, s, b) ->
|
|
51
|
-
@_send FADE_TO_HSB, [h, s, b]
|
|
52
|
-
|
|
53
|
-
_send: (cmd, values) ->
|
|
54
|
-
@wire.writeBytes cmd, values
|
|
55
|
-
|
|
56
|
-
_read: (cmd, length, callback) ->
|
|
57
|
-
@wire.readBytes cmd, length, callback
|
package/lib/i2c.coffee
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
_ = require 'underscore'
|
|
2
|
-
wire = require '../build/Release/i2c'
|
|
3
|
-
EventEmitter = require('events').EventEmitter
|
|
4
|
-
tick = setImmediate || process.nextTick
|
|
5
|
-
|
|
6
|
-
class i2c extends EventEmitter
|
|
7
|
-
|
|
8
|
-
history: []
|
|
9
|
-
|
|
10
|
-
constructor: (@address, @options = {}) ->
|
|
11
|
-
_.defaults @options,
|
|
12
|
-
debug: false
|
|
13
|
-
device: "/dev/i2c-1"
|
|
14
|
-
|
|
15
|
-
if @options.debug
|
|
16
|
-
require('repl').start(
|
|
17
|
-
prompt: "i2c > "
|
|
18
|
-
).context.wire = @
|
|
19
|
-
process.stdin.emit 'data', '' # trigger repl
|
|
20
|
-
|
|
21
|
-
process.on 'exit', => @close()
|
|
22
|
-
|
|
23
|
-
@on 'data', (data) =>
|
|
24
|
-
@history.push data
|
|
25
|
-
|
|
26
|
-
@on 'error', (err) ->
|
|
27
|
-
console.log "Error: #{err}"
|
|
28
|
-
|
|
29
|
-
@open @options.device, (err) =>
|
|
30
|
-
unless err then @setAddress @address
|
|
31
|
-
|
|
32
|
-
scan: (callback) ->
|
|
33
|
-
wire.scan (err, data) ->
|
|
34
|
-
tick ->
|
|
35
|
-
callback err, _.filter data, (num) -> return num >= 0
|
|
36
|
-
|
|
37
|
-
setAddress: (address) ->
|
|
38
|
-
wire.setAddress address
|
|
39
|
-
@address = address
|
|
40
|
-
|
|
41
|
-
open: (device, callback) ->
|
|
42
|
-
wire.open device, (err) ->
|
|
43
|
-
tick ->
|
|
44
|
-
callback err
|
|
45
|
-
|
|
46
|
-
close: ->
|
|
47
|
-
wire.close()
|
|
48
|
-
|
|
49
|
-
write: (buf, callback) ->
|
|
50
|
-
@setAddress @address
|
|
51
|
-
unless Buffer.isBuffer(buf) then buf = new Buffer(buf)
|
|
52
|
-
wire.write buf, (err) ->
|
|
53
|
-
tick ->
|
|
54
|
-
callback err
|
|
55
|
-
|
|
56
|
-
writeByte: (byte, callback) ->
|
|
57
|
-
@setAddress @address
|
|
58
|
-
wire.writeByte byte, (err) ->
|
|
59
|
-
tick ->
|
|
60
|
-
callback err
|
|
61
|
-
|
|
62
|
-
writeBytes: (cmd, buf, callback) ->
|
|
63
|
-
@setAddress @address
|
|
64
|
-
unless Buffer.isBuffer(buf) then buf = new Buffer(buf)
|
|
65
|
-
wire.writeBlock cmd, buf, (err) ->
|
|
66
|
-
tick ->
|
|
67
|
-
callback err
|
|
68
|
-
|
|
69
|
-
read: (len, callback) ->
|
|
70
|
-
@setAddress @address
|
|
71
|
-
wire.read len, (err, data) ->
|
|
72
|
-
tick ->
|
|
73
|
-
callback err, data
|
|
74
|
-
|
|
75
|
-
readByte: (callback) ->
|
|
76
|
-
@setAddress @address
|
|
77
|
-
wire.readByte (err, data) ->
|
|
78
|
-
tick ->
|
|
79
|
-
callback err, data
|
|
80
|
-
|
|
81
|
-
readBytes: (cmd, len, callback) ->
|
|
82
|
-
@setAddress @address
|
|
83
|
-
wire.readBlock cmd, len, null, (err, actualBuffer) ->
|
|
84
|
-
tick ->
|
|
85
|
-
callback err, actualBuffer
|
|
86
|
-
|
|
87
|
-
stream: (cmd, len, delay = 100) ->
|
|
88
|
-
@setAddress @address
|
|
89
|
-
wire.readBlock cmd, len, delay, (err, data) =>
|
|
90
|
-
if err
|
|
91
|
-
@emit 'error', err
|
|
92
|
-
else
|
|
93
|
-
@emit 'data',
|
|
94
|
-
address : @address
|
|
95
|
-
data : data
|
|
96
|
-
cmd : cmd
|
|
97
|
-
length : len
|
|
98
|
-
timestamp : Date.now()
|
|
99
|
-
|
|
100
|
-
module.exports = i2c
|