Skip to content

Instantly share code, notes, and snippets.

@reaper7

reaper7/apps.ino

Last active Mar 24, 2018
Embed
What would you like to do?
M5StackSAM MAX17043 1-Cell Fuel Gauge App
// 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