From 0f0408df9b06d028e8dd221e782371caa7237da6 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Sun, 12 Oct 2025 14:01:43 +0200 Subject: [PATCH] Simplify moisture value access and update header structure --- firmware/atmega/include/config.h | 2 ++ firmware/atmega/include/globals.h | 8 -------- firmware/atmega/include/twi.h | 4 +++- firmware/atmega/source/main.c | 12 ++++++------ firmware/atmega/source/twi.c | 20 ++++++++++++-------- 5 files changed, 23 insertions(+), 23 deletions(-) delete mode 100644 firmware/atmega/include/globals.h diff --git a/firmware/atmega/include/config.h b/firmware/atmega/include/config.h index c58e469..ad1d181 100644 --- a/firmware/atmega/include/config.h +++ b/firmware/atmega/include/config.h @@ -51,4 +51,6 @@ inline void eeprom_init() { for (int i = 0; i < DICT_SIZE; i++) if (eepromRead(keys[i]) == 0) eepromWrite(keys[i], values[i]); } +extern volatile uint16_t *pLastMoistureVal; + #endif diff --git a/firmware/atmega/include/globals.h b/firmware/atmega/include/globals.h deleted file mode 100644 index dd06370..0000000 --- a/firmware/atmega/include/globals.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef GLOBALS_H -#define GLOBALS_H - -#include - -const uint16_t* moisturePtr(void); - -#endif diff --git a/firmware/atmega/include/twi.h b/firmware/atmega/include/twi.h index b3222e4..180bd26 100644 --- a/firmware/atmega/include/twi.h +++ b/firmware/atmega/include/twi.h @@ -1,7 +1,9 @@ #ifndef TWI_H #define TWI_H +extern volatile uint16_t *pLastMoistureVal; + void twiInit(void); -void twiAlert(void); +void twiListen(void); #endif diff --git a/firmware/atmega/source/main.c b/firmware/atmega/source/main.c index 9a687eb..b888280 100644 --- a/firmware/atmega/source/main.c +++ b/firmware/atmega/source/main.c @@ -9,6 +9,9 @@ #include "pump.h" #include "twi.h" +volatile uint16_t lastMoistureVal = 0; +volatile uint16_t *pLastMoistureVal = &lastMoistureVal; + ISR(WDT_vect) { triggerMoistureRead(); } @@ -40,17 +43,14 @@ static void setup(void) { watchdogs(); } -static uint16_t lastMoistureVal = 0; - -// Encapsulate lastMoistureVal to read later from twi -const uint16_t *moisturePtr(void) { return &lastMoistureVal; } - int main(void) { setup(); + pLastMoistureVal = &lastMoistureVal; + while(true) { if (readSensors) { - lastMoistureVal = moistureRead(); + *pLastMoistureVal = moistureRead(); // nested if: (result outside range (see config.h)) {waterPlant()} } diff --git a/firmware/atmega/source/twi.c b/firmware/atmega/source/twi.c index 1a2e3e7..f405522 100644 --- a/firmware/atmega/source/twi.c +++ b/firmware/atmega/source/twi.c @@ -1,8 +1,8 @@ #include #include #include "config.h" -#include "globals.h" #include "pump.h" +#include "twi.h" #define BUFFER_SIZE 4 @@ -24,17 +24,21 @@ static inline void twiSendNack(void) { TWCR = (TWCR & ~(1 << TWEA)) | (1 << TWINT); } -static void twiRespond(int8_t buffer[]) { +static void twiSendData(uint8_t data) { + TWDR = data; + twiSendAck(); +} + +static void twiRespond(uint8_t buffer[]) { switch(buffer[0]) { - //TODO: simplify by using a more parametrized way go set and get data case 0x01: // Send moisture - twiRespond(*moisturePtr()); + twiSendData(*pLastMoistureVal); break; case 0x02: // Get MOISTURE_MIN - twiRespond(eepromRead(MOISTURE_MIN)); + twiSendData(eepromRead(MOISTURE_MIN)); break; case 0x03: // Get MOISTURE_MAX - twiRespond(eepromRead(MOISTURE_MAX)); + twiSendData(eepromRead(MOISTURE_MAX)); break; case 0x04: // Set parameters eepromWrite(MOISTURE_MIN, buffer[1]); @@ -43,7 +47,7 @@ static void twiRespond(int8_t buffer[]) { twiSendAck(); break; case 0x05: // Get SENSOR_READINGS - twiRespond(eepromRead(SENSOR_READINGS)); + twiSendData(eepromRead(SENSOR_READINGS)); break; case 0x06: // Set SENSOR_READINGS eepromWrite(SENSOR_READINGS, buffer[1]); @@ -73,7 +77,7 @@ void twiListen(void) { TODO: see comment on line 29 */ - static int8_t twiBuffer[BUFFER_SIZE] = {0}; + static uint8_t twiBuffer[BUFFER_SIZE] = {0}; static uint8_t bufferIndex = 0; switch (TWSR & 0xF8) {