From 540ebbab251c25e7cfea6cedf82bea013bc782e9 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Tue, 3 Mar 2015 22:21:16 +0100 Subject: [PATCH] maybe this float stuff already works --- src/display.cpp | 105 +++++++++++++++++++++++++----------------------- 1 file changed, 55 insertions(+), 50 deletions(-) diff --git a/src/display.cpp b/src/display.cpp index ac7e201..0485f5f 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -59,54 +59,60 @@ void dispSetDigit(uint8_t digit, uint8_t value) { void dispSetFloat(uint8_t unit, float value) { - // if ((value < -99) || (value > 999)) { - // dispSetError(); - // } else { - // uint8_t neg = (value < 0) ? 1 : 0; - // value = abs(value); - // uint16_t preComma = (uint16_t)value; - // uint16_t postComma = round((value - (float)preComma) * 10); - // if (postComma == 10) { - // postComma = 0; - // preComma += 1; - // } - // if ((preComma == 0) && (postComma == 0)) { - // neg = false; - // } - // - // uint8_t digit1 = preComma / 100; - // uint8_t remVal = preComma - (digit1 * 100); - // uint8_t digit2 = remVal / 10; - // remVal = remVal - (digit2 * 10); - // uint8_t digit3 = remVal; - // - // if (digit1 == 0) { - // if (neg) { - // digit1 = 0x0a; - // } else { - // digit1 = 0x0f; - // } - // } - // - // if (digit1 > 0x09 && digit2 == 0) { - // if (neg) { - // digit2 = 0x0a; - // digit1 = 0x0f; - // } else { - // digit2 = 0x0f; - // } - // } - // - // digit3 |= 0x80; - // - // spiXfer16(0x0100 | digit1); - // spiXfer16(0x0200 | digit2); - // spiXfer16(0x0300 | digit3); - // - // spiXfer16(0x0400 | postComma); - // - // - // } + if ((value < -99) || (value > 999)) { + dispSetError(); + } else { + bool neg = value < 0; + value = (value < 0) ? -1 * value : value; + uint16_t preComma = (uint16_t)value; + uint16_t postComma = round((value - (float)preComma) * 10); + if (postComma == 10) { + postComma = 0; + preComma += 1; + } + if ((preComma == 0) && (postComma == 0)) { + neg = false; + } + + uint8_t digit4 = 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; + + if (digit3 == 0) { + if (neg) { + digit3 = 0x0a; + } else { + digit3 = 0x0f; + } + } + + if (digit3 > 0x09 && digit2 == 0) { + if (neg) { + digit2 = 0x0a; + digit3 = 0x0f; + } else { + digit2 = 0x0f; + } + } + + // digit3 |= 0x80; + + if (unit == 0) { + spiXfer16(0x0100 | digit1); + spiXfer16(0x0200 | digit2); + spiXfer16(0x0300 | digit3); + spiXfer16(0x0400 | postComma); + } else { + spiXfer16(0x0500 | digit1); + spiXfer16(0x0600 | digit2); + spiXfer16(0x0700 | digit3); + spiXfer16(0x0800 | postComma); + } + } } void dispSet(uint8_t unit, int16_t value) { @@ -116,9 +122,8 @@ void dispSet(uint8_t unit, int16_t value) { if ((value < -999) || (value > 9999)) { dispSetError(); } else { - uint8_t neg = (value < 0) ? 1 : 0; + bool neg = value < 0; value = (value < 0) ? -1 * value : value; - // value = abs(value); uint8_t digit3 = value / 1000; uint16_t remVal = value - (digit3 * 1000);