41 Commits

Author SHA1 Message Date
rjbatista@gmail.com
80f687aff6 Version 2.1.3 2012-11-06 22:01:32 +00:00
rjbatista@gmail.com
00cd075f69 2012-11-06 22:00:56 +00:00
rjbatista@gmail.com
6b4cfae6e5 2012-11-06 21:57:35 +00:00
rjbatista@gmail.com
1508d99abb Version 2.1.2 2012-06-11 21:38:49 +00:00
rjbatista@gmail.com
b779a65eb9 ISSUE #22: corrected setDisplayDigit repeatedly with the dot set to true 2012-06-11 21:16:15 +00:00
rjbatista@gmail.com
7a94b31533 ISSUE #24: Correction on setDisplayToDecNumber bug (thanks to hbx3485); 2012-06-11 21:07:45 +00:00
rjbatista@gmail.com
e4a7aad4d7 ISSUE #24: Correction on setDisplayToDecNumber bug (thanks hbx3485) 2012-06-11 21:05:16 +00:00
rjbatista@gmail.com
5756b7ae7d 2012-03-13 00:05:08 +00:00
rjbatista@gmail.com
da0b7ec9e2 Version 2.1.1 2012-03-12 23:59:00 +00:00
rjbatista@gmail.com
8ba4623718 ISSUE #21: Problems on clearDisplay and setupDisplay 2012-03-12 23:58:28 +00:00
rjbatista@gmail.com
cb437405ea Version 2.1.0 2012-02-20 22:18:16 +00:00
rjbatista@gmail.com
f52570e011 ISSUE #20: Support for negative decimal numbers 2012-02-20 22:17:47 +00:00
rjbatista@gmail.com
8b1fda292d ISSUE #19: Corrected setDisplayToString dot bug 2012-02-20 21:29:32 +00:00
rjbatista@gmail.com
5e2afcdaeb Version 2.0.1 2012-01-18 00:02:19 +00:00
rjbatista@gmail.com
bdee0e3872 ISSUE #15: Backwards compatibility with previous Arduino IDE (pure virtual functions were not supported) 2012-01-17 22:52:47 +00:00
rjbatista@gmail.com
fb4924cc0a ISSUE #18: Corrected problem with setLEDs() caused by changes in the previous version 2012-01-17 22:19:00 +00:00
rjbatista@gmail.com
ca9bc6f3bb 2012-01-11 02:14:18 +00:00
rjbatista@gmail.com
749c559f05 Version 2.0.0 2012-01-11 01:45:17 +00:00
rjbatista@gmail.com
e4c0ca275f Support for the TM1640 2012-01-11 01:40:58 +00:00
rjbatista@gmail.com
c297ea9e9f Restructuring for supporting other modules 2012-01-11 01:39:34 +00:00
rjbatista@gmail.com
b32993401f Version 1.6.0 2011-12-15 23:02:39 +00:00
rjbatista@gmail.com
bb28428a44 ISSUE #10: Support for starting position on setDisplayToString() methods 2011-12-15 23:02:04 +00:00
rjbatista@gmail.com
cff5d047bf Version 1.5.2 2011-12-05 17:09:39 +00:00
rjbatista@gmail.com
eb4538e162 Backwards compatibility with previous Arduino IDE 2011-12-05 17:06:32 +00:00
rjbatista@gmail.com
a8bcd661c8 Version 1.5.1 2011-12-04 18:40:01 +00:00
rjbatista@gmail.com
7260c88f7d ISSUE #7: New character data in default font for '*' 2011-12-04 18:31:51 +00:00
rjbatista@gmail.com
7c6678133c Minor optimization 2011-12-04 18:29:34 +00:00
rjbatista@gmail.com
875235aa15 Version 1.5.0 2011-12-04 01:27:24 +00:00
rjbatista@gmail.com
dcf48adb26 New functions example 2011-12-04 01:26:10 +00:00
rjbatista@gmail.com
2c722493c1 Support for inverted module 2011-12-04 00:20:44 +00:00
rjbatista@gmail.com
39fd21d26f Support for Arduino IDE 1.0 2011-12-03 18:34:24 +00:00
rjbatista@gmail.com
e582d25e84 2011-11-14 21:10:15 +00:00
rjbatista@gmail.com
1f5d4b5d8d 2011-11-14 21:05:53 +00:00
rjbatista@gmail.com
8cd887a567 Support for specifying dots on setDisplayToString 2011-11-14 17:41:12 +00:00
rjbatista@gmail.com
51c24bfaee Version 1.3.2 2011-11-14 11:20:19 +00:00
rjbatista@gmail.com
b6a17f5989 ISSUE #5: Correction of string library import 2011-11-14 11:18:17 +00:00
rjbatista@gmail.com
19673f8650 2011-10-28 23:31:23 +00:00
rjbatista@gmail.com
2def3b9779 2011-10-28 22:43:52 +00:00
rjbatista@gmail.com
81aa413f28 Changed pins to match schematics on project site 2011-10-28 22:43:32 +00:00
rjbatista@gmail.com
320ef7d54d added support for String Object 2011-10-19 23:57:43 +00:00
rjbatista@gmail.com
e3075c02e2 added support for removing leading zeros in displaying numbers 2011-10-19 23:43:58 +00:00
17 changed files with 789 additions and 180 deletions

