Simplify moisture value access and update header structure

This commit is contained in:
2025-10-12 14:01:43 +02:00
parent 2d9a4f3bb5
commit 0f0408df9b
5 changed files with 23 additions and 23 deletions
+2
View File
@@ -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
-8
View File
@@ -1,8 +0,0 @@
#ifndef GLOBALS_H
#define GLOBALS_H
#include <stdint.h>
const uint16_t* moisturePtr(void);
#endif
+3 -1
View File
@@ -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
+6 -6
View File
@@ -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()}
}
+12 -8
View File
@@ -1,8 +1,8 @@
#include <stdint.h>
#include <avr/io.h>
#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) {