From 2c722493c14234abc07184120b45f3cabae8e3c5 Mon Sep 17 00:00:00 2001 From: "rjbatista@gmail.com" Date: Sun, 4 Dec 2011 00:20:44 +0000 Subject: [PATCH] Support for inverted module --- CHANGES.txt | 5 ++-- InvertedTM1638.cpp | 61 +++++++++++++++++++++++++++++++++++++ InvertedTM1638.h | 45 ++++++++++++++++++++++++++++ README.txt | 3 +- TM1638.cpp | 75 +++++++++++++++++++++++++++------------------- TM1638.h | 23 +++++++++----- TM1638Fonts.h | 8 ++--- keywords.txt | 2 ++ 8 files changed, 177 insertions(+), 45 deletions(-) create mode 100644 InvertedTM1638.cpp create mode 100644 InvertedTM1638.h diff --git a/CHANGES.txt b/CHANGES.txt index afa0a94..9473d64 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ -- Support for Arduino IDE 1.0 +- Support for inverted module; +- Support for Arduino IDE 1.0. Version 1.4.0 @@ -7,7 +8,7 @@ Version 1.4.0 Version 1.3.2 -- ISSUE #5: Correction of string library import +- ISSUE #5: Correction of string library import. Version 1.3.1 diff --git a/InvertedTM1638.cpp b/InvertedTM1638.cpp new file mode 100644 index 0000000..38b197a --- /dev/null +++ b/InvertedTM1638.cpp @@ -0,0 +1,61 @@ +/* +InvertedTM1638.cpp - Library implementation for inverted TM1638. + +Copyright (C) 2011 Ricardo Batista (rjbatista gmail com) + +This program is free software: you can redistribute it and/or modify +it under the terms of the version 3 GNU General Public License as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#if defined(ARDUINO) && ARDUINO >= 100 + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +#include "InvertedTM1638.h" + +InvertedTM1638::InvertedTM1638(byte dataPin, byte clockPin, byte strobePin, boolean activateDisplay, + byte intensity) : TM1638(dataPin, clockPin, strobePin, activateDisplay, intensity) +{ + // nothing to do +} + +void InvertedTM1638::setLED(byte color, byte pos) +{ + sendData(((7 - pos) << 1) + 1, color); +} + +byte InvertedTM1638::getButtons() +{ + byte buttons = TM1638::getButtons(); + + // swap each other + buttons = (buttons & 0b01010101) << 1 | (buttons & 0b10101010) >> 1; + + // swap each pair + buttons = (buttons & 0b00110011) << 2 | (buttons & 0b11001100) >> 2; + + // swap each quad + buttons = (buttons & 0b00001111) << 4 | (buttons & 0b11110000) >> 4; + + return buttons; +} + +void InvertedTM1638::sendChar(byte pos, byte data, boolean dot) +{ + byte n = data; + + n = n & 128 | n & 64 | (n & 4) << 3 | (n & 2) << 3 | (n & 1) << 3 | (n & 32) >> 3 | (n & 16) >> 3 | (n & 8) >> 3; + + TM1638::sendChar(7 - pos, n, dot); +} diff --git a/InvertedTM1638.h b/InvertedTM1638.h new file mode 100644 index 0000000..8b32b4f --- /dev/null +++ b/InvertedTM1638.h @@ -0,0 +1,45 @@ +/* +InvertedTM1638.h - Library for an inverted TM1638. + +Copyright (C) 2011 Ricardo Batista + +This program is free software: you can redistribute it and/or modify +it under the terms of the version 3 GNU General Public License as +published by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#ifndef InvertedTM1638_h +#define InvertedTM1638_h + +#if defined(ARDUINO) && ARDUINO >= 100 + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +#include "TM1638.h" + +class InvertedTM1638 : public TM1638 +{ + public: + /** Instantiate an inverted tm1638 module specifying the display state, the starting intensity (0-7) data, clock and stobe pins. */ + InvertedTM1638(byte dataPin, byte clockPin, byte strobePin, boolean activateDisplay = true, byte intensity = 7); + + /** Set the LED at pos to color (TM1638_COLOR_RED, TM1638_COLOR_GREEN or both) */ + virtual void setLED(byte color, byte pos); + /** Returns the pressed buttons as a bit set (left to right). */ + virtual byte getButtons(); + + protected: + virtual void sendChar(byte pos, byte data, boolean dot); +}; + +#endif diff --git a/README.txt b/README.txt index 1a3165e..8d61715 100644 --- a/README.txt +++ b/README.txt @@ -11,7 +11,8 @@ Includes: - Support for multiple chained tm1638; - Reading simultaneous button presses; - Support for dimming the display and LEDs; -- Support for writing text. +- Support for writing text; +- Support for module in inverted position. See: TM1638 Display/LED module at http://www.dealextreme.com/p/81873?r=68099021 diff --git a/TM1638.cpp b/TM1638.cpp index dcce9c7..03e017a 100644 --- a/TM1638.cpp +++ b/TM1638.cpp @@ -1,5 +1,5 @@ /* -TM1638.h - Library for TM1638. +TM1638.cpp - Library implementation for TM1638. Copyright (C) 2011 Ricardo Batista (rjbatista gmail com) @@ -49,65 +49,76 @@ TM1638::TM1638(byte dataPin, byte clockPin, byte strobePin, boolean activateDisp digitalWrite(strobePin, HIGH); } +TM1638::~TM1638() +{ + // nothing to do +} + void TM1638::setupDisplay(boolean active, byte intensity) { sendCommand(0x80 | (active ? 8 : 0) | min(7, intensity)); } - -void TM1638::setDisplayToHexNumber(unsigned long number, byte dots, boolean leadingZeros) +void TM1638::setDisplayToHexNumber(unsigned long number, byte dots, boolean leadingZeros, + const byte numberFont[]) { for (int i = 0; i < 8; i++) { - if (!leadingZeros && number == 0) { - clearDisplayDigit(7 - i, (dots & (1 << i)) != 0); - } else { - setDisplayDigit(number & 0xF, 7 - i, (dots & (1 << i)) != 0); - number >>= 4; + if (!leadingZeros && number == 0) { + clearDisplayDigit(7 - i, (dots & (1 << i)) != 0); + } else { + setDisplayDigit(number & 0xF, 7 - i, (dots & (1 << i)) != 0, numberFont); + number >>= 4; } } } -void TM1638::setDisplayToDecNumber(unsigned long number, byte dots, boolean leadingZeros) +void TM1638::setDisplayToDecNumber(unsigned long number, byte dots, boolean leadingZeros, + const byte numberFont[]) { if (number > 99999999L) { - setDisplay(ERROR); + setDisplayToError(); } else { for (int i = 0; i < 8; i++) { if (number != 0) { - setDisplayDigit(number % 10, 7 - i, (dots & (1 << i)) != 0); + setDisplayDigit(number % 10, 7 - i, (dots & (1 << i)) != 0, numberFont); number /= 10; } else { - if (leadingZeros) { - setDisplayDigit(0, 7 - i, (dots & (1 << i)) != 0); - } else { - clearDisplayDigit(7 - i, (dots & (1 << i)) != 0); - } + if (leadingZeros) { + setDisplayDigit(0, 7 - i, (dots & (1 << i)) != 0, numberFont); + } else { + clearDisplayDigit(7 - i, (dots & (1 << i)) != 0); + } } } } } -void TM1638::setDisplayToBinNumber(byte number, byte dots) +void TM1638::setDisplayToBinNumber(byte number, byte dots, const byte numberFont[]) { for (int i = 0; i < 8; i++) { - setDisplayDigit((number & (1 << i)) == 0 ? 0 : 1, 7 - i, (dots & (1 << i)) != 0); + setDisplayDigit((number & (1 << i)) == 0 ? 0 : 1, 7 - i, (dots & (1 << i)) != 0, numberFont); } } -void TM1638::setDisplayDigit(byte digit, byte pos, boolean dot) +void TM1638::setDisplayDigit(byte digit, byte pos, boolean dot, const byte numberFont[]) { - sendData(pos << 1, NUMBER_DATA[digit & 0xF] | (dot ? 0b10000000 : 0)); + sendChar(pos, numberFont[digit & 0xF], dot); +} + +void TM1638::setDisplayToError() +{ + setDisplay(ERROR_DATA); } void TM1638::clearDisplayDigit(byte pos, boolean dot) { - sendData(pos << 1, (dot ? 0b10000000 : 0)); + sendChar(pos, 0, dot); } void TM1638::setDisplay(const byte values[]) { for (int i = 0; i < 8; i++) { - sendData(i << 1, values[i]); + sendChar(i, values[i], 0); } } @@ -121,7 +132,7 @@ void TM1638::clearDisplay() void TM1638::setDisplayToString(const char* string, const byte dots, const byte font[]) { for (int i = 0; i < 8; i++) { - sendData(i << 1, font[string[i] - 32] | ((dots & (1 << (7 - i))) != 0 ? 0b10000000 : 0)); + sendChar(i, font[string[i] - 32], dots & (1 << (7 - i))); } } @@ -131,9 +142,9 @@ void TM1638::setDisplayToString(const String string, const byte dots, const byte for (int i = 0; i < 8; i++) { if (i < stringLength) { - sendData(i << 1, font[string.charAt(i) - 32] | ((dots & (1 << (7 - i))) != 0 ? 0b10000000 : 0)); + sendChar(i, font[string.charAt(i) - 32], dots & (1 << (7 - i))); } else { - sendData(i << 1, 0); + sendChar(i, 0, dots & (1 << (7 - i))); } } } @@ -146,17 +157,17 @@ void TM1638::setLED(byte color, byte pos) void TM1638::setLEDs(word leds) { for (int i = 0; i < 8; i++) { - byte val = 0; + byte color = 0; if (leds & (1 << i)) { - val |= 1; + color |= TM1638_COLOR_RED; } if (leds & (1 << (i + 8))) { - val |= 2; + color |= TM1638_COLOR_GREEN; } - sendData((i << 1) + 1, val); + setLED(color, i); } } @@ -174,6 +185,11 @@ byte TM1638::getButtons(void) return keys; } +void TM1638::sendChar(byte pos, byte data, boolean dot) +{ + sendData(pos << 1, data | (dot ? 0b10000000 : 0)); +} + void TM1638::sendCommand(byte cmd) { digitalWrite(strobePin, LOW); @@ -226,4 +242,3 @@ byte TM1638::receive() return temp; } - diff --git a/TM1638.h b/TM1638.h index 9132753..9e60f2c 100644 --- a/TM1638.h +++ b/TM1638.h @@ -35,19 +35,26 @@ class TM1638 public: /** Instantiate a tm1638 module specifying the display state, the starting intensity (0-7) data, clock and stobe pins. */ TM1638(byte dataPin, byte clockPin, byte strobePin, boolean activateDisplay = true, byte intensity = 7); + virtual ~TM1638(); /** Set the display (segments and LEDs) active or off and intensity (range from 0-7). */ void setupDisplay(boolean active, byte intensity); /** Set the display to a unsigned hexadecimal number (with or without leading zeros) */ - void setDisplayToHexNumber(unsigned long number, byte dots, boolean leadingZeros = true); + void setDisplayToHexNumber(unsigned long number, byte dots, boolean leadingZeros = true, + const byte numberFont[] = NUMBER_FONT); /** Set the display to a unsigned decimal number (with or without leading zeros) */ - void setDisplayToDecNumber(unsigned long number, byte dots, boolean leadingZeros = true); + void setDisplayToDecNumber(unsigned long number, byte dots, boolean leadingZeros = true, + const byte numberFont[] = NUMBER_FONT); /** Set the display to a unsigned binary number */ - void setDisplayToBinNumber(byte number, byte dots); + void setDisplayToBinNumber(byte number, byte dots, + const byte numberFont[] = NUMBER_FONT); /** Set a single display at pos (starting at 0) to a digit (left to right) */ - void setDisplayDigit(byte digit, byte pos, boolean dot); - /** Clear a single display at pos (starting at 0, left to right) */ + void setDisplayDigit(byte digit, byte pos, boolean dot, + const byte numberFont[] = NUMBER_FONT); + /** Set the display to an error message */ + void setDisplayToError(); + /** Clear a single display at pos (starting at 0, left to right) */ void clearDisplayDigit(byte pos, boolean dot); /** Set the display to the 8 values (left to right) */ void setDisplay(const byte values[]); @@ -59,14 +66,16 @@ class TM1638 void setDisplayToString(String string, const byte dots = 0, const byte font[] = FONT_DEFAULT); /** Set the LED at pos to color (TM1638_COLOR_RED, TM1638_COLOR_GREEN or both) */ - void setLED(byte color, byte pos); + virtual void setLED(byte color, byte pos); /** Set the LEDs. MSB byte for the green LEDs, LSB for the red LEDs */ void setLEDs(word led); /** Returns the pressed buttons as a bit set (left to right). */ - byte getButtons(); + virtual byte getButtons(); protected: + virtual void sendChar(byte pos, byte data, boolean dot); + void sendCommand(byte led); void sendData(byte add, byte data); void send(byte data); diff --git a/TM1638Fonts.h b/TM1638Fonts.h index d79edda..bb9fa9d 100644 --- a/TM1638Fonts.h +++ b/TM1638Fonts.h @@ -1,10 +1,8 @@ /* -TM1638.h - Library for TM1638. +TM1638Fonts.h - Font definition for TM1638. Copyright (C) 2011 Ricardo Batista (rjbatista gmail com) -Based on a sketch by: Martin Hubacek (http://www.martinhubacek.cz) - This program is free software: you can redistribute it and/or modify it under the terms of the version 3 GNU General Public License as published by the Free Software Foundation. @@ -33,7 +31,7 @@ The bits are displayed by mapping bellow #define TM1638Fonts_h // definition for standard hexadecimal numbers -const byte NUMBER_DATA[] = { +const byte NUMBER_FONT[] = { 0b00111111, // 0 0b00000110, // 1 0b01011011, // 2 @@ -53,7 +51,7 @@ const byte NUMBER_DATA[] = { }; // definition for error -const byte ERROR[] = { +const byte ERROR_DATA[] = { 0b01111001, // E 0b01010000, // r 0b01010000, // r diff --git a/keywords.txt b/keywords.txt index 620dcb9..42d2a9d 100644 --- a/keywords.txt +++ b/keywords.txt @@ -7,6 +7,7 @@ ####################################### TM1638 KEYWORD1 +InvertedTM1638 KEYWORD1 ####################################### # Methods and Functions (KEYWORD2) @@ -25,6 +26,7 @@ setLEDs KEYWORD2 getButtons KEYWORD2 sendCommand KEYWORD2 sendData KEYWORD2 +sendChar KEYWORD2 send KEYWORD2 receive KEYWORD2