up voltage

This commit is contained in:
Tomas Krejci 2024-05-29 23:51:26 +02:00
parent 820255925c
commit 8163706f08

23
main.py
View File

@ -51,10 +51,31 @@ async def battery_monitor(aadc):
# logger.debug("Out of range:", value)
voltage = aadc.read_u16() * (3.3 / 65535)
logger.debug(f"Battery Voltage: {voltage}")
logger.debug(
f"Battery Voltage: {voltage}V, Capacity: {int(voltage_to_capacity(voltage / 2))}%"
)
await asyncio.sleep_ms(5000)
# simple voltage to capacity, model for alkaline batteries
def voltage_to_capacity(voltage):
if voltage >= 1.6:
return 100
elif 1.5 <= voltage < 1.6:
return 90 + (voltage - 1.5) * 100
elif 1.4 <= voltage < 1.5:
return 70 + (voltage - 1.4) * 200
elif 1.3 <= voltage < 1.4:
return 40 + (voltage - 1.3) * 300
elif 1.2 <= voltage < 1.3:
return 10 + (voltage - 1.2) * 300
elif 1.0 <= voltage < 1.2:
return (voltage - 1.0) * 50
else:
return 0
#### BLE UART ###
import ble_uart
import bluetooth