const struct = require('python-struct');
const i2c = require('i2c-bus');
const address = 0x36;
const readVoltage = rawData=>{
console.log('rawData', rawData);
let packData = struct.pack(">H", rawData);
console.log('packData', packData);
let unpackData = struct.unpack("<H", packData);
console.log('unpackData', unpackData[0]);
let vol = unpackData[0]*1.25/1000/16;
return vol;
};
const readCapacity = rawData=>{
let packData = struct.pack(">H", rawData);
console.log('packData', packData);
let unpackData = struct.unpack("<H", packData);
console.log('unpackData', unpackData[0]);
let cap = unpackData[0]/256;
return cap;
};
i2c.openPromisified(1).
then(i2c1 => i2c1.readWord(address, 0x2).
then(rawData => console.log(readVoltage(rawData))).
then(_ => i2c1.close())
).catch(console.log);
i2c.openPromisified(1).
then(i2c1 => i2c1.readWord(address, 0x4).
then(rawData => console.log(readCapacity(rawData))).
then(_ => i2c1.close())
).catch(console.log);
评论