From 163c788a4517d2901300d7f60cdd4d8da9ea38ea Mon Sep 17 00:00:00 2001 From: hg Date: Wed, 4 Mar 2015 20:30:46 +0100 Subject: [PATCH] round --- src/display.cpp | 37 +++++++++++++++++++------------------ src/main.cpp | 2 +- src/utils.cpp | 7 +++++++ src/utils.h | 1 + 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/display.cpp b/src/display.cpp index 0d54842..28c318e 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -1,6 +1,7 @@ #include "mySpi.h" #include "display.h" +#include "utils.h" @@ -65,7 +66,7 @@ void dispSetFloat(uint8_t unit, float value) { bool neg = value < 0; value = (value < 0) ? -1 * value : value; uint16_t preComma = (uint16_t)value; - uint16_t postComma = round((value - (float)preComma) * 10); + uint16_t postComma = round16((value - (float)preComma) * 10); if (postComma == 10) { postComma = 0; preComma += 1; @@ -74,32 +75,32 @@ void dispSetFloat(uint8_t unit, float value) { neg = false; } - uint8_t digit4 = postComma; + uint8_t digit1 = postComma; - uint8_t digit3 = preComma / 100; - uint8_t remVal = preComma - (digit3 * 100); - uint8_t digit2 = remVal / 10; - remVal = remVal - (digit2 * 10); - uint8_t digit1 = remVal; + uint8_t digit4 = preComma / 100; + uint8_t remVal = preComma - (digit4 * 100); + uint8_t digit3 = remVal / 10; + remVal = remVal - (digit3 * 10); + uint8_t digit2 = remVal; - if (digit3 == 0) { + if (digit4 == 0) { + if (neg) { + digit4 = 0x0a; + } else { + digit4 = 0x0f; + } + } + + if (digit4 > 0x09 && digit3 == 0) { if (neg) { digit3 = 0x0a; + digit4 = 0x0f; } else { digit3 = 0x0f; } } - if (digit3 > 0x09 && digit2 == 0) { - if (neg) { - digit2 = 0x0a; - digit3 = 0x0f; - } else { - digit2 = 0x0f; - } - } - - // digit3 |= 0x80; + digit2 |= 0x80; if (unit == 0) { spiXfer16(0x0100 | digit1); diff --git a/src/main.cpp b/src/main.cpp index 97fb3a3..e57eb2d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,7 +35,7 @@ int main() { spiInit(); dispInit(); - dispSet(0, -12); + dispSetFloat(0, -12.0); dispSet(1, -990); __enable_interrupt(); diff --git a/src/utils.cpp b/src/utils.cpp index 6e8d073..34eb253 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -7,6 +7,7 @@ +#include #include "utils.h" @@ -31,3 +32,9 @@ int32_t minmax(int32_t lowerBound, int32_t value, int32_t upperBound){ } +int16_t round16(float x) { + if (x < 0.0) + return (int16_t)(x - 0.5); + else + return (int16_t)(x + 0.5); +} diff --git a/src/utils.h b/src/utils.h index 2348019..a23e9f5 100644 --- a/src/utils.h +++ b/src/utils.h @@ -14,6 +14,7 @@ int32_t max(int32_t a, int32_t b); int32_t min(int32_t a, int32_t b); int32_t minmax(int32_t lowerBound, int32_t value, int32_t upperBound); +int16_t round16(float x);