-
-
Save DoNck/2d3f77238c46b2bbd77bb3473ec27a07 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(){ | |
menuidx = 1; | |
menulock = 0; | |
menuDrawMenu(F("ACCU METER"),F(""),F("ESC"),F(""),sys_menucolor,sys_windowcolor,sys_menutextcolor); | |
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); | |
} | |
} | |
menuUpdate(menuidx, menulock); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment