bugfix RAM issue in battery monitor

This commit is contained in:
Klaus K Wilting 2018-07-22 11:08:55 +02:00
parent 4649c796b9
commit 6aed57474d
4 changed files with 34 additions and 35 deletions

View File

@ -5,50 +5,48 @@
// Local logging tag // Local logging tag
static const char TAG[] = "main"; 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 =
if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) { (esp_adc_cal_characteristics_t *)calloc(
ESP_LOGD(TAG, 1, sizeof(esp_adc_cal_characteristics_t));
"ADC characterization based on Two Point values stored in eFuse");
} else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
ESP_LOGD(TAG,
"ADC characterization based on reference voltage stored in eFuse");
} else {
ESP_LOGD(TAG, "ADC characterization based on default reference voltage");
}
}
uint16_t read_voltage(void) { static const adc1_channel_t adc_channel = HAS_BATTERY_PROBE;
static const adc1_channel_t channel = HAS_BATTERY_PROBE;
static const adc_atten_t atten = ADC_ATTEN_DB_11; static const adc_atten_t atten = ADC_ATTEN_DB_11;
static const adc_unit_t unit = ADC_UNIT_1; static const adc_unit_t unit = ADC_UNIT_1;
// configure ADC1 void calibrate_voltage(void) {
// configure ADC
ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_12)); ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_12));
ESP_ERROR_CHECK(adc1_config_channel_atten(channel, atten)); ESP_ERROR_CHECK(adc1_config_channel_atten(adc_channel, atten));
// calibrate ADC
// 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( esp_adc_cal_value_t val_type = esp_adc_cal_characterize(
unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars); unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_characs);
print_char_val_type(val_type); // show ADC characterization base
if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
// multisample ADC1 ESP_LOGI(TAG,
uint32_t adc_reading = 0; "ADC characterization based on Two Point values stored in eFuse");
for (int i = 0; i < NO_OF_SAMPLES; i++) { } else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
adc_reading += adc1_get_raw(channel); ESP_LOGI(TAG,
"ADC characterization based on reference voltage stored in eFuse");
} else {
ESP_LOGI(TAG, "ADC characterization based on default reference voltage");
}
} }
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(adc_channel);
}
adc_reading /= NO_OF_SAMPLES; adc_reading /= NO_OF_SAMPLES;
// Convert ADC reading to voltage in mV
// Convert adc_reading to voltage in mV
uint16_t voltage = 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 #ifdef BATT_FACTOR
voltage *= BATT_FACTOR; voltage *= BATT_FACTOR;
#endif #endif
ESP_LOGD(TAG, "Raw: %d / Voltage: %dmV", adc_reading, voltage); ESP_LOGI(TAG, "Raw: %d / Voltage: %dmV", adc_reading, voltage);
return voltage; return voltage;
} }
#endif // HAS_BATTERY_PROBE #endif // HAS_BATTERY_PROBE

View File

@ -5,8 +5,9 @@
#include <esp_adc_cal.h> #include <esp_adc_cal.h>
#define DEFAULT_VREF 1100 // tbd: use adc2_vref_to_gpio() for better estimate #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); uint16_t read_voltage(void);
void calibrate_voltage(void);
#endif #endif

View File

@ -332,6 +332,7 @@ void setup() {
// initialize battery status if present // initialize battery status if present
#ifdef HAS_BATTERY_PROBE #ifdef HAS_BATTERY_PROBE
strcat_P(features, " BATT"); strcat_P(features, " BATT");
calibrate_voltage();
batt_voltage = read_voltage(); batt_voltage = read_voltage();
#endif #endif
@ -370,7 +371,7 @@ void setup() {
#ifdef HAS_BATTERY_PROBE #ifdef HAS_BATTERY_PROBE
battCycle = timerBegin(3, 8000, true); battCycle = timerBegin(3, 8000, true);
timerAttachInterrupt(battCycle, &BattCycleIRQ, true); timerAttachInterrupt(battCycle, &BattCycleIRQ, true);
timerAlarmWrite(battCycle, 60 * 100, true); timerAlarmWrite(battCycle, BATTREADCYCLE * 10000, true);
timerAlarmEnable(battCycle); timerAlarmEnable(battCycle);
#endif #endif

View File

@ -58,11 +58,10 @@
#define BUTTONPORT 5 // Port on which device sends button pressed signal #define BUTTONPORT 5 // Port on which device sends button pressed signal
#define CAYENNEPORT 2 // Port for Cayenne LPP 2.0 packet sensor encoding #define CAYENNEPORT 2 // Port for Cayenne LPP 2.0 packet sensor encoding
// Default RGB LED luminosity (in %) // Some hardware settings
#define RGBLUMINOSITY 30 // 30% #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
// OLED Display refresh cycle (in Milliseconds) #define BATTREADCYCLE 60 // battery measuring cycle in seconds [default = 60 secs]
#define DISPLAYREFRESH_MS 40 // e.g. 40ms -> 1000/40 = 25 frames per second
// LMIC settings // LMIC settings
// define hardware independent LMIC settings here, settings of standard library in /lmic/config.h will be ignored // define hardware independent LMIC settings here, settings of standard library in /lmic/config.h will be ignored