diff --git a/lambda/interrupts.c b/lambda/interrupts.c index 5ae97e6..c6627ce 100644 --- a/lambda/interrupts.c +++ b/lambda/interrupts.c @@ -5,9 +5,81 @@ * Author: dode@luniks.net */ +#include +#include #include #include #include "interrupts.h" +#include "sensors.h" +#include "display.h" +#include "alert.h" + +volatile bool buttonPressed = false; +volatile bool usartReceived = false; +volatile uint8_t intCount = 0; + +char usartData[64]; + +/** + * Called every 16 ms. + */ +ISR(TIMER0_OVF_vect) { + intCount++; + oscillateBeep(); + if (bit_is_clear(PINB, PB0) && ! buttonPressed) { + buttonPressed = true; + cycleDisplay(); + } else if (bit_is_set(PINB, PB0)) { + buttonPressed = false; + } +} + +/** + * Called when data was received via USART. + */ +ISR(USART_RX_vect) { + if (bit_is_set(UCSR0A, RXC0) && ! usartReceived) { + char data = UDR0; + uint8_t length = strlen(usartData); + if (length < sizeof(usartData) - 1 && data != '\n' && data != '\r') { + usartData[length] = data; + } else { + usartData[length] = '\0'; + usartReceived = true; + } + } +} + +// TODO doesn't really belong in this source file +bool isButtonPressed(void) { + return buttonPressed; +} + +// TODO doesn't really belong in this source file +bool isUSARTReceived(void) { + return usartReceived; +} + +// TODO doesn't really belong in this source file +void getUSARTData(char* data, uint8_t size) { + if (size > 0) { + data[0] = '\0'; + strncat(data, usartData, size - 1); + } + memset(usartData, 0, sizeof(usartData)); + usartReceived = false; +} + +bool hasIntCount(uint8_t count, bool reset) { + if (intCount >= count) { + if (reset) { + intCount = 0; + } + return true; + } else { + return false; + } +} void setupPorts(void) { // pull-up resistor for the mouton @@ -31,7 +103,7 @@ TIMSK0 |= (1 << TOIE0); // enable USART RX complete interrupt 0 - // UCSR0B |= (1 << RXCIE0); + UCSR0B |= (1 << RXCIE0); // enable data register empty interrupt 0 // UCSR0B |= (1 << UDRIE0); diff --git a/lambda/interrupts.h b/lambda/interrupts.h index 11efa34..4fafbbf 100644 --- a/lambda/interrupts.h +++ b/lambda/interrupts.h @@ -8,6 +8,14 @@ #ifndef INTERRUPTS_H_ #define INTERRUPTS_H_ +bool isButtonPressed(void); + +bool isUSARTReceived(void); + +void getUSARTData(char*, uint8_t); + +bool hasIntCount(uint8_t count, bool reset); + /** * Sets up ports. */ diff --git a/lambda/lambda.c b/lambda/lambda.c index 570cd3a..78f3e68 100644 --- a/lambda/lambda.c +++ b/lambda/lambda.c @@ -27,47 +27,10 @@ #include "adc.h" #include "interrupts.h" #include "sensors.h" -#include "integers.h" #include "display.h" #include "alert.h" #include "command.h" -volatile bool buttonPressed = false; -volatile bool usartReceived = false; -volatile uint8_t intCount = 0; -volatile uint8_t dataCount = 0; - -char usartData[64]; - -/** - * Called every 16 ms. - */ -ISR(TIMER0_OVF_vect) { - intCount++; - oscillateBeep(); - if (bit_is_clear(PINB, PB0) && ! buttonPressed) { - buttonPressed = true; - cycleDisplay(); - } else if (bit_is_set(PINB, PB0)) { - buttonPressed = false; - } -} - -/** - * Called when data was received via USART. - */ -ISR(USART_RX_vect) { - if (bit_is_set(UCSR0A, RXC0) && ! usartReceived) { - char data = UDR0; - if (dataCount < sizeof(usartData) - 1 && data != '\n' && data != '\r') { - usartData[dataCount++] = data; - } else { - usartData[dataCount++] = 0; - usartReceived = true; - } - } -} - /** * Does initialization and measures, displays and logs the measurements * infinitely. @@ -87,23 +50,21 @@ // main loop while (1) { - if (intCount >= 62 && ! isSimulation()) { - intCount = 0; + if (hasIntCount(62, true) && ! isSimulation()) { meas = measure(); if (isLogging()) { printMeas(meas); } updateMeas(meas); } - if (buttonPressed) { + if (isButtonPressed()) { // update display immediately updateMeas(meas); } - if (usartReceived) { - command(usartData); - memset(usartData, 0, sizeof(usartData)); - dataCount = 0; - usartReceived = false; + if (isUSARTReceived()) { + char data[64]; + getUSARTData(data, sizeof(data)); + command(data); } }