-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmcp3008.js
138 lines (119 loc) · 3.35 KB
/
mcp3008.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
"use strict";
/**
* For the onoff doc,
* see https://github.com/fivdi/onoff
*/
const State = {
HIGH: 1,
LOW: 0
};
const Direction = {
IN: 'in',
OUT: 'out'
};
const Event = {
NONE: 'none',
RISING: 'rising',
FAILING: 'failing',
BOTH: 'both'
};
let utils = require('./utils/utils.js'); // This is a class. Explicit location (path), not in 'node_modules'.
let Gpio = require('onoff').Gpio; // Constructor function for Gpio objects.
Gpio.prototype.high = function() {
this.writeSync(State.HIGH);
};
Gpio.prototype.low = function() {
this.writeSync(State.LOW);
};
Gpio.prototype.isHigh = function() {
return this.readSync() === State.HIGH;
};
Gpio.prototype.tick = function() {
this.writeSync(State.HIGH);
this.writeSync(State.LOW);
};
let defaultClock = 18; // GPIO_18, Wiring/PI4J 1
let defaultMiso = 23; // GPIO_23, Wiring/PI4J 4
let defaultMosi = 24; // GPIO_24, Wiring/PI4J 5
let defaultCs = 25; // GPIO_25, Wiring/PI4J 6
let MCP3008 = function(clock, miso, mosi, cs) {
if (clock === undefined) { clock = defaultClock; }
if (miso === undefined) { miso = defaultMiso; }
if (mosi === undefined) { mosi = defaultMosi; }
if (cs === undefined) { cs = defaultCs; }
console.log("Reading MCP3008: CLK:", clock, "MISO:", miso, "MOSI:", mosi, "CS:", cs);
let debug = false;
this.setDebug = function(val) {
debug = val;
};
this.channels = {
CHANNEL_0: 0,
CHANNEL_1: 1,
CHANNEL_2: 2,
CHANNEL_3: 3,
CHANNEL_4: 4,
CHANNEL_5: 5,
CHANNEL_6: 6,
CHANNEL_7: 7 };
let SPI_CLK = new Gpio(clock, Direction.OUT),
SPI_MISO = new Gpio(miso, Direction.IN),
SPI_MOSI = new Gpio(mosi, Direction.OUT),
SPI_CS = new Gpio(cs, Direction.OUT);
SPI_CLK.low();
SPI_MOSI.low();
SPI_CS.low();
this.readAdc = function(ch) {
if (ch === undefined) {
ch = this.channels.CHANNEL_0;
}
SPI_CS.high();
SPI_CLK.low();
SPI_CS.low();
let adcCommand = ch;
if (debug) {
console.log(">> 1 - ADCCOMMAND", utils.hexFmt(adcCommand, 4), utils.binFmt(adcCommand, 16));
}
adcCommand |= 0x18; // 0x18: 00011000
if (debug) {
console.log(">> 2 - ADCCOMMAND", utils.hexFmt(adcCommand, 4), utils.binFmt(adcCommand, 16));
}
adcCommand <<= 3;
if (debug) {
console.log(">> 3 - ADCCOMMAND", utils.hexFmt(adcCommand, 4), utils.binFmt(adcCommand, 16));
}
// Send 5 bits: 8 - 3. 8 input channels on the MCP3008.
for (let i=0; i<5; i++) {
if (debug) {
console.log(">> 4 - (i=" + i + ") ADCCOMMAND", utils.hexFmt(adcCommand, 4), utils.binFmt(adcCommand, 16));
}
if ((adcCommand & 0x80) != 0x0) { // 0x80 = 0&10000000
SPI_MOSI.high();
} else {
SPI_MOSI.low();
}
adcCommand <<= 1;
SPI_CLK.tick();
}
let adcOut = 0;
for (let i=0; i<12; i++) { // Read in one empty bit, one null bit and 10 ADC bits
SPI_CLK.tick();
adcOut <<= 1;
if (SPI_MISO.isHigh()) {
adcOut |= 0x1;
}
if (debug) {
console.log(">> ADCOUT", utils.hexFmt(adcOut, 4), utils.binFmt(adcOut, 16));
}
}
SPI_CS.high();
adcOut >>= 1; // Drop first bit
return adcOut;
};
this.shutdown = function() {
SPI_CLK.unexport();
SPI_MISO.unexport();
SPI_CS.unexport();
SPI_MOSI.unexport();
};
};
exports.MCP3008 = MCP3008; // Made public.