diff options
| author | jacqueline <me@jacqueline.id.au> | 2022-09-30 19:56:12 +1000 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2022-09-30 19:57:00 +1000 |
| commit | d491085ba3eec342cc4a7591e423cbf4566aba5e (patch) | |
| tree | d4ec84ea85d5b6966dbfb8be4101d608fcb13188 /main/battery.cpp | |
| parent | 0fbc2ff0911f0acfce8bad4511159dab3c9a04d5 (diff) | |
| download | tangara-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?
Diffstat (limited to 'main/battery.cpp')
| -rw-r--r-- | main/battery.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
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 |
