Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TM16xx/ 0040777 0000000 0000000 00000000000 14026135507 007066 5 ustar 00 TM16xx/documents/ 0040777 0000000 0000000 00000000000 14026135626 011071 5 ustar 00 TM16xx/examples/ 0040777 0000000 0000000 00000000000 14026125125 010677 5 ustar 00 TM16xx/examples/TM1640_basic/ 0040777 0000000 0000000 00000000000 14026125035 012673 5 ustar 00 TM16xx/examples/TM1640_basic/TM1640_basic.ino 0100777 0000000 0000000 00000003420 14026127010 015370 0 ustar 00 /*
- Basic library example for TM1640. Kept small to fit in the limited space of an ATtiny44.
- Library based on TM1638 library by Ricardo Batista, adapted by Maxint-RD MMOLE 2018.
- Confirmed to work in the following environments:
- ATtiny44A using Arduino IDE 1.8.2 and ATTinyCore (8MHz, LTO enabled), 3232 bytes flash, 103 bytes RAM
- ATtiny44A using Arduino IDE 1.8.2 and ATtiny Microcontrolers (8MHz), 3212 bytes flash, 103 bytes RAM ==> 2892/95 ==> 2878/95
- Only compiled: not tested yet:
- Arduino Nano using Arduino IDE 1.8.2, Nano (Old Bootloader)), 3176 bytes flash, 95 bytes RAM
- For more information see https://github.com/maxint-rd/TM16xx
- */
- #include <TM1640.h>
- #if !defined(LED_BUILTIN)
- #define LED_BUILTIN 4
- #endif
- // Define a 4-digit display module. Pin suggestions:
- // ESP8266 (Wemos D1): data pin 5 (D1), clock pin 4 (D2)
- // ATtiny44A: data pin 9, clock pin 10 (LED_BUILTIN: 8 on ATTinyCore)
- TM1640 module(9, 10, 4); // data, clock, 4 digits
- void setup()
- {
- pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
- module.setDisplayToString("HALO");
- delay(500); // wait
- //module.clearDisplay();
- }
- void loop()
- {
- digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
- module.setupDisplay(true, 7); // sometimes noise on the line may change the intensity level
- int nTime = ((millis() / 1000) / 60) * 100 + (millis() / 1000) % 60; // minutes+seconds as integer
- module.setDisplayToDecNumber(nTime, _BV(4)); // display dot on digit 4
- delay(500); // wait
- digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
- module.setDisplayToDecNumber(nTime, _BV(3)); // display colon on digit 3
- delay(500); // wait
- } TM16xx/examples/TM1640_matrix/ 0040777 0000000 0000000 00000000000 14026127000 013110 5 ustar 00 TM16xx/examples/TM1640_matrix/TM1640_matrix.ino 0100777 0000000 0000000 00000006534 14026126674 016066 0 ustar 00 /*
- Matrix library example for TM1640. Kept small to fit in the limited space of an ATtiny44.
- NOTE: compile using LTO enabled!
- Library based on TM1638 library by Ricardo Batista, adapted by Maxint-RD MMOLE 2018.
- Confirmed to work in the following environments:
- * ATtiny44A using Arduino IDE 1.8.2 and ATTinyCore (8MHz, LTO enabled), 3232 bytes flash, 103 bytes RAM ==> 3086/137 ==> 2996/141 ==> 3000/115
- * ATtiny44A using Arduino IDE 1.8.2 and ATtiny Microcontrolers (8MHz), 3212 bytes flash, 103 bytes RAM == 3056/137 ==> 2976/115
- * WeMos D1-mini and TM1640 8x8 MatrixLED Shield using Arduino IDE 1.6.10: DIN=D7/13/MOSI, CLK=D5/14/SCK, 248644 bytes flash, 32312 bytes RAM
- Only compiled: not tested yet:
- * Arduino Nano using Arduino IDE 1.8.2, Nano (Old Bootloader)), 3388 bytes flash, 119 bytes RAM => 3298/115
- For more information see https://github.com/maxint-rd/TM16xx
- */
- #include <TM1640.h>
- #include <TM16xxMatrix.h>
- #if !defined(LED_BUILTIN)
- #define LED_BUILTIN 4
- #endif
- // Define a 4-digit display module. Pin suggestions:
- // ESP8266 (Wemos D1): data pin 5 (D1), clock pin 4 (D2)
- // ATtiny44A: data pin 9, clock pin 10 (LED_BUILTIN: 8 in ATtiny Core)
- //TM1640 module(9, 10, 4); // data, clock, 4 digits
- TM1640 module(13, 14); // For ESP8266/WeMos D1-mini: DIN=D7/13/MOSI, CLK=D5/14/SCK
- #define MATRIX_NUMCOLUMNS 8
- #define MATRIX_NUMROWS 8
- TM16xxMatrix matrix(&module, MATRIX_NUMCOLUMNS, MATRIX_NUMROWS); // TM16xx object, columns, rows
- void setup()
- {
- pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
- module.setDisplayToString("HALO");
- delay(500); // wait
- module.clearDisplay();
- // all at once
- matrix.setAll(true);
- delay(200);
- // Columns binary
- for(int i=0; i<MATRIX_NUMCOLUMNS; i++)
- {
- for(int j=0; j<=0xFF; j++)
- {
- matrix.setColumn(i,j);
- delay(5);
- }
- delay(200);
- }
- matrix.setAll(false);
- // One pixel, column by column
- for(int i=0; i<MATRIX_NUMCOLUMNS; i++)
- {
- for(int j=0; j<MATRIX_NUMROWS; j++)
- {
- matrix.setPixel(i,j, true);
- delay(50);
- matrix.setPixel(i,j, false);
- }
- }
- // One pixel, row by row
- for(int i=0; i<MATRIX_NUMROWS; i++)
- {
- for(int j=0; j<MATRIX_NUMCOLUMNS; j++)
- {
- matrix.setPixel(j,i, true);
- delay(50);
- matrix.setPixel(j,i, false);
- }
- }
- }
- void loop()
- {
- /*
- digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
- module.setupDisplay(true, 7); // sometimes noise on the line may change the intensity level
- int nTime=((millis()/1000)/60)*100+(millis()/1000)%60; // minutes+seconds as integer
- module.setDisplayToDecNumber(nTime, _BV(4)); // display dot on digit 4
- delay(500); // wait
- digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
- module.setDisplayToDecNumber(nTime, _BV(3)); // display colon on digit 3
- delay(500); // wait
- */
- static bool fLoop=true;
- // Fill/unfill columns
- for(int i=0; i<MATRIX_NUMROWS; i++)
- {
- for(int j=0; j<MATRIX_NUMCOLUMNS; j++)
- {
- matrix.setPixel(j, i, fLoop);
- delay(50);
- }
- /*
- for(int j=0; j<8; j++)
- {
- matrix.setPixel(i,j, false);
- delay(50);
- }
- */
- }
- fLoop=!fLoop;
- digitalWrite(LED_BUILTIN, fLoop); // turn the LED on (HIGH is the voltage level)
- }
- TM16xx/examples/TM16xxMatrix_Snake/ 0040777 0000000 0000000 00000000000 14026125130 014250 5 ustar 00 TM16xx/examples/TM16xxMatrix_Snake/TM16xxMatrix_Snake.ino 0100777 0000000 0000000 00000005760 14026127051 020350 0 ustar 00 /*
- * TM16xxMatrix_Snake Example
- *
- * Example to demonstrate TM16xxMatrix library.
- * Based on Snake.pde example of the MAX72xxPanel library
- *
- * Confirmed to work in the following environments:
- * - ATtiny44A using Arduino IDE 1.8.2 and ATtiny Microcontrolers (8MHz), DIN=9, CLK=10, 3976 bytes flash, 149 bytes RAM ==> 3962/149
- * - ATtiny44A using Arduino IDE 1.8.2 and ATTinyCore (8MHz, LTO enabled), DIN=9, CLK=10, 3974 bytes flash, 149 bytes RAM
- * - Arduino Nano and TM1637 5x6 mini-matrix using Arduino IDE 1.8.2, Nano (Old Bootloader), 4126 bytes flash, 149 bytes RAM
- * - WeMos D1-mini and TM1640 8x8 MatrixLED Shield using Arduino IDE 1.6.10: DIN=D7/13/MOSI, CLK=D5/14/SCK, 249176 bytes flash, 32356 bytes RAM
- *
- **/
- #include <TM1640.h>
- #include <TM16xxMatrix.h>
- //TM1640 module(9, 10); // DIN=9, CLK=10
- TM1640 module(13, 14); // For ESP8266/WeMos D1-mini: DIN=D7/13/MOSI, CLK=D5/14/SCK
- #define MATRIX_NUMCOLUMNS 8
- #define MATRIX_NUMROWS 8
- TM16xxMatrix matrix(&module, MATRIX_NUMCOLUMNS, MATRIX_NUMROWS); // TM16xx object, columns, rows
- const int pinRandom = A0;
- const int wait = 100; // In milliseconds
- const int length = 8;
- int x[length], y[length];
- int ptr, nextPtr;
- void setup()
- {
- // flash the matrix (using relative slow matrix.setAll)
- matrix.setAll(true);
- delay(200);
- module.setupDisplay(true, 2); // set intensity lower
- delay(400);
- matrix.setAll(false); // Note: module.clearDisplay() doesn't clear the offscreen bitmap!
- // Reset all variables
- for ( ptr = 0; ptr < length; ptr++ ) {
- x[ptr] = MATRIX_NUMCOLUMNS / 2;
- y[ptr] = MATRIX_NUMROWS / 2;
- }
- nextPtr = 0;
- randomSeed(analogRead(pinRandom)); // Initialize random generator
- }
- void loop()
- {
- // Shift pointer to the next segment
- ptr = nextPtr;
- nextPtr = next(ptr);
- matrix.setPixel(x[ptr], y[ptr], true); // Draw the head of the snake
- //matrix.write(); // Send bitmap to display
- delay(wait);
- if ( ! occupied(nextPtr) ) {
- matrix.setPixel(x[nextPtr], y[nextPtr], false); // Remove the tail of the snake
- }
- for ( int attempt = 0; attempt < 10; attempt++ ) {
- // Jump at random one step up, down, left, or right
- switch ( random(4) ) {
- case 0: x[nextPtr] = constrain(x[ptr] + 1, 0, MATRIX_NUMCOLUMNS - 1); y[nextPtr] = y[ptr]; break;
- case 1: x[nextPtr] = constrain(x[ptr] - 1, 0, MATRIX_NUMCOLUMNS - 1); y[nextPtr] = y[ptr]; break;
- case 2: y[nextPtr] = constrain(y[ptr] + 1, 0, MATRIX_NUMROWS - 1); x[nextPtr] = x[ptr]; break;
- case 3: y[nextPtr] = constrain(y[ptr] - 1, 0, MATRIX_NUMROWS - 1); x[nextPtr] = x[ptr]; break;
- }
- if ( ! occupied(nextPtr) ) {
- break; // The spot is empty, break out the for loop
- }
- }
- }
- boolean occupied(int ptrA) {
- for ( int ptrB = 0 ; ptrB < length; ptrB++ ) {
- if ( ptrA != ptrB ) {
- if ( equal(ptrA, ptrB) ) {
- return true;
- }
- }
- }
- return false;
- }
- int next(int ptr) {
- return (ptr + 1) % length;
- }
- boolean equal(int ptrA, int ptrB) {
- return x[ptrA] == x[ptrB] && y[ptrA] == y[ptrB];
- } TM16xx/keywords.txt 0100777 0000000 0000000 00000003473 14026127620 011502 0 ustar 00 #######################################
- # Syntax Coloring Map For TM16xx Lib
- #######################################
- #######################################
- # Datatypes (KEYWORD1)
- #######################################
- TM16xx KEYWORD1
- TM1620 KEYWORD1
- TM1628 KEYWORD1
- TM1637 KEYWORD1
- TM1638 KEYWORD1
- InvertedTM1638 KEYWORD1
- TM1638QYF KEYWORD1
- TM1640 KEYWORD1
- TM1650 KEYWORD1
- TM1668 KEYWORD1
- TM16xxMatrix KEYWORD1
- TM16xxDisplay KEYWORD1
- TM16xxMatrixGFX KEYWORD1
- TM16xxButtons KEYWORD1
- #######################################
- # Methods and Functions (KEYWORD2)
- #######################################
- setupDisplay KEYWORD2
- setDisplayToHexNumber KEYWORD2
- setDisplayToDecNumber KEYWORD2
- setDisplayToBinNumber KEYWORD2
- setDisplayDigit KEYWORD2
- setDisplay KEYWORD2
- clearDisplay KEYWORD2
- setDisplayToString KEYWORD2
- setLED KEYWORD2
- setLEDs KEYWORD2
- setRGBLED KEYWORD2
- setRGBLEDs KEYWORD2
- getButtons KEYWORD2
- sendCommand KEYWORD2
- sendData KEYWORD2
- sendChar KEYWORD2
- send KEYWORD2
- receive KEYWORD2
- setMirror KEYWORD2
- setPixel KEYWORD2
- getPixel KEYWORD2
- setPressTicks KEYWORD2
- attachRelease KEYWORD2
- attachClick KEYWORD2
- attachDoubleClick KEYWORD2
- attachLongPressStart KEYWORD2
- attachLongPressStop KEYWORD2
- attachDuringLongPress KEYWORD2
- tick KEYWORD2
- isLongPressed KEYWORD2
- getPressedTicks KEYWORD2
- #######################################
- # Constants (LITERAL1)
- #######################################
- TM1638_COLOR_RED LITERAL1
- TM1638_COLOR_GREEN LITERAL1
- TM1668_COLOR_NONE LITERAL1
- TM1668_COLOR_RED LITERAL1
- TM1668_COLOR_GREEN LITERAL1
- TM1668_COLOR_BLUE LITERAL1
- TM1668_COLOR_WHITE LITERAL1
- TM1668_COLOR_YELLOW LITERAL1
- TM1668_COLOR_PURPLE LITERAL1
- TM1668_COLOR_AQUA LITERAL1
- TM1668_DISPMODE_4x13 LITERAL1
- TM1668_DISPMODE_5x12 LITERAL1
- TM1668_DISPMODE_6x11 LITERAL1
- TM1668_DISPMODE_7x10 LITERAL1
- TM1650_DISPMODE_4x7 LITERAL1
- TM1650_DISPMODE_4x8 LITERAL1 TM16xx/library.properties 0100777 0000000 0000000 00000001021 14026127576 012651 0 ustar 00 name=TM16xx LEDs and Buttons
- version=0.4.3
- author=Maxint
- maintainer=Maxint R&D <mm_rd@maxint.nl>
- sentence=TM16xx library for Arduino. Supports LED and KEY modules based on TM1638 and similar chips.
- paragraph=Drive 7-segment displays or LED matrix displays that use a TM16xx chip. Read button states. Support modules/projects based on TM1620, TM1628, TM1637, TM1638, TM1640, TM1650, TM1668 and similar chips. Examples included.
- category=Signal Input/Output
- url=https://github.com/maxint-rd/TM16xx
- architectures=*
- includes=TM16xx.h TM16xx/README.md 0100777 0000000 0000000 00000035165 14026127545 010362 0 ustar 00 TM16xx
- Arduino TM16xx library for LED & KEY and LED Matrix modules based on TM1638, TM1637, TM1640 and similar chips. Simply use print() on 7-segment displays and use Adafruit GFX on matrix displays.
- TM16xx LEDs and Buttons library
- This Arduino library facilitates driving LED displays using TM16xx LED driver chips. The TM16xx chip family allows driving 7-segment LED displays or LED matrices. Next to built-in high-frequency LED multiplexing, they offer control of LED brightness. Most TM16xx chips also support reading key-scan data for button presses. Using this library you can simply use print() on a 7-segment display or use Adafruit GFX on a LED matrix. Currently this library supports the TM1620, TM1628, TM1630, TM1637, TM1638, TM1640, TM1650 and TM1668 chips. Note that there are similar chips made by other manufacturers that may be compatible with the Titan Micro chips. For instance: the HBS640 by WINRISE is compatible with the TM1640.
- Made by Maxint R&D. See https://github.com/maxint-rd/
- Initial version was based on the TM1638 library by Ricardo Batista. Further inspiration from the TM1637 library by Avishay, the Max72xxPanel library by Mark Ruys and the OneButton library by Matthias Hertel.
- Table of contents
- TM16xx chip features
- Library structure
- Basic usage
- TM16xxDisplay class
- TM16xxMatrix class
- TM16xxMatrixGFX class
- TM16xxButtons class
- New in this library
- Features & limitations
- More information
- TM16xx chip features
- Type segments x digits buttons interface
- TM1620 8 x 6 - 10 x 4 n/a DIN/CLK/STB
- TM1628 10 x 7 - 13 x 4 10 x 2 multi DIO/CLK/STB
- TM1630 7 x 5 - 8 x 4 7 x 1 multi DIO/CLK/STB
- TM1637 8 x 6 (common anode) 8 x 2 single DIO/CLK
- TM1638 10 x 8 8 x 3 multi DIO/CLK/STB
- TM1640 8 x 16 n/a DIN/CLK
- TM1650 8 x 4 7 x 4 single DIO/CLK (SDA/SCL)
- TM1668 10 x 7 - 13 x 4 10 x 2 multi DIO/CLK/STB
- See the documents folder for datasheets containing more information about these chips and their pinouts.
- Library structure
- This library has a layered structure to simplify the support of multiple TM16xx chips. By using a base class that provides a uniform API, your application doesn't need chip specific code. Likewise, the library can offer common functionality in display specific classes that support multiple chips.
- The figure below illustrates that concept:
- Layered structure
- Basic usage
- To use this library you need to include the class that matches the chip on your module and instantiate the object:
- #include <TM1638.h>
- TM1638 module(8, 9, 7); // DIO=8, CLK=9, STB=7
- In the setup() function you can set the intensity of the display, but that's not mandatory:
- void setup() {
- module.setupDisplay(true, 2); // on=true, intensity-2 (range 0-7)
- module.setDisplayToString("HALO"); // display simple text
- }
- In the loop() function you can use basic display methods provided by the base class:
- void loop() {
- int nTime = ((millis() / 1000) / 60) * 100 + (millis() / 1000) % 60; // convert time to minutes+seconds as integer
- module.setDisplayToDecNumber(nTime, _BV(4)); // display milliseconds with dot on digit 4
- }
- For the easy to use print() method and more advance display methods you can use the TM16xxDisplay class.
- The TM16xx chip makes it easy to see if a button is pressed. To check if a button was pressed you can use the getButtons() method:
- uint32_t dwButtons=module.getButtons();
- Serial.println(dwButtons, HEX);
- Please note that while you don't need to write any code for debouncing, the button state may be reset when you display something. For advanced detection of button clicks, double clicks and long presses you can use the TM16xxButtons class.
- TM16xxDisplay class
- The TM16xxDisplay class adds some bytes to the memory footprint, but it provides the familiar easy to use print() and println() functions. Next to that it also provides some more advanced display methods. To use that class on top of the base class, all you need to do is instantiate it, refering to the chip specific class:
- TM1638 module(8, 9, 7); // DIO=8, CLK=9, STB=7
- TM16xxDisplay display(&module, 8); // TM16xx object, 8 digits
- Simple print example using the TM16xxDisplay class:
- #include <TM1638.h>
- #include <TM16xxDisplay.h>
- TM1638 module(8, 9, 7); // DIO=8, CLK=9, STB=7
- TM16xxDisplay display(&module, 8); // TM16xx object, 8 digits
- void setup() {
- display.println(F("HELLO !"));
- }
- int nCount=0;
- void loop() {
- delay(1000);
- display.print("Count:");
- display.println(nCount++);
- }
- See TM16xxDisplay.h for the provided methods.
- TM16xxMatrix class
- The TM16xxMatrix class provides basic methods for using a single LED-matrix module. For more advanced graphics use the TM16xxMatrixGFX class. To use the TM16xxMatrix class on top of the base class, all you need to do is instantiate it, refering to the chip specific class:
- TM1640 module(9, 10); // DIN=9, CLK=10
- #define MATRIX_NUMCOLUMNS 16
- #define MATRIX_NUMROWS 8
- TM16xxMatrix matrix(&module, MATRIX_NUMCOLUMNS, MATRIX_NUMROWS); // TM16xx object, columns, rows
- Note that the TM1640 has sufficient outputs to drive two 8x8 matrices.
- These methods can be used to set the pixels of the matrix:
- matrix.setAll(true); // set all pixels on
- matrix.setPixel(5,6, true); // set one pixel on
- matrix.setPixel(3,2, false); // set another pixel off
- See TM16xxMatrix.h for the provided methods.
- TM16xxMatrixGFX class
- The TM16xxMatrixGFX class implements the popular Adafruit GFX interface to drive one or more TM16xx based LED-matrix modules. To use the TM16xxMatrixGFX class you first need to include the proper header files:
- #include <Adafruit_GFX.h>
- #include <TM1640.h>
- #include <TM16xxMatrixGFX.h>
- Then you can instantiate the TM16xxMatrixGFX class, refering to the chip specific class:
- TM1640 module(13, 14); // For ESP8266/WeMos D1-mini: DIN=D7/13/MOSI, CLK=D5/14/SCK
- #define MATRIX_NUMCOLUMNS 8
- #define MATRIX_NUMROWS 8
- TM16xxMatrixGFX matrix(&module, MATRIX_NUMCOLUMNS, MATRIX_NUMROWS); // TM16xx object, columns, rows
- Note that the TM1640 has sufficient outputs to drive two 8x8 matrices. The WeMOS D1 Mini Matrix LED Shield also uses the TM1640, but has only one 8x8 matrix.
- These methods can be used to draw on the matrix:
- matrix.setIntensity(1); // Use a value between 0 and 7 for brightness
- matrix.fillScreen(LOW); // Clear the matrix
- matrix.drawPixel(1, 4, HIGH); // set one pixel in the memory bitmap on
- matrix.write(); // Send the memory bitmap to the display
- In addition all the Adafruit GFX methods can be used, e.g.:
- matrix.drawChar(0, 0, 'A', HIGH, LOW, 1);
- matrix.drawLine(0, matrix. height(), matrix.width(), 0, HIGH);
- matrix.drawRect(0, 0, 6, 6, HIGH);
- Multiple identical modules can be combined to form a large matrix. The data line can be shared to reduce the number of pins:
- TM1640 module(D7, D5); // For ESP8266/WeMos D1-mini: DIN=D7/13/MOSI, CLK=D5/14/SCK
- TM1640 module2(D7, D6); // For ESP8266/WeMos D1-mini: shared DIN=D7/13/MOSI, different CLK
- TM16xx * modules[]={&module,&module2}; // put modules in an array
- TM16xxMatrixGFX matrix(modules, MODULE_SIZECOLUMNS, MODULE_SIZEROWS, 2, 1); // modules, size of each module, size combined
- See Adafruit GFX documentation and TM16xxMatrixGFX.h for the provided methods. See the library examples for more information.
- TM16xxButtons class
- The TM16xxButtons class enlarges the footprint a bit, but based on the popular OneButton library library, it adds more advanced methods to use buttons. Next to simply polling the state of each button, you can define callback functions that will be called when a button is released, clicked, double-clicked or long pressed. To use this class on top of the base class, all you need to do is include the proper headers and instantiate the buttons object, refering to the chip specific class, for example:
- #include <TM1638.h>
- #include <TM16xxButtons.h>
- TM1638 module(8, 9, 7); // DIO=8, CLK=9, STB=7
- TM16xxButtons buttons(&module); // TM16xx object
- Then you define the functions you want to use to handle the button events:
- void fnClick(byte nButton)
- { // byte nButton is the button-number (first button is number 0)
- Serial.print(F("Button "));
- Serial.print(nButton);
- Serial.println(F(" click."));
- }
- In setup() you need to attach the callback function:
- void setup()
- {
- .
- .
- buttons.attachClick(fnClick);
- }
- (BTW. Besides a click function, you can also attach a function to handle release, doubleclick and longpress events).
- In loop() you need to call the tick() function that detects all state changes and calls the callback functions as needed:
- void loop()
- {
- buttons.tick();
- .
- .
- // do your other things
- }
- To implement a shift key, you can use the isPressed() function. See TM16xxButtons.h for the provided methods and the Button clicks example for more information.
- New in this library
- Added library functionality:
- Revised library structure to simplify support of other TM16xx chips.
- Basic functionality in base class for a uniform API.
- Support for TM1637. Note: TM1637 does not support simultaneous button presses. (Method derived from TM1637 library but using pins in standard output mode when writing).
- Support for TM1668. Note: TM1668 can be used in 10x7 - 13x4 display modes. Datasheet partly translated.
- Support for TM1650. Note: TM1650 can be used in 8x4 or 7x4 display mode. Datasheet fully translated.
- Reduced required RAM memory by using PROGMEM fonts.
- Support for ATtiny44A and ESP8266 in addition to regular Arduinos.
- Separate classes for LED matrix and advanced LED display support.
- Simple display of text and numbers on 7-segment displays using familiar print() and println() methods.
- Support for the Adafruit GFX graphics library for advanced graphics on a LED matrix.
- Full support for QYF-TM1638 module (8 digit common anode LED display and 4x4 keypad)
- Support for TM1638 in Anode Mode (10 digit common anode LED 8 segment display) [TM1638Anode.h]
- Support for combining multiple modules into one large Adafruit GFX matrix.
- Support for scanning all possible keys (K1, K2 and K3 lines) on TM1638.
- Support for release, click, doubleclick and long press button detection using callback functions.
- Added library examples.
- Support for TM1620 (thanks @eddwhite)
- Support for TM1630 (thanks @tokuhira)
- Support for TM1628. Note: TM1628 can be used in 10x7 - 13x4 display modes.
- Functionality in original library by Ricardo Batista:
- Support for the TM1638 and TM1640, including common anode TM1638 module;
- Helper methods for displaying numbers in decimal, hexadecimal and binary;
- Support for multiple chained TM1638 and for TM1638 in inverted position;
- Support for dimming the display and LEDs and for writing text;
- Reading simultaneous button presses on TM1638;
- Features & limitations
- The current version of this library supports ESP8266/ESP32, Atmel ATmega (e.g. ATmega328 and ATmega168) and Atmel ATtiny MCUs. Due to the required memory, the smallest ATtiny MCU supported is the ATtiny44. Please let me know if you've successfully used this library with other MCUs.
- The TM16xx chips offer no support for daisychaining multiple chips, but when separate Clk or Latch lines are used the Din line can be shared for combined displays.
- The library doesn't support combining multiple 7-segment modules into one display, but it is possible to define multiple display objects for multiple different modules. See the TM1638_TM1637ex_two_modules example.
- The TM16xxMatrixGFX class does support combining multiple LED Matrix module into one large matrix. Please note that the TM1640 supports up to 16 digits or an 8x16 LED matrix.
- The QYF-TM1638 module (TM138 with common anode display) is fully supported. Please note that while multiple buttons can be pressed, pressing more than two buttons can give faulty results due to the lack of short-out preventing diodes on the module.
- The popular TM1638 LED & KEY module comes in a number of varieties. My version has some odd button wiring sequence: S1=KS1, S5=KS2, S2=KS3, S6=KS4, S3=KS5, S7=KS6, S4=KS7, S8=KS8
- The TM1668 class has experimental support for using RGB LEDs on Grids 5-7. Some information about the wiring can be found in the example code. Most likely future versions will have a specific class for using RGB LEDs. The TM1680 has 8x24 outputs which sounds ideal for creating a 8x8 RGB matrix. Unfortunately these chips don't support individual LED brightness, only intensity of the whole display.
- The TM1650 datasheet mentions SDA and SCL pins. The used protocol resembles I2C, but lacks addressing. For that reason this library doesn't use the I2C Wire library, but (slow) bitbanging using digitalWrite.
- The WeMOS D1 mini Matrix LED Shield and the TM1640 Mini LED Matrix 8x16 by Maxint R&D have R1 on the right-top. Call setMirror(true) to reverse the x-mirrorring.
- When using TM16xxButtons, the amount of memory used can become too large. To preserve RAM memory on smaller MCUs such as the ATtiny44A, the number of buttons is limited to 8 on the ATtiny MCUs. This can be changed by setting the maximum in the TM16xxButtons.h header file:
- #define TM16XX_BUTTONS_MAXBUTTONS 8 // Note: changing this define requires recompilation of the library
- An alternative RAM preserving implementation using dynamic memory allocation is optional, but not suitable for small MCUs as using malloc/free will increase the required FLASH program space by over 600 bytes. Modify the TM16XX_OPT_BUTTONS_... defines in the header file at your own risk.
- More information
- Examples
- See the library examples for more information on how to use this library. See also the original examples by Ricardo Batista. Most will still work or only require minor changes.
- Links
- Manufacturer: Titan Micro Electronics - LED driver datasheets
- Original TM1638/TM1640 library: https://github.com/rjbatista/tm1638-library
- TM1637 library used for reference: https://github.com/avishorp/TM1637
- A TM1637 library optimized for speed and size: https://github.com/Erriez/ErriezTM1637
- TM1650 library that uses the Wire interface: https://github.com/mozgy/Mozz_TM1650
- MAX7219 LED Matrix library: https://github.com/markruys/arduino-Max72xxPanel
- OneButton multi-state buttons: https://github.com/mathertel/OneButton
- Adafruit GFX library: https://github.com/adafruit/Adafruit-GFX-Library
- Adafruit GFX documentation: https://learn.adafruit.com/adafruit-gfx-graphics-library
- Matrix transposition used in TM1638QYF: https://www.chessprogramming.org/Flipping_Mirroring_and_Rotating#Anti-Diagonal
- Disclaimer
- All code on this GitHub account, including this library is provided to you on an as-is basis without guarantees and with all liability dismissed. It may be used at your own risk. Unfortunately I have no means to provide support.
- TM16xx/src/ 0040777 0000000 0000000 00000000000 14026124625 007654 5 ustar 00 TM16xx/src/TM1640.cpp 0100777 0000000 0000000 00000004146 14026127121 011212 0 ustar 00 /*
- TM1640.cpp - Library implementation for TM1640.
- Copyright (C) 2011 Ricardo Batista (rjbatista <at> gmail <dot> com)
- Adjusted for TM1640 by Maxint R&D, based on orignal code. See https://github.com/maxint-rd/
- 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"
- TM1640::TM1640(byte dataPin, byte clockPin, byte numDigits, boolean activateDisplay, byte intensity)
- : TM16xx(dataPin, clockPin, dataPin, TM1640_MAX_POS, numDigits, activateDisplay, intensity)
- { // NOTE: Like the TM1637, the TM1640 only has DIO and CLK. Therefor the DIO-pin is initialized as strobe in the constructor
- clearDisplay();
- setupDisplay(activateDisplay, intensity);
- }
- /*
- void TM1640::bitDelay()
- {
- //delayMicroseconds(5);
- }
- */
- void TM1640::start()
- { // if needed derived classes can use different patterns to start a command (eg. for TM1637)
- // Datasheet: The starting condition of data input is: when CLK is high, the DIN becomes low from high;
- digitalWrite(dataPin, LOW);
- digitalWrite(clockPin, LOW);
- bitDelay();
- }
- void TM1640::stop()
- { // if needed derived classes can use different patterns to stop a command (eg. for TM1637)
- // datasheet: the ending condition is: when CLK is high, the DIN becomes high from low.
- digitalWrite(clockPin, HIGH);
- digitalWrite(dataPin, HIGH);
- bitDelay();
- }
- void TM1640::send(byte data)
- {
- // MOLE 180514: TM1640 wants data and clock to be low after sending the data
- TM16xx::send(data);
- digitalWrite(clockPin, LOW); // first clock low
- digitalWrite(dataPin, LOW); // then data low
- bitDelay();
- } TM16xx/src/TM1640.h 0100777 0000000 0000000 00000002503 14026130322 010647 0 ustar 00 /*
- TM1640.h - Library for TM1640.
- Copyright (C) 2011 Ricardo Batista <rjbatista at gmail dot com>
- Adjusted for TM1640 by Maxint R&D, based on orignal code. See https://github.com/maxint-rd/
- 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"
- #define TM1640_MAX_POS 16
- class TM1640 : public TM16xx
- {
- public:
- // Instantiate a TM1640 module specifying data and clock pins, number of digits, the display state, the starting intensity (0-7).
- TM1640(byte dataPin, byte clockPin, byte numDigits=16, boolean activateDisplay = true, byte intensity = 7);
- protected:
- //virtual void bitDelay();
- virtual void start();
- virtual void stop();
- virtual void send(byte data);
- };
- #endif
- TM16xx/src/TM16xx.cpp 0100777 0000000 0000000 00000014447 14026127460 011441 0 ustar 00 /*
- TM16xx.h - Library for TM1637, TM1638 and similar chips.
- Modified by Maxint R&D. See https://github.com/maxint-rd/
- 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 maxDisplays, byte digits, boolean activateDisplay, byte intensity)
- {
- this->dataPin = dataPin;
- this->clockPin = clockPin;
- this->strobePin = strobePin;
- this->_maxDisplays = maxDisplays;
- this->digits = digits;
- pinMode(dataPin, OUTPUT);
- pinMode(clockPin, OUTPUT);
- pinMode(strobePin, OUTPUT);
- digitalWrite(strobePin, HIGH);
- digitalWrite(clockPin, HIGH);
- //sendCommand(TM16XX_CMD_DISPLAY | (activateDisplay ? 8 : 0) | min(7, intensity)); // display command: on or intensity
- /*
- sendCommand(TM16XX_CMD_DATA_AUTO); // data command: set data mode to auto-increment write mode
- start();
- send(TM16XX_CMD_ADDRESS); // address command + address C0H
- for (int i = 0; i < 16; i++) { // TM1638 and TM1640 have 16 data addresses, TM1637 and TM1668 have less, but will wrap.
- send(0x00);
- }
- stop();
- */
- // Note: calling these methods should be done in constructor of derived class in order to use properly initialized members!
- /*
- clearDisplay();
- setupDisplay(activateDisplay, intensity);
- */
- }
- void TM16xx::setupDisplay(boolean active, byte intensity)
- {
- sendCommand(TM16XX_CMD_DISPLAY | (active ? 8 : 0) | min(7, intensity));
- }
- void TM16xx::clearDisplay()
- { // Clear all data registers. The number of registers depends on the chip.
- // TM1638 (10x8): 10 segments per grid, stored in two bytes. The first byte contains the first 8 display segments, second byte has seg9+seg10 => 16 bytes
- // TM1640 (8x16): one byte per grid => 16 bytes
- // TM1637 (8x6): one byte per grid => 6 bytes
- // TM1668 (10x7 - 14x3): two bytes per grid => 14 bytes
- sendCommand(TM16XX_CMD_DATA_AUTO); // set auto increment addressing mode
- // send the address followed by bulk-sending of the data to clear the display memory
- start();
- send(TM16XX_CMD_ADDRESS);
- for (int i = 0; i < _maxDisplays; i++) {
- send(0x00);
- if(_maxSegments>8)
- send(0x00); // send second byte (applicable to TM1638 and TM1668)
- }
- stop();
- }
- void TM16xx::setSegments(byte segments, byte position)
- { // set 8 leds on common grd as specified
- // TODO: support 10-14 segments on chips like TM1638/TM1668
- if(position<_maxDisplays)
- sendData(position, segments);
- //sendData(TM16XX_CMD_ADDRESS | position, segments);
- }
- void TM16xx::sendChar(byte pos, byte data, boolean dot)
- {
- /*
- if(pos<_maxDisplays)
- sendData(pos, data | (dot ? 0b10000000 : 0));
- */
- setSegments(data | (dot ? 0b10000000 : 0), pos);
- }
- void TM16xx::setDisplayDigit(byte digit, byte pos, boolean dot, const byte numberFont[])
- {
- sendChar(pos, pgm_read_byte_near(numberFont + (digit & 0xF)), dot);
- }
- void TM16xx::setDisplayToDecNumber(int nNumber, byte bDots) // byte bDots=0
- { // Function to display a decimal number on a n-digit clock display.
- // Kept simple to fit in ATtiny44A
- // For extended display features use the TM16xxDisplay class
- // TODO: support large displays such as 8segx16 on TM1640
- for(byte nPos=1; nPos<=digits; nPos++)
- {
- setDisplayDigit(nNumber % 10, digits - nPos, bDots&_BV(nPos));
- nNumber/=10;
- }
- }
- void TM16xx::clearDisplayDigit(byte pos, boolean dot)
- {
- sendChar(pos, 0, dot);
- }
- void TM16xx::setDisplay(const byte values[], byte size)
- { // send an array of values to the display
- for (byte i = 0; i < size; i++) {
- sendChar(i, pgm_read_byte_near(values+i), 0);
- }
- }
- void TM16xx::setDisplayToString(const char* string, const word dots, const byte pos, const byte font[])
- {
- for (int i = 0; i < digits - pos; i++) {
- if (string[i] != '\0') {
- sendChar(i + pos, pgm_read_byte_near(font+(string[i] - 32)), (dots & (1 << (digits - i - 1))) != 0);
- } else {
- break;
- }
- }
- }
- // key-scanning method, implemented in chip specific derived class
- uint32_t TM16xx::getButtons()
- { // return state of up to 32 keys.
- return(0);
- }
- //
- // Protected methods
- //
- void TM16xx::bitDelay()
- { // if needed derived classes can add a delay (eg. for TM1637)
- //delayMicroseconds(50);
- }
- void TM16xx::start()
- { // if needed derived classes can use different patterns to start a command (eg. for TM1637)
- digitalWrite(strobePin, LOW);
- bitDelay();
- }
- void TM16xx::stop()
- { // if needed derived classes can use different patterns to stop a command (eg. for TM1637)
- digitalWrite(strobePin, HIGH);
- bitDelay();
- }
- void TM16xx::send(byte data)
- {
- // MMOLE 180203: shiftout does something, but is not okay (tested on TM1668)
- //shiftOut(dataPin, clockPin, LSBFIRST, data);
- for (int i = 0; i < 8; i++) {
- digitalWrite(clockPin, LOW);
- bitDelay();
- digitalWrite(dataPin, data & 1 ? HIGH : LOW);
- bitDelay();
- data >>= 1;
- digitalWrite(clockPin, HIGH);
- bitDelay();
- }
- bitDelay(); // NOTE: TM1638 specifies a Twait between bytes of minimal 1us.
- }
- void TM16xx::sendCommand(byte cmd)
- {
- start();
- send(cmd);
- stop();
- }
- void TM16xx::sendData(byte address, byte data)
- {
- sendCommand(TM16XX_CMD_DATA_FIXED); // use fixed addressing for data
- start();
- send(TM16XX_CMD_ADDRESS | address); // address command + address
- send(data);
- stop();
- }
- 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);
- bitDelay(); // NOTE: on TM1637 reading keys should be slower than 250Khz (see datasheet p3)
- if (digitalRead(dataPin)) {
- temp |= 0x80;
- }
- digitalWrite(clockPin, HIGH);
- bitDelay();
- }
- // Pull-up off
- pinMode(dataPin, OUTPUT);
- digitalWrite(dataPin, LOW);
- return temp;
- } TM16xx/src/TM16xx.h 0100777 0000000 0000000 00000007733 14026127460 011106 0 ustar 00 /*
- TM16xx.h - Library for TM1637, TM1638 and similar chips.
- Copyright (C) 2011 Ricardo Batista <rjbatista at gmail dot com>
- Modified by Maxint R&D. See https://github.com/maxint-rd/
- 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
- #if !defined(max)
- // MMOLE 180325:
- // min, max are no macro in ESP core 2.3.9 libraries, see https://github.com/esp8266/Arduino/issues/398
- #define min(a,b) ((a)<(b)?(a):(b))
- #define max(a,b) ((a)>(b)?(a):(b))
- #endif
- #define TM16XX_CMD_DATA_AUTO 0x40
- #define TM16XX_CMD_DATA_READ 0x42 // command to read data used on two wire interfaces of TM1637
- #define TM16XX_CMD_DATA_FIXED 0x44
- #define TM16XX_CMD_DISPLAY 0x80
- #define TM16XX_CMD_ADDRESS 0xC0
- #include "TM16xxFonts.h"
- class TM16xx
- {
- public:
- /**
- * Instantiate a tm16xx module specifying data, clock and stobe pins,
- * the maximum number of displays supported by the chip,
- * the number of digits used to display numbers or text,
- * display state and the starting intensity (0-7).
- */
- TM16xx(byte dataPin, byte clockPin, byte strobePin, byte maxDisplays, byte digits, 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);
- /** Clear the display */
- virtual void clearDisplay();
- virtual void setSegments(byte segments, byte position);
- // Basic display functions. For additional display features use the TM16xxDisplay class
- /** Set a single display at pos (starting at 0) to a digit (left to right) */
- virtual void setDisplayDigit(byte digit, byte pos=0, boolean dot=false, const byte numberFont[] = TM16XX_NUMBER_FONT);
- /** Set the display to a decimal number */
- virtual void setDisplayToDecNumber(int nNumber, byte bDots=0);
- /** Clear a single display at pos (starting at 0, left to right) */
- virtual void clearDisplayDigit(byte pos, boolean dot=false);
- /** Set the display to the values (left to right) */
- virtual void setDisplay(const byte values[], byte size=8);
- /** 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 sendChar(byte pos, byte data, boolean dot); // made public to allow calling from TM16xxDisplay
- // Key-scanning functions
- // Note: not all TM16xx chips support key-scanning and sizes are different per chip
- // Up to 32 key states are supported, but specific chips may support less keys or less combinations
- // The chip specific derived class method will return a 32-bit value representing the state of each key, containing 0 if no key is pressed
- virtual uint32_t getButtons(); // return state of up to 32 keys.
- protected:
- // virtual void sendChar(byte pos, byte data, boolean dot);
- virtual void bitDelay();
- virtual void start();
- virtual void stop();
- virtual void send(byte data);
- virtual void sendCommand(byte led);
- virtual void sendData(byte add, byte data);
- virtual byte receive();
- byte _maxDisplays=2; // maximum number of digits (grids), chip-dependent
- byte _maxSegments=8; // maximum number of segments per display, chip-dependent
- byte digits; // number of digits in the display, module dependent
- byte dataPin;
- byte clockPin;
- byte strobePin;
- };
- #endif TM16xx/src/TM16xxButtons.cpp 0100777 0000000 0000000 00000020630 14026127460 013007 0 ustar 00 /*
- TM16xxButtons.cpp - Buttons class for TM16xx.
- The TM16xxButtons class supports the key-scanning features of TM16xx chips, such as TM1637 or TM1638.
- It extends the getButtons() function of the base class and provides these features:
- - setting callback functions
- - multi-state keys (similar to OneButton): Press, LongPress, Click, Doubleclick
- These are some TM16xx chips that support key-scanning:
- TM1637 8 x 2 single DIO/CLK
- TM1638 8 x 3 multi DIO/CLK/STB
- TM1668 10 x 2 multi DIO/CLK/STB
- Made by Maxint R&D. See https://github.com/maxint-rd/
- Partially based on OneButton library by Matthias Hertel. See https://github.com/mathertel/OneButton
- */
- //#define TM16XX_DEBUG 1
- #include "TM16xxButtons.h"
- // constructor
- TM16xxButtons::TM16xxButtons(TM16xx *pTM16xx, byte nNumButtons) : _nNumButtons(nNumButtons), _pTM16xx(pTM16xx)
- {
- // TODO: reduce memory by using dynamic memory allocation instead of static arrays for button states
- // requires additional constructor parameter to allow less than TM16XX_BUTTONS_MAXBUTTONS
- //_pTM16xx=pTM16xx;
- #if(TM16XX_OPT_BUTTONS_MALLOC)
- _state=malloc(_nNumButtons*sizeof(byte));
- _startTime=malloc(_nNumButtons*sizeof(unsigned long));
- _stopTime=malloc(_nNumButtons*sizeof(unsigned long));
- #endif
- reset();
- }
- // explicitly set the number of millisec that have to pass by before a click is
- // detected.
- void TM16xxButtons::setClickTicks(int ticks)
- {
- _clickTicks = ticks;
- } // setClickTicks
- // explicitly set the number of millisec that have to pass by before a long
- // button press is detected.
- void TM16xxButtons::setLongPressTicks(int ticks)
- {
- _longPressTicks = ticks;
- } // setLongPressTicks
- #if(TM16XX_OPT_BUTTONS_EVENT)
- // set function for release event
- void TM16xxButtons::attachEventHandler(callbackTM16xxButtonsEvent newFunction)
- {
- _eventFunc = newFunction;
- } // attachEventHandler
- #else
- void TM16xxButtons::attachRelease(callbackTM16xxButtons newFunction)
- {
- _releaseFunc = newFunction;
- } // attachRelease
- // set function for click event
- void TM16xxButtons::attachClick(callbackTM16xxButtons newFunction)
- {
- _clickFunc = newFunction;
- } // attachClick
- // set function for doubleClick event
- void TM16xxButtons::attachDoubleClick(callbackTM16xxButtons newFunction)
- {
- _doubleClickFunc = newFunction;
- } // attachDoubleClick
- // set function for longPressStart event
- void TM16xxButtons::attachLongPressStart(callbackTM16xxButtons newFunction)
- {
- _longPressStartFunc = newFunction;
- } // attachLongPressStart
- // set function for longPressStop event
- void TM16xxButtons::attachLongPressStop(callbackTM16xxButtons newFunction)
- {
- _longPressStopFunc = newFunction;
- } // attachLongPressStop
- // set function for during longPress event
- void TM16xxButtons::attachDuringLongPress(callbackTM16xxButtons newFunction)
- {
- _duringLongPressFunc = newFunction;
- } // attachDuringLongPress
- #endif
- // function to get the current long pressed state
- bool TM16xxButtons::isPressed(byte nButton)
- {
- if(nButton>=_nNumButtons) return(false);
- return(_state[nButton]==TM16XX_BUTTONS_STATE_PRESSED || _state[nButton]==TM16XX_BUTTONS_STATE_DBLPRESS || _state[nButton]==TM16XX_BUTTONS_STATE_LPRESS);
- }
- // function to get the current long pressed state
- bool TM16xxButtons::isLongPressed(byte nButton)
- {
- if(nButton>=_nNumButtons) return(false);
- return(_state[nButton]==TM16XX_BUTTONS_STATE_LPRESS);
- }
- int TM16xxButtons::getPressedTicks(byte nButton)
- {
- if(nButton>=_nNumButtons) return(0);
- return((_stopTime[nButton] - _startTime[nButton])); // uint16_t subtraction may overflow, but is still fine 0x01 - 0xFC = 0x05
- }
- void TM16xxButtons::reset(void)
- {
- for(byte n=0; n<_nNumButtons; n++)
- {
- _state[n] = TM16XX_BUTTONS_STATE_START; // restart.
- _startTime[n] = 0;
- _stopTime[n] = 0;
- }
- }
- /**
- * @brief Check input of the configured pin and then advance the finite state
- * machine (FSM).
- */
- uint32_t TM16xxButtons::tick(void)
- { // update the state of each button and call callback functions as needed
- uint32_t dwButtons=_pTM16xx->getButtons();
- #ifdef TM16XX_DEBUG
- Serial.print(F("TM16xxButtons: "));
- Serial.print(dwButtons, HEX);
- Serial.print(F(", state: "));
- for(byte n=0; n<_nNumButtons; n++)
- {
- tick(n, (dwButtons&bit(n))>0); // MMOLE 181103: _BV only works on 16-bit values!
- Serial.print(_state[n]);
- }
- Serial.print(F(" "));
- #else
- for(byte n=0; n<_nNumButtons; n++)
- tick(n, dwButtons&bit(n)); // MMOLE 181103: _BV only works on 16-bit values!
- #endif
- return(dwButtons);
- }
- /**
- * @brief Advance the finite state machine (FSM) using the given level.
- */
- void TM16xxButtons::tick(byte nButton, bool activeLevel)
- {
- //unsigned long now = millis(); // current (relative) time in msecs.
- uint16_t now = (uint16_t) millis(); // current (relative) time in msecs. To safe RAM we only use the bottom word (16 bits for instead of 32 for approx. 50 days)
- // Implementation of the state machine
- switch(_state[nButton])
- {
- case TM16XX_BUTTONS_STATE_START: // waiting for button being pressed.
- if (activeLevel)
- {
- _state[nButton] = TM16XX_BUTTONS_STATE_PRESSED; // step to pressed state
- _startTime[nButton] = now; // remember starting time
- } // if
- break;
- case TM16XX_BUTTONS_STATE_PRESSED: // waiting for button being released.
- if (!activeLevel)
- {
- _state[nButton] = TM16XX_BUTTONS_STATE_RELEASED; // step to released state
- _stopTime[nButton] = now; // remember stopping time
- #if(TM16XX_OPT_BUTTONS_EVENT)
- if (_eventFunc)
- _eventFunc(TM16XX_BUTTONS_EVENT_RELEASE, nButton);
- #else
- if (_releaseFunc)
- _releaseFunc(nButton);
- #endif
- }
- else if ((activeLevel) && ((unsigned long)(now - _startTime[nButton]) > _longPressTicks))
- {
- _state[nButton] = TM16XX_BUTTONS_STATE_LPRESS; // step to long press state
- _stopTime[nButton] = now; // remember stopping time
- #if(TM16XX_OPT_BUTTONS_EVENT)
- if (_eventFunc)
- {
- _eventFunc(TM16XX_BUTTONS_EVENT_LONGPRESSSTART, nButton);
- _eventFunc(TM16XX_BUTTONS_EVENT_LONGPRESSBUSY, nButton);
- }
- #else
- if (_longPressStartFunc)
- _longPressStartFunc(nButton);
- if (_duringLongPressFunc)
- _duringLongPressFunc(nButton);
- #endif
- } else {
- // wait. Stay in this state.
- } // if
- break;
- case TM16XX_BUTTONS_STATE_RELEASED: // waiting for button being pressed the second time or timeout.
- #if(TM16XX_OPT_BUTTONS_EVENT)
- if ((unsigned long)(now - _startTime[nButton]) > _clickTicks)
- #else
- if (_doubleClickFunc == NULL || (unsigned long)(now - _startTime[nButton]) > _clickTicks)
- #endif
- {
- // this was only a single short click
- #if(TM16XX_OPT_BUTTONS_EVENT)
- if (_eventFunc)
- _eventFunc(TM16XX_BUTTONS_EVENT_CLICK, nButton);
- #else
- if (_clickFunc)
- _clickFunc(nButton);
- #endif
- _state[nButton] = TM16XX_BUTTONS_STATE_START; // restart.
- }
- else if ((activeLevel))
- {
- _state[nButton] = TM16XX_BUTTONS_STATE_DBLPRESS; // step to doubleclick state
- _startTime[nButton] = now; // remember starting time
- } // if
- break;
- case TM16XX_BUTTONS_STATE_DBLPRESS: // waiting for button being released finally.
- if ((!activeLevel))
- {
- // this was a 2 click sequence.
- _state[nButton] = TM16XX_BUTTONS_STATE_START; // restart.
- _stopTime[nButton] = now; // remember stopping time
- #if(TM16XX_OPT_BUTTONS_EVENT)
- if (_eventFunc)
- {
- _eventFunc(TM16XX_BUTTONS_EVENT_RELEASE, nButton);
- _eventFunc(TM16XX_BUTTONS_EVENT_DOUBLECLICK, nButton);
- }
- #else
- if (_releaseFunc)
- _releaseFunc(nButton);
- if (_doubleClickFunc)
- _doubleClickFunc(nButton);
- #endif
- } // if
- break;
- case TM16XX_BUTTONS_STATE_LPRESS: // waiting for button being release after long press.
- if (!activeLevel)
- {
- _state[nButton] = TM16XX_BUTTONS_STATE_START; // restart.
- _stopTime[nButton] = now; // remember stopping time
- #if(TM16XX_OPT_BUTTONS_EVENT)
- if (_eventFunc)
- {
- _eventFunc(TM16XX_BUTTONS_EVENT_RELEASE, nButton);
- _eventFunc(TM16XX_BUTTONS_EVENT_LONGPRESSSTOP, nButton);
- }
- #else
- if (_releaseFunc)
- _releaseFunc(nButton);
- if (_longPressStopFunc)
- _longPressStopFunc(nButton);
- #endif
- }
- else
- {
- // button is being long pressed
- #if(TM16XX_OPT_BUTTONS_EVENT)
- if (_eventFunc)
- _eventFunc(TM16XX_BUTTONS_EVENT_LONGPRESSBUSY, nButton);
- #else
- if (_duringLongPressFunc)
- _duringLongPressFunc(nButton);
- #endif
- } // if
- break;
- } // switch
- } // TM16xxButtons.tick(nButton) TM16xx/src/TM16xxButtons.h 0100777 0000000 0000000 00000012202 14026127460 012450 0 ustar 00 /*
- TM16xxButtons.h - Buttons class for TM16xx.
- The TM16xxButtons class supports the key-scanning features of TM16xx chips, such as TM1637 or TM1638.
- It extends the getButtons() function of the base class and provides these features:
- - setting callback functions
- - multi-state keys (similar to OneButton): Press, LongPress, Click, Doubleclick
- These are some TM16xx chips that support key-scanning:
- TM1637 8 x 2 single DIO/CLK
- TM1638 8 x 3 multi DIO/CLK/STB
- TM1668 10 x 2 multi DIO/CLK/STB
- Made by Maxint R&D. See https://github.com/maxint-rd/
- Partially based on OneButton library by Matthias Hertel. See https://github.com/mathertel/OneButton
- */
- #ifndef _TM16XX_BUTTONS_H
- #define _TM16XX_BUTTONS_H
- #include "TM16xx.h"
- #define TM16XX_OPT_BUTTONS_EVENT 0 // use a single callback function instead of multiple (more flash, less heap)
- #define TM16XX_OPT_BUTTONS_MALLOC 0 // use malloc to reserve button-state memory (more flash, less heap)
- #ifndef TM16XX_BUTTONS_MAXBUTTONS
- #if defined(__AVR_ATtiny85__) || defined(__AVR_ATtiny13__) || defined(__AVR_ATtiny44__)
- #define TM16XX_BUTTONS_MAXBUTTONS 8 // WARNING: changing this define outside of the header file requires recompilation of the library;
- // using without full recompile may cause very obscure crashes/resets
- #else
- #define TM16XX_BUTTONS_MAXBUTTONS 32 // maximum number of buttons supported (determines heap used when not using malloc)
- #endif
- #endif
- #define TM16XX_BUTTONS_STATE_START 0
- #define TM16XX_BUTTONS_STATE_PRESSED 1
- #define TM16XX_BUTTONS_STATE_RELEASED 2
- #define TM16XX_BUTTONS_STATE_DBLPRESS 3
- #define TM16XX_BUTTONS_STATE_LPRESS 4
- #if(TM16XX_OPT_BUTTONS_EVENT)
- #define TM16XX_BUTTONS_EVENT_RELEASE 10
- #define TM16XX_BUTTONS_EVENT_CLICK 20
- #define TM16XX_BUTTONS_EVENT_DOUBLECLICK 30
- #define TM16XX_BUTTONS_EVENT_LONGPRESSSTART 40
- #define TM16XX_BUTTONS_EVENT_LONGPRESSSTOP 50
- #define TM16XX_BUTTONS_EVENT_LONGPRESSBUSY 60
- #endif
- // ----- Callback function types -----
- extern "C" {
- typedef void (*callbackTM16xxButtons)(byte nButton);
- #if(TM16XX_OPT_BUTTONS_EVENT)
- typedef void (*callbackTM16xxButtonsEvent)(byte btEvent, byte nButton);
- #endif
- }
- class TM16xxButtons
- {
- public:
- TM16xxButtons(TM16xx *pTM16xx, byte nNumButtons=TM16XX_BUTTONS_MAXBUTTONS);
- // set # millisec after single click is assumed.
- void setClickTicks(int ticks);
- // set # millisec after long press is assumed.
- void setLongPressTicks(int ticks);
- // attach functions that will be called when button was pressed in the
- // specified way.
- #if(TM16XX_OPT_BUTTONS_EVENT)
- void attachEventHandler(callbackTM16xxButtonsEvent newFunction);
- #else
- void attachRelease(callbackTM16xxButtons newFunction);
- void attachClick(callbackTM16xxButtons newFunction);
- void attachDoubleClick(callbackTM16xxButtons newFunction);
- void attachLongPressStart(callbackTM16xxButtons newFunction);
- void attachLongPressStop(callbackTM16xxButtons newFunction);
- void attachDuringLongPress(callbackTM16xxButtons newFunction);
- #endif
- uint32_t tick(void);
- /**
- * @brief Call this function every time the input level has changed.
- * Using this function no digital input pin is checked because the current
- * level is given by the parameter.
- */
- void tick(byte nButton, bool level);
- bool isPressed(byte nButton);
- bool isLongPressed(byte nButton);
- int getPressedTicks(byte nButton);
- void reset(void);
- protected:
- TM16xx *_pTM16xx;
- private:
- byte _nNumButtons;
- unsigned int _clickTicks = 500; // number of ticks that have to pass by
- // before a click is detected.
- unsigned int _longPressTicks = 1000; // number of ticks that have to pass by
- // before a long button press is detected
- // These variables will hold functions acting as event source.
- #if(TM16XX_OPT_BUTTONS_EVENT)
- callbackTM16xxButtonsEvent _eventFunc = NULL;
- #else
- callbackTM16xxButtons _releaseFunc = NULL;
- callbackTM16xxButtons _clickFunc = NULL;
- callbackTM16xxButtons _doubleClickFunc = NULL;
- callbackTM16xxButtons _longPressStartFunc = NULL;
- callbackTM16xxButtons _longPressStopFunc = NULL;
- callbackTM16xxButtons _duringLongPressFunc = NULL;
- #endif
- // These variables that hold information across the upcoming tick calls.
- // They are initialized once on program start and are updated every time the
- // tick function is called.
- #if(TM16XX_OPT_BUTTONS_MALLOC)
- byte *_state; // allocated memory array
- unsigned long *_startTime; // allocated memory array, value is set in state TM16XX_BUTTONS_STATE_PRESSED
- unsigned long *_stopTime; // allocated memory array, value is set in state TM16XX_BUTTONS_STATE_RELEASED
- #else
- byte _state[TM16XX_BUTTONS_MAXBUTTONS]; // = TM16XX_BUTTONS_STATE_START;
- //unsigned long _startTime[TM16XX_BUTTONS_MAXBUTTONS]; // will be set in state TM16XX_BUTTONS_STATE_PRESSED
- //unsigned long _stopTime[TM16XX_BUTTONS_MAXBUTTONS]; // will be set in state TM16XX_BUTTONS_STATE_RELEASED
- uint16_t _startTime[TM16XX_BUTTONS_MAXBUTTONS]; // will be set in state TM16XX_BUTTONS_STATE_PRESSED
- uint16_t _stopTime[TM16XX_BUTTONS_MAXBUTTONS]; // will be set in state TM16XX_BUTTONS_STATE_RELEASED
- #endif
- };
- #endif TM16xx/src/TM16xxDisplay.cpp 0100777 0000000 0000000 00000011653 14026127460 012763 0 ustar 00 /*
- TM16xxDisplay.h - LED Display library for TM16xx.
- Methods for driving 7-segment LED displays using TM16xx chips.
- Made by Maxint R&D. See https://github.com/maxint-rd/
- Based on TM1638/1640 library by Ricardo Batista.
- */
- #include "TM16xxDisplay.h"
- #include "string.h"
- TM16xxDisplay::TM16xxDisplay(TM16xx *pTM16xx, byte nNumDigits)
- {
- _pTM16xx=pTM16xx;
- _nNumDigits=nNumDigits;
- }
- void TM16xxDisplay::setIntensity(byte intensity)
- { // set the intensity of the module; range 0-7, 0=off, 7=bright
- _pTM16xx->setupDisplay(intensity!=0, intensity);
- }
- void TM16xxDisplay::setDisplayToString(const char* string, const word dots, const byte pos, const byte font[])
- { // call basic implementation
- _pTM16xx->setDisplayToString(string, dots, pos, font);
- }
- void TM16xxDisplay::setDisplayToString(const String string, const word dots, const byte pos, const byte font[])
- { // additional implementation using String class (uses more memory than char * version)
- int stringLength = string.length();
- for (int i = 0; i < _nNumDigits - pos; i++) {
- if (i < stringLength) {
- _pTM16xx->sendChar(i + pos, pgm_read_byte_near(font+(string.charAt(i) - 32)), (dots & (1 << (_nNumDigits - i - 1))) != 0);
- } else {
- break;
- }
- }
- }
- void TM16xxDisplay::setDisplayToError()
- { // set the display to Error text
- _pTM16xx->setDisplay(TM16XX_ERROR_DATA, 8);
- // MMOLE TODO: just use clear before instead?
- for (int i = 8; i < _nNumDigits; i++) {
- _pTM16xx->clearDisplayDigit(i, false);
- }
- }
- void TM16xxDisplay::setDisplayToHexNumber(unsigned long number, byte dots, boolean leadingZeros, const byte numberFont[])
- {
- for (int i = 0; i < _nNumDigits; i++) {
- if (!leadingZeros && number == 0) {
- _pTM16xx->clearDisplayDigit(_nNumDigits - i - 1, (dots & (1 << i)) != 0);
- } else {
- _pTM16xx->setDisplayDigit(number & 0xF, _nNumDigits - i - 1, (dots & (1 << i)) != 0, numberFont);
- number >>= 4;
- }
- }
- }
- void TM16xxDisplay::setDisplayToDecNumberAt(unsigned long number, byte dots, byte startingPos, boolean leadingZeros, const byte numberFont[])
- {
- if (number > 99999999L) {
- setDisplayToError();
- } else {
- for (int i = 0; i < _nNumDigits - startingPos; i++) {
- if (number != 0) {
- _pTM16xx->setDisplayDigit(number % 10, _nNumDigits - i - 1, (dots & (1 << i)) != 0, numberFont);
- number /= 10;
- } else {
- if (leadingZeros) {
- _pTM16xx->setDisplayDigit(0, _nNumDigits - i - 1, (dots & (1 << i)) != 0, numberFont);
- } else {
- _pTM16xx->clearDisplayDigit(_nNumDigits - i - 1, (dots & (1 << i)) != 0);
- }
- }
- }
- }
- }
- void TM16xxDisplay::setDisplayToDecNumber(unsigned long number, byte dots, boolean leadingZeros,
- const byte numberFont[])
- {
- setDisplayToDecNumberAt(number, dots, 0, leadingZeros, numberFont);
- }
- void TM16xxDisplay::setDisplayToSignedDecNumber(signed long number, byte dots, boolean leadingZeros, const byte numberFont[])
- {
- if (number >= 0) {
- setDisplayToDecNumberAt(number, dots, 0, leadingZeros, numberFont);
- } else {
- if (-number > 9999999L) {
- setDisplayToError();
- } else {
- setDisplayToDecNumberAt(-number, dots, 1, leadingZeros, numberFont);
- _pTM16xx->sendChar(0, MINUS, (dots & (0x80)) != 0);
- }
- }
- }
- void TM16xxDisplay::setDisplayToBinNumber(byte number, byte dots, const byte numberFont[])
- {
- for (int i = 0; i < _nNumDigits; i++) {
- _pTM16xx->setDisplayDigit((number & (1 << i)) == 0 ? 0 : 1, _nNumDigits - i - 1, (dots & (1 << i)) != 0, numberFont);
- }
- }
- void TM16xxDisplay::clear()
- {
- _pTM16xx->clearDisplay();
- }
- void TM16xxDisplay::setCursor(int8_t nPos)
- { // Set the print position. Allow negative numbers to support scrolling
- _nPrintPos=nPos;
- }
- /*
- * Support for the Print class
- *
- * See https://playground.arduino.cc/Code/Printclass
- *
- */
- size_t TM16xxDisplay::write(uint8_t c)
- { //Code to display letter when given the ASCII code for it
- static uint8_t cPrevious=' '; // remember last character prnted, to add a dot when needed
- /*
- Serial.print(F("Pos "));
- Serial.print(_nPrintPos);
- Serial.print(F(" chr "));
- Serial.print(c);
- Serial.print("=");
- Serial.write(c);
- Serial.print(F(", prev "));
- Serial.print(cPrevious);
- Serial.print("=");
- Serial.write(cPrevious);
- Serial.println("");
- */
- if(c=='\0' || c=='\n' || c=='\r' || _nPrintPos>=_nNumDigits)
- {
- while(_nPrintPos>0 && _nPrintPos<_nNumDigits)
- { // clear the remainder of the line
- _pTM16xx->clearDisplayDigit(_nPrintPos);
- //Serial.println(_nPrintPos);
- _nPrintPos++;
- }
- _nPrintPos=0;
- return(0); // returning zero will stop printing rest of the string
- }
- bool fDot=false;
- if(c=='.' || c==',' || c==':' || c==';')
- {
- c=cPrevious;
- fDot=true;
- if(_nPrintPos>0) _nPrintPos--; // use same position to display the dot
- }
- if(_nPrintPos>=0 && _nPrintPos<_nNumDigits)
- _pTM16xx->sendChar(_nPrintPos, pgm_read_byte_near(TM16XX_FONT_DEFAULT+(c - 32)), fDot);
- cPrevious=c;
- _nPrintPos++;
- return(1);
- }
- /* MMOLE: not called by print()
- size_t TM16xxDisplay::write(const char *str)
- {
- Serial.println(F("write str"));
- setDisplayToString(str);
- _nPrintPos=0;
- }
- */ TM16xx/src/TM16xxDisplay.h 0100777 0000000 0000000 00000004772 14026127460 012434 0 ustar 00 /*
- TM16xxDisplay.h - LED Display class for TM16xx.
- The TM16xxDisplay class supports 7-segment LED displays of various sizes to be
- connected to a TM16xx chip, such as TM1637 or TM1640.
- These are the dimensions supported by the tested TM16xx chips:
- TM1637 8x6 (common anode)
- TM1638 10x8 (common cathode)
- TM1640 8x16 (common cathode)
- TM1650 8x4 (common cathode) (mode 7x4 not tested yet)
- TM1668 10x7 (common cathode) (not supported yet: modes 11x6, 12x5, 13x4)
- Made by Maxint R&D. See https://github.com/maxint-rd/
- */
- #ifndef _TM16XX_DISPLAY_H
- #define _TM16XX_DISPLAY_H
- #include "TM16xx.h"
- #include <Print.h>
- //#include "TM16xxFonts.h"
- class TM16xxDisplay : public Print
- {
- public:
- TM16xxDisplay(TM16xx *pTM16xx, byte nNumDigits);
- void setIntensity(byte intensity); // intensity 0-7, 0=off, 7=bright
- virtual void clear();
- // 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 setDisplayToError();
- // 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[] = TM16XX_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[] = TM16XX_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[] = TM16XX_NUMBER_FONT);
- // Set the display to a unsigned binary number
- void setDisplayToBinNumber(byte number, byte dots, const byte numberFont[] = TM16XX_NUMBER_FONT);
- // support for the Print class
- void setCursor(int8_t nPos); // allows setting negative to support scrolled printing
- using Print::write; // pull in write(str) and write(buf, size) from Print
- virtual size_t write(uint8_t character);
- /* virtual size_t write(const char *str); */
- protected:
- TM16xx *_pTM16xx;
- byte _nNumDigits;
- int8_t _nPrintPos=0;
- private:
- void setDisplayToDecNumberAt(unsigned long number, byte dots, byte startingPos, boolean leadingZeros, const byte numberFont[]);
- };
- #endif TM16xx/src/TM16xxFonts.h 0100777 0000000 0000000 00000007533 14026130270 012107 0 ustar 00 /*
- TM16xxFonts.h - Font definition 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/>.
- The bits are displayed by mapping bellow
- -- 0 --
- | |
- 5 1
- -- 6 --
- 4 2
- | |
- -- 3 -- .7
- */
- #ifndef TM16XXFonts_h
- #define TM16XXFonts_h
- // definition for standard hexadecimal numbers
- const PROGMEM byte TM16XX_NUMBER_FONT[] = {
- 0b00111111, // 0
- 0b00000110, // 1
- 0b01011011, // 2
- 0b01001111, // 3
- 0b01100110, // 4
- 0b01101101, // 5
- 0b01111101, // 6
- 0b00000111, // 7
- 0b01111111, // 8
- 0b01101111, // 9
- 0b01110111, // A
- 0b01111100, // B
- 0b00111001, // C
- 0b01011110, // D
- 0b01111001, // E
- 0b01110001 // F
- };
- const PROGMEM byte MINUS = 0b01000000;
- // definition for error
- const PROGMEM byte TM16XX_ERROR_DATA[] = {
- 0b01111001, // E
- 0b01010000, // r
- 0b01010000, // r
- 0b01011100, // o
- 0b01010000, // r
- 0,
- 0,
- 0
- };
- // definition for the displayable ASCII chars
- const PROGMEM byte TM16XX_FONT_DEFAULT[] = {
- 0b00000000, // (32) <space>
- 0b10000110, // (33) !
- 0b00100010, // (34) "
- 0b01111110, // (35) #
- 0b01101101, // (36) $
- 0b00000000, // (37) %
- 0b00000000, // (38) &
- 0b00000010, // (39) '
- 0b00110000, // (40) (
- 0b00000110, // (41) )
- 0b01100011, // (42) *
- 0b00000000, // (43) +
- 0b00000100, // (44) ,
- 0b01000000, // (45) -
- 0b10000000, // (46) .
- 0b01010010, // (47) /
- 0b00111111, // (48) 0
- 0b00000110, // (49) 1
- 0b01011011, // (50) 2
- 0b01001111, // (51) 3
- 0b01100110, // (52) 4
- 0b01101101, // (53) 5
- 0b01111101, // (54) 6
- 0b00100111, // (55) 7
- 0b01111111, // (56) 8
- 0b01101111, // (57) 9
- 0b00000000, // (58) :
- 0b00000000, // (59) ;
- 0b00000000, // (60) <
- 0b01001000, // (61) =
- 0b00000000, // (62) >
- 0b01010011, // (63) ?
- 0b01011111, // (64) @
- 0b01110111, // (65) A
- 0b01111111, // (66) B
- 0b00111001, // (67) C
- 0b00111111, // (68) D
- 0b01111001, // (69) E
- 0b01110001, // (70) F
- 0b00111101, // (71) G
- 0b01110110, // (72) H
- 0b00000110, // (73) I
- 0b00011111, // (74) J
- 0b01101001, // (75) K
- 0b00111000, // (76) L
- 0b00010101, // (77) M
- 0b00110111, // (78) N
- 0b00111111, // (79) O
- 0b01110011, // (80) P
- 0b01100111, // (81) Q
- 0b00110001, // (82) R
- 0b01101101, // (83) S
- 0b01111000, // (84) T
- 0b00111110, // (85) U
- 0b00101010, // (86) V
- 0b00011101, // (87) W
- 0b01110110, // (88) X
- 0b01101110, // (89) Y
- 0b01011011, // (90) Z
- 0b00111001, // (91) [
- 0b01100100, // (92) \ (this can't be the last char on a line, even in comment or it'll concat)
- 0b00001111, // (93) ]
- 0b00000000, // (94) ^
- 0b00001000, // (95) _
- 0b00100000, // (96) `
- 0b01011111, // (97) a
- 0b01111100, // (98) b
- 0b01011000, // (99) c
- 0b01011110, // (100) d
- 0b01111011, // (101) e
- 0b00110001, // (102) f
- 0b01101111, // (103) g
- 0b01110100, // (104) h
- 0b00000100, // (105) i
- 0b00001110, // (106) j
- 0b01110101, // (107) k
- 0b00110000, // (108) l
- 0b01010101, // (109) m
- 0b01010100, // (110) n
- 0b01011100, // (111) o
- 0b01110011, // (112) p
- 0b01100111, // (113) q
- 0b01010000, // (114) r
- 0b01101101, // (115) s
- 0b01111000, // (116) t
- 0b00011100, // (117) u
- 0b00101010, // (118) v
- 0b00011101, // (119) w
- 0b01110110, // (120) x
- 0b01101110, // (121) y
- 0b01000111, // (122) z
- 0b01000110, // (123) {
- 0b00000110, // (124) |
- 0b01110000, // (125) }
- 0b00000001, // (126) ~
- };
- #endif
- TM16xx/src/TM16xxMatrix.cpp 0100777 0000000 0000000 00000001765 14026127460 012625 0 ustar 00 /*
- TM16xxMatrix.h - LED Matrix library for TM16xx.
- Made by Maxint R&D. See https://github.com/maxint-rd/
- */
- #include "TM16xxMatrix.h"
- TM16xxMatrix::TM16xxMatrix(TM16xx *pTM16xx, byte nColumns, byte nRows)
- {
- _pTM16xx=pTM16xx;
- _nColumns=nColumns;
- _nRows=nRows;
- // offscreen bitmap is required to set an individual pixel, while retaining the others
- // TODO: use dynamic memory allocation for the off-screen bitmap
- // as different chips support different sizes
- }
- void TM16xxMatrix::setColumn(byte nCol, byte bPixels)
- {
- _btColumns[nCol]=bPixels;
- _pTM16xx->setSegments(bPixels, nCol);
- }
- void TM16xxMatrix::setAll(bool fOn)
- {
- for(byte nCol=0; nCol<_nColumns; nCol++)
- setColumn(nCol, fOn?0xFF:0);
- }
- void TM16xxMatrix::setPixel(byte nCol, byte nRow, bool fOn)
- {
- byte btColumn=_btColumns[nCol];
- if(fOn)
- btColumn=btColumn | _BV(nRow);
- else
- btColumn=btColumn & ~_BV(nRow);
- setColumn(nCol, btColumn);
- }
- bool TM16xxMatrix::getPixel(byte nCol, byte nRow)
- {
- return((_btColumns[nCol]&_BV(nRow))!=0);
- } TM16xx/src/TM16xxMatrix.h 0100777 0000000 0000000 00000001761 14026127460 012266 0 ustar 00 /*
- TM16xxMatrix.h - LED Matrix class for TM16xx.
- The TM16xxMatrix class supports LED matrices of various sizes to be connected
- to a TM16xx chip, such as TM1640 or TM1638.
- These are the resolutions supported by the most popular TM16xx chips:
- TM1637 8x6 (common anode)
- TM1638 10x8 (common cathode)
- TM1640 8x16 (common cathode)
- TM1668 10x7 (common cathode)
- Made by Maxint R&D. See https://github.com/maxint-rd/
- */
- #ifndef _TM16XX_MATRIX_H
- #define _TM16XX_MATRIX_H
- #include "TM16xx.h"
- #define TM16XX_MATRIX_MAXCOLUMNS 16
- class TM16xxMatrix
- {
- public:
- TM16xxMatrix(TM16xx *pTM16xx, byte nColumns, byte nRows);
- void setColumn(byte nCol, byte bPixels);
- void setAll(bool fOn);
- void setPixel(byte nCol, byte nRow, bool fOn);
- bool getPixel(byte nCol, byte nRow);
- inline byte getNumRows() { return(_nRows); }
- inline byte getNumColumns() { return(_nColumns); }
- protected:
- TM16xx *_pTM16xx;
- byte _nColumns;
- byte _nRows;
- byte _btColumns[TM16XX_MATRIX_MAXCOLUMNS]={0};
- };
- #endif TM16xx/src/TM16xxMatrixGFX.cpp 0100777 0000000 0000000 00000012200 14026127460 013154 0 ustar 00 /*
- TM16xxMatrixGFX.h - Adafruit GFX LED Matrix library for TM16xx.
- Made by Maxint R&D. See https://github.com/maxint-rd/
- */
- #include <Adafruit_GFX.h>
- #include "TM16xxMatrixGFX.h"
- #define TM16xxMatrixGFX_swap(a, b) { int16_t t = a; a = b; b = t; }
- TM16xxMatrixGFX::TM16xxMatrixGFX(TM16xx *pModule, byte nColumns, byte nRows) : Adafruit_GFX(nRows, nColumns)
- {
- _nColumns=nColumns;
- _nRows=nRows;
- _fMirrorX=false;
- _fMirrorY=false;
- // Allocate a module array for just one module
- _nModules=1;
- _aModules=(TM16xx **)malloc(sizeof(TM16xx *));
- _aModules[0]=pModule;
- // An offscreen bitmap is required to set an individual pixel, while retaining the others
- // We use dynamic memory allocation for the off-screen bitmap
- // as different chips support different sizes
- TM16xxMatrixGFX::bitmapSize = _nColumns;
- TM16xxMatrixGFX::bitmap = (byte*)malloc(bitmapSize);
- fillScreen(0);
- }
- TM16xxMatrixGFX::TM16xxMatrixGFX(TM16xx *aModules[], byte nColumns, byte nRows, byte nModulesCol, byte nModulesRow) : Adafruit_GFX(nRows*nModulesRow, nColumns*nModulesCol)
- {
- _nColumns=nColumns;
- _nRows=nRows;
- _fMirrorX=false;
- _fMirrorY=false;
- // Allocate memory to copy pointers to the modules
- _nModules=nModulesRow*nModulesCol;
- _aModules=(TM16xx **)malloc(_nModules*sizeof(TM16xx *));
- for(byte n=0; n<_nModules; n++)
- _aModules[n]=aModules[n];
- // Allocate memory for the memory bitmap
- _nModulesRow=nModulesRow;
- _nModulesCol=nModulesCol;
- TM16xxMatrixGFX::bitmapSize = _nColumns*nModulesRow*nModulesCol; // assume 8 rows per byte
- TM16xxMatrixGFX::bitmap = (byte*)malloc(bitmapSize);
- fillScreen(0);
- }
- void TM16xxMatrixGFX::setIntensity(byte intensity)
- { // set the intensity of all modules
- for(byte n=0; n<_nModules; n++)
- _aModules[n]->setupDisplay(true, intensity);
- }
- void TM16xxMatrixGFX::setMirror(boolean fMirrorX, boolean fMirrorY) // fMirrorX=false, fMirrorY=false
- {
- _fMirrorX=fMirrorX;
- _fMirrorY=fMirrorY;
- }
- void TM16xxMatrixGFX::fillScreen(uint16_t color)
- { // set the offscreen bitmap to the specified color
- memset(bitmap, color ? 0xff : 0, bitmapSize);
- }
- bool TM16xxMatrixGFX::convertToMemPos(int16_t &x, int16_t &y)
- { // Convert x/y coordinates to bitmap memory position (array with rows of 8 pixels per byte)
- // Given coordinates are passed by reference and changed to the required range
- // Returns false if coordinates fall outside of canvas after processing rotation
- if (rotation)
- {
- // Implement Adafruit's rotation.
- byte tmp;
- if ( rotation >= 2 ) { // rotation == 2 || rotation == 3
- x = _width - 1 - x;
- }
- if ( rotation == 1 || rotation == 2 ) { // rotation == 1 || rotation == 2
- y = _height - 1 - y;
- }
- if ( rotation & 1 ) { // rotation == 1 || rotation == 3
- tmp = x; x = y; y = tmp;
- }
- }
- if ( x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT ) {
- // Ignore pixels outside the canvas.
- return(false);
- }
- /* // TODO?: support for different module orientaton and layout? (currently only left-top to right-bottom)
- // Translate the x, y coordinate according to the layout of the
- // displays. They can be ordered and rotated (0, 90, 180, 270).
- byte display = matrixPosition[(x >> 3) + hDisplays * (y >> 3)];
- x &= 0b111;
- y &= 0b111;
- byte r = matrixRotation[display];
- if ( r >= 2 ) { // 180 or 270 degrees
- x = 7 - x;
- }
- if ( r == 1 || r == 2 ) { // 90 or 180 degrees
- y = 7 - y;
- }
- if ( r & 1 ) { // 90 or 270 degrees
- tmp = x; x = y; y = tmp;
- }
- byte d = display / hDisplays;
- x += (display - d * hDisplays) << 3; // x += (display % hDisplays) * 8
- y += d << 3; // y += (display / hDisplays) * 8
- // Update the color bit in our bitmap buffer.
- byte *ptr = bitmap + x + WIDTH * (y >> 3);
- byte val = 1 << (y & 0b111);
- if ( color ) {
- *ptr |= val;
- }
- else {
- *ptr &= ~val;
- }
- */
- // mirror display (fMirrorX true for WeMOS mini matrix)
- if(_fMirrorX)
- x=WIDTH-x-1;
- if(_fMirrorY)
- y=HEIGHT-y-1;
- // Translation for multiple modules.
- if(_nModules>1)
- { // Assume modules are identical and ordered left to right, top to bottom
- // The columns are stacked in memory in module order
- uint8_t _nModule=x/_nRows + _nModulesRow*(y/_nColumns);
- y=y%_nColumns+(_nModule*_nColumns);
- x=x%_nRows;
- }
- return(true);
- }
- void TM16xxMatrixGFX::drawPixel(int16_t xx, int16_t yy, uint16_t color)
- { // set the specified pixel as wanted in the memory
- // Operating in bytes is faster and takes less code to run. We don't
- // need values above 200, so switch from 16 bit ints to 8 bit unsigned
- // ints (bytes).
- //int8_t x = xx;
- //int8_t y = yy;
- int16_t x = xx;
- int16_t y = yy;
- if(!convertToMemPos(x, y))
- return;
- if(color)
- {
- bitmap[y]|=(1<<x);
- }
- else
- {
- bitmap[y]&=~(1<<x);
- }
- }
- // required for scroll support as implemented by Adafruit GFX pull request #60
- uint16_t TM16xxMatrixGFX::getPixel(int16_t x, int16_t y)
- {
- if ((x < 0) || (x >= _width) || (y < 0) || (y >= _height))
- return 0;
- if(!convertToMemPos(x, y))
- return 0;
- return (bitmap[y+ (x/8)*WIDTH] >> (x%8)) & 0x1;
- }
- void TM16xxMatrixGFX::write()
- { // write the memory to the display
- for(uint8_t n=0;n<_nModules;n++)
- {
- for(uint8_t i=0;i<_nColumns;i++)
- {
- _aModules[n]->setSegments(bitmap[i+(n*_nColumns)],i);
- }
- }
- } TM16xx/src/TM16xxMatrixGFX.h 0100777 0000000 0000000 00000003004 14026127460 012623 0 ustar 00 /*
- TM16xxMatrixGFX.h - Adafruit GFX LED Matrix class for TM16xx.
- The TM16xxMatrixGFX class supports LED matrices of various sizes to be connected
- to a TM16xx chip, such as TM1640 or TM1638.
- These are the resolutions supported by the most popular TM16xx chips:
- TM1637 8x6 (common anode)
- TM1638 10x8 (common cathode)
- TM1640 8x16 (common cathode)
- TM1668 10x7 (common cathode)
- The library supports modules with either 8x8 or 8x16 pixels
- Made by Maxint R&D. See https://github.com/maxint-rd/
- */
- #ifndef _TM16XX_MATRIXGFX_H
- #define _TM16XX_MATRIXGFX_H
- #include "TM16xx.h"
- #define TM16XX_MATRIXGFX_MAXCOLUMNS 16
- class TM16xxMatrixGFX : public Adafruit_GFX
- {
- public:
- TM16xxMatrixGFX(TM16xx *pModule, byte nColumns, byte nRows);
- TM16xxMatrixGFX(TM16xx *aModules[], byte nColumns, byte nRows, byte nModulesCol, byte nModulesRow); // module layout left-top to right-bottom
- void setIntensity(byte intensity); // intensity 0-7, 0=off, 7=bright
- void setMirror(boolean fMirrorX=false, boolean fMirrorY=false);
- void fillScreen(uint16_t color);
- void drawPixel(int16_t x, int16_t y, uint16_t color);
- uint16_t getPixel(int16_t x, int16_t y); // required for scroll support as implemented by Adafruit GFX pull request #60
- void write();
- protected:
- byte _nModules;
- byte _nModulesCol;
- byte _nModulesRow;
- TM16xx **_aModules;
- byte _nColumns;
- byte _nRows;
- boolean _fMirrorX;
- boolean _fMirrorY;
- byte *bitmap;
- byte bitmapSize;
- private:
- bool convertToMemPos(int16_t &x, int16_t &y);
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement