-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
68 lines (58 loc) · 2.19 KB
/
index.js
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
var fs = require('fs'),
_spi = require("./build/Release/spi_binding");
exports.mode = {
CPHA: 0x01,
CPOL: 0x02
};
exports.order = {
MSB_FIRST: 0,
LSB_FIRST: 1
};
exports.initialize = function (dev) {
var spi = {},
_fd = fs.openSync(dev, 'r+'),
_speed = 4e6,
_mode = 0,
_order = exports.order.MSB_FIRST;
spi.clockSpeed = function (speed) {
if (arguments.length < 1) return _speed;
else if (typeof speed === 'number') _speed = speed;
else throw TypeError("Clock speed must be a number.");
};
spi.dataMode = function (mode) {
if (arguments.length < 1) return _mode;
else if (typeof mode === 'number') _mode = mode;
else throw TypeError("Data mode should be CPHA or CPOL.");
};
spi.bitOrder = function (order) {
if (arguments.length < 1) return _order;
else if (typeof order === 'number') _order = order;
else throw TypeError("Bit order should be MSB_FIRST or LSB_FIRST.");
};
function _transfer(w,r,cb) {
_spi.Transfer(_fd, _speed, _mode, _order, w, r, cb);
}
spi.write = function (writebuf, cb) {
if (!Buffer.isBuffer(writebuf)) throw TypeError("Write data is not a buffer");
if (typeof cb !== 'function') throw TypeError("Callback not provided");
_transfer(writebuf, 0, cb);
};
spi.read = function (readcount, cb) {
if (typeof readcount !== 'number') throw TypeError("Read count is not a number");
if (typeof cb !== 'function') throw TypeError("Callback not provided");
_transfer(null, readcount, cb);
};
spi.transfer = function (writebuf, readcount, cb) {
if (!Buffer.isBuffer(writebuf)) throw TypeError("Write data is not a buffer");
if (typeof readcount === 'function') {
cb = readcount;
readcount = writebuf.length;
} else if (typeof readcount !== 'number') throw TypeError("Read count is not a number");
if (typeof cb !== 'function') throw TypeError("Callback not provided");
_transfer(writebuf, readcount, cb);
};
spi.close = function (cb) {
fs.close(_fd, cb);
};
return spi;
};