summaryrefslogtreecommitdiff
path: root/src/drivers
diff options
context:
space:
mode:
authorailurux <ailuruxx@gmail.com>2023-08-28 14:59:52 +1000
committerailurux <ailuruxx@gmail.com>2023-08-28 14:59:52 +1000
commitdb601935c6145445467692c0a4ff2b81e27cf6ce (patch)
treee2eed4a38abd03f14dba504ce5e8dedee0da6a12 /src/drivers
parent6f4ace1dd4b9b34f95af1ba365b68624e209d147 (diff)
parent3a0c42f9240eedfbc6a1e94ad3a59c52664fb5b5 (diff)
downloadtangara-fw-db601935c6145445467692c0a4ff2b81e27cf6ce.tar.gz
Merge branch 'main' of git.sr.ht:~jacqueline/tangara-fw
Diffstat (limited to 'src/drivers')
-rw-r--r--src/drivers/CMakeLists.txt2
-rw-r--r--src/drivers/adc.cpp (renamed from src/drivers/battery.cpp)38
-rw-r--r--src/drivers/include/adc.hpp (renamed from src/drivers/include/battery.hpp)16
3 files changed, 12 insertions, 44 deletions
diff --git a/src/drivers/CMakeLists.txt b/src/drivers/CMakeLists.txt
index a64495f0..1df58f72 100644
--- a/src/drivers/CMakeLists.txt
+++ b/src/drivers/CMakeLists.txt
@@ -3,7 +3,7 @@
# SPDX-License-Identifier: GPL-3.0-only
idf_component_register(
- SRCS "touchwheel.cpp" "i2s_dac.cpp" "gpios.cpp" "battery.cpp" "storage.cpp" "i2c.cpp"
+ SRCS "touchwheel.cpp" "i2s_dac.cpp" "gpios.cpp" "adc.cpp" "storage.cpp" "i2c.cpp"
"spi.cpp" "display.cpp" "display_init.cpp" "samd.cpp" "relative_wheel.cpp" "wm8523.cpp"
"nvs.cpp" "bluetooth.cpp"
INCLUDE_DIRS "include"
diff --git a/src/drivers/battery.cpp b/src/drivers/adc.cpp
index 1755fd64..56d2cbb4 100644
--- a/src/drivers/battery.cpp
+++ b/src/drivers/adc.cpp
@@ -4,7 +4,7 @@
* SPDX-License-Identifier: GPL-3.0-only
*/
-#include "battery.hpp"
+#include "adc.hpp"
#include <cstdint>
#include "esp_adc/adc_cali.h"
@@ -14,21 +14,6 @@
namespace drivers {
-/*
- * Battery voltage, in millivolts, at which the battery charger IC will stop
- * charging.
- */
-static const uint32_t kFullChargeMilliVolts = 4200;
-
-/*
- * Battery voltage, in millivolts, at which *we* will consider the battery to
- * be completely discharged. This is intentionally higher than the charger IC
- * cut-off and the protection on the battery itself; we want to make sure we
- * finish up and have everything unmounted and snoozing before the BMS cuts us
- * off.
- */
-static const uint32_t kEmptyChargeMilliVolts = 3200; // BMS limit is 3100.
-
static const adc_bitwidth_t kAdcBitWidth = ADC_BITWIDTH_12;
static const adc_unit_t kAdcUnit = ADC_UNIT_1;
// Max battery voltage should be a little over 2V due to our divider, so we need
@@ -37,7 +22,7 @@ static const adc_atten_t kAdcAttenuation = ADC_ATTEN_DB_11;
// Corresponds to SENSOR_VP.
static const adc_channel_t kAdcChannel = ADC_CHANNEL_0;
-Battery::Battery() {
+AdcBattery::AdcBattery() {
adc_oneshot_unit_init_cfg_t unit_config = {
.unit_id = kAdcUnit,
};
@@ -59,16 +44,14 @@ Battery::Battery() {
};
ESP_ERROR_CHECK(adc_cali_create_scheme_line_fitting(
&calibration_config, &adc_calibration_handle_));
-
- UpdatePercent();
}
-Battery::~Battery() {
+AdcBattery::~AdcBattery() {
adc_cali_delete_scheme_line_fitting(adc_calibration_handle_);
ESP_ERROR_CHECK(adc_oneshot_del_unit(adc_handle_));
}
-auto Battery::Millivolts() -> uint32_t {
+auto AdcBattery::Millivolts() -> uint32_t {
// GPIO 34
int raw = 0;
ESP_ERROR_CHECK(adc_oneshot_read(adc_handle_, kAdcChannel, &raw));
@@ -81,17 +64,4 @@ auto Battery::Millivolts() -> uint32_t {
return voltage * 2;
}
-auto Battery::UpdatePercent() -> bool {
- auto old_percent = percent_;
- // FIXME: So what we *should* do here is measure the actual real-life
- // time from full battery -> empty battery, store it in NVS, then rely on
- // that. If someone could please do this, it would be lovely. Thanks!
- uint32_t mV = std::max(Millivolts(), kEmptyChargeMilliVolts);
- percent_ = static_cast<uint_fast8_t>(std::min<double>(
- std::max<double>(0.0, mV - kEmptyChargeMilliVolts) /
- (kFullChargeMilliVolts - kEmptyChargeMilliVolts) * 100.0,
- 100.0));
- return old_percent != percent_;
-}
-
} // namespace drivers
diff --git a/src/drivers/include/battery.hpp b/src/drivers/include/adc.hpp
index 64a00135..3e94a9ee 100644
--- a/src/drivers/include/battery.hpp
+++ b/src/drivers/include/adc.hpp
@@ -15,25 +15,23 @@
namespace drivers {
-class Battery {
+/*
+ * Handles measuring the battery's current voltage.
+ */
+class AdcBattery {
public:
- static auto Create() -> Battery* { return new Battery(); }
- Battery();
- ~Battery();
+ static auto Create() -> AdcBattery* { return new AdcBattery(); }
+ AdcBattery();
+ ~AdcBattery();
/**
* Returns the current battery level in millivolts.
*/
auto Millivolts() -> uint32_t;
- auto UpdatePercent() -> bool;
- auto Percent() -> uint_fast8_t { return percent_; }
-
private:
adc_oneshot_unit_handle_t adc_handle_;
adc_cali_handle_t adc_calibration_handle_;
-
- uint_fast8_t percent_;
};
} // namespace drivers