diff --git a/src/battery.cpp b/src/battery.cpp index 147e5d0b..454538a9 100644 --- a/src/battery.cpp +++ b/src/battery.cpp @@ -5,50 +5,48 @@ // Local logging tag static const char TAG[] = "main"; -static void print_char_val_type(esp_adc_cal_value_t val_type) { +esp_adc_cal_characteristics_t *adc_characs = + (esp_adc_cal_characteristics_t *)calloc( + 1, sizeof(esp_adc_cal_characteristics_t)); + +static const adc1_channel_t adc_channel = HAS_BATTERY_PROBE; +static const adc_atten_t atten = ADC_ATTEN_DB_11; +static const adc_unit_t unit = ADC_UNIT_1; + +void calibrate_voltage(void) { + // configure ADC + ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_12)); + ESP_ERROR_CHECK(adc1_config_channel_atten(adc_channel, atten)); + // calibrate ADC + esp_adc_cal_value_t val_type = esp_adc_cal_characterize( + unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_characs); + // show ADC characterization base if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) { - ESP_LOGD(TAG, + ESP_LOGI(TAG, "ADC characterization based on Two Point values stored in eFuse"); } else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) { - ESP_LOGD(TAG, + ESP_LOGI(TAG, "ADC characterization based on reference voltage stored in eFuse"); } else { - ESP_LOGD(TAG, "ADC characterization based on default reference voltage"); + ESP_LOGI(TAG, "ADC characterization based on default reference voltage"); } } -uint16_t read_voltage(void) { - static const adc1_channel_t channel = HAS_BATTERY_PROBE; - static const adc_atten_t atten = ADC_ATTEN_DB_11; - static const adc_unit_t unit = ADC_UNIT_1; - - // configure ADC1 - ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_12)); - ESP_ERROR_CHECK(adc1_config_channel_atten(channel, atten)); - - // calibrate ADC1 - esp_adc_cal_characteristics_t *adc_chars = - (esp_adc_cal_characteristics_t *)calloc( - 1, sizeof(esp_adc_cal_characteristics_t)); - esp_adc_cal_value_t val_type = esp_adc_cal_characterize( - unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars); - print_char_val_type(val_type); - - // multisample ADC1 +uint16_t read_voltage() { + // multisample ADC uint32_t adc_reading = 0; for (int i = 0; i < NO_OF_SAMPLES; i++) { - adc_reading += adc1_get_raw(channel); + adc_reading += adc1_get_raw(adc_channel); } - adc_reading /= NO_OF_SAMPLES; - - // Convert adc_reading to voltage in mV + // Convert ADC reading to voltage in mV uint16_t voltage = - (uint16_t)esp_adc_cal_raw_to_voltage(adc_reading, adc_chars); + (uint16_t)esp_adc_cal_raw_to_voltage(adc_reading, adc_characs); #ifdef BATT_FACTOR voltage *= BATT_FACTOR; #endif - ESP_LOGD(TAG, "Raw: %d / Voltage: %dmV", adc_reading, voltage); + ESP_LOGI(TAG, "Raw: %d / Voltage: %dmV", adc_reading, voltage); return voltage; } + #endif // HAS_BATTERY_PROBE \ No newline at end of file diff --git a/src/battery.h b/src/battery.h index d05dc9b1..93696fb5 100644 --- a/src/battery.h +++ b/src/battery.h @@ -5,8 +5,9 @@ #include #define DEFAULT_VREF 1100 // tbd: use adc2_vref_to_gpio() for better estimate -#define NO_OF_SAMPLES 64 // we do multisampling +#define NO_OF_SAMPLES 64 // we do some multisampling to get better values uint16_t read_voltage(void); +void calibrate_voltage(void); #endif diff --git a/src/main.cpp b/src/main.cpp index 8e124d8b..784fe0ad 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -332,6 +332,7 @@ void setup() { // initialize battery status if present #ifdef HAS_BATTERY_PROBE strcat_P(features, " BATT"); + calibrate_voltage(); batt_voltage = read_voltage(); #endif @@ -370,7 +371,7 @@ void setup() { #ifdef HAS_BATTERY_PROBE battCycle = timerBegin(3, 8000, true); timerAttachInterrupt(battCycle, &BattCycleIRQ, true); - timerAlarmWrite(battCycle, 60 * 100, true); + timerAlarmWrite(battCycle, BATTREADCYCLE * 10000, true); timerAlarmEnable(battCycle); #endif diff --git a/src/paxcounter.conf b/src/paxcounter.conf index 14d76816..501cc1c2 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -58,11 +58,10 @@ #define BUTTONPORT 5 // Port on which device sends button pressed signal #define CAYENNEPORT 2 // Port for Cayenne LPP 2.0 packet sensor encoding -// Default RGB LED luminosity (in %) -#define RGBLUMINOSITY 30 // 30% - -// OLED Display refresh cycle (in Milliseconds) -#define DISPLAYREFRESH_MS 40 // e.g. 40ms -> 1000/40 = 25 frames per second +// Some hardware settings +#define RGBLUMINOSITY 30 // RGB LED luminosity [default = 30%] +#define DISPLAYREFRESH_MS 40 // OLED refresh cycle in ms [default = 40] -> 1000/40 = 25 frames per second +#define BATTREADCYCLE 60 // battery measuring cycle in seconds [default = 60 secs] // LMIC settings // define hardware independent LMIC settings here, settings of standard library in /lmic/config.h will be ignored