Browse Source

print battery voltage reported by axp192 every 10 seconds

Fabian Peter Hammerle 2 years ago
commit
9fe96e2716
5 changed files with 38 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 21 0
      axp192-battery-voltage.toit
  3. 5 0
      job.yaml
  4. 7 0
      package.lock
  5. 4 0
      package.yaml

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+/.packages/

+ 21 - 0
axp192-battery-voltage.toit

@@ -0,0 +1,21 @@
+import gpio
+import i2c
+
+// https://github.com/toitware/toit-axp192/blob/v1.0.1/src/axp192.toit
+import axp192
+
+main:
+  led_pin := gpio.Pin 10 --output
+  led_pin.set 0
+  sleep --ms=100
+  i2c_bus := i2c.Bus
+    --sda=gpio.Pin 21
+    --scl=gpio.Pin 22
+  axp192_device := i2c_bus.device 0x34
+  axp192.set_bits axp192_device axp192.ADC_ENABLE_SETTING_REGISTER_1 axp192.ADC_ENABLE_BATTERY_VOLTAGE
+  // https://github.com/m5stack/M5StickC/blob/b4c4b15d4608472948af069afe8081e218296b06/src/AXP192.cpp#L354
+  battery_voltage := (
+    ((axp192_device.registers.read_u8 0x78) << 4) \
+     + (axp192_device.registers.read_u8 0x79) \
+    ) * 1.1 / 1000
+  print "battery $(%.03f battery_voltage)V"

+ 5 - 0
job.yaml

@@ -0,0 +1,5 @@
+name: axp192 battery voltage
+entrypoint: axp192-battery-voltage.toit
+triggers:
+  on_install: true
+  on_interval: 10s

+ 7 - 0
package.lock

@@ -0,0 +1,7 @@
+prefixes:
+  axp192: toit-axp192
+packages:
+  toit-axp192:
+    url: github.com/toitware/toit-axp192
+    version: 1.0.1
+    hash: c43a495d9068a7eaf9a7664a2a451eadb2e91101

+ 4 - 0
package.yaml

@@ -0,0 +1,4 @@
+dependencies:
+  axp192:
+    url: github.com/toitware/toit-axp192
+    version: ^1.0.1