forked from meshtastic/firmware
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
don't show battery status on boards that can't sense that meshtastic#336
- Loading branch information
1 parent
83ae3c7
commit 780b7e3
Showing
3 changed files
with
83 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,91 @@ | ||
#pragma once | ||
#include <Arduino.h> | ||
#include "Status.h" | ||
#include "configuration.h" | ||
#include <Arduino.h> | ||
|
||
namespace meshtastic { | ||
|
||
/// Describes the state of the GPS system. | ||
class PowerStatus : public Status | ||
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. | ||
class PowerStatus : public Status | ||
{ | ||
|
||
private: | ||
CallbackObserver<PowerStatus, const PowerStatus *> statusObserver = | ||
CallbackObserver<PowerStatus, const PowerStatus *>(this, &PowerStatus::updateStatus); | ||
|
||
/// Whether we have a battery connected | ||
OptionalBool hasBattery = OptUnknown; | ||
/// Battery voltage in mV, valid if haveBattery is true | ||
int batteryVoltageMv; | ||
/// Battery charge percentage, either read directly or estimated | ||
int8_t batteryChargePercent; | ||
/// Whether USB is connected | ||
OptionalBool hasUSB = OptUnknown; | ||
/// Whether we are charging the battery | ||
OptionalBool isCharging = OptUnknown; | ||
|
||
public: | ||
PowerStatus() { statusType = STATUS_TYPE_POWER; } | ||
PowerStatus(OptionalBool hasBattery, OptionalBool hasUSB, OptionalBool isCharging, int batteryVoltageMv = -1, | ||
int8_t batteryChargePercent = -1) | ||
: Status() | ||
{ | ||
this->hasBattery = hasBattery; | ||
this->hasUSB = hasUSB; | ||
this->isCharging = isCharging; | ||
this->batteryVoltageMv = batteryVoltageMv; | ||
this->batteryChargePercent = batteryChargePercent; | ||
} | ||
PowerStatus(const PowerStatus &); | ||
PowerStatus &operator=(const PowerStatus &); | ||
|
||
private: | ||
CallbackObserver<PowerStatus, const PowerStatus *> statusObserver = CallbackObserver<PowerStatus, const PowerStatus *>(this, &PowerStatus::updateStatus); | ||
|
||
/// Whether we have a battery connected | ||
bool hasBattery; | ||
/// Battery voltage in mV, valid if haveBattery is true | ||
int batteryVoltageMv; | ||
/// Battery charge percentage, either read directly or estimated | ||
uint8_t batteryChargePercent; | ||
/// Whether USB is connected | ||
bool hasUSB; | ||
/// Whether we are charging the battery | ||
bool isCharging; | ||
|
||
public: | ||
void observe(Observable<const PowerStatus *> *source) { statusObserver.observe(source); } | ||
|
||
PowerStatus() { | ||
statusType = STATUS_TYPE_POWER; | ||
} | ||
PowerStatus( bool hasBattery, bool hasUSB, bool isCharging, int batteryVoltageMv, uint8_t batteryChargePercent ) : Status() | ||
{ | ||
this->hasBattery = hasBattery; | ||
this->hasUSB = hasUSB; | ||
this->isCharging = isCharging; | ||
this->batteryVoltageMv = batteryVoltageMv; | ||
this->batteryChargePercent = batteryChargePercent; | ||
} | ||
PowerStatus(const PowerStatus &); | ||
PowerStatus &operator=(const PowerStatus &); | ||
|
||
void observe(Observable<const PowerStatus *> *source) | ||
{ | ||
statusObserver.observe(source); | ||
} | ||
bool getHasBattery() const { return hasBattery == OptTrue; } | ||
|
||
bool getHasBattery() const | ||
{ | ||
return hasBattery; | ||
} | ||
bool getHasUSB() const { return hasUSB == OptTrue; } | ||
|
||
bool getHasUSB() const | ||
{ | ||
return hasUSB; | ||
} | ||
/// Can we even know if this board has USB power or not | ||
bool knowsUSB() const { return hasUSB != OptUnknown; } | ||
|
||
bool getIsCharging() const | ||
{ | ||
return isCharging; | ||
} | ||
bool getIsCharging() const { return isCharging == OptTrue; } | ||
|
||
int getBatteryVoltageMv() const | ||
{ | ||
return batteryVoltageMv; | ||
} | ||
int getBatteryVoltageMv() const { return batteryVoltageMv; } | ||
|
||
uint8_t getBatteryChargePercent() const | ||
{ | ||
return batteryChargePercent; | ||
} | ||
uint8_t getBatteryChargePercent() const { return batteryChargePercent; } | ||
|
||
bool matches(const PowerStatus *newStatus) const | ||
bool matches(const PowerStatus *newStatus) const | ||
{ | ||
return (newStatus->getHasBattery() != hasBattery || newStatus->getHasUSB() != hasUSB || | ||
newStatus->getBatteryVoltageMv() != batteryVoltageMv); | ||
} | ||
int updateStatus(const PowerStatus *newStatus) | ||
{ | ||
// Only update the status if values have actually changed | ||
bool isDirty; | ||
{ | ||
return ( | ||
newStatus->getHasBattery() != hasBattery || | ||
newStatus->getHasUSB() != hasUSB || | ||
newStatus->getBatteryVoltageMv() != batteryVoltageMv | ||
); | ||
isDirty = matches(newStatus); | ||
initialized = true; | ||
hasBattery = newStatus->hasBattery; | ||
batteryVoltageMv = newStatus->getBatteryVoltageMv(); | ||
batteryChargePercent = newStatus->getBatteryChargePercent(); | ||
hasUSB = newStatus->hasUSB; | ||
isCharging = newStatus->isCharging; | ||
} | ||
int updateStatus(const PowerStatus *newStatus) { | ||
// Only update the status if values have actually changed | ||
bool isDirty; | ||
{ | ||
isDirty = matches(newStatus); | ||
initialized = true; | ||
hasBattery = newStatus->getHasBattery(); | ||
batteryVoltageMv = newStatus->getBatteryVoltageMv(); | ||
batteryChargePercent = newStatus->getBatteryChargePercent(); | ||
hasUSB = newStatus->getHasUSB(); | ||
isCharging = newStatus->getIsCharging(); | ||
} | ||
if(isDirty) { | ||
DEBUG_MSG("Battery %dmV %d%%\n", batteryVoltageMv, batteryChargePercent); | ||
onNewStatus.notifyObservers(this); | ||
} | ||
return 0; | ||
if (isDirty) { | ||
DEBUG_MSG("Battery %dmV %d%%\n", batteryVoltageMv, batteryChargePercent); | ||
onNewStatus.notifyObservers(this); | ||
} | ||
return 0; | ||
} | ||
}; | ||
|
||
}; | ||
|
||
} | ||
} // namespace meshtastic | ||
|
||
extern meshtastic::PowerStatus *powerStatus; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters