diff --git a/lambda-test/Makefile b/lambda-test/Makefile index 76c37ca..b6451a1 100644 --- a/lambda-test/Makefile +++ b/lambda-test/Makefile @@ -14,8 +14,8 @@ ## If you've split your program into multiple .c / .h files, ## include the additional source (in same directory) here -LOCAL_SOURCE = avrjunit.c adc-test.c command-test.c display-test.c \ -integers-test.c interrupts-test.c sensors-test.c strings-test.c +LOCAL_SOURCE = avrjunit.c adc-test.c alert-test.c command-test.c \ +display-test.c integers-test.c interrupts-test.c sensors-test.c strings-test.c ## Here you can link to one more directory (and multiple .c files) EXTRA_SOURCE_DIR = ../lambda/ diff --git a/lambda-test/alert-test.c b/lambda-test/alert-test.c new file mode 100644 index 0000000..8d9c29b --- /dev/null +++ b/lambda-test/alert-test.c @@ -0,0 +1,145 @@ +/* + * adc-test.c + * + * Unit tests for the lambda project. + * + * Created on: 15.05.2015 + * Author: dode@luniks.net + * + */ + +#include "avrjunit.h" +#include "interrupts.h" +#include "alert.h" + +/* Module alert */ + +bool testOscillateBeep(void) { + extern uint8_t beepCount; + extern uint8_t oscCount; + + beepCount = 0; + oscCount = 0; + + // alert with two beep2 with a length of 2 + alert(2, 2, "a", "b"); + oscillateBeep(); + oscillateBeep(); + + // beep on, alertActive true + assertTrue(beepCount == 2); + assertTrue(oscCount == 2); + assertTrue(bit_is_set(TCCR1A, COM1A0)); + assertTrue(isAlertActive()); + + oscillateBeep(); + oscillateBeep(); + + // beep off, alertActive still true + assertTrue(beepCount == 1); + assertTrue(oscCount == 0); + assertTrue(bit_is_clear(TCCR1A, COM1A0)); + assertTrue(isAlertActive()); + + oscillateBeep(); + oscillateBeep(); + + // beep on, alertActive still true + assertTrue(beepCount == 1); + assertTrue(oscCount == 2); + assertTrue(bit_is_set(TCCR1A, COM1A0)); + assertTrue(isAlertActive()); + + oscillateBeep(); + oscillateBeep(); + + // beep off, alertActive false + assertTrue(beepCount == 0); + assertTrue(oscCount == 0); + assertTrue(bit_is_clear(TCCR1A, COM1A0)); + assertFalse(isAlertActive()); + + return true; +} + +bool testBeep(void) { + extern uint8_t beepCount; + extern uint16_t beepLength; + extern uint8_t oscCount; + + beepCount = 0; + beepLength = 0; + oscCount = 0; + + beep(1, 2); + + assertTrue(beepCount == 1); + assertTrue(beepLength == 2); + assertTrue(oscCount == 0); + + return true; +} + +bool testAlert(void) { + extern uint8_t beepCount; + extern uint16_t beepLength; + extern uint8_t oscCount; + + beepCount = 0; + beepLength = 0; + oscCount = 0; + + alert(1, 2, "a", "b"); + + assertTrue(beepCount == 1); + assertTrue(beepLength == 2); + assertTrue(oscCount == 0); + assertTrue(isAlertActive()); + + return true; +} + +bool testCancelAlert(void) { + extern uint8_t beepCount; + extern uint16_t beepLength; + extern uint8_t oscCount; + + beepCount = 0; + beepLength = 0; + oscCount = 0; + + alert(1, 2, "a", "b"); + + assertTrue(beepCount == 1); + assertTrue(beepLength == 2); + assertTrue(oscCount == 0); + assertTrue(isAlertActive()); + + cancelAlert(); + + assertTrue(beepCount == 0); + assertTrue(oscCount == 0); + assertTrue(bit_is_clear(TCCR1A, COM1A0)); + assertFalse(isAlertActive()); + + return true; +} + +/* Test "class" */ +static const char class[] PROGMEM = "alert"; + +/* Test names */ +static const char testOscillateBeep_P[] PROGMEM = "testOscillateBeep"; +static const char testBeep_P[] PROGMEM = "testBeep"; +static const char testAlert_P[] PROGMEM = "testAlert"; +static const char testCancelAlert_P[] PROGMEM = "testCancelAlert"; + +/* Tests */ +static TestCase const tests[] = { + {class, testOscillateBeep_P, testOscillateBeep}, + {class, testBeep_P, testBeep}, + {class, testAlert_P, testAlert}, + {class, testCancelAlert_P, testCancelAlert} +}; + +TestClass alertClass = {tests, sizeof(tests) / sizeof(tests[0])}; diff --git a/lambda-test/lambda-test.c b/lambda-test/lambda-test.c index b0289d9..18272d8 100644 --- a/lambda-test/lambda-test.c +++ b/lambda-test/lambda-test.c @@ -24,6 +24,7 @@ initUSART(); extern TestClass adcClass; + extern TestClass alertClass; extern TestClass commandClass; extern TestClass displayClass; extern TestClass integersClass; @@ -33,6 +34,7 @@ beginSuite("lambda"); runClass(adcClass); + runClass(alertClass); runClass(commandClass); runClass(displayClass); runClass(integersClass); diff --git a/lambda/alert.c b/lambda/alert.c index 87c6565..53d1264 100644 --- a/lambda/alert.c +++ b/lambda/alert.c @@ -17,12 +17,13 @@ uint8_t beepCount = 0; uint16_t beepLength = 0; +uint8_t oscCount = 0; -static uint8_t oscCount = 0; static bool alertActive = false; void oscillateBeep(void) { if (beepCount == 0) { + oscCount = 0; return; } if (oscCount == 0) { @@ -37,7 +38,7 @@ alertActive = false; } } - oscCount == beepLength * 2 ? oscCount = 0 : oscCount++; + oscCount == beepLength * 2 - 1 ? oscCount = 0 : oscCount++; } void beep(uint8_t const beeps, uint8_t const length) { @@ -57,6 +58,7 @@ void cancelAlert(void) { beepCount = 0; + oscCount = 0; // turn beep off TCCR1A &= ~(1 << COM1A0); alertActive = false;