Refactor ADC MQ_SAMPLES saving code and RAM

This commit is contained in:
Theo Arends 2024-07-26 11:01:09 +02:00
parent 55cb473e13
commit 9af25c73d6
1 changed files with 7 additions and 2 deletions

View File

@ -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++;