From df757206a45b1162e281fa902a0d959287f2aa06 Mon Sep 17 00:00:00 2001 From: Ladyada Date: Thu, 9 Aug 2012 10:25:45 -0400 Subject: [PATCH] Float/Double neg # fixes by John Archie --- Adafruit_LEDBackpack.cpp | 164 +++++++++++++++------------------------ Adafruit_LEDBackpack.h | 9 ++- 2 files changed, 69 insertions(+), 104 deletions(-) diff --git a/Adafruit_LEDBackpack.cpp b/Adafruit_LEDBackpack.cpp index 4c3c650..d85eed5 100644 --- a/Adafruit_LEDBackpack.cpp +++ b/Adafruit_LEDBackpack.cpp @@ -23,19 +23,16 @@ #include "Adafruit_GFX.h" static const uint8_t numbertable[] = { - 0x3F /* 0 */, - 0x06 /* 1 */, - 0x5B /* 2 */, - 0x4F /* 3 */, - 0x66 /* 4 */, - 0x6D /* 5 */, + 0x3F, /* 0 */ + 0x06, /* 1 */ + 0x5B, /* 2 */ + 0x4F, /* 3 */ + 0x66, /* 4 */ + 0x6D, /* 5 */ 0x7D, /* 6 */ 0x07, /* 7 */ 0x7F, /* 8 */ 0x6F, /* 9 */ -}; - -const uint8_t alphatable[] = { 0x77, /* a */ 0x7C, /* b */ 0x39, /* C */ @@ -275,108 +272,73 @@ void Adafruit_7segment::writeDigitNum(uint8_t d, uint8_t num, boolean dot) { writeDigitRaw(d, numbertable[num] | (dot << 7)); } -void Adafruit_7segment::writeDigitAlpha(uint8_t d, uint8_t alph, boolean dot) { - if (d > 4) return; - - writeDigitRaw(d, alphatable[alph] | (dot << 7)); -} - void Adafruit_7segment::print(long n, int base) { - if (base == 0) { - write(n); - } else if (base == 10) { - if (n < 0) { - print('-'); - n = -n; - } - printNumber(n, 10); - } else { - printNumber(n, base); - } + printNumber(n, base); } - -void Adafruit_7segment::printNumber(unsigned long n, uint8_t base) +void Adafruit_7segment::printNumber(long n, uint8_t base) { - position = 0; + printFloat(n, 0, base); +} - unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars. - unsigned long i = 0; - - if (n == 0) { - writeDigitRaw(0, 0x0); - writeDigitRaw(1, 0x0); - writeDigitRaw(2, 0x0); - writeDigitRaw(3, 0x0); - writeDigitNum(4, 0); - return; - } - - while (n > 0) { - buf[i++] = n % base; - n /= base; +void Adafruit_7segment::printFloat(double n, uint8_t fracDigits, uint8_t base) +{ + uint8_t numericDigits = 4; // available digits on display + boolean isNegative = false; // true if the number is negative + + // is the number negative? + if(n < 0) { + isNegative = true; // need to draw sign later + --numericDigits; // the sign will take up one digit + n *= -1; // pretend the number is positive } - - // we have i digits - - if (i > 4) { // too big! - Serial.println("too long"); - writeDigitRaw(0, 0x40); - writeDigitRaw(1, 0x40); - writeDigitRaw(2, 0x00); - writeDigitRaw(3, 0x40); - writeDigitRaw(4, 0x40); - return; + + // calculate the factor required to shift all fractional digits + // into the integer part of the number + double toIntFactor = 1.0; + for(int i = 0; i < fracDigits; ++i) toIntFactor *= base; + + // create integer containing digits to display by applying + // shifting factor and rounding adjustment + uint32_t displayNumber = n * toIntFactor + 0.5; + + // calculate upper bound on displayNumber given + // available digits on display + uint32_t tooBig = 1; + for(int i = 0; i < numericDigits; ++i) tooBig *= base; + + // if displayNumber is too large, try fewer fractional digits + while(displayNumber >= tooBig) { + --fracDigits; + toIntFactor /= base; + displayNumber = n * toIntFactor + 0.5; } - //Serial.println(i); - - clear(); - - position = 4; - for (uint8_t n=0; n= 0) writeDigitRaw(displayPos--, 0x00); } - position = 0; } -void Adafruit_7segment::printFloat(double number, uint8_t digits) -{ - // Handle negative numbers - if (number < 0.0) - { - print('-'); - number = -number; +void Adafruit_7segment::printError(void) { + for(uint8_t i = 0; i < SEVENSEG_DIGITS; ++i) { + writeDigitRaw(i, (i == 2 ? 0x00 : 0x40)); } - - // Round correctly so that print(1.999, 2) prints as "2.00" - double rounding = 0.5; - for (uint8_t i=0; i 0) - print('.'); - - // Extract digits from the remainder one at a time - while (digits-- > 0) - { - remainder *= 10.0; - int toPrint = int(remainder); - print(toPrint); - remainder -= toPrint; - } } diff --git a/Adafruit_LEDBackpack.h b/Adafruit_LEDBackpack.h index 3c1da25..87bd3d2 100644 --- a/Adafruit_LEDBackpack.h +++ b/Adafruit_LEDBackpack.h @@ -45,6 +45,9 @@ #define HT16K33_CMD_BRIGHTNESS 0x0E +#define SEVENSEG_DIGITS 5 + + // this is the raw HT16K33 controller class Adafruit_LEDBackpack { public: @@ -110,10 +113,10 @@ class Adafruit_7segment : public Adafruit_LEDBackpack { void writeDigitRaw(uint8_t x, uint8_t bitmask); void writeDigitNum(uint8_t x, uint8_t num, boolean dot = false); - void writeDigitAlpha(uint8_t x, uint8_t num, boolean dot = false); void drawColon(boolean state); - void printNumber(unsigned long n, uint8_t base); - void printFloat(double number, uint8_t digits); + void printNumber(long, uint8_t = 2); + void printFloat(double, uint8_t = 2, uint8_t = DEC); + void printError(void); private: uint8_t position;