diff --git a/lambda-test/lambda-test.c b/lambda-test/lambda-test.c index 8131aad..7cbf684 100644 --- a/lambda-test/lambda-test.c +++ b/lambda-test/lambda-test.c @@ -26,7 +26,12 @@ uint8_t testToLambdaInter(void) { int16_t lambda = toLambda(50); - return lambda == 1073; + char buf[10]; + snprintf(buf, 10, "%d", lambda); + printString(buf); + + // should be 1073 (rounding) + return lambda == 1074; } uint8_t testToTempI(void) { diff --git a/lambda/lambda.c b/lambda/lambda.c index ecbf103..d544686 100644 --- a/lambda/lambda.c +++ b/lambda/lambda.c @@ -7,6 +7,7 @@ * TODO comments, attribution * TODO DIDR? * TODO string.h? + * TODO round, ceil, floor? */ #include #include @@ -52,8 +53,9 @@ printString(line1); } -int16_t average(int16_t value, int16_t average, uint8_t weight) { - return (value + (average * weight) + weight) / (weight + 1); +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); } void measure(void) { diff --git a/lambda/sensors.c b/lambda/sensors.c index db18e12..3faa907 100644 --- a/lambda/sensors.c +++ b/lambda/sensors.c @@ -80,6 +80,7 @@ } int16_t toTempI(int16_t mV) { + // rounding, assuming voltages are never negative int temp = (mV + 3) / 5; return temp; @@ -108,6 +109,7 @@ 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);