diff --git a/lambda/TODO b/lambda/TODO index 1606b8c..2310783 100644 --- a/lambda/TODO +++ b/lambda/TODO @@ -1,6 +1,11 @@ * C - What do static inline volatile extern in C do? - Read up on "global" variables and modules +- Flexarray? http://en.wikipedia.org/wiki/Sizeof +- License? (github) + +* AVR +- What happens if USART data is sent to the AVR too fast (GtkTerm Send Raw File) * Functionality - Add rules and alerts according to FIRE RULEZ! diff --git a/lambda/alert.c b/lambda/alert.c index 50ce6e8..1a5740d 100644 --- a/lambda/alert.c +++ b/lambda/alert.c @@ -8,7 +8,6 @@ #include #include #include -#include "USART.h" #include "alert.h" #include "sensors.h" #include "display.h" diff --git a/lambda/alert.h b/lambda/alert.h index 85eec2b..b9a6203 100644 --- a/lambda/alert.h +++ b/lambda/alert.h @@ -10,12 +10,26 @@ void oscillateBeep(void); +/** + * Beeps the given number of beeps with the given length. Returns quickly so + * it can be called from an ISR. + */ void beep(uint8_t beeps, uint8_t length); +/** + * Beeps the given number of beeps with the given length and displays the + * given two texts on the first and second line of the display, respectively. + */ void alert(uint8_t beeps, uint8_t length, char* line0, char* line1); +/** + * Stops beeping and blocking display updates. + */ void cancelAlert(void); +/** + * Returns true if an alert is active, false otherwise. + */ bool isAlertActive(void); #endif /* ALERT_H_ */ diff --git a/lambda/command.c b/lambda/command.c index ff97dc1..4e39579 100644 --- a/lambda/command.c +++ b/lambda/command.c @@ -18,7 +18,15 @@ bool simulation = false; bool logging = false; -void command(char* data) { +bool isSimulation(void) { + return simulation; +} + +bool isLogging(void) { + return logging; +} + +void runCommand(char* data) { char* fields[8]; char* token = strtok(data, " "); uint8_t index = 0; @@ -26,6 +34,9 @@ fields[index++] = token; token = strtok(NULL, " "); } + for (; index < 8; index++) { + fields[index] = '\0'; + } if (strcmp(fields[0], "se") == 0) { // simulation enable simulation = true; @@ -49,7 +60,6 @@ else if (strcmp(fields[0], "cm") == 0) { // cycle menu cycleDisplay(); - // updateMeas(meas); } else if (strcmp(fields[0], "ta") == 0) { // test alert @@ -60,11 +70,3 @@ updateMeas(meas); } } - -bool isSimulation(void) { - return simulation; -} - -bool isLogging(void) { - return logging; -} diff --git a/lambda/command.h b/lambda/command.h index 7807be0..02f2269 100644 --- a/lambda/command.h +++ b/lambda/command.h @@ -8,10 +8,19 @@ #ifndef COMMAND_H_ #define COMMAND_H_ -void command(char* data); - +/** + * Returns true if simulation mode is enabled, false otherwise. + */ bool isSimulation(void); +/** + * Returns true if logging is enabled, false otherwise. + */ bool isLogging(void); +/** + * Evaluates and runs the given command and/or data. + */ +void runCommand(char* data); + #endif /* COMMAND_H_ */ diff --git a/lambda/display.c b/lambda/display.c index 0b3d7eb..468c540 100644 --- a/lambda/display.c +++ b/lambda/display.c @@ -17,19 +17,19 @@ #include "display.h" #include "alert.h" -#define MAX(x, y) (((x) > (y)) ? (x) : (y)) -#define MIN(x, y) (((x) < (y)) ? (x) : (y)) - #define MENU_OFF 0 #define MENU_MIN_VALUES 1 #define MENU_MAX_VALUES 2 uint8_t position = MENU_OFF; +bool updatePending = false; +measurement measLatest; measurement measMin = {0, 20, 0, 20, 0, 1000}; measurement measMax = {0, 0, 0, 0, 0, 2000}; void cycleDisplay(void) { + updatePending = true; if (isAlertActive()) { // button pressed during alert cancelAlert(); @@ -43,9 +43,8 @@ } void updateMeas(measurement meas) { - if (isAlertActive()) { - return; - } + measLatest = meas; + measMin.tempI = MIN(measMin.tempI, meas.tempI); measMin.tempO = MIN(measMin.tempO, meas.tempO); measMin.lambda = MAX(measMin.lambda, meas.lambda); @@ -54,12 +53,28 @@ measMax.tempO = MAX(measMax.tempO, meas.tempO); measMax.lambda = MIN(measMax.lambda, meas.lambda); + updateDisplay(); +} + +void updateDisplay(void) { + if (isAlertActive()) { + return; + } + if (position == MENU_MIN_VALUES) { displayMeas(measMin, "|<"); } else if (position == MENU_MAX_VALUES) { displayMeas(measMax, ">|"); } else { - displayMeas(meas, " "); + displayMeas(measLatest, " "); + } + + updatePending = false; +} + +void updateDisplayIfRequested() { + if (updatePending) { + updateDisplay(); } } diff --git a/lambda/display.h b/lambda/display.h index de2bb4c..231fb23 100644 --- a/lambda/display.h +++ b/lambda/display.h @@ -9,29 +9,41 @@ #define DISPLAY_H_ /** - * Cycles through the "menu" (display options). + * Cycles through the "menu" (display options). Returns quickly so it can + * be called from an ISR. */ void cycleDisplay(void); /** * Updates the measurements, tracks min and max values since last start/reset - * and displays the selected measurement values. + * and updates the display. */ void updateMeas(measurement); /** - * Formats the given measurement values and prints them via USART. + * Updates the display with the selected measurement values. */ -void printMeas(measurement); +void updateDisplay(void); /** - * Formats the given measurement values and displays them on an 16x2 LCD. + * Updates the display if an update is pending. */ -void displayMeas(measurement, char*); +void updateDisplayIfRequested(void); + +/** + * Formats the given measurement values and prints them via USART. + */ +void printMeas(measurement meas); + +/** + * Formats the given measurement values and displays them on an 16x2 LCD along + * with the given hint. + */ +void displayMeas(measurement meas, char* hint); /** * Displays the given two lines of text. */ -void displayText(char*, char*); +void displayText(char* line0, char* line1); #endif /* DISPLAY_H_ */ diff --git a/lambda/integers.h b/lambda/integers.h index 42458a8..92ab527 100644 --- a/lambda/integers.h +++ b/lambda/integers.h @@ -12,6 +12,9 @@ #ifndef INTEGERS_H_ #define INTEGERS_H_ +#define MAX(x, y) (((x) > (y)) ? (x) : (y)) +#define MIN(x, y) (((x) < (y)) ? (x) : (y)) + /** * Divides the given numerator by the given denominator, * rounds to the nearest int and returns it. diff --git a/lambda/interrupts.c b/lambda/interrupts.c index c6627ce..3d95b15 100644 --- a/lambda/interrupts.c +++ b/lambda/interrupts.c @@ -51,11 +51,6 @@ } // 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; } @@ -116,15 +111,12 @@ // timer clock prescaler/64 = 15.625 kHz overflowing every 16 ms TCCR0B |= (1 << CS01) | (1 << CS00); - // TODO use timer2 which continues running during SLEEP_MODE_ADC? - // toggle pin PB1 on compare match - // TCCR1A |= (1 << COM1A0); - // CTC mode, TOP OCR1A + // Clear Timer on Compare Match mode, TOP OCR1A TCCR1B |= (1 << WGM12); // timer clock prescaler/8 TCCR1B |= (1 << CS11); // toggles PB1 at 7.8 kHz generating a 3.9 kHz beep // OCR1A = 16; - // less noisy 1.8 kHz + // 1.8 kHz is less noisy on the small piezo beeper OCR1A = 32; } diff --git a/lambda/interrupts.h b/lambda/interrupts.h index 4fafbbf..254e39b 100644 --- a/lambda/interrupts.h +++ b/lambda/interrupts.h @@ -8,12 +8,21 @@ #ifndef INTERRUPTS_H_ #define INTERRUPTS_H_ -bool isButtonPressed(void); - +/** + * Returns true if a CR or LF terminated line of data was received via USART. + */ bool isUSARTReceived(void); -void getUSARTData(char*, uint8_t); +/** + * Appends the data received via USART to the given string with the given + * length. + */ +void getUSARTData(char* data, uint8_t length); +/** + * Returns true if the current timer interrupt count is equal or greater to + * the given count and resets it if the given boolean is true. + */ bool hasIntCount(uint8_t count, bool reset); /** diff --git a/lambda/lambda.c b/lambda/lambda.c index 78f3e68..9ad7a58 100644 --- a/lambda/lambda.c +++ b/lambda/lambda.c @@ -32,8 +32,8 @@ #include "command.h" /** - * Does initialization and measures, displays and logs the measurements - * infinitely. + * Does initialization, measures, displays and logs the measurements and + * runs commands sent via USART. */ int main(void) { initUSART(); @@ -57,15 +57,12 @@ } updateMeas(meas); } - if (isButtonPressed()) { - // update display immediately - updateMeas(meas); - } if (isUSARTReceived()) { char data[64]; getUSARTData(data, sizeof(data)); - command(data); + runCommand(data); } + updateDisplayIfRequested(); } // never reached