mirror of
https://github.com/ocogeclub/ocoge.git
synced 2024-11-22 07:39:49 +00:00
59 lines
1.6 KiB
JavaScript
59 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
const err_msg = 'AMG8833 is already opened. Please close old connection to use new one.';
|
|
// const pig = require(`${apptool.gpio_lib}`);
|
|
|
|
// let pi = -1;
|
|
let rg;
|
|
let i2c_hand = -1;
|
|
exports.init = async (_rg, i2c_bus, i2c_addr, wael = null) => {
|
|
// if (wael !== null) {
|
|
// wael('beforeunload', async () => {
|
|
// await exports.stop();
|
|
// });
|
|
// }
|
|
// if (pi >= 0) { throw new Error(err_msg); return; }
|
|
// pi = await pig._rgpiod_start('', '');
|
|
// console.log('pi=' + pi);
|
|
rg = _rg;
|
|
if (i2c_hand >= 0) { throw new Error(err_msg); return; }
|
|
i2c_hand = await rg.i2c_open(i2c_bus, i2c_addr, 0);
|
|
// i2c_hand = await pig._i2c_open(pi, i2c_bus, i2c_addr, 0);
|
|
console.log('i2c_hand=' + i2c_hand);
|
|
await rg.i2c_write_byte_data(i2c_hand, 0x00, 0x00); //Normal mode
|
|
await rg.i2c_write_byte_data(i2c_hand, 0x02, 0x00); //10FPS
|
|
}
|
|
|
|
exports.read_thermistor = async () => {
|
|
let temp = await rg.i2c_read_word_data(i2c_hand, 0x0e);
|
|
return temp * 0.0625;
|
|
}
|
|
|
|
exports.read_temp_array = async () => {
|
|
let linedata = [];
|
|
for (let i = 0; i < 8; i++) {
|
|
let data = await rg.i2c_read_i2c_block_data(i2c_hand, 0x80 + 0x10 * i, 16);
|
|
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;
|
|
}
|
|
|
|
exports.stop = async () => {
|
|
if (i2c_hand >= 0) {
|
|
await rg.i2c_close(i2c_hand);
|
|
i2c_hand = -1;
|
|
}
|
|
// if (pi >= 0) {
|
|
// await pig._rgpiod_stop(pi);
|
|
// pi = -1;
|
|
// }
|
|
}
|
|
|
|
/*
|
|
* This code was ported from https://www.denshi.club/pc/raspi/5raspberry-pi-zeroiot381i2c-amg8833.html
|
|
*/
|