Move TWI response handling from watchdog to main loop
This commit is contained in:
@@ -5,5 +5,6 @@ extern volatile uint16_t *pLastMoistureVal;
|
|||||||
|
|
||||||
void twiInit(void);
|
void twiInit(void);
|
||||||
void twiListen(void);
|
void twiListen(void);
|
||||||
|
void twiRespond(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
volatile uint16_t lastMoistureVal = 0;
|
volatile uint16_t lastMoistureVal = 0;
|
||||||
volatile uint16_t *pLastMoistureVal = &lastMoistureVal;
|
volatile uint16_t *pLastMoistureVal = &lastMoistureVal;
|
||||||
|
extern bool respondFlag;
|
||||||
|
|
||||||
ISR(WDT_vect) {
|
ISR(WDT_vect) {
|
||||||
triggerMoistureRead();
|
triggerMoistureRead();
|
||||||
@@ -52,10 +53,14 @@ int main(void) {
|
|||||||
if (readSensors) {
|
if (readSensors) {
|
||||||
*pLastMoistureVal = moistureRead();
|
*pLastMoistureVal = moistureRead();
|
||||||
// nested if: (result outside range (see config.h)) {waterPlant()}
|
// nested if: (result outside range (see config.h)) {waterPlant()}
|
||||||
|
readSensors = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(respondFlag) {
|
||||||
|
twiRespond();
|
||||||
|
respondFlag = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
readSensors = false;
|
|
||||||
enterSleep();
|
enterSleep();
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "pump.h"
|
#include "pump.h"
|
||||||
@@ -6,6 +7,10 @@
|
|||||||
|
|
||||||
#define BUFFER_SIZE 4
|
#define BUFFER_SIZE 4
|
||||||
|
|
||||||
|
static uint8_t twiBuffer[BUFFER_SIZE] = {0};
|
||||||
|
static uint8_t bufferIndex = 0;
|
||||||
|
bool respondFlag = false;
|
||||||
|
|
||||||
static inline void twiReset(void) {
|
static inline void twiReset(void) {
|
||||||
// Enable TWI; enable ACK; clear interrupt flag; enable twi external interrupt
|
// Enable TWI; enable ACK; clear interrupt flag; enable twi external interrupt
|
||||||
TWCR = (1 << TWEN) | (1 << TWEA) | (1 << TWINT) | (1 << TWIE);
|
TWCR = (1 << TWEN) | (1 << TWEA) | (1 << TWINT) | (1 << TWIE);
|
||||||
@@ -29,8 +34,9 @@ static void twiSendData(uint8_t data) {
|
|||||||
twiSendAck();
|
twiSendAck();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void twiRespond(uint8_t buffer[]) {
|
void twiRespond(void) {
|
||||||
switch(buffer[0]) {
|
//FIXME: refactor this implementation
|
||||||
|
switch(twiBuffer[0]) {
|
||||||
case 0x01: // Send moisture
|
case 0x01: // Send moisture
|
||||||
twiSendData(*pLastMoistureVal);
|
twiSendData(*pLastMoistureVal);
|
||||||
break;
|
break;
|
||||||
@@ -41,16 +47,16 @@ static void twiRespond(uint8_t buffer[]) {
|
|||||||
twiSendData(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, twiBuffer[1]);
|
||||||
eepromWrite(MOISTURE_MAX, buffer[2]);
|
eepromWrite(MOISTURE_MAX, twiBuffer[2]);
|
||||||
eepromWrite(SENSOR_READINGS, buffer[3]);
|
eepromWrite(SENSOR_READINGS, twiBuffer[3]);
|
||||||
twiSendAck();
|
twiSendAck();
|
||||||
break;
|
break;
|
||||||
case 0x05: // Get SENSOR_READINGS
|
case 0x05: // Get SENSOR_READINGS
|
||||||
twiSendData(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, twiBuffer[1]);
|
||||||
twiSendAck();
|
twiSendAck();
|
||||||
break;
|
break;
|
||||||
case 0x07: // Force water plants (one pumping)
|
case 0x07: // Force water plants (one pumping)
|
||||||
@@ -73,15 +79,11 @@ void twiListen(void) {
|
|||||||
So when parsing:
|
So when parsing:
|
||||||
buffer[0] is the message itself
|
buffer[0] is the message itself
|
||||||
buffer[1] to buffer[3] are parameters
|
buffer[1] to buffer[3] are parameters
|
||||||
|
|
||||||
TODO: see comment on line 29
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static uint8_t twiBuffer[BUFFER_SIZE] = {0};
|
|
||||||
static uint8_t bufferIndex = 0;
|
|
||||||
|
|
||||||
switch (TWSR & 0xF8) {
|
switch (TWSR & 0xF8) {
|
||||||
case 0x60: // SLA+W received
|
case 0x60: // SLA+W received
|
||||||
|
bufferIndex = 0;
|
||||||
twiSendAck();
|
twiSendAck();
|
||||||
break;
|
break;
|
||||||
case 0x80: // data received
|
case 0x80: // data received
|
||||||
@@ -91,7 +93,7 @@ void twiListen(void) {
|
|||||||
} else twiSendNack();
|
} else twiSendNack();
|
||||||
break;
|
break;
|
||||||
case 0xA0: // stop condition
|
case 0xA0: // stop condition
|
||||||
twiRespond(twiBuffer);
|
respondFlag = true;
|
||||||
twiReset();
|
twiReset();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in New Issue
Block a user