From 74aab766fcacbc7e69cb1945c2faef2786ddf8a3 Mon Sep 17 00:00:00 2001 From: Erick Ahmed Date: Sun, 5 Oct 2025 22:21:21 +0200 Subject: [PATCH] Add read-only pointer accessor for previously read moisture value --- firmware/atmega/include/globals.h | 8 ++++++++ firmware/atmega/source/main.c | 7 ++++++- firmware/atmega/source/twi.c | 14 ++++++++++++-- 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 firmware/atmega/include/globals.h diff --git a/firmware/atmega/include/globals.h b/firmware/atmega/include/globals.h new file mode 100644 index 0000000..dd06370 --- /dev/null +++ b/firmware/atmega/include/globals.h @@ -0,0 +1,8 @@ +#ifndef GLOBALS_H +#define GLOBALS_H + +#include + +const uint16_t* moisturePtr(void); + +#endif diff --git a/firmware/atmega/source/main.c b/firmware/atmega/source/main.c index 621978b..3a895e9 100644 --- a/firmware/atmega/source/main.c +++ b/firmware/atmega/source/main.c @@ -40,12 +40,17 @@ 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(); while(true) { if (readSensors) { - uint16_t result = moistureRead(); + lastMoistureVal = 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 7494d20..864bffe 100644 --- a/firmware/atmega/source/twi.c +++ b/firmware/atmega/source/twi.c @@ -1,6 +1,7 @@ #include #include #include "config.h" +#include "globals.h" #define BUFFER_SIZE 8 @@ -26,9 +27,18 @@ static void twiRespond(int8_t response[]) { // Respond to TWI requests } - static void twiExecute(int8_t buffer[]) { - // Perform actions based on received message + switch(buffer[0]) { + case 0x01: // Send moisture + twiRespond(*moisturePtr()); + break; + case 0x02: + // Perform action for command 0x02 + break; + default: + // twiRespond with unkown command + break; + } // if needed: // twiRespond(); }