77
CHANGES.txt Normal file
View File

@@ -0,0 +1,77 @@
Version 2.1.3
- ISSUE #26: Added a define TM1638_COLOR_NONE for clarity when clearing a single LED.
Version 2.1.2
- ISSUE #22: Corrected setDisplayDigit repeatedly with the dot set to true;
- ISSUE #24: Correction on setDisplayToDecNumber bug (thanks to hbx3485).
Version 2.1.1
- ISSUE #21: Problems on clearDisplay and setupDisplay.
Version 2.1.0
- ISSUE #20: Support for negative decimal numbers;
- ISSUE #19: Corrected setDisplayToString dot bug.
Version 2.0.1
- ISSUE #15: Backwards compatibility with previous Arduino IDE (pure virtual functions were not supported);
- ISSUE #18: Corrected problem with setLEDs() caused by changes in the previous version.
Version 2.0.0
- Support for the TM1640;
- Restructuring for supporting other modules.
Version 1.6.0
- ISSUE #10: Support for starting position on setDisplayToString() methods.
WARNING: setDisplayToString methods have changed prototype and are incompatible with code compiled for 1.5.x (if you specified a custom font)
Version 1.5.2
- ISSUE #9: Backwards compatibility with previous Arduino IDE.
Version 1.5.1
- ISSUE #7: New character data in default font for '*';
- Minor optimization on inverted TM1638.
Version 1.5.0
- support for inverted module;
- support for Arduino IDE 1.0;
- some restructuring.
Version 1.4.0
- ISSUE #6: Support for specifying dots on setDisplayToString
WARNING: setDisplayToString methods have changed prototype and are incompatible with code compiled for 1.3.x (if you specified a custom font)
Version 1.3.2
- ISSUE #5: Correction of string library import.
Version 1.3.1
- changed pins on the examples to match schematics on project site.
Version 1.3.0
- added support for String Object;
- added support for removing leading zeros in displaying numbers.
Version 1.2.0
- added support for writing text.
Version 1.1.0
- restructuring and added 2 proper examples, available in the arduino ide.
Version 1.0.0
- initial release.

57
InvertedTM1638.cpp Normal file
View File

@@ -0,0 +1,57 @@
/*
InvertedTM1638.cpp - Library implementation for inverted TM1638.
Copyright (C) 2011 Ricardo Batista (rjbatista <at> gmail <dot> 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 <http://www.gnu.org/licenses/>.
*/
#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)
{
TM1638::sendChar(7 - pos, data & 0xC0 | (data & 0x07) << 3 | (data & 0x38) >> 3, dot);
}

45
InvertedTM1638.h Normal file
View File

@@ -0,0 +1,45 @@
/*
InvertedTM1638.h - Library for an inverted TM1638.
Copyright (C) 2011 Ricardo Batista <rjbatista at gmail dot 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 <http://www.gnu.org/licenses/>.
*/
#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

