bugfix RAM issue in battery monitor
This commit is contained in:
parent
4649c796b9
commit
6aed57474d
@ -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) {
|
||||
if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
|
||||
ESP_LOGD(TAG,
|
||||
"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");
|
||||
}
|
||||
}
|
||||
esp_adc_cal_characteristics_t *adc_characs =
|
||||
(esp_adc_cal_characteristics_t *)calloc(
|
||||
1, sizeof(esp_adc_cal_characteristics_t));
|
||||
|
||||
uint16_t read_voltage(void) {
|
||||
static const adc1_channel_t channel = HAS_BATTERY_PROBE;
|
||||
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;
|
||||
|
||||
// configure ADC1
|
||||
void calibrate_voltage(void) {
|
||||
// configure ADC
|
||||
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_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_chars);
|
||||
print_char_val_type(val_type);
|
||||
|
||||
// multisample ADC1
|
||||
uint32_t adc_reading = 0;
|
||||
for (int i = 0; i < NO_OF_SAMPLES; i++) {
|
||||
adc_reading += adc1_get_raw(channel);
|
||||
unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_characs);
|
||||
// show ADC characterization base
|
||||
if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
|
||||
ESP_LOGI(TAG,
|
||||
"ADC characterization based on Two Point values stored in eFuse");
|
||||
} else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
|
||||
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;
|
||||
|
||||
// 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
|
@ -5,8 +5,9 @@
|
||||
#include <esp_adc_cal.h>
|
||||
|
||||
#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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user