2021-10-22 14:33:59 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const pig = require('@ocogeclub/pigpio');
|
|
|
|
|
|
|
|
let pi = -1;
|
|
|
|
let i2c_hand = -1;
|
2021-11-25 14:23:54 +00:00
|
|
|
exports.init = async (i2c_bus, i2c_addr) => {
|
|
|
|
if (pi < 0) pi = await pig._pigpio_start('', '');
|
|
|
|
if (i2c_hand < 0) await pig._i2c_close(pi, i2c_hand);
|
|
|
|
i2c_hand = await pig._i2c_open(pi, i2c_bus, i2c_addr);
|
|
|
|
await pig._i2c_write_byte_data(pi, i2c_hand, 0x00, 0x00); //Normal mode
|
|
|
|
await pig._i2c_write_byte_data(pi, i2c_hand, 0x02, 0x00); //10FPS
|
2021-10-22 14:33:59 +00:00
|
|
|
}
|
|
|
|
|
2021-11-25 14:23:54 +00:00
|
|
|
exports.read_thermistor = async () => {
|
|
|
|
let temp = await pig._i2c_read_word_data(pi, i2c_hand, 0x0e);
|
2021-10-31 04:27:59 +00:00
|
|
|
return temp * 0.0625;
|
|
|
|
}
|
|
|
|
|
2021-11-25 14:23:54 +00:00
|
|
|
exports.read_temp_array = async () => {
|
2021-10-22 14:33:59 +00:00
|
|
|
let linedata = [];
|
|
|
|
for (let i = 0; i < 8; i++) {
|
2021-11-25 14:23:54 +00:00
|
|
|
let data = await pig._i2c_read_i2c_block_data(pi, i2c_hand, 0x80 + 0x10 * i, 16);
|
2021-10-22 14:33:59 +00:00
|
|
|
let oneline = [];
|
|
|
|
for (let j = 0; j < 8; j++) {
|
|
|
|
oneline.push(((data[2 * j + 1] & 0x07) * 256 + data[2 * j]) * 0.25);
|
|
|
|
}
|
|
|
|
linedata.push(oneline);
|
|
|
|
}
|
|
|
|
return linedata;
|
|
|
|
}
|
|
|
|
|
2021-11-25 14:23:54 +00:00
|
|
|
exports.stop = async () => {
|
2021-10-22 14:33:59 +00:00
|
|
|
if (i2c_hand >= 0) {
|
2021-11-25 14:23:54 +00:00
|
|
|
await pig._i2c_close(pi, i2c_hand);
|
2021-10-22 14:33:59 +00:00
|
|
|
i2c_hand = -1;
|
|
|
|
}
|
|
|
|
if (pi >= 0) {
|
2021-11-25 14:23:54 +00:00
|
|
|
await pig._pigpio_stop(pi);
|
2021-10-22 14:33:59 +00:00
|
|
|
pi = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This code was ported from https://www.denshi.club/pc/raspi/5raspberry-pi-zeroiot381i2c-amg8833.html
|
|
|
|
*/
|