View File

@@ -4,22 +4,24 @@ Ricardo Batista
Email: rjbatista(at)gmail.com
URL: http://code.google.com/p/tm1638-library/
A library for interacting an arduino with a tm1638.
A library for interacting an arduino with a TM1638/TM1640.
Includes:
- Support for the TM1638 and TM1640;
- Helper methods for displaying numbers in decimal, hexadecimal and binary;
- 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
See: TM1640 Display module at http://www.dealextreme.com/p/104311?r=68099021
USAGE NOTES
-----------
Just put the files on a TM1638 directory under "arduino/libraries" on your instalation
Just put the files on a TM1638 directory under "arduino/libraries" on your arduino IDE instalation
PROJECT HOME

View File

@@ -1,10 +1,8 @@
/*
TM1638.h - Library for TM1638.
TM1638.cpp - Library implementation for TM1638.
Copyright (C) 2011 Ricardo Batista (rjbatista <at> gmail <dot> 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.
@@ -18,93 +16,80 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "WProgram.h"
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "TM1638.h"
#include "string.h"
TM1638::TM1638(byte dataPin, byte clockPin, byte strobePin, boolean activateDisplay, byte intensity)
: TM16XX(dataPin, clockPin, strobePin, 8, activateDisplay, intensity)
{
this->dataPin = dataPin;
this->clockPin = clockPin;
this->strobePin = strobePin;
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(strobePin, OUTPUT);
digitalWrite(strobePin, HIGH);
digitalWrite(clockPin, HIGH);
sendCommand(0x40);
sendCommand(0x80 | (activateDisplay ? 8 : 0) | min(7, intensity));
digitalWrite(strobePin, LOW);
send(0xC0);
for (int i = 0; i < 16; i++) {
send(0x00);
}
digitalWrite(strobePin, HIGH);
// nothing else to do - calling super is enough
}
void TM1638::setupDisplay(boolean active, byte intensity)
void TM1638::setDisplayToHexNumber(unsigned long number, byte dots, boolean leadingZeros,
const byte numberFont[])
{
sendCommand(0x80 | (active ? 8 : 0) | min(7, intensity));
}
void TM1638::setDisplayToHexNumber(unsigned long number, byte dots)
{
for (int i = 0; i < 8; i++) {
setDisplayDigit(number & 0xF, 7 - i, (dots & (1 << i)) != 0);
number >>= 4;
for (int i = 0; i < displays; i++) {
if (!leadingZeros && number == 0) {
clearDisplayDigit(displays - i - 1, (dots & (1 << i)) != 0);
} else {
setDisplayDigit(number & 0xF, displays - i - 1, (dots & (1 << i)) != 0, numberFont);
number >>= 4;
}
}
}
void TM1638::setDisplayToDecNumber(unsigned long number, byte dots)
void TM1638::setDisplayToDecNumberAt(unsigned long number, byte dots, byte startingPos, boolean leadingZeros,
const byte numberFont[])
{
if (number > 99999999L) {
setDisplay(ERROR);
setDisplayToError();
} else {
for (int i = 0; i < 8; i++) {
for (int i = 0; i < displays - startingPos; i++) {
if (number != 0) {
setDisplayDigit(number % 10, 7 - i, (dots & (1 << i)) != 0);
setDisplayDigit(number % 10, displays - i - 1, (dots & (1 << i)) != 0, numberFont);
number /= 10;
} else {
setDisplayDigit(0, 7 - i, (dots & (1 << i)) != 0);
if (leadingZeros) {
setDisplayDigit(0, displays - i - 1, (dots & (1 << i)) != 0, numberFont);
} else {
clearDisplayDigit(displays - i - 1, (dots & (1 << i)) != 0);
}
}
}
}
}
void TM1638::setDisplayToBinNumber(byte number, byte dots)
void TM1638::setDisplayToDecNumber(unsigned long number, byte dots, boolean leadingZeros,
const byte numberFont[])
{
for (int i = 0; i < 8; i++) {
setDisplayDigit((number & (1 << i)) == 0 ? 0 : 1, 7 - i, (dots & (1 << i)) != 0);
}
setDisplayToDecNumberAt(number, dots, 0, leadingZeros, numberFont);
}
void TM1638::setDisplayDigit(byte digit, byte pos, boolean dot)
void TM1638::setDisplayToSignedDecNumber(signed long number, byte dots, boolean leadingZeros,
const byte numberFont[])
{
sendData(pos << 1, NUMBER_DATA[digit & 0xF] | (dot ? 0b10000000 : 0));
if (number >= 0) {
setDisplayToDecNumberAt(number, dots, 0, leadingZeros, numberFont);
} else {
if (-number > 9999999L) {
setDisplayToError();
} else {
setDisplayToDecNumberAt(-number, dots, 1, leadingZeros, numberFont);
sendChar(0, MINUS, (dots & (0x80)) != 0);
}
}
}
void TM1638::setDisplay(const byte values[])
void TM1638::setDisplayToBinNumber(byte number, byte dots, const byte numberFont[])
{
for (int i = 0; i < 8; i++) {
sendData(i << 1, values[i]);
}
}
void TM1638::clearDisplay()
{
for (int i = 0; i < 8; i++) {
sendData(i << 1, 0);
}
}
void TM1638::setDisplayToString(const char* string, const byte font[])
{
for (int i = 0; i < 8; i++) {
sendData(i << 1, font[string[i] - 32]);
for (int i = 0; i < displays; i++) {
setDisplayDigit((number & (1 << i)) == 0 ? 0 : 1, displays - i - 1, (dots & (1 << i)) != 0, numberFont);
}
}
@@ -115,18 +100,18 @@ void TM1638::setLED(byte color, byte pos)
void TM1638::setLEDs(word leds)
{
for (int i = 0; i < 8; i++) {
byte val = 0;
for (int i = 0; i < displays; i++) {
byte color = 0;
if (leds & (1 << i)) {
val |= 1;
if ((leds & (1 << i)) != 0) {
color |= TM1638_COLOR_RED;
}
if (leds & (1 << (i + 8))) {
val |= 2;
if ((leds & (1 << (i + 8))) != 0) {
color |= TM1638_COLOR_GREEN;
}
sendData((i << 1) + 1, val);
setLED(color, i);
}
}
@@ -144,56 +129,7 @@ byte TM1638::getButtons(void)
return keys;
}
void TM1638::sendCommand(byte cmd)
void TM1638::sendChar(byte pos, byte data, boolean dot)
{
digitalWrite(strobePin, LOW);
send(cmd);
digitalWrite(strobePin, HIGH);
sendData(pos << 1, data | (dot ? 0b10000000 : 0));
}
void TM1638::sendData(byte add, byte data)
{
sendCommand(0x44);
digitalWrite(strobePin, LOW);
send(0xc0 | add);
send(data);
digitalWrite(strobePin, HIGH);
}
void TM1638::send(byte data)
{
for (int i = 0; i < 8; i++) {
digitalWrite(clockPin, LOW);
digitalWrite(dataPin, data & 1 ? HIGH : LOW);
data >>= 1;
digitalWrite(clockPin, HIGH);
}
}
byte TM1638::receive()
{
byte temp = 0;
// Pull-up on
pinMode(dataPin, INPUT);
digitalWrite(dataPin, HIGH);
for (int i = 0; i < 8; i++) {
temp >>= 1;
digitalWrite(clockPin, LOW);
if (digitalRead(dataPin)) {
temp |= 0x80;
}
digitalWrite(clockPin, HIGH);
}
// Pull-up off
pinMode(dataPin, OUTPUT);
digitalWrite(dataPin, LOW);
return temp;
}

View File

@@ -3,8 +3,6 @@ TM1638.h - Library for TM1638.
Copyright (C) 2011 Ricardo Batista <rjbatista at gmail dot 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.
@@ -21,55 +19,50 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#ifndef TM1638_h
#define TM1638_h
#include <WProgram.h>
#include "TM1638Fonts.h"
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "TM16XX.h"
#include "TM16XXFonts.h"
#define TM1638_COLOR_NONE 0
#define TM1638_COLOR_RED 1
#define TM1638_COLOR_GREEN 2
class TM1638
class TM1638 : public TM16XX
{
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);
/** 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 */
void setDisplayToHexNumber(unsigned long number, byte dots);
/** Set the display to a unsigned decimal number */
void setDisplayToDecNumber(unsigned long number, byte dots);
/** Set the display to a unsigned hexadecimal number (with or without leading zeros) */
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,
const byte numberFont[] = NUMBER_FONT);
/** Set the display to a signed decimal number (with or without leading zeros) */
void setDisplayToSignedDecNumber(signed 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);
/** Set a single display at pos (starting at 0) to a digit (left to right) */
void setDisplayDigit(byte digit, byte pos, boolean dot);
/** Set the display to the 8 values (left to right) */
void setDisplay(const byte values[]);
/** Clear the display */
void clearDisplay();
/** Set the display to the string (defaults to built in font) */
void setDisplayToString(const char* string, const byte font[] = FONT_DEFAULT);
void setDisplayToBinNumber(byte number, byte dots,
const byte numberFont[] = NUMBER_FONT);
/** 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:
void sendCommand(byte led);
void sendData(byte add, byte data);
void send(byte data);
byte receive();
private:
byte dataPin;
byte clockPin;
byte strobePin;
virtual void sendChar(byte pos, byte data, boolean dot);
void setDisplayToDecNumberAt(unsigned long number, byte dots, byte startingPos,
boolean leadingZeros, const byte numberFont[]);
};
#endif

54
TM1640.cpp Normal file
View File

@@ -0,0 +1,54 @@
/*
TM1640.cpp - Library implementation for TM1640.
Copyright (C) 2011 Ricardo Batista (rjbatista <at> gmail <dot> 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 <http://www.gnu.org/licenses/>.
*/
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "TM1640.h"
#include "string.h"
TM1640::TM1640(byte dataPin, byte clockPin, boolean activateDisplay, byte intensity)
: TM16XX(dataPin, clockPin, dataPin, 16, activateDisplay, intensity)
{
// nothing else to do - calling super is enough
}
void TM1640::sendChar(byte pos, byte data, boolean dot)
{
sendData(pos, data | (dot ? 0b10000000 : 0));
// necessary for the TM1640
digitalWrite(strobePin, LOW);
digitalWrite(clockPin, LOW);
digitalWrite(clockPin, HIGH);
digitalWrite(strobePin, HIGH);
}
void TM1640::clearDisplay()
{
digitalWrite(strobePin, LOW);
send(0xC0);
for (int i = 0; i < 16; i++) {
send(0x00);
}
digitalWrite(strobePin, HIGH);
}

