summaryrefslogtreecommitdiff
path: root/src/drivers/battery.cpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2022-11-07 12:01:29 +1100
committerjacqueline <me@jacqueline.id.au>2022-11-07 12:01:29 +1100
commit28d73ad8660e27f9c7b20b6e978d3d0c412dec00 (patch)
treec50b739ae4712f5ddb9fb6e44e39e01e4c20356d /src/drivers/battery.cpp
parentb13a9793e17e7e0e52ea08fa5fb69ca9b90ad56d (diff)
downloadtangara-fw-28d73ad8660e27f9c7b20b6e978d3d0c412dec00.tar.gz
Split driver-y things into a separate component
Diffstat (limited to 'src/drivers/battery.cpp')
-rw-r--r--src/drivers/battery.cpp31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/drivers/battery.cpp b/src/drivers/battery.cpp
new file mode 100644
index 00000000..66c96daf
--- /dev/null
+++ b/src/drivers/battery.cpp
@@ -0,0 +1,31 @@
+#include "battery.hpp"
+
+#include "driver/adc.h"
+#include "esp_adc_cal.h"
+#include "hal/adc_types.h"
+
+namespace gay_ipod {
+
+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);
+
+ return ESP_OK;
+}
+
+uint32_t read_battery_voltage(void) {
+ // GPIO 34
+ int raw = adc1_get_raw(ADC1_CHANNEL_6);
+ return esp_adc_cal_raw_to_voltage(raw, &calibration);
+}
+
+} // namespace gay_ipod