Closed slavino closed 3 years ago
Without testing it, the theory is the same.
The below lines of code worked for me.
`#include "Arduino.h"
/* * IP5306 Power Module /
/ M5 Defaults KeyOff: Enabled BoostOutput: Disabled PowerOnLoad: Enabled Charger: Enabled Boost: Enabled LowBatShutdown: Enabled ShortPressBoostSwitch: Disabled FlashlightClicks: Double Press BoostOffClicks: Long Press BoostAfterVin: Open LongPressTime: 2s ChargeUnderVoltageLoop: 4.55V ChargeCCLoop: Vin VinCurrent: 2250mA VoltagePressure: 28mV ChargingFullStopVoltage: 4.17V LightLoadShutdownTime: 32s EndChargeCurrentDetection: 500mA ChargeCutoffVoltage: 4.2V /
((byte & 0x01 ? 25 : 0) + \ (byte & 0x02 ? 25 : 0) + \ (byte & 0x04 ? 25 : 0) + \ (byte & 0x08 ? 25 : 0))
int ip5306_get_reg(uint8_t reg){ Wire.beginTransmission(0x75); Wire.write(reg); if(Wire.endTransmission(false) == 0 && Wire.requestFrom(0x75, 1)){ return Wire.read(); } return -1; }
int ip5306_set_reg(uint8_t reg, uint8_t value){ Wire.beginTransmission(0x75); Wire.write(reg); Wire.write(value); if(Wire.endTransmission(true) == 0){ return 0; } return -1; }
uint8_t ip5306_get_bits(uint8_t reg, uint8_t index, uint8_t bits){ int value = ip5306_get_reg(reg); if(value < 0){ Serial.printf("ip5306_get_bits fail: 0x%02x\n", reg); return 0; } return (value >> index) & ((1 << bits)-1); }
void ip5306_set_bits(uint8_t reg, uint8_t index, uint8_t bits, uint8_t value){ uint8_t mask = (1 << bits) - 1; int v = ip5306_get_reg(reg); if(v < 0){ Serial.printf("ip5306_get_reg fail: 0x%02x\n", reg); return; } v &= ~(mask << index); v |= ((value & mask) << index); if(ip5306_set_reg(reg, v)){ Serial.printf("ip5306_set_bits fail: 0x%02x\n", reg); } }
void printIP5306Stats(){ bool usb = IP5306_GetPowerSource(); bool full = IP5306_GetBatteryFull(); uint8_t leds = IP5306_GetLevelLeds(); Serial.printf("%u%%\n", IP5306_LEDS2PCT(leds)); Serial.printf("%s\n", usb?"USB":"BATTERY"); //Serial.printf("IP5306: Power Source: %s, Battery State: %s, Battery Available: %u%%\n", usb?"USB":"BATTERY", full?"CHARGED":(usb?"CHARGING":"DISCHARGING"), IP5306_LEDS2PCT(leds)); //IP5306: Power Source: USB, Battery State: CHARGED, Battery Available: 100% }
void printIP5306Settings(){ Serial.println("IP5306 Settings:"); Serial.printf(" KeyOff: %s\n", IP5306_GetKeyOffEnabled()?"Enabled":"Disabled"); Serial.printf(" BoostOutput: %s\n", IP5306_GetBoostOutputEnabled()?"Enabled":"Disabled"); Serial.printf(" PowerOnLoad: %s\n", IP5306_GetPowerOnLoadEnabled()?"Enabled":"Disabled"); Serial.printf(" Charger: %s\n", IP5306_GetChargerEnabled()?"Enabled":"Disabled"); Serial.printf(" Boost: %s\n", IP5306_GetBoostEnabled()?"Enabled":"Disabled"); Serial.printf(" LowBatShutdown: %s\n", IP5306_GetLowBatShutdownEnable()?"Enabled":"Disabled"); Serial.printf(" ShortPressBoostSwitch: %s\n", IP5306_GetShortPressBoostSwitchEnable()?"Enabled":"Disabled"); Serial.printf(" FlashlightClicks: %s\n", IP5306_GetFlashlightClicks()?"Long Press":"Double Press"); Serial.printf(" BoostOffClicks: %s\n", IP5306_GetBoostOffClicks()?"Double Press":"Long Press"); Serial.printf(" BoostAfterVin: %s\n", IP5306_GetBoostAfterVin()?"Open":"Not Open"); Serial.printf(" LongPressTime: %s\n", IP5306_GetLongPressTime()?"3s":"2s"); Serial.printf(" ChargeUnderVoltageLoop: %.2fV\n", 4.45 + (IP5306_GetChargeUnderVoltageLoop() 0.05)); Serial.printf(" ChargeCCLoop: %s\n", IP5306_GetChargeCCLoop()?"Vin":"Bat"); Serial.printf(" VinCurrent: %dmA\n", (IP5306_GetVinCurrent() 100) + 50); Serial.printf(" VoltagePressure: %dmV\n", IP5306_GetVoltagePressure()*14); Serial.printf(" ChargingFullStopVoltage: %u\n", IP5306_GetChargingFullStopVoltage()); Serial.printf(" LightLoadShutdownTime: %u\n", IP5306_GetLightLoadShutdownTime()); Serial.printf(" EndChargeCurrentDetection: %u\n", IP5306_GetEndChargeCurrentDetection()); Serial.printf(" ChargeCutoffVoltage: %u\n", IP5306_GetChargeCutoffVoltage()); Serial.println(); }
void setup(){ Serial.begin(115200); Wire.begin(); printIP5306Settings(); }
void loop(){ printIP5306Stats(); delay(1000); }`
printIP5306Stats() function will provide every data you need from charging state, battery percentage, etc.
https://github.com/m5stack/M5Stack/issues/74