diff --git a/lambda/Makefile b/lambda/Makefile index 0fb356a..69df61a 100644 --- a/lambda/Makefile +++ b/lambda/Makefile @@ -13,7 +13,7 @@ MAIN = lambda.c ## en: 0 (default), de: 1 -LANG = 1 +LANG = 0 ## If you've split your program into multiple .c / .h files, ## include the additional source (in same directory) here diff --git a/lambda/display.c b/lambda/display.c index cb9578d..b9e748a 100644 --- a/lambda/display.c +++ b/lambda/display.c @@ -17,12 +17,15 @@ #include "usart.h" #include "lcdroutines.h" #include "integers.h" +#include "interrupts.h" #include "display.h" #include "alert.h" +#include "messages.h" #define MENU_OFF 0 #define MENU_MAX_VALUES 1 #define MENU_LAST_TEXT 2 +#define MENU_TIME 3 uint8_t position = MENU_OFF; bool updatePending = false; @@ -43,8 +46,8 @@ } /** - * Formats the given measurement values and displays them on an 16x2 LCD along - * with the given hint. + * Formats the given measurement values and displays them along with the + * given hint. */ static void displayMeas(Measurement const meas, char* const hint) { uint16_t lambdax100 = divRoundNearest(meas.lambda, 10); @@ -58,6 +61,20 @@ setText(line0, line1); } +/** + * Formats and displays the time since last start/reset. + */ +static void displayTime(void) { + uint32_t time = getTime(); + uint16_t hours = time / 3600; + uint8_t mins = time % 3600 / 60; + uint8_t secs = time % 60; + + char line1[17]; + snprintf(line1, sizeof(line1), "%u:%02u:%02u", hours, mins, secs); + setText(MSG_TIME_SINCE_START, line1); +} + void cycleDisplay(void) { if (isAlertActive()) { // button pressed during alert @@ -66,12 +83,10 @@ return; } position++; - if (position > MENU_LAST_TEXT) { + if (position > MENU_TIME) { position = MENU_OFF; } - if (position == MENU_LAST_TEXT) { - lcd_clear(); - } + lcd_clear(); updatePending = true; beep(1, 2, 31); } @@ -102,6 +117,8 @@ displayMeas(measMax, "|>"); } else if (position == MENU_LAST_TEXT) { setText(lastLine0, lastLine1); + } else if (position == MENU_TIME) { + displayTime(); } else { displayMeas(measLatest, " "); } diff --git a/lambda/interrupts.c b/lambda/interrupts.c index 454389a..6e325b8 100644 --- a/lambda/interrupts.c +++ b/lambda/interrupts.c @@ -19,13 +19,13 @@ #include "alert.h" static volatile bool buttonPressed = false; -static volatile uint32_t time = 0; +static volatile uint32_t ints = 0; /** * Called about every 16.4 ms. */ ISR(TIMER0_OVF_vect) { - time++; + ints++; oscillateBeep(); if (bit_is_clear(PINB, PB0) && ! buttonPressed) { buttonPressed = true; @@ -35,17 +35,21 @@ } } -uint32_t getTime(void) { - uint32_t atomicTime; +uint32_t getInts(void) { + uint32_t atomicInts; ATOMIC_BLOCK(ATOMIC_FORCEON) { - atomicTime = time; + atomicInts = ints; } - return atomicTime; + return atomicInts; +} + +uint32_t getTime(void) { + return getInts() / INTS_PER_SEC; } void resetTime(void) { ATOMIC_BLOCK(ATOMIC_FORCEON) { - time = 0; + ints = 0; } } diff --git a/lambda/interrupts.h b/lambda/interrupts.h index 37ebcd6..343a8e1 100644 --- a/lambda/interrupts.h +++ b/lambda/interrupts.h @@ -11,10 +11,17 @@ #ifndef INTERRUPTS_H_ #define INTERRUPTS_H_ -#define SECOND 61 +// 61 +/-1 depending on internal oscillator? +#define INTS_PER_SEC 61 /** - * Returns the time in units of about 16.4 ms since last reset. + * Returns the count of interrupts occurring about every 16.4 ms + * since last reset. + */ +uint32_t getInts(void); + +/** + * Returns the time in about seconds since last reset. */ uint32_t getTime(void); diff --git a/lambda/lambda.c b/lambda/lambda.c index 6c088e1..b9bb9af 100644 --- a/lambda/lambda.c +++ b/lambda/lambda.c @@ -52,12 +52,14 @@ // spend some time on being polite _delay_ms(3000); setHeatingOn(true); + _delay_ms(1000); Measurement meas; // main loop while (true) { - if (getTime() % SECOND == 0 && ! isSimulation() && + // TODO use getTime() here as well and get rid of getInts()? + if (getInts() % INTS_PER_SEC == 0 && ! isSimulation() && getHeatingState() != HEATING_FAULT) { meas = measure(); if (isLogging()) { diff --git a/lambda/messages.h b/lambda/messages.h index 2b90403..c5c7810 100644 --- a/lambda/messages.h +++ b/lambda/messages.h @@ -36,6 +36,7 @@ #define MSG_HEATING_READY_1 "ready" #define MSG_HEATING_FAULT_0 "Oxygen sensor" #define MSG_HEATING_FAULT_1 "heating fault!" + #define MSG_TIME_SINCE_START "Time since start" #elif LANG == 1 @@ -61,6 +62,7 @@ #define MSG_HEATING_READY_1 "bereit" #define MSG_HEATING_FAULT_0 "Lambdasonden-" #define MSG_HEATING_FAULT_1 "heizung Fehler!" + #define MSG_TIME_SINCE_START "Zeit seit Start" #endif diff --git a/lambda/rules.c b/lambda/rules.c index a370e14..86cd15b 100644 --- a/lambda/rules.c +++ b/lambda/rules.c @@ -121,7 +121,7 @@ return; } if (meas.current > HEATING_SHORT_MA || meas.current < HEATING_DISCONN_MA || - (getTime() > SECOND * 120 && meas.current > HEATING_READY_MA)) { + (getTime() > 120 && meas.current > HEATING_READY_MA)) { // short circuit or disconnected or did not warm up within 2 minutes setHeatingOn(false); setHeatingState(HEATING_FAULT); @@ -136,7 +136,7 @@ */ static void heatingTimeout(bool* const fired, int8_t const dir, Measurement const meas) { - if (isHeatingOn() && getTime() > SECOND * 10800UL && meas.tempI < 400) { + if (isHeatingOn() && getTime() > 10800 && meas.tempI < 400) { setHeatingOn(false); } }