From 9af25c73d6474520b9d0cceecc06083aa082694f Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Fri, 26 Jul 2024 11:01:09 +0200 Subject: [PATCH] Refactor ADC MQ_SAMPLES saving code and RAM --- tasmota/tasmota_xsns_sensor/xsns_02_analog.ino | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tasmota/tasmota_xsns_sensor/xsns_02_analog.ino b/tasmota/tasmota_xsns_sensor/xsns_02_analog.ino index 88928ab19..f085cdcc8 100644 --- a/tasmota/tasmota_xsns_sensor/xsns_02_analog.ino +++ b/tasmota/tasmota_xsns_sensor/xsns_02_analog.ino @@ -163,6 +163,8 @@ struct { } Adcs; struct { +// float mq_samples[ANALOG_MQ_SAMPLES]; + float *mq_samples; float temperature = 0; float current = 0; float energy = 0; @@ -170,12 +172,11 @@ struct { uint32_t param2 = 0; int param3 = 0; int param4 = 0; + int indexOfPointer = -1; uint32_t previous_millis = 0; uint16_t last_value = 0; uint8_t type = 0; uint8_t pin = 0; - float mq_samples[ANALOG_MQ_SAMPLES]; - int indexOfPointer = -1; } Adc[MAX_ADCS]; bool adcAttachPin(uint8_t pin) { @@ -261,6 +262,10 @@ void AdcAttach(uint32_t pin, uint8_t type) { if (Adcs.present == MAX_ADCS) { return; } Adc[Adcs.present].pin = pin; if (adcAttachPin(Adc[Adcs.present].pin)) { + if (ADC_MQ == type) { + Adc[Adcs.present].mq_samples = (float*)calloc(sizeof(float), ANALOG_MQ_SAMPLES); // Need calloc to reset registers to 0/false + if (nullptr == Adc[Adcs.present].mq_samples) { return; } + } Adc[Adcs.present].type = type; // analogSetPinAttenuation(Adc[Adcs.present].pin, ADC_11db); // Default Adcs.present++;