forked from themicp/i2c-mcp4725
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
33 lines (26 loc) · 847 Bytes
/
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
const i2c = require('i2c-bus')
const i2c1 = i2c.openSync(1)
const DEFAULT_DEVICE_ADDRESS = 0x62
const CMD_WRITEDAC = 0x40
const CMD_WRITEDACEEPROM = 0x60
const writeI2cBlockPromise = (addr, cmd, length, buffer) => {
return new Promise((resolve, reject) => {
i2c1.writeI2cBlock(addr, cmd, length, buffer, (err, bufferWritten, buffer) => {
if (err) {
return reject(err);
}
resolve(bufferWritten, buffer);
});
})
}
const setVoltage = (value, persist = false) => {
const command = persist ? CMD_WRITEDACEEPROM : CMD_WRITEDAC;
value = value > 4095 ? 4095 : value;
return writeI2cBlockPromise(
DEFAULT_DEVICE_ADDRESS,
command,
2,
Buffer.from([(value >> 4) & 0xFF, (value << 4) & 0xFF])
);
}
module.exports = {setVoltage};