Skip to content

Commit

Permalink
Add atten to battery service
Browse files Browse the repository at this point in the history
  • Loading branch information
Wizmo2 committed May 13, 2021
1 parent c7b1c7c commit 2699216
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions components/services/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ static const char *TAG = "battery";
static struct {
int channel;
float sum, avg, scale;
int count;
int count, attenuation;
int cells;
TimerHandle_t timer;
} battery = {
.channel = CONFIG_BAT_CHANNEL,
.cells = 2,
.attenuation = ADC_ATTEN_DB_0,
};

/****************************************************************************************
Expand Down Expand Up @@ -82,14 +83,15 @@ void battery_svc_init(void) {
#ifndef CONFIG_BAT_LOCKED
if ((p = strcasestr(nvs_item, "channel")) != NULL) battery.channel = atoi(strchr(p, '=') + 1);
if ((p = strcasestr(nvs_item, "scale")) != NULL) battery.scale = atof(strchr(p, '=') + 1);
if ((p = strcasestr(nvs_item, "atten")) != NULL) battery.attenuation = atoi(strchr(p, '=') + 1);
#endif
if ((p = strcasestr(nvs_item, "cells")) != NULL) battery.cells = atof(strchr(p, '=') + 1);
free(nvs_item);
}

if (battery.channel != -1) {
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(battery.channel, ADC_ATTEN_DB_0);
adc1_config_channel_atten(battery.channel, battery.attenuation);

battery.avg = adc1_get_raw(battery.channel) * battery.scale / 4095.0;
battery.timer = xTimerCreate("battery", BATTERY_TIMER / portTICK_RATE_MS, pdTRUE, NULL, battery_callback);
Expand Down

0 comments on commit 2699216

Please sign in to comment.