axp192-battery-voltage.toit 683 B

123456789101112131415161718192021
  1. import gpio
  2. import i2c
  3. // https://github.com/toitware/toit-axp192/blob/v1.0.1/src/axp192.toit
  4. import axp192
  5. main:
  6. led_pin := gpio.Pin 10 --output
  7. led_pin.set 0
  8. sleep --ms=100
  9. i2c_bus := i2c.Bus
  10. --sda=gpio.Pin 21
  11. --scl=gpio.Pin 22
  12. axp192_device := i2c_bus.device 0x34
  13. axp192.set_bits axp192_device axp192.ADC_ENABLE_SETTING_REGISTER_1 axp192.ADC_ENABLE_BATTERY_VOLTAGE
  14. // https://github.com/m5stack/M5StickC/blob/b4c4b15d4608472948af069afe8081e218296b06/src/AXP192.cpp#L354
  15. battery_voltage := (
  16. ((axp192_device.registers.read_u8 0x78) << 4) \
  17. + (axp192_device.registers.read_u8 0x79) \
  18. ) * 1.1 / 1000
  19. print "battery $(%.03f battery_voltage)V"