summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2022-09-30 19:56:12 +1000
committerjacqueline <me@jacqueline.id.au>2022-09-30 19:57:00 +1000
commitd491085ba3eec342cc4a7591e423cbf4566aba5e (patch)
treed4ec84ea85d5b6966dbfb8be4101d608fcb13188
parent0fbc2ff0911f0acfce8bad4511159dab3c9a04d5 (diff)
downloadtangara-fw-d491085ba3eec342cc4a7591e423cbf4566aba5e.tar.gz
factor out adc battery measurement
idk if this should be classy or if this is fine. maybe should just pick a convention at some point?
-rw-r--r--main/CMakeLists.txt2
-rw-r--r--main/battery.cpp31
-rw-r--r--main/battery.h15
-rw-r--r--main/gay-ipod-fw.cpp14
4 files changed, 51 insertions, 11 deletions
diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt
index 4a1fe814..d8e98ac6 100644
--- a/main/CMakeLists.txt
+++ b/main/CMakeLists.txt
@@ -1,2 +1,2 @@
-idf_component_register(SRCS "gay-ipod-fw.cpp" "gpio-expander.cpp"
+idf_component_register(SRCS "gay-ipod-fw.cpp" "gpio-expander.cpp" "battery.cpp"
INCLUDE_DIRS ".")
diff --git a/main/battery.cpp b/main/battery.cpp
new file mode 100644
index 00000000..ef23bfc0
--- /dev/null
+++ b/main/battery.cpp
@@ -0,0 +1,31 @@
+#include "battery.c"
+
+#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_error_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
diff --git a/main/battery.h b/main/battery.h
new file mode 100644
index 00000000..c5140212
--- /dev/null
+++ b/main/battery.h
@@ -0,0 +1,15 @@
+#ifndef BATTERY_H
+#define BATTERY_H
+
+namespace gay_ipod {
+
+esp_error_t init_adc(void);
+
+/**
+ * Returns the current battery level in millivolts.
+ */
+uint32_t read_battery_voltage(void);
+
+} // namespace gay_ipod
+
+#endif
diff --git a/main/gay-ipod-fw.cpp b/main/gay-ipod-fw.cpp
index f466bef1..55ff451f 100644
--- a/main/gay-ipod-fw.cpp
+++ b/main/gay-ipod-fw.cpp
@@ -8,7 +8,7 @@
#include "esp_adc_cal.h"
#include "esp_log.h"
#include "gpio-expander.h"
-#include "gpio-expander.h"
+#include "battery.h"
#include "hal/adc_types.h"
#include "hal/gpio_types.h"
#include "hal/spi_types.h"
@@ -97,20 +97,14 @@ extern "C" void app_main(void)
ESP_ERROR_CHECK(expander.Write());
ESP_LOGI(TAG, "Init ADC");
+ ESP_ERROR_CHECK(init_adc());
- // Attentuate our battery voltage pin to read between 150mV and 2450mV.
- esp_adc_cal_characteristics_t adc1_chars;
- esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 0, &adc1_chars);
- adc1_config_width(ADC_WIDTH_BIT_12);
- adc1_config_channel_atten(ADC1_CHANNEL_6, ADC_ATTEN_DB_11);
while (1) {
- int adc_raw = adc1_get_raw(ADC1_CHANNEL_6);
- uint32_t adc_millivolts = esp_adc_cal_raw_to_voltage(adc_raw, &adc1_chars);
-
+ uint32_t battery = read_battery_voltage();
expander.Read();
- ESP_LOGI(TAG, "raw adc: %d, millivolts: %d, wall power? %d", adc_raw, adc_millivolts, expander.charge_power_ok());
+ ESP_LOGI(TAG, "millivolts: %d, wall power? %d", battery, expander.charge_power_ok());
vTaskDelay(pdMS_TO_TICKS(1000));
ESP_ERROR_CHECK(expander.Write());