43
TM1640.h Normal file
View File

@@ -0,0 +1,43 @@
/*
TM1640.h - Library for TM1640.
Copyright (C) 2011 Ricardo Batista <rjbatista at gmail dot 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 <http://www.gnu.org/licenses/>.
*/
#ifndef TM1640_h
#define TM1640_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "TM16XX.h"
#include "TM16XXFonts.h"
class TM1640 : public TM16XX
{
public:
/** Instantiate a tm1640 module specifying the display state, the starting intensity (0-7) data and clock pins. */
TM1640(byte dataPin, byte clockPin, boolean activateDisplay = true, byte intensity = 7);
/** Clear the display */
virtual void clearDisplay();
protected:
virtual void sendChar(byte pos, byte data, boolean dot);
};
#endif

179
TM16XX.cpp Normal file
View File

@@ -0,0 +1,179 @@
/*
TM16XX.cpp - Library implementation for TM16XX.
Copyright (C) 2011 Ricardo Batista (rjbatista <at> gmail <dot> 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 <http://www.gnu.org/licenses/>.
*/
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "TM16XX.h"
#include "string.h"
TM16XX::TM16XX(byte dataPin, byte clockPin, byte strobePin, byte displays, boolean activateDisplay,
byte intensity)
{
this->dataPin = dataPin;
this->clockPin = clockPin;
this->strobePin = strobePin;
this->displays = displays;
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(strobePin, OUTPUT);
digitalWrite(strobePin, HIGH);
digitalWrite(clockPin, HIGH);
sendCommand(0x40);
sendCommand(0x80 | (activateDisplay ? 8 : 0) | min(7, intensity));
digitalWrite(strobePin, LOW);
send(0xC0);
for (int i = 0; i < 16; i++) {
send(0x00);
}
digitalWrite(strobePin, HIGH);
}
void TM16XX::setupDisplay(boolean active, byte intensity)
{
sendCommand(0x80 | (active ? 8 : 0) | min(7, intensity));
// necessary for the TM1640
digitalWrite(strobePin, LOW);
digitalWrite(clockPin, LOW);
digitalWrite(clockPin, HIGH);
digitalWrite(strobePin, HIGH);
}
void TM16XX::setDisplayDigit(byte digit, byte pos, boolean dot, const byte numberFont[])
{
sendChar(pos, numberFont[digit & 0xF], dot);
}
void TM16XX::setDisplayToError()
{
setDisplay(ERROR_DATA, 8);
for (int i = 8; i < displays; i++) {
clearDisplayDigit(i, 0);
}
}
void TM16XX::clearDisplayDigit(byte pos, boolean dot)
{
sendChar(pos, 0, dot);
}
void TM16XX::setDisplay(const byte values[], unsigned int size)
{
for (int i = 0; i < size; i++) {
sendChar(i, values[i], 0);
}
}
void TM16XX::clearDisplay()
{
for (int i = 0; i < displays; i++) {
sendData(i << 1, 0);
}
}
void TM16XX::setDisplayToString(const char* string, const word dots, const byte pos, const byte font[])
{
for (int i = 0; i < displays - pos; i++) {
if (string[i] != '\0') {
sendChar(i + pos, font[string[i] - 32], (dots & (1 << (displays - i - 1))) != 0);
} else {
break;
}
}
}
void TM16XX::setDisplayToString(const String string, const word dots, const byte pos, const byte font[])
{
int stringLength = string.length();
for (int i = 0; i < displays - pos; i++) {
if (i < stringLength) {
sendChar(i + pos, font[string.charAt(i) - 32], (dots & (1 << (displays - i - 1))) != 0);
} else {
break;
}
}
}
void TM16XX::sendCommand(byte cmd)
{
digitalWrite(strobePin, LOW);
send(cmd);
digitalWrite(strobePin, HIGH);
}
void TM16XX::sendData(byte address, byte data)
{
sendCommand(0x44);
digitalWrite(strobePin, LOW);
send(0xC0 | address);
send(data);
digitalWrite(strobePin, HIGH);
}
void TM16XX::send(byte data)
{
for (int i = 0; i < 8; i++) {
digitalWrite(clockPin, LOW);
digitalWrite(dataPin, data & 1 ? HIGH : LOW);
data >>= 1;
digitalWrite(clockPin, HIGH);
}
}
byte TM16XX::receive()
{
byte temp = 0;
// Pull-up on
pinMode(dataPin, INPUT);
digitalWrite(dataPin, HIGH);
for (int i = 0; i < 8; i++) {
temp >>= 1;
digitalWrite(clockPin, LOW);
if (digitalRead(dataPin)) {
temp |= 0x80;
}
digitalWrite(clockPin, HIGH);
}
// Pull-up off
pinMode(dataPin, OUTPUT);
digitalWrite(dataPin, LOW);
return temp;
}
#if !defined(ARDUINO) || ARDUINO < 100
// empty implementation instead of pure virtual for older Arduino IDE
void TM16XX::sendChar(byte pos, byte data, boolean dot) {}
#endif

