diff --git a/lambda-test/Makefile b/lambda-test/Makefile index 6a08fcb..051c63a 100644 --- a/lambda-test/Makefile +++ b/lambda-test/Makefile @@ -19,7 +19,7 @@ ## Here you can link to one more directory (and multiple .c files) EXTRA_SOURCE_DIR = ../lambda/ EXTRA_SOURCE_FILES = USART.c interrupts.c adc.c sensors.c integers.c \ -lcdroutines.c display.c alert.c command.c +lcdroutines.c display.c alert.c command.c strings.c ##########------------------------------------------------------########## ########## Programmer Defaults ########## diff --git a/lambda-test/lambda-test.c b/lambda-test/lambda-test.c index a64f83b..1628fc8 100644 --- a/lambda-test/lambda-test.c +++ b/lambda-test/lambda-test.c @@ -24,13 +24,15 @@ #include #include #include "USART.h" +#include "avrjunit.h" #include "interrupts.h" #include "adc.h" #include "integers.h" #include "sensors.h" #include "display.h" #include "pins.h" -#include "avrjunit.h" +#include "command.h" +#include "strings.h" static const tableEntry testTable[] = { {10, 10}, @@ -338,6 +340,57 @@ return true; } +/* Module command */ + +bool testIsSimulation(void) { + assertFalse(isSimulation()); + runCommand("se"); + assertTrue(isSimulation()); + runCommand("sd"); + assertFalse(isSimulation()); + + return true; +} + +bool testIsLogging(void) { + assertFalse(isLogging()); + runCommand("le"); + assertTrue(isLogging()); + runCommand("ld"); + assertFalse(isLogging()); + + return true; +} + +/* Module strings */ + +bool testSplit(void) { + char* string = "f1 f2 f3 "; + char* fields[4]; + split(string, " ", fields, 4); + + assertTrue(strcmp("f1", fields[0]) == 0); + assertTrue(strcmp("f2", fields[1]) == 0); + assertTrue(strcmp("f3", fields[2]) == 0); + assertTrue(strcmp("", fields[3]) == 0); + + return true; +} + +/* + * Whoa. Trying to write more elements than its size in the fields array + * seems to cause the AVR to reset and rerun the tests in an infinite loop. + */ +bool testSplitSizeTooSmall(void) { + char* string = "f1 f2"; + char* fields[1]; + split(string, " ", fields, 1); + + assertTrue(strcmp("f1", fields[0]) == 0); + + return true; +} + // TODO these long function names passed along as strings use a lot of memory // use PROGMEM? @@ -373,7 +426,11 @@ {"display", "testCycle", testCycle}, {"display", "testUpdateInitial", testUpdateInitial}, {"display", "testUpdate", testUpdate}, - {"display", "testDisplay", testDisplay} + {"display", "testDisplay", testDisplay}, + {"command", "testIsSimulation", testIsSimulation}, + {"command", "testIsLogging", testIsLogging}, + {"strings", "testSplit", testSplit}, + {"strings", "testSplitSizeTooSmall", testSplitSizeTooSmall} }; int main(void) { diff --git a/lambda/Makefile b/lambda/Makefile index 85e8557..ff730d0 100644 --- a/lambda/Makefile +++ b/lambda/Makefile @@ -17,7 +17,7 @@ ## If you've split your program into multiple .c / .h files, ## include the additional source (in same directory) here LOCAL_SOURCE = USART.c interrupts.c adc.c sensors.c integers.c lcdroutines.c \ -display.c alert.c command.c +display.c alert.c command.c strings.c ## Here you can link to one more directory (and multiple .c files) EXTRA_SOURCE_DIR = diff --git a/lambda/command.c b/lambda/command.c index c3b9214..15f0389 100644 --- a/lambda/command.c +++ b/lambda/command.c @@ -14,6 +14,7 @@ #include "display.h" #include "alert.h" #include "command.h" +#include "strings.h" bool simulation = false; bool logging = false; @@ -28,15 +29,7 @@ void runCommand(char* data) { char* fields[8]; - char* token = strtok(data, " "); - uint8_t index = 0; - while (token != NULL) { - fields[index++] = token; - token = strtok(NULL, " "); - } - for (; index < 8; index++) { - fields[index] = '\0'; - } + split(data, " ", fields, 8); if (strcmp(fields[0], "se") == 0) { // simulation enable resetMeas(); @@ -72,3 +65,4 @@ updateMeas(meas); } } + diff --git a/lambda/strings.c b/lambda/strings.c new file mode 100644 index 0000000..79c683d --- /dev/null +++ b/lambda/strings.c @@ -0,0 +1,21 @@ +/* + * strings.c + * + * Created on: 10.05.2015 + * Author: dode@luniks.net + */ + +#include +#include + +void split(char* string, char* delim, char* fields[], uint8_t size) { + uint8_t index = 0; + char* token = strtok(string, delim); + while (token != NULL && index < size) { + fields[index++] = token; + token = strtok(NULL, delim); + } + for (; index < size; index++) { + fields[index] = '\0'; + } +} diff --git a/lambda/strings.h b/lambda/strings.h new file mode 100644 index 0000000..1a1154e --- /dev/null +++ b/lambda/strings.h @@ -0,0 +1,17 @@ +/* + * strings.h + * + * Created on: 10.05.2015 + * Author: dode@luniks.net + */ + +#ifndef STRINGS_H_ +#define STRINGS_H_ + +/** + * Splits the given string at the given delimiter and puts the split strings in + * the given array with the given size. + */ +void split(char* string, char* delim, char* fields[], uint8_t size); + +#endif /* STRINGS_H_ */