i2c 0.2.5 → 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/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 +49 -74
- package/examples/accelerometer/accel.coffee +0 -55
- package/examples/blinkm/blinkm.coffee +0 -57
- package/lib/i2c.coffee +0 -100
|
@@ -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
|
|
|
@@ -57,11 +58,7 @@ void Scan(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
57
58
|
if (res >= 0) {
|
|
58
59
|
res = i;
|
|
59
60
|
}
|
|
60
|
-
|
|
61
|
-
Nan::GetCurrentContext(),
|
|
62
|
-
i,
|
|
63
|
-
Nan::New<Integer>(res)
|
|
64
|
-
);
|
|
61
|
+
Nan::Set(results, i, Nan::New<Integer>(res));
|
|
65
62
|
}
|
|
66
63
|
|
|
67
64
|
setAddress(addr);
|
|
@@ -69,7 +66,7 @@ void Scan(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
69
66
|
const unsigned argc = 2;
|
|
70
67
|
Local<Value> argv[argc] = { err, results };
|
|
71
68
|
|
|
72
|
-
Nan::
|
|
69
|
+
Nan::Call(callback, Nan::GetCurrentContext()->Global(), argc, argv);
|
|
73
70
|
|
|
74
71
|
info.GetReturnValue().Set(results);
|
|
75
72
|
}
|
|
@@ -97,7 +94,7 @@ void Open(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
97
94
|
const unsigned argc = 1;
|
|
98
95
|
Local<Function> callback = Local<Function>::Cast(info[1]);
|
|
99
96
|
Local<Value> argv[argc] = { err };
|
|
100
|
-
Nan::
|
|
97
|
+
Nan::Call(callback, Nan::GetCurrentContext()->Global(), argc, argv);
|
|
101
98
|
}
|
|
102
99
|
}
|
|
103
100
|
|
|
@@ -115,7 +112,7 @@ void Read(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
115
112
|
err = Nan::Error(Nan::New("Cannot read from device").ToLocalChecked());
|
|
116
113
|
} else {
|
|
117
114
|
for (int i = 0; i < len; ++i) {
|
|
118
|
-
|
|
115
|
+
Nan::Set(data, i, Nan::New<Integer>(buf[i]));
|
|
119
116
|
}
|
|
120
117
|
}
|
|
121
118
|
delete[] buf;
|
|
@@ -124,7 +121,7 @@ void Read(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
124
121
|
const unsigned argc = 2;
|
|
125
122
|
Local<Function> callback = Local<Function>::Cast(info[1]);
|
|
126
123
|
Local<Value> argv[argc] = { err, data };
|
|
127
|
-
Nan::
|
|
124
|
+
Nan::Call(callback, Nan::GetCurrentContext()->Global(), argc, argv);
|
|
128
125
|
}
|
|
129
126
|
}
|
|
130
127
|
|
|
@@ -146,7 +143,7 @@ void ReadByte(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
146
143
|
const unsigned argc = 2;
|
|
147
144
|
Local<Function> callback = Local<Function>::Cast(info[0]);
|
|
148
145
|
Local<Value> argv[argc] = { err, data };
|
|
149
|
-
Nan::
|
|
146
|
+
Nan::Call(callback, Nan::GetCurrentContext()->Global(), argc, argv);
|
|
150
147
|
}
|
|
151
148
|
|
|
152
149
|
info.GetReturnValue().Set(data);
|
|
@@ -157,25 +154,25 @@ void ReadBlock(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
157
154
|
|
|
158
155
|
int8_t cmd = info[0]->Int32Value(Nan::GetCurrentContext()).FromJust();
|
|
159
156
|
int32_t len = info[1]->Int32Value(Nan::GetCurrentContext()).FromJust();
|
|
160
|
-
|
|
157
|
+
|
|
158
|
+
std::vector<uint8_t> data(len);
|
|
161
159
|
Local<Value> err = Nan::New<Value>(Nan::Null());
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
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();
|
|
165
162
|
|
|
166
163
|
|
|
167
164
|
while (fd > 0) {
|
|
168
|
-
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) {
|
|
169
166
|
err = Nan::Error(Nan::New("Error reading length of bytes").ToLocalChecked());
|
|
170
167
|
}
|
|
171
168
|
|
|
172
|
-
memcpy(node::Buffer::Data(buffer), data, len);
|
|
169
|
+
memcpy(node::Buffer::Data(buffer), data.data(), len);
|
|
173
170
|
|
|
174
171
|
if (info[3]->IsFunction()) {
|
|
175
172
|
const unsigned argc = 2;
|
|
176
173
|
Local<Function> callback = Local<Function>::Cast(info[3]);
|
|
177
174
|
Local<Value> argv[argc] = { err, buffer };
|
|
178
|
-
Nan::
|
|
175
|
+
Nan::Call(callback, Nan::GetCurrentContext()->Global(), argc, argv);
|
|
179
176
|
}
|
|
180
177
|
|
|
181
178
|
if (info[2]->IsNumber()) {
|
|
@@ -192,7 +189,6 @@ void ReadBlock(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
192
189
|
void Write(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
193
190
|
Nan::HandleScope scope;
|
|
194
191
|
|
|
195
|
-
// v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext();
|
|
196
192
|
auto bufferObj = info[0]->ToObject(Nan::GetCurrentContext()).ToLocalChecked();
|
|
197
193
|
|
|
198
194
|
int len = node::Buffer::Length(bufferObj);
|
|
@@ -208,7 +204,7 @@ void Write(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
208
204
|
const unsigned argc = 1;
|
|
209
205
|
Local<Function> callback = Local<Function>::Cast(info[1]);
|
|
210
206
|
Local<Value> argv[argc] = { err };
|
|
211
|
-
Nan::
|
|
207
|
+
Nan::Call(callback, Nan::GetCurrentContext()->Global(), argc, argv);
|
|
212
208
|
}
|
|
213
209
|
}
|
|
214
210
|
|
|
@@ -226,7 +222,7 @@ void WriteByte(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
226
222
|
const unsigned argc = 1;
|
|
227
223
|
Local<Function> callback = Local<Function>::Cast(info[1]);
|
|
228
224
|
Local<Value> argv[argc] = { err };
|
|
229
|
-
Nan::
|
|
225
|
+
Nan::Call(callback, Nan::GetCurrentContext()->Global(), argc, argv);
|
|
230
226
|
}
|
|
231
227
|
}
|
|
232
228
|
|
|
@@ -250,7 +246,7 @@ void WriteBlock(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
250
246
|
const unsigned argc = 1;
|
|
251
247
|
Local<Function> callback = Local<Function>::Cast(info[2]);
|
|
252
248
|
Local<Value> argv[argc] = { err };
|
|
253
|
-
Nan::
|
|
249
|
+
Nan::Call(callback, Nan::GetCurrentContext()->Global(), argc, argv);
|
|
254
250
|
}
|
|
255
251
|
}
|
|
256
252
|
|
|
@@ -270,62 +266,41 @@ void WriteWord(const Nan::FunctionCallbackInfo<v8::Value>& info) {
|
|
|
270
266
|
const unsigned argc = 1;
|
|
271
267
|
Local<Function> callback = Local<Function>::Cast(info[2]);
|
|
272
268
|
Local<Value> argv[argc] = { err };
|
|
273
|
-
Nan::
|
|
269
|
+
Nan::Call(callback, Nan::GetCurrentContext()->Global(), argc, argv);
|
|
274
270
|
}
|
|
275
271
|
}
|
|
276
272
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
Nan::
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
Nan::New(
|
|
287
|
-
|
|
288
|
-
)
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
Nan::New<
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
Nan::
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
Nan::New(
|
|
302
|
-
|
|
303
|
-
)
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
Nan::New<
|
|
308
|
-
);
|
|
309
|
-
exports->Set(
|
|
310
|
-
Nan::GetCurrentContext(),
|
|
311
|
-
Nan::New("writeBlock").ToLocalChecked(),
|
|
312
|
-
Nan::New<v8::FunctionTemplate>(WriteBlock)->GetFunction(Nan::GetCurrentContext()).FromMaybe(v8::Local<v8::Function>())
|
|
313
|
-
);
|
|
314
|
-
exports->Set(
|
|
315
|
-
Nan::GetCurrentContext(),
|
|
316
|
-
Nan::New("read").ToLocalChecked(),
|
|
317
|
-
Nan::New<v8::FunctionTemplate>(Read)->GetFunction(Nan::GetCurrentContext()).FromMaybe(v8::Local<v8::Function>())
|
|
318
|
-
);
|
|
319
|
-
exports->Set(
|
|
320
|
-
Nan::GetCurrentContext(),
|
|
321
|
-
Nan::New("readByte").ToLocalChecked(),
|
|
322
|
-
Nan::New<v8::FunctionTemplate>(ReadByte)->GetFunction(Nan::GetCurrentContext()).FromMaybe(v8::Local<v8::Function>())
|
|
323
|
-
);
|
|
324
|
-
exports->Set(
|
|
325
|
-
Nan::GetCurrentContext(),
|
|
326
|
-
Nan::New("readBlock").ToLocalChecked(),
|
|
327
|
-
Nan::New<v8::FunctionTemplate>(ReadBlock)->GetFunction(Nan::GetCurrentContext()).FromMaybe(v8::Local<v8::Function>())
|
|
328
|
-
);
|
|
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());
|
|
329
304
|
|
|
330
305
|
}
|
|
331
306
|
|
|
@@ -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
|