Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f9eecc06e3 | ||
|
|
76e0696880 | ||
|
|
749c559f05 | ||
|
|
e4c0ca275f | ||
|
|
c297ea9e9f | ||
|
|
b32993401f | ||
|
|
bb28428a44 | ||
|
|
cff5d047bf | ||
|
|
eb4538e162 | ||
|
|
a8bcd661c8 | ||
|
|
7260c88f7d | ||
|
|
7c6678133c |
19
CHANGES.txt
19
CHANGES.txt
@@ -1,3 +1,22 @@
|
|||||||
|
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
|
Version 1.5.0
|
||||||
|
|
||||||
- support for inverted module;
|
- support for inverted module;
|
||||||
|
|||||||
@@ -53,9 +53,5 @@ byte InvertedTM1638::getButtons()
|
|||||||
|
|
||||||
void InvertedTM1638::sendChar(byte pos, byte data, boolean dot)
|
void InvertedTM1638::sendChar(byte pos, byte data, boolean dot)
|
||||||
{
|
{
|
||||||
byte n = data;
|
TM1638::sendChar(7 - pos, data & 0xC0 | (data & 0x07) << 3 | (data & 0x38) >> 3, dot);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,10 @@ Ricardo Batista
|
|||||||
Email: rjbatista(at)gmail.com
|
Email: rjbatista(at)gmail.com
|
||||||
URL: http://code.google.com/p/tm1638-library/
|
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:
|
Includes:
|
||||||
|
- Support for the TM1638 and TM1640;
|
||||||
- Helper methods for displaying numbers in decimal, hexadecimal and binary;
|
- Helper methods for displaying numbers in decimal, hexadecimal and binary;
|
||||||
- Support for multiple chained tm1638;
|
- Support for multiple chained tm1638;
|
||||||
- Reading simultaneous button presses;
|
- Reading simultaneous button presses;
|
||||||
@@ -15,12 +16,12 @@ Includes:
|
|||||||
- Support for module in inverted position.
|
- Support for module in inverted position.
|
||||||
|
|
||||||
See: TM1638 Display/LED module at http://www.dealextreme.com/p/81873?r=68099021
|
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
|
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
|
PROJECT HOME
|
||||||
|
|||||||
158
TM1638.cpp
158
TM1638.cpp
@@ -26,47 +26,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
#include "string.h"
|
#include "string.h"
|
||||||
|
|
||||||
TM1638::TM1638(byte dataPin, byte clockPin, byte strobePin, boolean activateDisplay, byte intensity)
|
TM1638::TM1638(byte dataPin, byte clockPin, byte strobePin, boolean activateDisplay, byte intensity)
|
||||||
|
: TM16XX(dataPin, clockPin, strobePin, 8, activateDisplay, intensity)
|
||||||
{
|
{
|
||||||
this->dataPin = dataPin;
|
// nothing else to do - calling super is enough
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
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[])
|
const byte numberFont[])
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < displays; i++) {
|
||||||
if (!leadingZeros && number == 0) {
|
if (!leadingZeros && number == 0) {
|
||||||
clearDisplayDigit(7 - i, (dots & (1 << i)) != 0);
|
clearDisplayDigit(displays - i - 1, (dots & (1 << i)) != 0);
|
||||||
} else {
|
} else {
|
||||||
setDisplayDigit(number & 0xF, 7 - i, (dots & (1 << i)) != 0, numberFont);
|
setDisplayDigit(number & 0xF, displays - i - 1, (dots & (1 << i)) != 0, numberFont);
|
||||||
number >>= 4;
|
number >>= 4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -78,15 +50,15 @@ void TM1638::setDisplayToDecNumber(unsigned long number, byte dots, boolean lead
|
|||||||
if (number > 99999999L) {
|
if (number > 99999999L) {
|
||||||
setDisplayToError();
|
setDisplayToError();
|
||||||
} else {
|
} else {
|
||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < displays; i++) {
|
||||||
if (number != 0) {
|
if (number != 0) {
|
||||||
setDisplayDigit(number % 10, 7 - i, (dots & (1 << i)) != 0, numberFont);
|
setDisplayDigit(number % 10, displays - i - 1, (dots & (1 << i)) != 0, numberFont);
|
||||||
number /= 10;
|
number /= 10;
|
||||||
} else {
|
} else {
|
||||||
if (leadingZeros) {
|
if (leadingZeros) {
|
||||||
setDisplayDigit(0, 7 - i, (dots & (1 << i)) != 0, numberFont);
|
setDisplayDigit(0, displays - i - 1, (dots & (1 << i)) != 0, numberFont);
|
||||||
} else {
|
} else {
|
||||||
clearDisplayDigit(7 - i, (dots & (1 << i)) != 0);
|
clearDisplayDigit(displays - i - 1, (dots & (1 << i)) != 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -95,57 +67,8 @@ void TM1638::setDisplayToDecNumber(unsigned long number, byte dots, boolean lead
|
|||||||
|
|
||||||
void TM1638::setDisplayToBinNumber(byte number, byte dots, const byte numberFont[])
|
void TM1638::setDisplayToBinNumber(byte number, byte dots, const byte numberFont[])
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < displays; i++) {
|
||||||
setDisplayDigit((number & (1 << i)) == 0 ? 0 : 1, 7 - i, (dots & (1 << i)) != 0, numberFont);
|
setDisplayDigit((number & (1 << i)) == 0 ? 0 : 1, displays - i - 1, (dots & (1 << i)) != 0, numberFont);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void TM1638::setDisplayDigit(byte digit, byte pos, boolean dot, const byte numberFont[])
|
|
||||||
{
|
|
||||||
sendChar(pos, numberFont[digit & 0xF], dot);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TM1638::setDisplayToError()
|
|
||||||
{
|
|
||||||
setDisplay(ERROR_DATA);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TM1638::clearDisplayDigit(byte pos, boolean dot)
|
|
||||||
{
|
|
||||||
sendChar(pos, 0, dot);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TM1638::setDisplay(const byte values[])
|
|
||||||
{
|
|
||||||
for (int i = 0; i < 8; i++) {
|
|
||||||
sendChar(i, values[i], 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void TM1638::clearDisplay()
|
|
||||||
{
|
|
||||||
for (int i = 0; i < 8; i++) {
|
|
||||||
sendData(i << 1, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void TM1638::setDisplayToString(const char* string, const byte dots, const byte font[])
|
|
||||||
{
|
|
||||||
for (int i = 0; i < 8; i++) {
|
|
||||||
sendChar(i, font[string[i] - 32], dots & (1 << (7 - i)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void TM1638::setDisplayToString(const String string, const byte dots, const byte font[])
|
|
||||||
{
|
|
||||||
int stringLength = string.length();
|
|
||||||
|
|
||||||
for (int i = 0; i < 8; i++) {
|
|
||||||
if (i < stringLength) {
|
|
||||||
sendChar(i, font[string.charAt(i) - 32], dots & (1 << (7 - i)));
|
|
||||||
} else {
|
|
||||||
sendChar(i, 0, dots & (1 << (7 - i)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,14 +79,14 @@ void TM1638::setLED(byte color, byte pos)
|
|||||||
|
|
||||||
void TM1638::setLEDs(word leds)
|
void TM1638::setLEDs(word leds)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 8; i++) {
|
for (int i = 0; i < displays; i++) {
|
||||||
byte color = 0;
|
byte color = 0;
|
||||||
|
|
||||||
if (leds & (1 << i)) {
|
if (leds & (1 << i) != 0) {
|
||||||
color |= TM1638_COLOR_RED;
|
color |= TM1638_COLOR_RED;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (leds & (1 << (i + 8))) {
|
if (leds & (1 << (i + 8)) != 0) {
|
||||||
color |= TM1638_COLOR_GREEN;
|
color |= TM1638_COLOR_GREEN;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,56 +112,3 @@ void TM1638::sendChar(byte pos, byte data, boolean dot)
|
|||||||
{
|
{
|
||||||
sendData(pos << 1, data | (dot ? 0b10000000 : 0));
|
sendData(pos << 1, data | (dot ? 0b10000000 : 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
void TM1638::sendCommand(byte cmd)
|
|
||||||
{
|
|
||||||
digitalWrite(strobePin, LOW);
|
|
||||||
send(cmd);
|
|
||||||
digitalWrite(strobePin, HIGH);
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|||||||
36
TM1638.h
36
TM1638.h
@@ -25,20 +25,17 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
#include "WProgram.h"
|
#include "WProgram.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "TM1638Fonts.h"
|
#include "TM16XX.h"
|
||||||
|
#include "TM16XXFonts.h"
|
||||||
|
|
||||||
#define TM1638_COLOR_RED 1
|
#define TM1638_COLOR_RED 1
|
||||||
#define TM1638_COLOR_GREEN 2
|
#define TM1638_COLOR_GREEN 2
|
||||||
|
|
||||||
class TM1638
|
class TM1638 : public TM16XX
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/** Instantiate a tm1638 module specifying the display state, the starting intensity (0-7) data, clock and stobe pins. */
|
/** 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);
|
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) */
|
/** 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,
|
||||||
@@ -49,21 +46,6 @@ class TM1638
|
|||||||
/** Set the display to a unsigned binary number */
|
/** 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);
|
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,
|
|
||||||
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[]);
|
|
||||||
/** Clear the display */
|
|
||||||
void clearDisplay();
|
|
||||||
/** Set the display to the string (defaults to built in font) */
|
|
||||||
void setDisplayToString(const char* string, const byte dots = 0, const byte font[] = FONT_DEFAULT);
|
|
||||||
/** Set the display to the String (defaults to built in font) */
|
|
||||||
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) */
|
/** Set the LED at pos to color (TM1638_COLOR_RED, TM1638_COLOR_GREEN or both) */
|
||||||
virtual void setLED(byte color, byte pos);
|
virtual void setLED(byte color, byte pos);
|
||||||
@@ -74,17 +56,7 @@ class TM1638
|
|||||||
virtual byte getButtons();
|
virtual byte getButtons();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void sendChar(byte pos, byte data, boolean dot);
|
virtual void sendChar(byte pos, byte data, boolean dot);
|
||||||
|
|
||||||
void sendCommand(byte led);
|
|
||||||
void sendData(byte add, byte data);
|
|
||||||
void send(byte data);
|
|
||||||
byte receive();
|
|
||||||
|
|
||||||
private:
|
|
||||||
byte dataPin;
|
|
||||||
byte clockPin;
|
|
||||||
byte strobePin;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
38
TM1640.cpp
Normal file
38
TM1640.cpp
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
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));
|
||||||
|
}
|
||||||
41
TM1640.h
Normal file
41
TM1640.h
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
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);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void sendChar(byte pos, byte data, boolean dot);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
167
TM16XX.cpp
Normal file
167
TM16XX.cpp
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
/*
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
75
TM16XX.h
Normal file
75
TM16XX.h
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
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:
|
||||||
|
virtual void sendChar(byte pos, byte data, boolean dot) = 0;
|
||||||
|
|
||||||
|
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
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
TM1638Fonts.h - Font definition for TM1638.
|
TM16XXFonts.h - Font definition for TM16XX.
|
||||||
|
|
||||||
Copyright (C) 2011 Ricardo Batista (rjbatista <at> gmail <dot> com)
|
Copyright (C) 2011 Ricardo Batista (rjbatista <at> gmail <dot> com)
|
||||||
|
|
||||||
@@ -27,8 +27,8 @@ The bits are displayed by mapping bellow
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef TM1638Fonts_h
|
#ifndef TM16XXFonts_h
|
||||||
#define TM1638Fonts_h
|
#define TM16XXFonts_h
|
||||||
|
|
||||||
// definition for standard hexadecimal numbers
|
// definition for standard hexadecimal numbers
|
||||||
const byte NUMBER_FONT[] = {
|
const byte NUMBER_FONT[] = {
|
||||||
@@ -74,7 +74,7 @@ const byte FONT_DEFAULT[] = {
|
|||||||
0b00000010, // (39) '
|
0b00000010, // (39) '
|
||||||
0b00110000, // (40) (
|
0b00110000, // (40) (
|
||||||
0b00000110, // (41) )
|
0b00000110, // (41) )
|
||||||
0b00000000, // (42) *
|
0b01100011, // (42) *
|
||||||
0b00000000, // (43) +
|
0b00000000, // (43) +
|
||||||
0b00000100, // (44) ,
|
0b00000100, // (44) ,
|
||||||
0b01000000, // (45) -
|
0b01000000, // (45) -
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
TM1638.h - Library for TM1638.
|
Library examples for TM1638.
|
||||||
|
|
||||||
Copyright (C) 2011 Ricardo Batista <rjbatista at gmail dot com>
|
Copyright (C) 2011 Ricardo Batista <rjbatista at gmail dot com>
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
TM1638.h - Library for TM1638.
|
Library examples for TM1638.
|
||||||
|
|
||||||
Copyright (C) 2011 Ricardo Batista <rjbatista at gmail dot com>
|
Copyright (C) 2011 Ricardo Batista <rjbatista at gmail dot com>
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
TM1638.h - Library for TM1638.
|
Library examples for TM1638.
|
||||||
|
|
||||||
Copyright (C) 2011 Ricardo Batista <rjbatista at gmail dot com>
|
Copyright (C) 2011 Ricardo Batista <rjbatista at gmail dot com>
|
||||||
|
|
||||||
|
|||||||
36
examples/tm1640_example/tm1640_example.pde
Normal file
36
examples/tm1640_example/tm1640_example.pde
Normal 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);
|
||||||
|
}
|
||||||
@@ -6,7 +6,9 @@
|
|||||||
# Datatypes (KEYWORD1)
|
# Datatypes (KEYWORD1)
|
||||||
#######################################
|
#######################################
|
||||||
|
|
||||||
|
TM16XX KEYWORD1
|
||||||
TM1638 KEYWORD1
|
TM1638 KEYWORD1
|
||||||
|
TM1640 KEYWORD1
|
||||||
InvertedTM1638 KEYWORD1
|
InvertedTM1638 KEYWORD1
|
||||||
|
|
||||||
#######################################
|
#######################################
|
||||||
|
|||||||
Reference in New Issue
Block a user