diff --git a/lambda-test/lambda-test.c b/lambda-test/lambda-test.c index 850bf24..b53b6bd 100644 --- a/lambda-test/lambda-test.c +++ b/lambda-test/lambda-test.c @@ -11,6 +11,7 @@ #include #include "USART.h" #include "adc.h" +#include "integers.h" #include "sensors.h" #include "avrjunit.h" @@ -19,7 +20,12 @@ {10, 10} }; +/* Module adc */ + uint8_t testGetVoltage(void) { + setupADC(); + setupSleepMode(); + // enable pull-up resistor so the measured voltage // should be (close to?) to AREF PORTC |= (1 << PC1); @@ -35,6 +41,130 @@ return mV > 4950; } +/* Module integers */ + +uint8_t testDivRoundNearest(void) { + int32_t round = 0; + + round = divRoundNearest(4, 2); + if (round != 2) return 0; + + round = divRoundNearest(5, 2); + if (round != 3) return 0; + + round = divRoundNearest(10, 3); + if (round != 3) return 0; + + return 1; +} + +uint8_t testDivRoundNearestNumNeg(void) { + int32_t round = 0; + + round = divRoundNearest(-4, 2); + if (round != -2) return 0; + + round = divRoundNearest(-5, 2); + if (round != -3) return 0; + + round = divRoundNearest(-10, 3); + if (round != -3) return 0; + + return 1; +} + +uint8_t testDivRoundNearestDenNeg(void) { + int32_t round = 0; + + round = divRoundNearest(4, -2); + if (round != -2) return 0; + + round = divRoundNearest(5, -2); + if (round != -3) return 0; + + round = divRoundNearest(10, -3); + if (round != -3) return 0; + + return 1; +} + +uint8_t testDivRoundNearestBothNeg(void) { + int32_t round = 0; + + round = divRoundNearest(-4, -2); + if (round != 2) return 0; + + round = divRoundNearest(-5, -2); + if (round != 3) return 0; + + round = divRoundNearest(-10, -3); + if (round != 3) return 0; + + return 1; +} + +uint8_t testDivRoundUp(void) { + int32_t round = 0; + + round = divRoundUp(4, 2); + if (round != 2) return 0; + + round = divRoundUp(5, 2); + if (round != 3) return 0; + + round = divRoundUp(10, 3); + if (round != 4) return 0; + + return 1; +} + +uint8_t testDivRoundUpNumNeg(void) { + int32_t round = 0; + + round = divRoundUp(-4, 2); + if (round != -2) return 0; + + round = divRoundUp(-5, 2); + if (round != -3) return 0; + + round = divRoundUp(-10, 3); + if (round != -4) return 0; + + return 1; +} + +uint8_t testDivRoundUpDenNeg(void) { + int32_t round = 0; + + round = divRoundUp(4, -2); + if (round != -2) return 0; + + round = divRoundUp(5, -2); + if (round != -3) return 0; + + round = divRoundUp(10, -3); + if (round != -4) return 0; + + return 1; +} + +uint8_t testDivRoundUpBothNeg(void) { + int32_t round = 0; + + round = divRoundUp(-4, -2); + if (round != 2) return 0; + + round = divRoundUp(-5, -2); + if (round != 3) return 0; + + round = divRoundUp(-10, -3); + if (round != 4) return 0; + + return 1; +} + +/* Module sensors */ + uint8_t testAverage(void) { int16_t avg = 0; avg = average(8, avg, 4); @@ -111,6 +241,14 @@ test tests[] = { {"adc", "testGetVoltage", testGetVoltage}, + {"integers", "testDivRoundNearest", testDivRoundNearest}, + {"integers", "testDivRoundNearestNumNeg", testDivRoundNearestNumNeg}, + {"integers", "testDivRoundNearestDenNeg", testDivRoundNearestDenNeg}, + {"integers", "testDivRoundNearestBothNeg", testDivRoundNearestBothNeg}, + {"integers", "testDivRoundUp", testDivRoundUp}, + {"integers", "testDivRoundUpNumNeg", testDivRoundUpNumNeg}, + {"integers", "testDivRoundUpDenNeg", testDivRoundUpDenNeg}, + {"integers", "testDivRoundUpBothNeg", testDivRoundUpBothNeg}, {"sensors", "testAverage", testAverage}, {"sensors", "testToLambdaValue", testToLambdaValue}, {"sensors", "testToLambdaInter", testToLambdaInter}, @@ -124,8 +262,6 @@ }; int main(void) { - setupADC(); - setupSleepMode(); initUSART(); uint16_t count = sizeof(tests) / sizeof(tests[0]); diff --git a/lambda/integers.c b/lambda/integers.c index 00984a7..19b74fe 100644 --- a/lambda/integers.c +++ b/lambda/integers.c @@ -7,15 +7,15 @@ #include #include "integers.h" -int32_t roundNearest(int32_t num, int32_t den) { +int32_t divRoundNearest(int32_t num, int32_t den) { return ((num < 0) ^ (den < 0)) ? ((num - den / 2) / den) : ((num + den / 2) / den); } -int32_t roundUp(int32_t num, int32_t den) { +int32_t divRoundUp(int32_t num, int32_t den) { return ((num < 0) ^ (den < 0)) ? - ((num - den + 1) / den) : - ((num + den - 1) / den); + ((num - ((den < 0) ? den + 1 : den - 1)) / den) : + ((num + ((den < 0) ? den + 1 : den - 1)) / den); } diff --git a/lambda/integers.h b/lambda/integers.h index feaf115..3e5227c 100644 --- a/lambda/integers.h +++ b/lambda/integers.h @@ -10,13 +10,13 @@ * rounds to the nearest int and returns it. * http://stackoverflow.com/a/18067292/709426 */ -int32_t roundNearest(int32_t num, int32_t den); +int32_t divRoundNearest(int32_t num, int32_t den); /** * Divides the given numerator by the given denominator, * rounds up and returns it. */ -int32_t roundUp(int32_t num, int32_t den); +int32_t divRoundUp(int32_t num, int32_t den); // TODO function to divide and return result as decimal string? // char* toDecimalString(int32_t num, int32_t den); diff --git a/lambda/sensors.c b/lambda/sensors.c index 0cef3ea..8f8b745 100644 --- a/lambda/sensors.c +++ b/lambda/sensors.c @@ -97,11 +97,11 @@ } int16_t average(int16_t value, int16_t average, uint8_t weight) { - return roundUp(value + (average * weight), weight + 1); + return divRoundUp(value + (average * weight), weight + 1); } int16_t toTempI(int16_t mV) { - int temp = roundNearest(mV, 5); + int temp = divRoundNearest(mV, 5); return temp; } @@ -137,7 +137,7 @@ int16_t diffVoltage = table[i + 1].mV - table[i].mV; int16_t diffValue = table[i + 1].value - table[i].value; int16_t value = table[i].value + - roundNearest((int32_t)(mV - table[i].mV) * diffValue, diffVoltage); + divRoundNearest((int32_t)(mV - table[i].mV) * diffValue, diffVoltage); return value; }