Cleanup and improvements (#194)
- Lots of changes for migrating C code to C++ - Improved `Lockable` in several ways like adding `withLock()` (+ tests) - Improved `Semaphore` a bit for improved readability, and also added some tests - Upgrade Linux machine in GitHub Actions so that we can compile with a newer GCC - Simplification of WiFi connection - Updated funding options - (and more)
This commit is contained in:
committed by
GitHub
parent
1bb1260ea0
commit
6c67845645
@@ -26,9 +26,9 @@ static adc_oneshot_chan_cfg_t adcChannelConfig = {
|
||||
};
|
||||
|
||||
static uint8_t estimateChargeLevelFromVoltage(uint32_t milliVolt) {
|
||||
float volts = TT_MIN((float)milliVolt / 1000.f, BATTERY_VOLTAGE_MAX);
|
||||
float volts = std::min((float)milliVolt / 1000.f, BATTERY_VOLTAGE_MAX);
|
||||
float voltage_percentage = (volts - BATTERY_VOLTAGE_MIN) / (BATTERY_VOLTAGE_MAX - BATTERY_VOLTAGE_MIN);
|
||||
float voltage_factor = TT_MIN(1.0f, voltage_percentage);
|
||||
float voltage_factor = std::min(1.0f, voltage_percentage);
|
||||
auto charge_level = (uint8_t) (voltage_factor * 100.f);
|
||||
TT_LOG_V(TAG, "mV = %lu, scaled = %.2f, factor = %.2f, result = %d", milliVolt, volts, voltage_factor, charge_level);
|
||||
return charge_level;
|
||||
@@ -56,11 +56,11 @@ TdeckPower::~TdeckPower() {
|
||||
|
||||
bool TdeckPower::supportsMetric(MetricType type) const {
|
||||
switch (type) {
|
||||
case MetricType::BatteryVoltage:
|
||||
case MetricType::ChargeLevel:
|
||||
using enum MetricType;
|
||||
case BatteryVoltage:
|
||||
case ChargeLevel:
|
||||
return true;
|
||||
case MetricType::IsCharging:
|
||||
case MetricType::Current:
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -69,17 +69,17 @@ bool TdeckPower::supportsMetric(MetricType type) const {
|
||||
|
||||
bool TdeckPower::getMetric(Power::MetricType type, Power::MetricData& data) {
|
||||
switch (type) {
|
||||
case MetricType::BatteryVoltage:
|
||||
using enum MetricType;
|
||||
case BatteryVoltage:
|
||||
return readBatteryVoltageSampled(data.valueAsUint32);
|
||||
case MetricType::ChargeLevel:
|
||||
case ChargeLevel:
|
||||
if (readBatteryVoltageSampled(data.valueAsUint32)) {
|
||||
data.valueAsUint32 = estimateChargeLevelFromVoltage(data.valueAsUint32);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
case MetricType::IsCharging:
|
||||
case MetricType::Current:
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,11 +8,12 @@ extern axp192_t axpDevice;
|
||||
|
||||
bool Core2Power::supportsMetric(MetricType type) const {
|
||||
switch (type) {
|
||||
case MetricType::BatteryVoltage:
|
||||
case MetricType::ChargeLevel:
|
||||
case MetricType::IsCharging:
|
||||
using enum MetricType;
|
||||
case BatteryVoltage:
|
||||
case ChargeLevel:
|
||||
case IsCharging:
|
||||
return true;
|
||||
case MetricType::Current:
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -21,16 +22,17 @@ bool Core2Power::supportsMetric(MetricType type) const {
|
||||
|
||||
bool Core2Power::getMetric(Power::MetricType type, Power::MetricData& data) {
|
||||
switch (type) {
|
||||
case MetricType::BatteryVoltage: {
|
||||
using enum MetricType;
|
||||
case BatteryVoltage: {
|
||||
float voltage;
|
||||
if (axp192_read(&axpDevice, AXP192_BATTERY_VOLTAGE, &voltage) == ESP_OK) {
|
||||
data.valueAsUint32 = (uint32_t)TT_MAX((voltage * 1000.f), 0.0f);
|
||||
data.valueAsUint32 = (uint32_t)std::max((voltage * 1000.f), 0.0f);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
case MetricType::ChargeLevel: {
|
||||
case ChargeLevel: {
|
||||
float vbat, charge_current;
|
||||
if (
|
||||
axp192_read(&axpDevice, AXP192_BATTERY_VOLTAGE, &vbat) == ESP_OK &&
|
||||
@@ -51,7 +53,7 @@ bool Core2Power::getMetric(Power::MetricType type, Power::MetricData& data) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
case MetricType::IsCharging: {
|
||||
case IsCharging: {
|
||||
float charge_current;
|
||||
if (axp192_read(&axpDevice, AXP192_CHARGE_CURRENT, &charge_current) == ESP_OK) {
|
||||
data.valueAsBool = charge_current > 0.001f;
|
||||
@@ -60,7 +62,7 @@ bool Core2Power::getMetric(Power::MetricType type, Power::MetricData& data) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
case MetricType::Current: {
|
||||
case Current: {
|
||||
float charge_current, discharge_current;
|
||||
if (
|
||||
axp192_read(&axpDevice, AXP192_CHARGE_CURRENT, &charge_current) == ESP_OK &&
|
||||
@@ -76,9 +78,9 @@ bool Core2Power::getMetric(Power::MetricType type, Power::MetricData& data) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
return false; // Safety guard for when new enum values are introduced
|
||||
}
|
||||
|
||||
bool Core2Power::isAllowedToCharge() const {
|
||||
|
||||
@@ -5,11 +5,12 @@
|
||||
|
||||
bool CoreS3Power::supportsMetric(MetricType type) const {
|
||||
switch (type) {
|
||||
case MetricType::BatteryVoltage:
|
||||
case MetricType::IsCharging:
|
||||
case MetricType::ChargeLevel:
|
||||
using enum MetricType;
|
||||
case BatteryVoltage:
|
||||
case IsCharging:
|
||||
case ChargeLevel:
|
||||
return true;
|
||||
case MetricType::Current:
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -18,7 +19,8 @@ bool CoreS3Power::supportsMetric(MetricType type) const {
|
||||
|
||||
bool CoreS3Power::getMetric(Power::MetricType type, Power::MetricData& data) {
|
||||
switch (type) {
|
||||
case MetricType::BatteryVoltage: {
|
||||
using enum MetricType;
|
||||
case BatteryVoltage: {
|
||||
float milliVolt;
|
||||
if (axpDevice.getBatteryVoltage(milliVolt)) {
|
||||
data.valueAsUint32 = (uint32_t)milliVolt;
|
||||
@@ -27,7 +29,7 @@ bool CoreS3Power::getMetric(Power::MetricType type, Power::MetricData& data) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
case MetricType::ChargeLevel: {
|
||||
case ChargeLevel: {
|
||||
float vbatMillis;
|
||||
if (axpDevice.getBatteryVoltage(vbatMillis)) {
|
||||
float vbat = vbatMillis / 1000.f;
|
||||
@@ -44,7 +46,7 @@ bool CoreS3Power::getMetric(Power::MetricType type, Power::MetricData& data) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
case MetricType::IsCharging: {
|
||||
case IsCharging: {
|
||||
Axp2101::ChargeStatus status;
|
||||
if (axpDevice.getChargeStatus(status)) {
|
||||
data.valueAsBool = (status == Axp2101::CHARGE_STATUS_CHARGING);
|
||||
@@ -53,11 +55,9 @@ bool CoreS3Power::getMetric(Power::MetricType type, Power::MetricData& data) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
case MetricType::Current:
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
return false; // Safety guard for when new enum values are introduced
|
||||
}
|
||||
|
||||
bool CoreS3Power::isAllowedToCharge() const {
|
||||
|
||||
@@ -4,10 +4,11 @@
|
||||
|
||||
bool SimulatorPower::supportsMetric(MetricType type) const {
|
||||
switch (type) {
|
||||
case MetricType::IsCharging:
|
||||
case MetricType::Current:
|
||||
case MetricType::BatteryVoltage:
|
||||
case MetricType::ChargeLevel:
|
||||
using enum MetricType;
|
||||
case IsCharging:
|
||||
case Current:
|
||||
case BatteryVoltage:
|
||||
case ChargeLevel:
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -16,16 +17,17 @@ bool SimulatorPower::supportsMetric(MetricType type) const {
|
||||
|
||||
bool SimulatorPower::getMetric(Power::MetricType type, Power::MetricData& data) {
|
||||
switch (type) {
|
||||
case MetricType::IsCharging:
|
||||
using enum MetricType;
|
||||
case IsCharging:
|
||||
data.valueAsBool = true;
|
||||
return true;
|
||||
case MetricType::Current:
|
||||
case Current:
|
||||
data.valueAsInt32 = 42;
|
||||
return true;
|
||||
case MetricType::BatteryVoltage:
|
||||
case BatteryVoltage:
|
||||
data.valueAsUint32 = 4032;
|
||||
return true;
|
||||
case MetricType::ChargeLevel:
|
||||
case ChargeLevel:
|
||||
data.valueAsUint8 = 100;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -8,9 +8,9 @@
|
||||
#define BATTERY_VOLTAGE_MAX 4.2f
|
||||
|
||||
static uint8_t estimateChargeLevelFromVoltage(uint32_t milliVolt) {
|
||||
float volts = TT_MIN((float)milliVolt / 1000.f, BATTERY_VOLTAGE_MAX);
|
||||
float volts = std::min((float)milliVolt / 1000.f, BATTERY_VOLTAGE_MAX);
|
||||
float voltage_percentage = (volts - BATTERY_VOLTAGE_MIN) / (BATTERY_VOLTAGE_MAX - BATTERY_VOLTAGE_MIN);
|
||||
float voltage_factor = TT_MIN(1.0f, voltage_percentage);
|
||||
float voltage_factor = std::min(1.0f, voltage_percentage);
|
||||
auto charge_level = (uint8_t) (voltage_factor * 100.f);
|
||||
TT_LOG_V(TAG, "mV = %lu, scaled = %.2f, factor = %.2f, result = %d", milliVolt, volts, voltage_factor, charge_level);
|
||||
return charge_level;
|
||||
@@ -18,8 +18,9 @@ static uint8_t estimateChargeLevelFromVoltage(uint32_t milliVolt) {
|
||||
|
||||
bool UnPhonePower::supportsMetric(MetricType type) const {
|
||||
switch (type) {
|
||||
case MetricType::BatteryVoltage:
|
||||
case MetricType::ChargeLevel:
|
||||
using enum MetricType;
|
||||
case BatteryVoltage:
|
||||
case ChargeLevel:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
@@ -28,9 +29,10 @@ bool UnPhonePower::supportsMetric(MetricType type) const {
|
||||
|
||||
bool UnPhonePower::getMetric(Power::MetricType type, Power::MetricData& data) {
|
||||
switch (type) {
|
||||
case MetricType::BatteryVoltage:
|
||||
using enum MetricType;
|
||||
case BatteryVoltage:
|
||||
return readBatteryVoltageSampled(data.valueAsUint32);
|
||||
case MetricType::ChargeLevel: {
|
||||
case ChargeLevel: {
|
||||
uint32_t milli_volt;
|
||||
if (readBatteryVoltageSampled(milli_volt)) {
|
||||
data.valueAsUint8 = estimateChargeLevelFromVoltage(milli_volt);
|
||||
|
||||
Reference in New Issue
Block a user