81
TM16XX.h Normal file
View File

@@ -0,0 +1,81 @@
/*
TM16XX.h - Library for TM1638.
Copyright (C) 2011 Ricardo Batista <rjbatista at gmail dot 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 <http://www.gnu.org/licenses/>.
*/
#ifndef TM16XX_h
#define TM16XX_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "TM16XXFonts.h"
class TM16XX
{
public:
/**
* Instantiate a tm16xx module specifying the number of displays, display state,
* the starting intensity (0-7) data, clock and stobe pins.
*/
TM16XX(byte dataPin, byte clockPin, byte strobePin, byte displays, boolean activateDisplay = true,
byte intensity = 7);
/** Set the display (segments and LEDs) active or off and intensity (range from 0-7). */
virtual void setupDisplay(boolean active, byte intensity);
/** Set a single display at pos (starting at 0) to a digit (left to right) */
virtual void setDisplayDigit(byte digit, byte pos, boolean dot, const byte numberFont[] = NUMBER_FONT);
/** Set the display to an error message */
virtual void setDisplayToError();
/** Clear a single display at pos (starting at 0, left to right) */
virtual void clearDisplayDigit(byte pos, boolean dot);
/** Set the display to the values (left to right) */
virtual void setDisplay(const byte values[], unsigned int length = 8);
/** Clear the display */
virtual void clearDisplay();
/** 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[] = FONT_DEFAULT);
/** Set the display to the String (defaults to built in font) */
virtual void setDisplayToString(String string, const word dots = 0, const byte pos = 0,
const byte font[] = FONT_DEFAULT);
protected:
#if defined(ARDUINO) && ARDUINO >= 100
// pure virtual is NOT supported in older Arduino IDE
virtual void sendChar(byte pos, byte data, boolean dot) = 0;
#else
virtual void sendChar(byte pos, byte data, boolean dot);
#endif
virtual void sendCommand(byte led);
virtual void sendData(byte add, byte data);
virtual void send(byte data);
virtual byte receive();
byte displays;
byte dataPin;
byte clockPin;
byte strobePin;
};
#endif

