Last active
March 24, 2018 06:00
-
-
Save reaper7/c9f8dba68edae8c3d53075b10f6bbba4 to your computer and use it in GitHub Desktop.
M5StackSAM MAX17043 1-Cell Fuel Gauge App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MAX17043 1-Cell Fuel Gauge | |
// APP for https://github.com/tomsuch/M5StackSAM | |
void appAccuMeter(){ | |
MyMenu.drawAppMenu(F("ACCU METER"),F(""),F("ESC"),F("")); | |
const int MAX17043ADDR=0x36; | |
const int MAX17043CMDADDR=0xFE; | |
const int MAX17043SOCADDR=0x04; | |
const int MAX17043VCELLADDR=0x02; | |
time_t tmp_tmr; | |
double soc, volt; | |
char buffer[20]; | |
// reset MAX17043 | |
Wire.beginTransmission(MAX17043ADDR); | |
Wire.write(MAX17043CMDADDR); | |
Wire.write(0x54); | |
Wire.write(0x00); | |
Wire.endTransmission(); | |
delay(200); | |
while(M5.BtnB.wasPressed()){ | |
M5.update(); | |
} | |
while(!M5.BtnB.wasPressed()){ | |
M5.update(); | |
if(millis()-tmp_tmr > 1000){ | |
tmp_tmr = millis(); | |
// get SoC | |
Wire.beginTransmission(MAX17043ADDR); | |
Wire.write(MAX17043SOCADDR); | |
Wire.endTransmission(false); | |
Wire.requestFrom(MAX17043ADDR, (uint8_t)2); | |
soc = Wire.read() + (double) Wire.read() / 256; | |
// get voltage | |
Wire.beginTransmission(MAX17043ADDR); | |
Wire.write(MAX17043VCELLADDR); | |
Wire.endTransmission(false); | |
Wire.requestFrom(MAX17043ADDR, (uint8_t)2); | |
volt = ( (Wire.read() << 4) + (Wire.read() >> 4) ) * 0.00125; | |
sprintf(buffer, "VCell: %6.4fV ", volt); | |
M5.Lcd.drawString(buffer,70,80,4); | |
sprintf(buffer, "SoC : %5.2f%% ", soc); | |
M5.Lcd.drawString(buffer,70,120,4); | |
} | |
} | |
MyMenu.show(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment