mirror of
https://github.com/meshtastic/firmware.git
synced 2025-04-26 18:09:04 +00:00
Merge pull request #338 from geeksville/master
fix heltec battery display
This commit is contained in:
commit
0caf534b65
@ -16,6 +16,8 @@ bool pmu_irq = false;
|
|||||||
|
|
||||||
Power *power;
|
Power *power;
|
||||||
|
|
||||||
|
using namespace meshtastic;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If this board has a battery level sensor, set this to a valid implementation
|
* If this board has a battery level sensor, set this to a valid implementation
|
||||||
*/
|
*/
|
||||||
@ -97,7 +99,7 @@ void Power::readPowerStatus()
|
|||||||
if (batteryLevel) {
|
if (batteryLevel) {
|
||||||
bool hasBattery = batteryLevel->isBatteryConnect();
|
bool hasBattery = batteryLevel->isBatteryConnect();
|
||||||
int batteryVoltageMv = 0;
|
int batteryVoltageMv = 0;
|
||||||
uint8_t batteryChargePercent = 0;
|
int8_t batteryChargePercent = 0;
|
||||||
if (hasBattery) {
|
if (hasBattery) {
|
||||||
batteryVoltageMv = batteryLevel->getBattVoltage();
|
batteryVoltageMv = batteryLevel->getBattVoltage();
|
||||||
// If the AXP192 returns a valid battery percentage, use it
|
// If the AXP192 returns a valid battery percentage, use it
|
||||||
@ -114,13 +116,18 @@ void Power::readPowerStatus()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Notify any status instances that are observing us
|
// Notify any status instances that are observing us
|
||||||
const meshtastic::PowerStatus powerStatus = meshtastic::PowerStatus(
|
const PowerStatus powerStatus =
|
||||||
hasBattery, batteryLevel->isVBUSPlug(), batteryLevel->isChargeing(), batteryVoltageMv, batteryChargePercent);
|
PowerStatus(hasBattery ? OptTrue : OptFalse, batteryLevel->isVBUSPlug() ? OptTrue : OptFalse,
|
||||||
|
batteryLevel->isChargeing() ? OptTrue : OptFalse, batteryVoltageMv, batteryChargePercent);
|
||||||
newStatus.notifyObservers(&powerStatus);
|
newStatus.notifyObservers(&powerStatus);
|
||||||
|
|
||||||
// If we have a battery at all and it is less than 10% full, force deep sleep
|
// If we have a battery at all and it is less than 10% full, force deep sleep
|
||||||
if (powerStatus.getHasBattery() && !powerStatus.getHasUSB() && batteryLevel->getBattVoltage() < MIN_BAT_MILLIVOLTS)
|
if (powerStatus.getHasBattery() && !powerStatus.getHasUSB() && batteryLevel->getBattVoltage() < MIN_BAT_MILLIVOLTS)
|
||||||
powerFSM.trigger(EVENT_LOW_BATTERY);
|
powerFSM.trigger(EVENT_LOW_BATTERY);
|
||||||
|
} else {
|
||||||
|
// No power sensing on this board - tell everyone else we have no idea what is happening
|
||||||
|
const PowerStatus powerStatus = PowerStatus(OptUnknown, OptUnknown, OptUnknown, -1, -1);
|
||||||
|
newStatus.notifyObservers(&powerStatus);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,34 +1,40 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <Arduino.h>
|
|
||||||
#include "Status.h"
|
#include "Status.h"
|
||||||
#include "configuration.h"
|
#include "configuration.h"
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
namespace meshtastic {
|
namespace meshtastic
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A boolean where we have a third state of Unknown
|
||||||
|
*/
|
||||||
|
enum OptionalBool { OptFalse = 0, OptTrue = 1, OptUnknown = 2 };
|
||||||
|
|
||||||
/// Describes the state of the GPS system.
|
/// Describes the state of the GPS system.
|
||||||
class PowerStatus : public Status
|
class PowerStatus : public Status
|
||||||
{
|
{
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CallbackObserver<PowerStatus, const PowerStatus *> statusObserver = CallbackObserver<PowerStatus, const PowerStatus *>(this, &PowerStatus::updateStatus);
|
CallbackObserver<PowerStatus, const PowerStatus *> statusObserver =
|
||||||
|
CallbackObserver<PowerStatus, const PowerStatus *>(this, &PowerStatus::updateStatus);
|
||||||
|
|
||||||
/// Whether we have a battery connected
|
/// Whether we have a battery connected
|
||||||
bool hasBattery;
|
OptionalBool hasBattery = OptUnknown;
|
||||||
/// Battery voltage in mV, valid if haveBattery is true
|
/// Battery voltage in mV, valid if haveBattery is true
|
||||||
int batteryVoltageMv;
|
int batteryVoltageMv = 0;
|
||||||
/// Battery charge percentage, either read directly or estimated
|
/// Battery charge percentage, either read directly or estimated
|
||||||
uint8_t batteryChargePercent;
|
int8_t batteryChargePercent = 0;
|
||||||
/// Whether USB is connected
|
/// Whether USB is connected
|
||||||
bool hasUSB;
|
OptionalBool hasUSB = OptUnknown;
|
||||||
/// Whether we are charging the battery
|
/// Whether we are charging the battery
|
||||||
bool isCharging;
|
OptionalBool isCharging = OptUnknown;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
PowerStatus() { statusType = STATUS_TYPE_POWER; }
|
||||||
PowerStatus() {
|
PowerStatus(OptionalBool hasBattery, OptionalBool hasUSB, OptionalBool isCharging, int batteryVoltageMv = -1,
|
||||||
statusType = STATUS_TYPE_POWER;
|
int8_t batteryChargePercent = 0)
|
||||||
}
|
: Status()
|
||||||
PowerStatus( bool hasBattery, bool hasUSB, bool isCharging, int batteryVoltageMv, uint8_t batteryChargePercent ) : Status()
|
|
||||||
{
|
{
|
||||||
this->hasBattery = hasBattery;
|
this->hasBattery = hasBattery;
|
||||||
this->hasUSB = hasUSB;
|
this->hasUSB = hasUSB;
|
||||||
@ -39,55 +45,41 @@ namespace meshtastic {
|
|||||||
PowerStatus(const PowerStatus &);
|
PowerStatus(const PowerStatus &);
|
||||||
PowerStatus &operator=(const PowerStatus &);
|
PowerStatus &operator=(const PowerStatus &);
|
||||||
|
|
||||||
void observe(Observable<const PowerStatus *> *source)
|
void observe(Observable<const PowerStatus *> *source) { statusObserver.observe(source); }
|
||||||
{
|
|
||||||
statusObserver.observe(source);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool getHasBattery() const
|
bool getHasBattery() const { return hasBattery == OptTrue; }
|
||||||
{
|
|
||||||
return hasBattery;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool getHasUSB() const
|
bool getHasUSB() const { return hasUSB == OptTrue; }
|
||||||
{
|
|
||||||
return hasUSB;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool getIsCharging() const
|
/// Can we even know if this board has USB power or not
|
||||||
{
|
bool knowsUSB() const { return hasUSB != OptUnknown; }
|
||||||
return isCharging;
|
|
||||||
}
|
|
||||||
|
|
||||||
int getBatteryVoltageMv() const
|
bool getIsCharging() const { return isCharging == OptTrue; }
|
||||||
{
|
|
||||||
return batteryVoltageMv;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t getBatteryChargePercent() const
|
int getBatteryVoltageMv() const { return batteryVoltageMv; }
|
||||||
{
|
|
||||||
return batteryChargePercent;
|
/**
|
||||||
}
|
* Note: 0% battery means 'unknown/this board doesn't have a battery installed'
|
||||||
|
*/
|
||||||
|
uint8_t getBatteryChargePercent() const { return getHasBattery() ? batteryChargePercent : 0; }
|
||||||
|
|
||||||
bool matches(const PowerStatus *newStatus) const
|
bool matches(const PowerStatus *newStatus) const
|
||||||
{
|
{
|
||||||
return (
|
return (newStatus->getHasBattery() != hasBattery || newStatus->getHasUSB() != hasUSB ||
|
||||||
newStatus->getHasBattery() != hasBattery ||
|
newStatus->getBatteryVoltageMv() != batteryVoltageMv);
|
||||||
newStatus->getHasUSB() != hasUSB ||
|
|
||||||
newStatus->getBatteryVoltageMv() != batteryVoltageMv
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
int updateStatus(const PowerStatus *newStatus) {
|
int updateStatus(const PowerStatus *newStatus)
|
||||||
|
{
|
||||||
// Only update the status if values have actually changed
|
// Only update the status if values have actually changed
|
||||||
bool isDirty;
|
bool isDirty;
|
||||||
{
|
{
|
||||||
isDirty = matches(newStatus);
|
isDirty = matches(newStatus);
|
||||||
initialized = true;
|
initialized = true;
|
||||||
hasBattery = newStatus->getHasBattery();
|
hasBattery = newStatus->hasBattery;
|
||||||
batteryVoltageMv = newStatus->getBatteryVoltageMv();
|
batteryVoltageMv = newStatus->getBatteryVoltageMv();
|
||||||
batteryChargePercent = newStatus->getBatteryChargePercent();
|
batteryChargePercent = newStatus->getBatteryChargePercent();
|
||||||
hasUSB = newStatus->getHasUSB();
|
hasUSB = newStatus->hasUSB;
|
||||||
isCharging = newStatus->getIsCharging();
|
isCharging = newStatus->isCharging;
|
||||||
}
|
}
|
||||||
if (isDirty) {
|
if (isDirty) {
|
||||||
DEBUG_MSG("Battery %dmV %d%%\n", batteryVoltageMv, batteryChargePercent);
|
DEBUG_MSG("Battery %dmV %d%%\n", batteryVoltageMv, batteryChargePercent);
|
||||||
@ -95,9 +87,8 @@ namespace meshtastic {
|
|||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
} // namespace meshtastic
|
||||||
|
|
||||||
extern meshtastic::PowerStatus *powerStatus;
|
extern meshtastic::PowerStatus *powerStatus;
|
||||||
|
@ -774,7 +774,7 @@ void DebugInfo::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state, int16
|
|||||||
// Display power status
|
// Display power status
|
||||||
if (powerStatus->getHasBattery())
|
if (powerStatus->getHasBattery())
|
||||||
drawBattery(display, x, y + 2, imgBattery, powerStatus);
|
drawBattery(display, x, y + 2, imgBattery, powerStatus);
|
||||||
else
|
else if (powerStatus->knowsUSB())
|
||||||
display->drawFastImage(x, y + 2, 16, 8, powerStatus->getHasUSB() ? imgUSB : imgPower);
|
display->drawFastImage(x, y + 2, 16, 8, powerStatus->getHasUSB() ? imgUSB : imgPower);
|
||||||
// Display nodes status
|
// Display nodes status
|
||||||
drawNodes(display, x + (SCREEN_WIDTH * 0.25), y + 2, nodeStatus);
|
drawNodes(display, x + (SCREEN_WIDTH * 0.25), y + 2, nodeStatus);
|
||||||
|
Loading…
Reference in New Issue
Block a user