https://toitlang.org/ logo
Battery level
# help
d
Hi there, @floitsch @kasperl thanks in advance for any help Im building an app on Toit, running on an ESP32 is a TTGO TBeam v1.1 PCB, that holds a OLED screen.. i would like to add somewhere on screen an indicator that shows the battery level of the device, just like on a cell phone... may be a battery icon with some progress depeding on the battery charge and a percentage number.. is there an easy way on Toit to read the current battery charge out of the PCB?? That is my question, what i would need to know, also if you have some sample code will be sure helpful Best, Diego
Just found a way... seems to work, thanks import i2c import gpio import ..src.ttgo_tbeam AXP192_I2C_ADDR := 0x34 main: bus := i2c.Bus --scl=(gpio.Pin TTGO_TBEAM_SCL) --sda=(gpio.Pin TTGO_TBEAM_SDA) --frequency=TTGO_TBEAM_I2C_FREQUENCY device := bus.device TTGO_TBEAM_DEVICE_AXP192 while true: voltage_raw := read_register device 0x78 2 voltage := ((voltage_raw >> 4) * 1.1) / 1000 print "Battery voltage: $(voltage) V" battery_percentage := ((voltage - 3.0) / (4.2 - 3.0)) * 100 print "Battery charge: $(battery_percentage) %" sleep --ms=5000 // Espera antes de la próxima medición read_register device reg length -> int: buf := device.registers.read_bytes reg length return (buf[0] << 8) | buf[1]