Add read-only pointer accessor for previously read moisture value
This commit is contained in:
@@ -0,0 +1,8 @@
|
|||||||
|
#ifndef GLOBALS_H
|
||||||
|
#define GLOBALS_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
const uint16_t* moisturePtr(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -40,12 +40,17 @@ static void setup(void) {
|
|||||||
watchdogs();
|
watchdogs();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static uint16_t lastMoistureVal = 0;
|
||||||
|
|
||||||
|
// Encapsulate lastMoistureVal to read later from twi
|
||||||
|
const uint16_t *moisturePtr(void) { return &lastMoistureVal; }
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
setup();
|
setup();
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
if (readSensors) {
|
if (readSensors) {
|
||||||
uint16_t result = moistureRead();
|
lastMoistureVal = moistureRead();
|
||||||
// nested if: (result outside range (see config.h)) {waterPlant()}
|
// nested if: (result outside range (see config.h)) {waterPlant()}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
#include "globals.h"
|
||||||
|
|
||||||
#define BUFFER_SIZE 8
|
#define BUFFER_SIZE 8
|
||||||
|
|
||||||
@@ -26,9 +27,18 @@ static void twiRespond(int8_t response[]) {
|
|||||||
// Respond to TWI requests
|
// Respond to TWI requests
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void twiExecute(int8_t buffer[]) {
|
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:
|
// if needed:
|
||||||
// twiRespond();
|
// twiRespond();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user