I plan to give some kind of visual indication of the amount of battery power remaining, but the problem is determining some kind of quantification for that. M5 does not provide a direct API to answer the question "how much battery left?", but they do provide APIs for the AXP192 power controller to get all sorts of interesting information.
I naively thought there would be some kind of common knowledge way of taking LiPo battery voltage and translating that directly into what I want. And, it's possible that there is, but I didn't stumble across it. I did find quite a bit of discussion about it. Here is a link to one discussion of it. This person provided a handy table of voltages and percentages, but I'm kind of skeptical, because
- In the first place, my fully charged battery voltage was a bit lower than their 100% figure,
- and I have no idea what my low-voltage is when the device shuts itself down.
- I'm also pretty sure the discharge rate isn't linear, but I don't know what it is instead.
- As my device ages, the battery capacity will degrade.
- And, finally, there could be variation so that your battery is different from my battery (or two of my batteries might be different from each other).
Here is my tentative plan. I haven't implemented it yet, so it's subject to change.
- I'll use the "Preferences" API to store some battery-related values in flash.
- When the device is fully charges (as detected from the power in and out values), I'll record the battery voltage if it's different by more than a few percent from the last "high" value I stored, whether higher or lower. I don't expect any higher "high" values, except for a little jitter, but I expect lower "high" values as the battery ages.
- Periodically, maybe every 10 minutes or so, I'll store the current battery voltage if it's lower by more than a few percent from the last "low" value I stored.
- I'll run the device on battery power until it shuts off, fully charge it, and I'll repeat that a few times.
- Using those "high" and "low" values, I'll ignore science and assume a linear discharge between them and convert that to a percentage.
Note to self: consult the wisdom of crowds about how LiPo voltages change as they degrade over time.