Simple test
Ensure your device works with this simple test, it show the battery voltage if a battery is connected to AXP192
examples/axp192_simpletest.py
1# SPDX-FileCopyrightText: Copyright (c) 2023 Dario Cammi
2#
3# SPDX-License-Identifier: MIT
4
5import time
6import board
7from axp192 import AXP192
8
9i2c = board.I2C()
10pmic = AXP192(i2c)
11
12while True:
13 if pmic.is_battery_connected:
14 battery_voltage = pmic.battery_voltage
15 print(f"Battery voltage {battery_voltage}V")
16 else:
17 print("No battery connected")
18
19 time.sleep(1)
Power key pressed test
Simple test to show how to read the power key button status
examples/axp192_power_key_press.py
1# SPDX-FileCopyrightText: Copyright (c) 2023 Dario Cammi
2#
3# SPDX-License-Identifier: MIT
4
5import time
6import board
7from axp192 import AXP192
8
9i2c = board.I2C()
10pmic = AXP192(i2c)
11
12while True:
13 short_press, long_press = pmic.power_key_was_pressed
14 if short_press:
15 print("Power key was short pressed")
16 elif long_press:
17 print("Power key was long pressed")
18 else:
19 print("Power key isn't press")
20
21 time.sleep(1)
Extending AXP192 library
A more complex example that show how to extend the AXP192 library. This example extend
the AXP192 library and create a PMIC for the M5Stack Core2
examples/axp192_m5stack_core2_pmic.py
1# SPDX-FileCopyrightText: Copyright (c) 2023 Dario Cammi
2#
3# SPDX-License-Identifier: MIT
4
5import time
6import board
7from axp192 import AXP192
8
9__LCD_BRIGHTNESS_MIN_V = 2.45
10__LCD_BRIGHTNESS_MAX_V = 3.3
11
12
13class PMIC(AXP192):
14 def __init__(self):
15 super().__init__(board.I2C())
16
17 @property
18 def speaker_enabled(self) -> bool:
19 """Enable/disable onboard speaker"""
20 return self._is_gpio_floating(2)
21
22 @speaker_enabled.setter
23 def speaker_enabled(self, enabled: bool) -> None:
24 if enabled is True:
25 self._set_gpio_floating(2)
26 else:
27 self._set_gpio_output_low(2)
28
29 @property
30 def green_led_brightness(self) -> int:
31 """
32 Green led brightness
33
34 Brightness range goes from 0 (led off) to 255 (full brightness)
35 """
36 return 255 - self._get_gpio_pwm_out(1)
37
38 @green_led_brightness.setter
39 def green_led_brightness(self, brightness: int) -> None:
40 brightness = min(255, max(0, brightness))
41 self._set_gpio_pwm_out(1, 255 - brightness)
42
43 @property
44 def vibration_motor_strength(self) -> int:
45 """
46 Vibration motor strength
47
48 Streght range goes from 0 (motor off) to 15 (max intensity)
49 """
50 volt = self._ldo3_setpoint
51 if volt == 0:
52 return 0
53
54 return round((self._ldo3_setpoint - 1.8) / 0.1)
55
56 @vibration_motor_strength.setter
57 def vibration_motor_strength(self, strength: int) -> None:
58 if isinstance(strength, int):
59 if strength <= 0:
60 self._ldo3_setpoint = 0
61 else:
62 strength = min(15, strength)
63 self._ldo3_setpoint = 1.8 + strength * 0.1
64 else:
65 raise ValueError("strengh must be an integer value in range 0-15")
66
67 @property
68 def lcd_brightness(self) -> float:
69 """LCD backlight brightness
70
71 Brightness range goes from 0.0 (no backlight) to 1.0 (max brightness)
72 """
73 volt = self._dcdc3_setpoint
74 if volt == 0:
75 return 0
76
77 dV = __LCD_BRIGHTNESS_MAX_V - __LCD_BRIGHTNESS_MIN_V
78 brightness = (volt - __LCD_BRIGHTNESS_MIN_V) / dV
79 return max(0.0, brightness)
80
81 @lcd_brightness.setter
82 def lcd_brightness(self, brightness: float) -> None:
83 if brightness < 0.1:
84 self._dcdc3_setpoint = 0
85 else:
86 brightness = min(1.0, brightness)
87 dV = __LCD_BRIGHTNESS_MAX_V - __LCD_BRIGHTNESS_MIN_V
88 self._dcdc3_setpoint = brightness * dV + __LCD_BRIGHTNESS_MIN_V
89
90 @property
91 def power_supply_from_m_bus_enabled(self) -> bool:
92 """Enable/disable if Core2 power supply should comes from an M-BUS stackable module"""
93 return self._is_gpio_floating(0)
94
95 @power_supply_from_m_bus_enabled.setter
96 def power_supply_from_m_bus_enabled(self, enabled: bool) -> None:
97 if not isinstance(enabled, bool):
98 raise ValueError(
99 "power_supply_from_m_bus_enabled must be True/False boolean value"
100 )
101
102 # If the requested power source is already active do not take any action
103 if enabled == self.power_supply_from_m_bus_enabled:
104 return
105
106 if enabled:
107 # Swith to M_BUS
108
109 # Enable IPSOUT regardless of N_VBUSEN
110 self._set_bit_in_register(0x30, 0b10000000)
111 # Disable the 5V power boost
112 self._exten = False
113 # Set GPIO0 to float and the pull down resistor set N_VBUSEN low
114 # When N_VBUSEN is low IPSOUT select VBUS as source (BUS_5V)
115 self._set_gpio_floating(0)
116 else:
117 # Swtch to USB or battery power supply
118
119 # Set GPIO0 to LDO_OUTPUT to set N_VBUSEN high
120 # When N_VBUSEN is high IPSOUT do not select VBUS as source (BUS_5V)
121 self._set_gpio_ldo_voltage_out(0, 3.3)
122 # Enable the 5V power boost
123 self._exten = True
124 # Enable IPSOUT only when N_VBUSEN is set
125 self._clear_bit_in_register(0x30, 0b10000000)
126
127 @property
128 def bus_5v_power_boost_enabled(self) -> bool:
129 """Enable/disable BUS_5V power boost"""
130 return self._exten
131
132 @bus_5v_power_boost_enabled.setter
133 def bus_5v_power_boost_enabled(self, enabled: bool) -> None:
134 self._exten = enabled
135
136
137pmic = PMIC()
138
139while True:
140 for i in range(255):
141 pmic.green_led_brightness = i
142 time.sleep(0.001)
143
144 for i in range(255, 0, -1):
145 pmic.green_led_brightness = i
146 time.sleep(0.001)