diff --git a/lambda-test/Makefile b/lambda-test/Makefile index 1ccaab4..8ae915a 100644 --- a/lambda-test/Makefile +++ b/lambda-test/Makefile @@ -22,7 +22,7 @@ ## Link to directory of project under test and its source files TEST_SOURCE_DIR = /data/Job/git/lambda-avr/lambda/ -TEST_SOURCE_FILES = sensors.c +TEST_SOURCE_FILES = sensors.c integers.c ##########------------------------------------------------------########## ########## Programmer Defaults ########## diff --git a/lambda-test/lambda-test.c b/lambda-test/lambda-test.c index 7cbf684..8131aad 100644 --- a/lambda-test/lambda-test.c +++ b/lambda-test/lambda-test.c @@ -26,12 +26,7 @@ uint8_t testToLambdaInter(void) { int16_t lambda = toLambda(50); - char buf[10]; - snprintf(buf, 10, "%d", lambda); - printString(buf); - - // should be 1073 (rounding) - return lambda == 1074; + return lambda == 1073; } uint8_t testToTempI(void) { diff --git a/lambda/Makefile b/lambda/Makefile index feaaa11..a8bc706 100644 --- a/lambda/Makefile +++ b/lambda/Makefile @@ -14,7 +14,7 @@ ## If you've split your program into multiple .c / .h files, ## include the additional source (in same directory) here -LOCAL_SOURCE = sensors.c +LOCAL_SOURCE = sensors.c integers.c ## Here you can link to one more directory (and multiple .c files) EXTRA_SOURCE_DIR = /data/Work/AVR/AVR-Programming-master/AVR-Programming-Library/ diff --git a/lambda/integers.c b/lambda/integers.c new file mode 100644 index 0000000..8d874a5 --- /dev/null +++ b/lambda/integers.c @@ -0,0 +1,15 @@ +/* + * integers.c + * + * Created on: 07.03.2015 + * Author: dode@luniks.net + */ +#include +#include "integers.h" + +int32_t roundNearest(int32_t num, int32_t den) { + return ((num < 0) ^ (den < 0)) ? + ((num - den / 2) / den) : + ((num + den / 2) / den); +} + diff --git a/lambda/integers.h b/lambda/integers.h new file mode 100644 index 0000000..0058661 --- /dev/null +++ b/lambda/integers.h @@ -0,0 +1,15 @@ +/* + * integers.h + * + * Created on: 07.03.2015 + * Author: dode@luniks.net + */ + +/** + * Divides the given numerator by the given denominator + * to the nearest int and returns it. + * http://stackoverflow.com/a/18067292/709426 + */ +int32_t roundNearest(int32_t num, int32_t den); + +// char* toDecimalString(int32_t num, int32_t den); diff --git a/lambda/lambda.c b/lambda/lambda.c index d544686..9d9234e 100644 --- a/lambda/lambda.c +++ b/lambda/lambda.c @@ -7,7 +7,6 @@ * TODO comments, attribution * TODO DIDR? * TODO string.h? - * TODO round, ceil, floor? */ #include #include @@ -18,6 +17,7 @@ #include #include "USART.h" #include "sensors.h" +#include "integers.h" int16_t lambdaVoltageAvg = 0; int16_t tempIVoltageAvg = 0; @@ -48,14 +48,13 @@ char line0[40]; char line1[40]; snprintf(line0, sizeof(line0), "Ti %3d C %d To %3d C %d\r\n", tempI, tempIVoltage, tempO, tempOVoltage); - snprintf(line1, sizeof(line1), "L %d.%03d %d\r\n", lambdaT.quot, lambdaT.rem, lambdaVoltage); + snprintf(line1, sizeof(line1), "L %d.%03d %d\r\n", lambdaT.quot, abs(lambdaT.rem), lambdaVoltage); printString(line0); printString(line1); } int16_t average(int16_t voltage, int16_t average, uint8_t weight) { - // rounding up, assuming voltages are never negative - return (voltage + (average * weight) + weight) / (weight + 1); + return roundNearest(voltage + (average * weight) + weight, weight + 1); } void measure(void) { diff --git a/lambda/sensors.c b/lambda/sensors.c index 3faa907..457df80 100644 --- a/lambda/sensors.c +++ b/lambda/sensors.c @@ -11,9 +11,7 @@ #include #include #include "sensors.h" - -#include -#include "USART.h" +#include "integers.h" // #define AREF_MV 4850 #define AREF_MV 5000 @@ -80,8 +78,7 @@ } int16_t toTempI(int16_t mV) { - // rounding, assuming voltages are never negative - int temp = (mV + 3) / 5; + int temp = roundNearest(mV, 5); return temp; } @@ -109,9 +106,8 @@ int16_t diffVoltage = table[i + 1].mV - table[i].mV; int16_t diffValue = table[i + 1].value - table[i].value; - // TODO could do rounding here int16_t value = table[i].value + - ((int32_t)(mV - table[i].mV) * diffValue / diffVoltage); + roundNearest((int32_t)(mV - table[i].mV) * diffValue, diffVoltage); return value; }