Add read-only pointer accessor for previously read moisture value

This commit is contained in:
2025-10-05 22:21:21 +02:00
parent 6c48ebb65f
commit 74aab766fc
3 changed files with 26 additions and 3 deletions
+8
View File
@@ -0,0 +1,8 @@
#ifndef GLOBALS_H
#define GLOBALS_H
#include <stdint.h>
const uint16_t* moisturePtr(void);
#endif
+6 -1
View File
@@ -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()}
}
+12 -2
View File
@@ -1,6 +1,7 @@
#include <stdint.h>
#include <avr/io.h>
#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();
}