summaryrefslogtreecommitdiff
path: root/src/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'src/drivers')
-rw-r--r--src/drivers/CMakeLists.txt2
-rw-r--r--src/drivers/battery.cpp59
-rw-r--r--src/drivers/include/battery.hpp21
3 files changed, 57 insertions, 25 deletions
diff --git a/src/drivers/CMakeLists.txt b/src/drivers/CMakeLists.txt
index 4c8d4275..c4e4c172 100644
--- a/src/drivers/CMakeLists.txt
+++ b/src/drivers/CMakeLists.txt
@@ -2,5 +2,5 @@ idf_component_register(
SRCS "dac.cpp" "gpio_expander.cpp" "battery.cpp" "storage.cpp" "i2c.cpp"
"spi.cpp" "display.cpp" "display_init.cpp"
INCLUDE_DIRS "include"
- REQUIRES "esp_adc_cal" "fatfs" "result" "lvgl" "span")
+ REQUIRES "esp_adc" "fatfs" "result" "lvgl" "span")
target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS})
diff --git a/src/drivers/battery.cpp b/src/drivers/battery.cpp
index b3341eab..b45e20cf 100644
--- a/src/drivers/battery.cpp
+++ b/src/drivers/battery.cpp
@@ -1,31 +1,54 @@
#include "battery.hpp"
+#include <cstdint>
-#include "driver/adc.h"
-#include "esp_adc_cal.h"
+#include "esp_adc/adc_oneshot.h"
#include "hal/adc_types.h"
namespace drivers {
-static esp_adc_cal_characteristics_t calibration;
-
-esp_err_t init_adc(void) {
- // Calibration should already be fused into the chip from the factory, so
- // we should only need to read it back out again.
- esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 0,
- &calibration);
-
- // Max battery voltage should be a little over 2V due to our divider, so
- // we need the max attenuation to properly handle the full range.
- adc1_config_width(ADC_WIDTH_BIT_12);
- adc1_config_channel_atten(ADC1_CHANNEL_6, ADC_ATTEN_DB_11);
+static const uint8_t kAdcBitWidth = ADC_BITWIDTH_12;
+static const uint8_t kAdcUnit = ADC_UNIT_1;
+// Max battery voltage should be a little over 2V due to our divider, so we need
+// the max attenuation to properly handle the full range.
+static const uint8_t kAdcAttenuation = ADC_ATTEN_DB_11;
+// Corresponds to GPIO 34.
+static const uint8_t kAdcChannel = ADC_CHANNEL_6;
+
+Battery::Battery() {
+ adc_oneshot_unit_init_cfg_t unit_config = {
+ .unit_id = kAdcUnit,
+ };
+ ESP_ERROR_CHECK(adc_oneshot_new_unit(&unit_config, &adc_handle_));
+
+ adc_oneshot_chan_cfg_t channel_config = {
+ .bitwidth = kAdcBitWidth,
+ .atten = kAdcAttenuation,
+ };
+ ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle_, kAdcChannel, &channel_config));
+
+ // calibrate
+ // TODO: compile-time assert our scheme is available
+ adc_cali_line_fitting_config_t calibration_config = {
+ .unit_id = kAdcUnit,
+ .atten = kAdcAttenuation,
+ .bitwidth = kAdcBitWidth,
+ };
+ ESP_ERROR_CHECK(adc_cali_create_scheme_line_fitting(&calibration_config, &adc_calibration_handle_));
+}
- return ESP_OK;
+Battery::~Battery() {
+ adc_cali_delete_scheme_line_fitting(adc_calibration_handle_);
}
-uint32_t read_battery_voltage(void) {
+auto Battery::Millivolts() -> uint32_t {
// GPIO 34
- int raw = adc1_get_raw(ADC1_CHANNEL_6);
- return esp_adc_cal_raw_to_voltage(raw, &calibration);
+ int raw;
+ ESP_ERROR_CHECK(adc_oneshot_read(adc_handle, kAdcChannel &raw));
+
+ int voltage;
+ ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc_calibration_handle, raw, &voltage));
+
+ return voltage;
}
} // namespace drivers
diff --git a/src/drivers/include/battery.hpp b/src/drivers/include/battery.hpp
index 9358f9b0..9366a5b1 100644
--- a/src/drivers/include/battery.hpp
+++ b/src/drivers/include/battery.hpp
@@ -1,16 +1,25 @@
#pragma once
-#include <stdint.h>
+#include <cstdint>
+#include "esp_adc/adc_oneshot.h"
#include "esp_err.h"
+#include "result.hpp"
namespace drivers {
-esp_err_t init_adc(void);
+class Battery {
+ public:
+ Battery();
+ ~Battery();
-/**
- * Returns the current battery level in millivolts.
- */
-uint32_t read_battery_voltage(void);
+ /**
+ * Returns the current battery level in millivolts.
+ */
+ auto Millivolts() -> uint32_t;
+ private:
+ adc_oneshot_handle_t adc_handle_;
+ adc_cali_handle_t adc_calibration_handle_;
+};
} // namespace drivers