diff --git a/keywords.txt b/keywords.txt index 9d3c62d..f224309 100644 --- a/keywords.txt +++ b/keywords.txt @@ -52,6 +52,10 @@ setDisplayDigit KEYWORD2 setDisplay KEYWORD2 clearDisplay KEYWORD2 setDisplayToString KEYWORD2 +setDefaultAlphaFont KEYWORD2 +getDefaultAlphaFont KEYWORD2 +setDefaultNumberFont KEYWORD2 +getDefaultNumberFont KEYWORD2 setLED KEYWORD2 setLEDs KEYWORD2 setRGBLED KEYWORD2 diff --git a/src/TM1628.h b/src/TM1628.h index ab7b773..45aa501 100644 --- a/src/TM1628.h +++ b/src/TM1628.h @@ -29,7 +29,7 @@ class TM1628 : public TM16xx virtual void clearDisplay(); /** Set an Ascii character on a specific location (overloaded for 15-segment display) */ - virtual void sendAsciiChar(byte pos, char c, bool dot, const byte font[] = TM16XX_FONT_DEFAULT); // public method to allow calling from TM16xxDisplay + virtual void sendAsciiChar(byte pos, char c, bool dot, const byte font[] = nullptr); // public method to allow calling from TM16xxDisplay // Set mapping array to be used when displaying segments // The array should contain _maxSegments bytes specifying the desired mapping diff --git a/src/TM1640Anode.h b/src/TM1640Anode.h index ed2b4a1..e1138dc 100644 --- a/src/TM1640Anode.h +++ b/src/TM1640Anode.h @@ -21,7 +21,7 @@ class TM1640Anode : public TM1640 TM1640Anode(byte dataPin, byte clockPin, byte numDigits=4, bool activateDisplay = true, byte intensity = 7); /** Set an Ascii character on a specific location (overloaded for 15-segment display) */ - virtual void sendAsciiChar(byte pos, char c, bool dot, const byte font[] = TM16XX_FONT_DEFAULT); // public method to allow calling from TM16xxDisplay + virtual void sendAsciiChar(byte pos, char c, bool dot, const byte font[] = nullptr); // public method to allow calling from TM16xxDisplay /** Set the segments at a specific position on or off */ virtual void setSegments(byte segments, byte position); // will duplicate G to G1/G2 in 15-segment diff --git a/src/TM16xx.cpp b/src/TM16xx.cpp index f668c77..8092440 100644 --- a/src/TM16xx.cpp +++ b/src/TM16xx.cpp @@ -19,14 +19,14 @@ along with this program. If not, see . #include "TM16xx.h" -TM16xx::TM16xx(byte dataPin, byte clockPin, byte strobePin, byte maxDisplays, byte nDigitsUsed, bool activateDisplay, byte intensity) +TM16xx::TM16xx(byte dataPin, byte clockPin, byte strobePin, byte maxDisplays, + byte nDigitsUsed, bool activateDisplay, byte intensity, + const byte defaultAlphaFont[95], const byte defaultNumberFont[16]) + : dataPin(dataPin), clockPin(clockPin), strobePin(strobePin), + _maxDisplays(maxDisplays), digits(nDigitsUsed), + defaultFontAlpha(defaultAlphaFont), defaultFontNum(defaultNumberFont) { - // DEPRECATED: activation, intensity (0-7) and display mode are no longer used by constructor. - this->dataPin = dataPin; - this->clockPin = clockPin; - this->strobePin = strobePin; - this->_maxDisplays = maxDisplays; - this->digits = nDigitsUsed; + // DEPRECATED: activation, intensity (0-7) and display mode are no longer used by constructor. pinMode(dataPin, OUTPUT); pinMode(clockPin, OUTPUT); @@ -79,8 +79,23 @@ void TM16xx::begin(bool activateDisplay, byte intensity) fBeginDone=true; clearDisplay(); setupDisplay(activateDisplay, intensity); -} - +} + +void TM16xx::setDefaultAlphaFont(const byte font[95]) +{ // Change the default font used to interpret printable ASCII characters. + // The font table should be defined as a byte array with at least 95 elements, + // one for each printable ASCII character in the range char(32) to char(126). + // See TM16xxFonts.h for the definition of the default font TM16XX_FONT_DEFAULT + defaultFontAlpha = font; +} + +void TM16xx::setDefaultNumberFont(const byte font[16]) +{ // Change the default font used to interpret (hexadecimal) digits. + // The font table should be defined as a byte array with at least 16 elements, + // one for each number 0-9 and letter A-F. + // See TM16xxFonts.h for the definition of the default font TM16XX_NUMBER_FONT + defaultFontNum = font; +} void TM16xx::setSegments(byte segments, byte position) { // set 8 leds on common grd as specified @@ -123,7 +138,8 @@ void TM16xx::sendAsciiChar(byte pos, char c, bool fDot, const byte font[]) // This method is also called by TM16xxDisplay.print to display characters // The base class uses the default 7-segment font to find the LED pattern. // Derived classes for multi-segment displays or alternate layout displays can override this method - sendChar(pos, pgm_read_byte_near(font+(c - 32)), fDot); + const byte *activeFont = font ? font : defaultFontAlpha; + sendChar(pos, pgm_read_byte_near(activeFont + (c - 32)), fDot); } void TM16xx::setDisplayFlipped(bool fFlipped) @@ -141,7 +157,8 @@ void TM16xx::setDisplayReversed(bool fReversed) void TM16xx::setDisplayDigit(byte digit, byte pos, bool dot, const byte numberFont[]) { - sendChar(pos, pgm_read_byte_near(numberFont + (digit & 0xF)), dot); + const byte *activeFont = numberFont ? numberFont : defaultFontNum; + sendChar(pos, pgm_read_byte_near(activeFont + (digit & 0xF)), dot); } void TM16xx::setDisplayToDecNumber(int nNumber, byte bDots, bool fLeadingZeros) // byte bDots=0, bool fLeadingZeros=true diff --git a/src/TM16xx.h b/src/TM16xx.h index 12e5a71..6c95937 100644 --- a/src/TM16xx.h +++ b/src/TM16xx.h @@ -59,10 +59,14 @@ class TM16xx public: /** * Instantiate a TM16xx module specifying data, clock and strobe pins (no strobe on some modules), - * maxDisplays - the maximum number of displays supported by the chip (as provided by derived chip specific class), - * nDigitsUsed - the number of digits used to display numbers or text, + * maxDisplays - the maximum number of displays supported by the chip (as provided by derived chip specific class), + * nDigitsUsed - the number of digits used to display numbers or text, + * defaultFont - the default font to use to interpret printable ASCII characters */ - TM16xx(byte dataPin, byte clockPin, byte strobePin, byte maxDisplays, byte nDigitsUsed, bool activateDisplay=true, byte intensity=7); + TM16xx(byte dataPin, byte clockPin, byte strobePin, byte maxDisplays, + byte nDigitsUsed, bool activateDisplay = true, byte intensity = 7, + const byte defaultAlphaFont[95] = TM16XX_FONT_DEFAULT, + const byte defaultNumberFont[16] = TM16XX_NUMBER_FONT); /** DEPRECATED: activation, intensity (0-7) and display mode are no longer used by constructor. */ /** Set the display (segments and LEDs) active or off and intensity (range from 0-7). */ @@ -80,6 +84,13 @@ class TM16xx /** Use explicit call in setup() or rely on implicit call by sendData(); calls setupDisplay() and clearDisplay() */ virtual void begin(bool activateDisplay=true, byte intensity=7); + /** Set a new default alphanumeric font */ + virtual void setDefaultAlphaFont(const byte font[95] = TM16XX_FONT_DEFAULT); + virtual byte *getDefaultAlphaFont() { return defaultFontAlpha; }; + /** Set a new default numeric font */ + virtual void setDefaultNumberFont(const byte font[16] = TM16XX_NUMBER_FONT); + virtual byte *getDefaultNumberFont() { return defaultFontNum; }; + /** Set segments of the display */ virtual void setSegments(byte segments, byte position); virtual void setSegments16(uint16_t segments, byte position); // some modules support more than 8 segments @@ -89,7 +100,7 @@ class TM16xx // /** Set a single display at pos (starting at 0) to a digit (left to right) */ - virtual void setDisplayDigit(byte digit, byte pos=0, bool dot=false, const byte numberFont[] = TM16XX_NUMBER_FONT); + virtual void setDisplayDigit(byte digit, byte pos=0, bool dot=false, const byte numberFont[] = nullptr); /** Set the display to a decimal number */ virtual void setDisplayToDecNumber(int nNumber, byte bDots=0, bool fLeadingZeros=true); @@ -100,12 +111,12 @@ class TM16xx virtual void setDisplay(const byte values[], byte size=8); /** Set the display to the string (defaults to built in 7-segment alphanumeric font) */ - virtual void setDisplayToString(const char* string, const word dots=0, const byte pos=0, const byte font[] = TM16XX_FONT_DEFAULT); + virtual void setDisplayToString(const char *string, const word dots = 0, const byte pos = 0, const byte font[] = nullptr); virtual void sendChar(byte pos, byte data, bool dot); // made public to allow calling from TM16xxDisplay virtual void setNumDigits(byte numDigitsUsed); // set number of digits used for alignment virtual byte getNumDigits(); // called by TM16xxDisplay to combine multiple modules - virtual void sendAsciiChar(byte pos, char c, bool dot, const byte font[] = TM16XX_FONT_DEFAULT); // made public to allow calling from TM16xxDisplay + virtual void sendAsciiChar(byte pos, char c, bool dot, const byte font[] = nullptr); // made public to allow calling from TM16xxDisplay // Key-scanning functions // Note: not all TM16xx chips support key-scanning and sizes are different per chip @@ -144,6 +155,8 @@ auto min(T x, U y) -> decltype(x>y ? x : y) byte _maxDisplays=2; // maximum number of digits (grids), chip-dependent byte _maxSegments=8; // maximum number of segments per display, chip-dependent + byte *defaultFontAlpha;// default font table for alphanumerics (printable ACSII) + byte *defaultFontNum; // default font table for (hexadecimal) digits bool flipped=false; // sets the flipped state of the display; bool reversed=false; // sets the reversed state of the display; bool fBeginDone=false; // for implicit begin checking; diff --git a/src/TM16xxDisplay.cpp b/src/TM16xxDisplay.cpp index bf39f3c..331aefc 100644 --- a/src/TM16xxDisplay.cpp +++ b/src/TM16xxDisplay.cpp @@ -159,9 +159,10 @@ void TM16xxDisplay::setDisplayToError() void TM16xxDisplay::setDisplayToHexNumber(unsigned long number, byte dots, bool leadingZeros, const byte numberFont[]) { + const byte *activeFont = numberFont ? numberFont : _pTM16xx->getDefaultNumberFont(); for (int nPos = 0; nPos < _nNumDigits; nPos++) { if (number > 0 || leadingZeros || nPos==0) { - sendCharAt(_nNumDigits - nPos - 1, pgm_read_byte_near(numberFont + (number & 0xF)), (dots & (1 << nPos)) != 0); + sendCharAt(_nNumDigits - nPos - 1, pgm_read_byte_near(activeFont + (number & 0xF)), (dots & (1 << nPos)) != 0); } else { sendCharAt(_nNumDigits - nPos - 1, 0, (dots & (1 << nPos)) != 0); // clearDisplayDigit } @@ -174,9 +175,10 @@ void TM16xxDisplay::setDisplayToDecNumberAt(unsigned long number, byte dots, byt if (number > 99999999L) { setDisplayToError(); // original code: limit to 8 digit numbers } else { + const byte *activeFont = numberFont ? numberFont : _pTM16xx->getDefaultNumberFont(); for (int nPos = 0; nPos < _nNumDigits - startingPos; nPos++) { if (number != 0 || nPos==0 || leadingZeros) { - sendCharAt(_nNumDigits - nPos - 1, pgm_read_byte_near(numberFont + (number % 10)), (dots & (1 << nPos)) != 0); + sendCharAt(_nNumDigits - nPos - 1, pgm_read_byte_near(activeFont + (number % 10)), (dots & (1 << nPos)) != 0); } else { sendCharAt(_nNumDigits - nPos - 1, 0, (dots & (1 << nPos)) != 0); // clearDisplayDigit } @@ -206,8 +208,9 @@ void TM16xxDisplay::setDisplayToSignedDecNumber(signed long number, byte dots, b void TM16xxDisplay::setDisplayToBinNumber(byte number, byte dots, const byte numberFont[]) { + const byte *activeFont = numberFont ? numberFont : _pTM16xx->getDefaultNumberFont(); for (int nPos = 0; nPos < _nNumDigits; nPos++) { - sendCharAt(_nNumDigits - nPos - 1, pgm_read_byte_near(numberFont + ((number & (1 << nPos)) == 0 ? 0 : 1)), (dots & (1 << nPos)) != 0); + sendCharAt(_nNumDigits - nPos - 1, pgm_read_byte_near(activeFont + ((number & (1 << nPos)) == 0 ? 0 : 1)), (dots & (1 << nPos)) != 0); } } diff --git a/src/TM16xxDisplay.h b/src/TM16xxDisplay.h index 1271826..18c6d8a 100644 --- a/src/TM16xxDisplay.h +++ b/src/TM16xxDisplay.h @@ -40,18 +40,18 @@ class TM16xxDisplay : public Print virtual void setDisplayFlipped(bool flipped); // Set the display to the String (defaults to built in font) - virtual void setDisplayToString(const char* string, const word dots=0, const byte pos=0, const byte font[] = TM16XX_FONT_DEFAULT); - virtual void setDisplayToString(String string, const word dots=0, const byte pos=0, const byte font[] = TM16XX_FONT_DEFAULT); + virtual void setDisplayToString(const char* string, const word dots=0, const byte pos=0, const byte font[] = nullptr); + virtual void setDisplayToString(String string, const word dots=0, const byte pos=0, const byte font[] = nullptr); virtual void setDisplayToError(); // Set the display to a unsigned hexadecimal number (with or without leading zeros) - void setDisplayToHexNumber(unsigned long number, byte dots, bool leadingZeros = true, const byte numberFont[] = TM16XX_NUMBER_FONT); + void setDisplayToHexNumber(unsigned long number, byte dots, bool leadingZeros = true, const byte numberFont[] = nullptr); // Set the display to a unsigned decimal number (with or without leading zeros) - void setDisplayToDecNumber(unsigned long number, byte dots, bool leadingZeros = true, const byte numberFont[] = TM16XX_NUMBER_FONT); + void setDisplayToDecNumber(unsigned long number, byte dots, bool leadingZeros = true, const byte numberFont[] = nullptr); // Set the display to a signed decimal number (with or without leading zeros) - void setDisplayToSignedDecNumber(signed long number, byte dots, bool leadingZeros = true, const byte numberFont[] = TM16XX_NUMBER_FONT); + void setDisplayToSignedDecNumber(signed long number, byte dots, bool leadingZeros = true, const byte numberFont[] = nullptr); // Set the display to a unsigned binary number - void setDisplayToBinNumber(byte number, byte dots, const byte numberFont[] = TM16XX_NUMBER_FONT); + void setDisplayToBinNumber(byte number, byte dots, const byte numberFont[] = nullptr); // support for the Print class void setCursor(int8_t nPos); // allows setting negative to support scrolled printing @@ -76,6 +76,6 @@ class TM16xxDisplay : public Print void setDisplayToDecNumberAt(unsigned long number, byte dots, byte startingPos, bool leadingZeros, const byte numberFont[]); //TM16xx *TM16xxDisplay::findModuleByPos(const byte nPosFind); void sendCharAt(const byte nPos, byte btData, bool fDot); - void sendAsciiCharAt(const byte nPosCombi, char c, bool fDot, const byte font[] = TM16XX_FONT_DEFAULT); + void sendAsciiCharAt(const byte nPosCombi, char c, bool fDot, const byte font[] = nullptr); }; #endif diff --git a/src/TM16xxIC.h b/src/TM16xxIC.h index 336dcf1..240aab8 100644 --- a/src/TM16xxIC.h +++ b/src/TM16xxIC.h @@ -162,7 +162,7 @@ class TM16xxIC : public TM16xx /** use alphanumeric display (yes/no) with or without segment map */ virtual void setAlphaNumeric(bool fAlpha=true, const byte *pMap=NULL); // const byte aMap[] /** Set an Ascii character on a specific location (overloaded for 15-segment display) */ - virtual void sendAsciiChar(byte pos, char c, bool dot, const byte font[] = TM16XX_FONT_DEFAULT); // public method to allow calling from TM16xxDisplay + virtual void sendAsciiChar(byte pos, char c, bool dot, const byte font[] = nullptr); // public method to allow calling from TM16xxDisplay /** Clear the display */ virtual void clearDisplay(); diff --git a/src/TMHT16K33.h b/src/TMHT16K33.h index b38b926..ad6fd5b 100644 --- a/src/TMHT16K33.h +++ b/src/TMHT16K33.h @@ -61,7 +61,7 @@ class TMHT16K33 : public TM16xx virtual void setSegments(byte segments, byte position); // will duplicate G to G1/G2 in 15-segment virtual void setSegments16(uint16_t segments, byte position); // some modules support more than 8 segments /** Set an Ascii character on a specific location (overloaded for 15-segment display) */ - virtual void sendAsciiChar(byte pos, char c, bool dot, const byte font[] = TM16XX_FONT_DEFAULT); // public method to allow calling from TM16xxDisplay + virtual void sendAsciiChar(byte pos, char c, bool dot, const byte font[] = nullptr); // public method to allow calling from TM16xxDisplay // Set mapping array to be used when displaying segments // The array should contain _maxSegments bytes specifying the desired mapping