12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- /*
- This example code is in the Public Domain (or CC0 licensed, at your option.)
- Unless required by applicable law or agreed to in writing, this
- software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
- CONDITIONS OF ANY KIND, either express or implied.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/timers.h"
- #include "esp_system.h"
- #include "esp_log.h"
- #include "driver/adc.h"
- #include "battery.h"
- /*
- There is a bug in esp32 which causes a spurious interrupt on gpio 36/39 when
- using ADC, AMP and HALL sensor. Rather than making battery aware, we just ignore
- if as the interrupt lasts 80ns and should be debounced (and the ADC read does not
- happen very often)
- */
- #define BATTERY_TIMER (10*1000)
- static const char *TAG = "battery";
- static struct {
- float sum, avg;
- int count;
- TimerHandle_t timer;
- } battery;
- /****************************************************************************************
- *
- */
- int battery_value_svc(void) {
- return battery.avg;
- }
- /****************************************************************************************
- *
- */
- #ifdef CONFIG_SQUEEZEAMP
- static void battery_callback(TimerHandle_t xTimer) {
- battery.sum += adc1_get_raw(ADC1_CHANNEL_7) / 4095. * (10+174)/10. * 1.1;
- if (++battery.count == 30) {
- battery.avg = battery.sum / battery.count;
- battery.sum = battery.count = 0;
- ESP_LOGI(TAG, "Voltage %.2fV", battery.avg);
- }
- }
- #endif
- /****************************************************************************************
- *
- */
- void battery_svc_init(void) {
- #ifdef CONFIG_SQUEEZEAMP
- ESP_LOGI(TAG, "Initializing battery");
- adc1_config_width(ADC_WIDTH_BIT_12);
- adc1_config_channel_atten(ADC1_CHANNEL_7, ADC_ATTEN_DB_0);
- battery.timer = xTimerCreate("battery", BATTERY_TIMER / portTICK_RATE_MS, pdTRUE, NULL, battery_callback);
- xTimerStart(battery.timer, portMAX_DELAY);
- #endif
- }
|