Add data receiving capabilities using a buffer with overflow protection
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
#ifndef TWI_H
|
||||||
|
#define TWI_H
|
||||||
|
|
||||||
|
void twiInit(void);
|
||||||
|
void twiAlert(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -3,15 +3,21 @@
|
|||||||
#include <avr/sleep.h>
|
#include <avr/sleep.h>
|
||||||
#include <avr/wdt.h>
|
#include <avr/wdt.h>
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
|
#include "config.h"
|
||||||
#include "adc.h"
|
#include "adc.h"
|
||||||
#include "moisture.h"
|
#include "moisture.h"
|
||||||
#include "pump.h"
|
#include "pump.h"
|
||||||
|
#include "twi.h"
|
||||||
|
|
||||||
ISR(WDT_vect) {
|
ISR(WDT_vect) {
|
||||||
triggerMoistureRead();
|
triggerMoistureRead();
|
||||||
}
|
}
|
||||||
|
|
||||||
void watchdogs(void) {
|
ISR(TWI_vect) {
|
||||||
|
twiAlert();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void watchdogs(void) {
|
||||||
cli();
|
cli();
|
||||||
MCUSR &= ~(1 << WDRF);
|
MCUSR &= ~(1 << WDRF);
|
||||||
WDTCSR |= (1 << WDCE) | (1 << WDE);
|
WDTCSR |= (1 << WDCE) | (1 << WDE);
|
||||||
@@ -19,15 +25,16 @@ void watchdogs(void) {
|
|||||||
sei();
|
sei();
|
||||||
}
|
}
|
||||||
|
|
||||||
void enterSleep(void) {
|
static inline void enterSleep(void) {
|
||||||
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
|
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
|
||||||
sleep_enable();
|
sleep_enable();
|
||||||
sleep_cpu();
|
sleep_cpu();
|
||||||
sleep_disable();
|
sleep_disable();
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup(void) {
|
static void setup(void) {
|
||||||
adcInit();
|
adcInit();
|
||||||
|
twiInit();
|
||||||
sensorsInit();
|
sensorsInit();
|
||||||
pumpsInit();
|
pumpsInit();
|
||||||
watchdogs();
|
watchdogs();
|
||||||
@@ -39,10 +46,9 @@ int main(void) {
|
|||||||
while(true) {
|
while(true) {
|
||||||
if (readSensors) {
|
if (readSensors) {
|
||||||
uint16_t result = moistureRead();
|
uint16_t result = moistureRead();
|
||||||
//espSend(VASE_NUM, result)
|
// nested if: (result outside range (see config.h)) {waterPlant()}
|
||||||
|
|
||||||
}
|
}
|
||||||
// if(espGet("ask if i need to water plants") == true) {waterPlant()}
|
|
||||||
// or maybe it's better to do this with interrupts?
|
|
||||||
|
|
||||||
readSensors = false;
|
readSensors = false;
|
||||||
enterSleep();
|
enterSleep();
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <avr/io.h>
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#define BUFFER_SIZE 8
|
||||||
|
|
||||||
|
static inline void twiReset(void) {
|
||||||
|
// Enable TWI; enable ACK; clear interrupt flag; enable twi external interrupt
|
||||||
|
TWCR = (1 << TWEN) | (1 << TWEA) | (1 << TWINT) | (1 << TWIE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void twiInit(void) {
|
||||||
|
TWAR = TWI_ADDR; //address definition found in config.h
|
||||||
|
twiReset();
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void twiSendAck(void) {
|
||||||
|
TWCR = (1 << TWEA) | (1 << TWINT);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void twiSendNack(void) {
|
||||||
|
TWCR = (TWCR & ~(1 << TWEA)) | (1 << TWINT);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void twiRespond(int8_t response[]) {
|
||||||
|
// Respond to TWI requests
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void twiExecute(int8_t buffer[]) {
|
||||||
|
// Perform actions based on received message
|
||||||
|
// if needed:
|
||||||
|
// twiRespond();
|
||||||
|
}
|
||||||
|
|
||||||
|
void twiAlert(void) {
|
||||||
|
static int8_t twiBuffer[BUFFER_SIZE] = {0};
|
||||||
|
static uint8_t bufferIndex = 0;
|
||||||
|
|
||||||
|
switch (TWSR & 0xF8) {
|
||||||
|
case 0x60: // SLA+W received
|
||||||
|
twiSendAck();
|
||||||
|
break;
|
||||||
|
case 0x80: // data received
|
||||||
|
if(bufferIndex < BUFFER_SIZE) {
|
||||||
|
twiBuffer[bufferIndex++] = TWDR;
|
||||||
|
twiSendAck();
|
||||||
|
} else twiSendNack();
|
||||||
|
break;
|
||||||
|
case 0xA0: // stop condition
|
||||||
|
twiExecute(twiBuffer);
|
||||||
|
twiReset();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
twiSendNack();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user