View File

@@ -1,10 +1,8 @@
/*
TM1638.h - Library for TM1638.
TM16XXFonts.h - Font definition for TM16XX.
Copyright (C) 2011 Ricardo Batista (rjbatista <at> gmail <dot> 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.
@@ -29,11 +27,11 @@ The bits are displayed by mapping bellow
*/
#ifndef TM1638Fonts_h
#define TM1638Fonts_h
#ifndef TM16XXFonts_h
#define TM16XXFonts_h
// definition for standard hexadecimal numbers
const byte NUMBER_DATA[] = {
const byte NUMBER_FONT[] = {
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
@@ -52,8 +50,10 @@ const byte NUMBER_DATA[] = {
0b01110001 // F
};
const byte MINUS = 0b01000000;
// definition for error
const byte ERROR[] = {
const byte ERROR_DATA[] = {
0b01111001, // E
0b01010000, // r
0b01010000, // r
@@ -76,7 +76,7 @@ const byte FONT_DEFAULT[] = {
0b00000010, // (39) '
0b00110000, // (40) (
0b00000110, // (41) )
0b00000000, // (42) *
0b01100011, // (42) *
0b00000000, // (43) +
0b00000100, // (44) ,
0b01000000, // (45) -

View File

@@ -0,0 +1,102 @@
/*
Library examples for TM1638.
Copyright (C) 2011 Ricardo Batista <rjbatista at gmail dot 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 <http://www.gnu.org/licenses/>.
*/
#include <TM1638.h>
#include <InvertedTM1638.h>
#define NO_MODULES 2
// define a regular module and a inverted module
TM1638 module1(3, 2, 4);
InvertedTM1638 module2(3, 2, 5);
TM1638* modules[NO_MODULES] = {
&module1,
&module2
};
byte modes[NO_MODULES];
unsigned long startTime;
void setup() {
startTime = millis();
for (int i = 0; i < NO_MODULES; i++) {
modules[i]->setupDisplay(true, 7);
modes[i] = 0;
}
}
void update(TM1638* module, byte* mode) {
byte buttons = module->getButtons();
unsigned long runningSecs = (millis() - startTime) / 1000;
// button pressed - change mode
if (buttons != 0) {
*mode = buttons >> 1;
module->clearDisplay();
module->setLEDs(0);
}
switch (*mode) {
case 0:
module->setDisplayToDecNumber(runningSecs, 1 << 7);
break;
case 1:
module->setDisplayToDecNumber(runningSecs, 1 << 6, false);
break;
case 2:
module->setDisplayToHexNumber(runningSecs, 1 << 5);
break;
case 4:
module->setDisplayToHexNumber(runningSecs, 1 << 4, false);
break;
case 8:
module->setDisplayToBinNumber(runningSecs, 1 << 3);
break;
case 16:
module->clearDisplayDigit((runningSecs - 1) % 8, 0);
module->setDisplayDigit(runningSecs % 8, runningSecs % 8, 0);
break;
case 32:
char s[8];
sprintf(s, "Secs %03d", runningSecs % 999);
module->setDisplayToString(s);
break;
case 64:
if (runningSecs % 2 == 0) {
module->setDisplayToString("TM1638 ");
} else {
module->setDisplayToString("LIBRARY ");
}
module->setLED(0, (runningSecs - 1) % 8);
module->setLED(1 + runningSecs % 3, runningSecs % 8);
break;
case 65:
module->setDisplayToError();
break;
}
}
void loop() {
for (int i = 0; i < NO_MODULES; i++) {
update(modules[i], &modes[i]);
}
}

View File

@@ -1,5 +1,5 @@
/*
TM1638.h - Library for TM1638.
Library examples for TM1638.
Copyright (C) 2011 Ricardo Batista <rjbatista at gmail dot com>
@@ -18,8 +18,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <TM1638.h>
// define a module on data pin 3, clock pin 2 and strobe pin 4
TM1638 module(3, 2, 4);
// define a module on data pin 8, clock pin 9 and strobe pin 7
TM1638 module(8, 9, 7);
void setup() {
// display a hexadecimal number and set the left 4 dots

View File

@@ -1,5 +1,5 @@
/*
TM1638.h - Library for TM1638.
Library examples for TM1638.
Copyright (C) 2011 Ricardo Batista <rjbatista at gmail dot com>
@@ -22,10 +22,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
// define a modules
TM1638 modules[] = {
TM1638(3, 2, 4),
TM1638(3, 2, 5),
TM1638(3, 2, 6),
TM1638(3, 2, 7)
TM1638(8, 9, 7),
TM1638(8, 9, 6),
TM1638(8, 9, 5),
TM1638(8, 9, 4)
};
void setup() {

View File

@@ -1,5 +1,5 @@
/*
TM1638.h - Library for TM1638.
Library examples for TM1638.
Copyright (C) 2011 Ricardo Batista <rjbatista at gmail dot com>
@@ -24,9 +24,9 @@ const byte hello[] = {
};
// define the first module
TM1638 module1(3, 2, 4);
TM1638 module1(8, 9, 7);
// to chain modules, use the same clk and data - just specify a different strobe pin
TM1638 module2(3, 2, 5);
TM1638 module2(8, 9, 6);
unsigned long value = 0L;
boolean state = true;

View File

@@ -0,0 +1,36 @@
/*
Library examples for TM1640.
Copyright (C) 2011 Ricardo Batista <rjbatista at gmail dot 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 <http://www.gnu.org/licenses/>.
*/
#include <TM1638.h> // required because the way arduino deals with libraries
#include <TM1640.h>
// define a module on data pin 3, clock pin 2
TM1640 module(3, 2);
void setup()
{
// nothing to do here
}
void loop()
{
char text[17];
sprintf(text, "testing %u", millis());
module.setDisplayToString(text);
}

View File

@@ -6,7 +6,10 @@
# Datatypes (KEYWORD1)
#######################################
TM16XX KEYWORD1
TM1638 KEYWORD1
TM1640 KEYWORD1
InvertedTM1638 KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
@@ -25,6 +28,7 @@ setLEDs KEYWORD2
getButtons KEYWORD2
sendCommand KEYWORD2
sendData KEYWORD2
sendChar KEYWORD2
send KEYWORD2
receive KEYWORD2