-
Notifications
You must be signed in to change notification settings - Fork 10
/
mcp-spi-adc.js
155 lines (126 loc) · 4.4 KB
/
mcp-spi-adc.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict';
const spi = require('spi-device');
const clone = require('lodash.clone');
const CONFIG_MCP3008 = Object.freeze({
channelCount: 8,
maxRawValue: 1023,
defaultSpeedHz: 1350000, // See MCP3008 datasheet. 75000 * 18 = 1350000.
transferLength: 3,
readChannelCommand: channel =>
Buffer.from([0x01, 0x80 + (channel << 4), 0x00]),
rawValue: buffer => ((buffer[1] & 0x03) << 8) + buffer[2]
});
const CONFIG_MCP3004 = clone(CONFIG_MCP3008);
CONFIG_MCP3004.channelCount = 4;
Object.freeze(CONFIG_MCP3004);
const CONFIG_MCP3002 = Object.freeze({
channelCount: 2,
maxRawValue: 1023,
defaultSpeedHz: 1200000, // See MCP3002 datasheet. 75000 * 16 = 1200000.
transferLength: 2,
readChannelCommand: channel =>
Buffer.from([0x68 + (channel << 4), 0x00]),
rawValue: buffer => ((buffer[0] & 0x03) << 8) + buffer[1]
});
const CONFIG_MCP3208 = Object.freeze({
channelCount: 8,
maxRawValue: 4095,
defaultSpeedHz: 1000000, // See MCP3208 datasheet. 50000 * 20 = 1000000.
transferLength: 3,
readChannelCommand: channel =>
Buffer.from([0x06 + (channel >> 2), (channel & 0x03) << 6, 0x00]),
rawValue: buffer => ((buffer[1] & 0x0f) << 8) + buffer[2]
});
const CONFIG_MCP3204 = clone(CONFIG_MCP3208);
CONFIG_MCP3204.channelCount = 4;
Object.freeze(CONFIG_MCP3204);
const CONFIG_MCP3202 = Object.freeze({
channelCount: 2,
maxRawValue: 4095,
defaultSpeedHz: 900000, // See MCP3202 datasheet. 50000 * 18 = 900000.
transferLength: 3,
readChannelCommand: channel =>
Buffer.from([0x01, 0xa0 + (channel << 6), 0x00]),
rawValue: buffer => ((buffer[1] & 0x0f) << 8) + buffer[2]
});
const CONFIG_MCP3201 = Object.freeze({
channelCount: 1,
maxRawValue: 4095,
defaultSpeedHz: 800000, // See MCP3201 datasheet. 50000 * 16 = 800000.
transferLength: 2,
readChannelCommand: channel =>
Buffer.from([0x00, 0x00]),
rawValue: buffer => ((buffer[0] & 0x1f) << 7) + (buffer[1] >> 1)
});
const CONFIG_MCP3304 = Object.freeze({
channelCount: 8,
maxRawValue: 4095,
defaultSpeedHz: 1050000, // See MCP3304 datasheet. 50000 * 21 = 1050000
transferLength: 3,
readChannelCommand: channel =>
Buffer.from([0x0c + (channel >> 1), (channel & 0x01) << 7, 0x00]),
rawValue: buffer => ((buffer[1] & 0x0f) << 8) + buffer[2]
});
class AdcChannel {
constructor(config, channel, options, cb) {
if (typeof options === 'function') {
cb = options;
options = {};
}
if (!Number.isInteger(channel) ||
channel < 0 ||
channel > config.channelCount - 1) {
throw RangeError(
'\'' + channel + '\'' + ' is not a valid channel number'
);
}
this._config = config;
this._readChannelCommand = config.readChannelCommand(channel);
this._speedHz = options.speedHz || config.defaultSpeedHz;
this._device = spi.open(
options.busNumber || 0,
options.deviceNumber || 0,
cb
);
return this;
}
read(cb) {
const message = [{
byteLength: this._config.transferLength,
sendBuffer: this._readChannelCommand,
receiveBuffer: Buffer.alloc(this._config.transferLength),
speedHz: this._speedHz
}];
this._device.transfer(message, (err, message) => {
const reading = {};
if (err) {
return cb(err);
}
reading.rawValue = this._config.rawValue(message[0].receiveBuffer);
reading.value = reading.rawValue / this._config.maxRawValue;
cb(null, reading);
});
return this;
}
close(cb) {
this._device.close(cb);
return null;
}
}
module.exports.openMcp3002 = (channel, options, cb) =>
new AdcChannel(CONFIG_MCP3002, channel, options, cb);
module.exports.openMcp3004 = (channel, options, cb) =>
new AdcChannel(CONFIG_MCP3004, channel, options, cb);
module.exports.openMcp3008 = (channel, options, cb) =>
new AdcChannel(CONFIG_MCP3008, channel, options, cb);
module.exports.open = module.exports.openMcp3008;
module.exports.openMcp3201 = (channel, options, cb) =>
new AdcChannel(CONFIG_MCP3201, channel, options, cb);
module.exports.openMcp3202 = (channel, options, cb) =>
new AdcChannel(CONFIG_MCP3202, channel, options, cb);
module.exports.openMcp3204 = (channel, options, cb) =>
new AdcChannel(CONFIG_MCP3204, channel, options, cb);
module.exports.openMcp3208 = (channel, options, cb) =>
new AdcChannel(CONFIG_MCP3208, channel, options, cb);
module.exports.openMcp3304 = (channel, options, cb) =>
new AdcChannel(CONFIG_MCP3304, channel, options, cb);