Simplify moisture value access and update header structure
This commit is contained in:
@@ -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]);
|
for (int i = 0; i < DICT_SIZE; i++) if (eepromRead(keys[i]) == 0) eepromWrite(keys[i], values[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern volatile uint16_t *pLastMoistureVal;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
#ifndef GLOBALS_H
|
|
||||||
#define GLOBALS_H
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
const uint16_t* moisturePtr(void);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
#ifndef TWI_H
|
#ifndef TWI_H
|
||||||
#define TWI_H
|
#define TWI_H
|
||||||
|
|
||||||
|
extern volatile uint16_t *pLastMoistureVal;
|
||||||
|
|
||||||
void twiInit(void);
|
void twiInit(void);
|
||||||
void twiAlert(void);
|
void twiListen(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -9,6 +9,9 @@
|
|||||||
#include "pump.h"
|
#include "pump.h"
|
||||||
#include "twi.h"
|
#include "twi.h"
|
||||||
|
|
||||||
|
volatile uint16_t lastMoistureVal = 0;
|
||||||
|
volatile uint16_t *pLastMoistureVal = &lastMoistureVal;
|
||||||
|
|
||||||
ISR(WDT_vect) {
|
ISR(WDT_vect) {
|
||||||
triggerMoistureRead();
|
triggerMoistureRead();
|
||||||
}
|
}
|
||||||
@@ -40,17 +43,14 @@ 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();
|
||||||
|
|
||||||
|
pLastMoistureVal = &lastMoistureVal;
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
if (readSensors) {
|
if (readSensors) {
|
||||||
lastMoistureVal = moistureRead();
|
*pLastMoistureVal = moistureRead();
|
||||||
// nested if: (result outside range (see config.h)) {waterPlant()}
|
// nested if: (result outside range (see config.h)) {waterPlant()}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "globals.h"
|
|
||||||
#include "pump.h"
|
#include "pump.h"
|
||||||
|
#include "twi.h"
|
||||||
|
|
||||||
#define BUFFER_SIZE 4
|
#define BUFFER_SIZE 4
|
||||||
|
|
||||||
@@ -24,17 +24,21 @@ static inline void twiSendNack(void) {
|
|||||||
TWCR = (TWCR & ~(1 << TWEA)) | (1 << TWINT);
|
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]) {
|
switch(buffer[0]) {
|
||||||
//TODO: simplify by using a more parametrized way go set and get data
|
|
||||||
case 0x01: // Send moisture
|
case 0x01: // Send moisture
|
||||||
twiRespond(*moisturePtr());
|
twiSendData(*pLastMoistureVal);
|
||||||
break;
|
break;
|
||||||
case 0x02: // Get MOISTURE_MIN
|
case 0x02: // Get MOISTURE_MIN
|
||||||
twiRespond(eepromRead(MOISTURE_MIN));
|
twiSendData(eepromRead(MOISTURE_MIN));
|
||||||
break;
|
break;
|
||||||
case 0x03: // Get MOISTURE_MAX
|
case 0x03: // Get MOISTURE_MAX
|
||||||
twiRespond(eepromRead(MOISTURE_MAX));
|
twiSendData(eepromRead(MOISTURE_MAX));
|
||||||
break;
|
break;
|
||||||
case 0x04: // Set parameters
|
case 0x04: // Set parameters
|
||||||
eepromWrite(MOISTURE_MIN, buffer[1]);
|
eepromWrite(MOISTURE_MIN, buffer[1]);
|
||||||
@@ -43,7 +47,7 @@ static void twiRespond(int8_t buffer[]) {
|
|||||||
twiSendAck();
|
twiSendAck();
|
||||||
break;
|
break;
|
||||||
case 0x05: // Get SENSOR_READINGS
|
case 0x05: // Get SENSOR_READINGS
|
||||||
twiRespond(eepromRead(SENSOR_READINGS));
|
twiSendData(eepromRead(SENSOR_READINGS));
|
||||||
break;
|
break;
|
||||||
case 0x06: // Set SENSOR_READINGS
|
case 0x06: // Set SENSOR_READINGS
|
||||||
eepromWrite(SENSOR_READINGS, buffer[1]);
|
eepromWrite(SENSOR_READINGS, buffer[1]);
|
||||||
@@ -73,7 +77,7 @@ void twiListen(void) {
|
|||||||
TODO: see comment on line 29
|
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;
|
static uint8_t bufferIndex = 0;
|
||||||
|
|
||||||
switch (TWSR & 0xF8) {
|
switch (TWSR & 0xF8) {
|
||||||
|
|||||||
Reference in New Issue
Block a user