diff --git a/TM1638.cpp b/TM1638.cpp index 8785789..04ede46 100644 --- a/TM1638.cpp +++ b/TM1638.cpp @@ -61,7 +61,7 @@ const byte ERROR[] = { 0 }; -TM1638::TM1638(byte dataPin, byte clockPin, byte strobePin) +TM1638::TM1638(byte dataPin, byte clockPin, byte strobePin, boolean activateDisplay, byte intensity) { this->dataPin = dataPin; this->clockPin = clockPin; @@ -74,8 +74,8 @@ TM1638::TM1638(byte dataPin, byte clockPin, byte strobePin) digitalWrite(strobePin, HIGH); digitalWrite(clockPin, HIGH); - sendCommand(0x8B); sendCommand(0x40); + sendCommand(0x80 | (activateDisplay ? 8 : 0) | min(7, intensity)); digitalWrite(strobePin, LOW); send(0xC0); @@ -85,6 +85,12 @@ TM1638::TM1638(byte dataPin, byte clockPin, byte strobePin) digitalWrite(strobePin, HIGH); } +void TM1638::setupDisplay(boolean active, byte intensity) +{ + sendCommand(0x80 | (active ? 8 : 0) | min(7, intensity)); +} + + void TM1638::setDisplayToHexNumber(unsigned long number, byte dots) { for (int i = 0; i < 8; i++) { diff --git a/TM1638.h b/TM1638.h index 65e320d..cfb0646 100644 --- a/TM1638.h +++ b/TM1638.h @@ -28,8 +28,12 @@ along with this program. If not, see . class TM1638 { - public: - TM1638(byte dataPin, byte clockPin, byte strobePin); + 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); diff --git a/tm1638_lib.pde b/tm1638_lib.pde index e82228a..8b0fe62 100644 --- a/tm1638_lib.pde +++ b/tm1638_lib.pde @@ -6,13 +6,13 @@ unsigned char hello[] = { TM1638 module1(2, 3, 4); TM1638 module2(2, 3, 5); - -unsigned long value = 87654321L; +unsigned long value = 0L; +boolean state = true; void setup() { - module1.setDisplay(helloLCD); - module2.setDisplay(helloLCD); + module1.setDisplay(hello); + module2.setDisplay(hello); module1.setLEDs(0b00011111 | 0b11111000 << 8); @@ -21,7 +21,6 @@ void setup() module2.setLED(TM1638_COLOR_RED | TM1638_COLOR_GREEN, 7); } - void loop() { byte key1, key2; @@ -42,6 +41,13 @@ void loop() module2.setDisplayToBinNumber(key1, 0); module1.setLEDs(key1); + + if (key1 & 128) { + state = !state; + delay(200); // just wait for button up + } + + module1.setupDisplay(state, key1 >> 1); } if (key2 != 0) {