├── _config.yml ├── ledandkey-256.png ├── xtm1638_gauge_modes-16.png ├── .gitignore ├── Examples ├── xtm1638Example02 │ ├── Timer.cpp │ ├── Timer.h │ └── xtm1638Example02.ino ├── xtm1638Example04 │ ├── xtm1638Example04.h │ └── xtm1638Example04.ino ├── xtm1638Example01 │ └── xtm1638Example01.ino ├── xtm1638Example05 │ └── xtm1638Example05.ino └── xtm1638Example03 │ └── xtm1638Example03.ino ├── xtm1638.config.h ├── README.md ├── xtm1638.h ├── xtm1638.cpp └── LICENSE /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-tactile -------------------------------------------------------------------------------- /ledandkey-256.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codebeat-nl/xtm1638/HEAD/ledandkey-256.png -------------------------------------------------------------------------------- /xtm1638_gauge_modes-16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codebeat-nl/xtm1638/HEAD/xtm1638_gauge_modes-16.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | -------------------------------------------------------------------------------- /Examples/xtm1638Example02/Timer.cpp: -------------------------------------------------------------------------------- 1 | #include "Timer.h" 2 | 3 | /* constructors */ 4 | TTimer::TTimer(unsigned long ulInterval, bool bAutoReset, bool bCycleMode ) 5 | { 6 | autoreset = bAutoReset; 7 | cyclemode = bCycleMode; 8 | active = true; 9 | previous = 0; 10 | interval = ulInterval; 11 | } 12 | 13 | TTimer::TTimer(unsigned long ulCurrent,unsigned long ulInterval, bool bAutoReset, bool bCycleMode ) 14 | { 15 | autoreset = bAutoReset; 16 | cyclemode = bCycleMode; 17 | active = true; 18 | previous = ulCurrent; 19 | interval = ulInterval; 20 | } 21 | 22 | /* Operation functions */ 23 | void TTimer::reset() 24 | { 25 | if( cyclemode ) 26 | { previous = interval; } 27 | else { previous = millis(); } 28 | } 29 | 30 | void TTimer::disable() 31 | { active = false; } 32 | 33 | void TTimer::enable() 34 | { active = true; } 35 | 36 | bool TTimer::setInterval( unsigned long ulInterval) 37 | { 38 | if( ulInterval > 0 ) 39 | { 40 | interval = ulInterval; 41 | reset(); 42 | return true; 43 | } 44 | return false; 45 | } 46 | 47 | bool TTimer::setCycleMode( bool bCycleMode ) 48 | { 49 | if( bCycleMode != cyclemode ) 50 | { 51 | cyclemode = bCycleMode; 52 | reset(); 53 | return true; 54 | } 55 | return false; 56 | } 57 | 58 | 59 | /* status functions */ 60 | bool TTimer::isActive() 61 | { return (interval > 0 && active); } 62 | 63 | bool TTimer::isCycleMode() 64 | { return (interval > 0 && active); } 65 | 66 | bool TTimer::isTime( bool bForceTimeResetIfTime ) 67 | { 68 | if( isActive() && (( cyclemode && (previous == 0 || (--previous == 0))) || ( !cyclemode && (millis()-previous >= interval) )) ) 69 | { 70 | if( autoreset || bForceTimeResetIfTime ) 71 | { reset(); } 72 | return true; 73 | } 74 | return false; 75 | } 76 | 77 | unsigned long TTimer::getInterval() 78 | { return interval; } 79 | 80 | 81 | -------------------------------------------------------------------------------- /Examples/xtm1638Example02/Timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | || 3 | || @file TTimer.cpp 4 | || @version 1.6 5 | || @author Erwin Haantjes 6 | || @contact erwin@illumation.net 7 | || 8 | || @description 9 | || | Provide an easy way of triggering functions at a set interval 10 | || # 11 | || 12 | || @license 13 | || | This library is free software; you can redistribute it and/or 14 | || | modify it under the terms of the GNU Lesser General Public 15 | || | License as published by the Free Software Foundation; version 16 | || | 2.1 of the License. 17 | || | 18 | || | This library is distributed in the hope that it will be useful, 19 | || | but WITHOUT ANY WARRANTY; without even the implied warranty of 20 | || | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 21 | || | Lesser General Public License for more details. 22 | || | 23 | || | You should have received a copy of the GNU Lesser General Public 24 | || | License along with this library; if not, write to the Free Software 25 | || | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 26 | || # 27 | || 28 | */ 29 | 30 | #ifndef TIMER_H 31 | #define TIMER_H 32 | 33 | #if defined(ARDUINO) && ARDUINO >= 100 34 | #include "Arduino.h" 35 | #else 36 | #include "WProgram.h" 37 | #endif 38 | 39 | class TTimer 40 | { 41 | public: 42 | unsigned long tag = 0; 43 | 44 | TTimer(unsigned long ulInterval, bool bAutoReset = true, bool bCycleMode = false); 45 | TTimer(unsigned long ulCurrent, unsigned long ulInterval, bool bAutoReset = true, bool bCycleMode = false); 46 | 47 | void reset(); 48 | void disable(); 49 | void enable(); 50 | bool setInterval( unsigned long ulInterval ); 51 | bool setCycleMode( bool bCycleMode = true ); 52 | 53 | bool isActive(); 54 | bool isTime( bool bForceTimeResetIfTime = false ); 55 | bool isCycleMode(); 56 | unsigned long getInterval(); 57 | 58 | private: 59 | bool active; 60 | bool autoreset; 61 | bool cyclemode; 62 | unsigned long previous; 63 | unsigned long interval; 64 | }; 65 | 66 | #endif 67 | 68 | 69 | -------------------------------------------------------------------------------- /Examples/xtm1638Example04/xtm1638Example04.h: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | // Include the library first 5 | #ifdef LIB_1638_RBATISTA 6 | #include 7 | #else 8 | #include 9 | #endif 10 | 11 | // ARDUINO COMPATIBLE MODE EXPLAINED 12 | // --------------------------------- 13 | // If the code detects it is not compiled on an ATMEL AVR, it will switch to Arduino 14 | // compatible mode, which means the code doesn't use port registers directly (Arduino's 15 | // library handles this). In this case you need to specify (digital) PIN numbers. 16 | // Overall, compatible mode is slower than the direct port access methods the code 17 | // provides, however, when your application is not time critical, it just works fine. 18 | // Also, in compatible mode, the code consumes more memory, a few hundred bytes more. 19 | 20 | #if defined(LIB_1638_RBATISTA) || defined(XTM_ARDUINO_COMPATIBLE) 21 | // Specify (digital) PINS as described on the board 22 | #define PIN_DIG_LEDKEY_DATAIO 8 23 | #define PIN_DIG_LEDKEY_CLOCK 9 24 | #define PIN_DIG_LEDKEY_STROBE 10 25 | #else 26 | // Specify register port PINS specified in the documentation of the board 27 | #define PIN_REG_LEDKEY_DATAIO PB0 28 | #define PIN_REG_LEDKEY_CLOCK PB1 29 | #define PIN_REG_LEDKEY_STROBE PB2 30 | #endif 31 | 32 | 33 | #ifdef LIB_1638_RBATISTA 34 | 35 | #define XTM_NOBUTTON 0x40 36 | #define XTM_BUTTON1 0x00 37 | #define XTM_BUTTON2 0x01 38 | #define printStr(x) ledandkey.clearDisplay(); ledandkey.setDisplayToString(x) 39 | #define printNum(x) ledandkey.setDisplayToDecNumber(x, 0x00, false) 40 | #define clearLEDs() ledandkey.setLEDs(0x00) 41 | #define clearLED(x) ledandkey.setLED( 0x00, x ); 42 | #define setLed(x) ledandkey.setLED( 0x01, x ); 43 | #define setOrientation(x) 44 | #define cls() ledandkey.clearDisplay() 45 | 46 | static TM1638 ledandkey( PIN_DIG_LEDKEY_DATAIO, 47 | PIN_DIG_LEDKEY_CLOCK, 48 | PIN_DIG_LEDKEY_STROBE 49 | ); 50 | #else 51 | //ledandkey.clear(); 52 | 53 | // Create the class object 54 | #ifdef XTM_ARDUINO_COMPATIBLE 55 | // Specify (digital) PINS as described on the board 56 | static xtm1638 ledandkey( PIN_DIG_LEDKEY_DATAIO, 57 | PIN_DIG_LEDKEY_CLOCK, 58 | PIN_DIG_LEDKEY_STROBE 59 | ); 60 | #else 61 | // Specify register port PINS specified in the documentation of the board 62 | static xtm1638 ledandkey( PIN_REG_LEDKEY_DATAIO, 63 | PIN_REG_LEDKEY_CLOCK, 64 | PIN_REG_LEDKEY_STROBE 65 | ); 66 | 67 | // Read the xtm1638.config.h file to find more options 68 | #endif 69 | 70 | #define printStr(x) ledandkey.clear(); ledandkey.setChars(x) 71 | #define printNum(x) ledandkey.setNumber(x) 72 | #define clearLEDs() ledandkey.clearLEDs() 73 | #define clearLED(x) ledandkey.clearLED(x) 74 | #define setLed(x) ledandkey.setLED( x ) 75 | #define setOrientation(x) ledandkey.setOrientation( x ) 76 | #define cls() ledandkey.clear() 77 | #endif 78 | -------------------------------------------------------------------------------- /Examples/xtm1638Example01/xtm1638Example01.ino: -------------------------------------------------------------------------------- 1 | // Project: xtm1638 usage examples, nr. 1 2 | // Author : codebeat - Erwin Haantjes - http://codebeat.nl 3 | // Source : https://github.com/codebeat-nl/xtm1638 4 | // Date : 17 may 2017 5 | // --------------------------------- 6 | // WHAT IT DOES 7 | // Sample program, a hello world example. 8 | // A program to show you how to work with the xtm1638 class, 9 | // this example includes: 10 | // o Handle compatible mode (so you can use the library on any 'Arduino') 11 | // o Show some 'info' 12 | // o Read and use a button 13 | // o Set leds on top of the board 14 | // o Do an animation with custom characters 15 | // 16 | // BUTTON ASSIGMENTS (LEFT TO RIGHT) 17 | // o Button #1 - SAY hello 18 | // o Button #2 - Show numbers (hex) 19 | // o Button #3 - Do a little wave animation with custom chars 20 | 21 | 22 | // Include the library first 23 | #include 24 | 25 | 26 | // ARDUINO COMPATIBLE MODE EXPLAINED 27 | // --------------------------------- 28 | // If the code detects it is not compiled on an ATMEL AVR, it will switch to Arduino 29 | // compatible mode, which means the code doesn't use port registers directly (Arduino's 30 | // library handles this). In this case you need to specify (digital) PIN numbers. 31 | // Overall, compatible mode is slower than the direct port access methods the code 32 | // provides, but when your application is not time critical, it just works fine. 33 | // Also, in compatible mode, the code consumes more memory, a few hundred bytes more. 34 | 35 | #ifdef XTM_ARDUINO_COMPATIBLE 36 | // Specify (digital) PINS as described on the board 37 | #define PIN_DIG_LEDKEY_DATAIO 8 38 | #define PIN_DIG_LEDKEY_CLOCK 9 39 | #define PIN_DIG_LEDKEY_STROBE 10 40 | #else 41 | // Specify register port PINS specified in the documentation of the board 42 | #define PIN_REG_LEDKEY_DATAIO PB0 43 | #define PIN_REG_LEDKEY_CLOCK PB1 44 | #define PIN_REG_LEDKEY_STROBE PB2 45 | 46 | // Read the xtm1638.config.h file to find more options 47 | #endif 48 | 49 | // Create the class object 50 | #ifdef XTM_ARDUINO_COMPATIBLE 51 | // Specify (digital) PINS as described on the board 52 | static xtm1638 ledandkey( PIN_DIG_LEDKEY_DATAIO, 53 | PIN_DIG_LEDKEY_CLOCK, 54 | PIN_DIG_LEDKEY_STROBE 55 | ); 56 | #else 57 | // Specify register port PINS specified in the documentation of the board 58 | static xtm1638 ledandkey( PIN_REG_LEDKEY_DATAIO, 59 | PIN_REG_LEDKEY_CLOCK, 60 | PIN_REG_LEDKEY_STROBE 61 | ); 62 | 63 | // Read the xtm1638.config.h file to find more options 64 | #endif 65 | 66 | void setup() 67 | { 68 | // Nothing to do here 69 | } 70 | 71 | void loop() 72 | { 73 | // Check if button pressed 74 | uint8_t iButton = ledandkey.getButtonPressed(); 75 | 76 | ledandkey.setChars("INPUT ?"); 77 | 78 | delay(300); // Give processing some time 79 | 80 | // Button press detected? 81 | if( iButton != XTM_NOBUTTON ) 82 | { 83 | // User want to say hello? 84 | if( iButton == XTM_BUTTON1 ) 85 | { 86 | ledandkey.setLED(0); 87 | ledandkey.setLED(7); 88 | ledandkey.setChars("HELLO"); 89 | delay(2000); 90 | ledandkey.setChars("WORLD"); 91 | delay(2000); 92 | ledandkey.clearLEDs(); 93 | } 94 | else 95 | 96 | // User want to show some numbers? 97 | if( iButton == XTM_BUTTON2 ) 98 | { 99 | ledandkey.setLED(3); 100 | ledandkey.setLED(4); 101 | 102 | ledandkey.setChars("01234567"); 103 | delay(2000); 104 | ledandkey.setChars("89ABCDEF"); 105 | delay(2000); 106 | ledandkey.setLED(3); 107 | ledandkey.clearLEDs(); 108 | } 109 | else 110 | 111 | // User want to do animation? 112 | if( iButton == XTM_BUTTON3 ) 113 | { 114 | ledandkey.setLED(6); 115 | ledandkey.setLED(7); 116 | 117 | uint8_t i = 40; 118 | bool b = false; 119 | 120 | while( i-- ) 121 | { 122 | for( uint8_t x=0; x<8; x++ ) 123 | { 124 | ledandkey.setByte( x, b?0x37:0x08 ); 125 | b=!b; 126 | } 127 | 128 | delay(100); 129 | b=!b; 130 | } 131 | 132 | ledandkey.clearLEDs(); 133 | } 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /Examples/xtm1638Example04/xtm1638Example04.ino: -------------------------------------------------------------------------------- 1 | // Project: xtm1638 usage examples, nr. 4 2 | // Author : codebeat - Erwin Haantjes - http://codebeat.nl 3 | // Source : https://github.com/codebeat-nl/xtm1638 4 | // Dev Date : 17 may 2017 5 | // Modified : 30 jan 2019 (new scores by using updates) 6 | // ------------------------------------------------------- 7 | // WHAT IT DOES 8 | // Sample program to show you how to work with the object, this example includes: 9 | // o Handle compatible mode (so you can use the library on any 'Arduino') 10 | // o Read and use buttons 11 | // o Performance test (between libs) 12 | // o Learn that compiled size, performance and memory usage matters 13 | // 14 | // BUTTON ASSIGMENTS (LEFT TO RIGHT) 15 | // o Button #1 - Start test with leds 16 | // o Button #2 - Start test without leds 17 | // 18 | // To do the same with different libs: 19 | // On an Arduino Nano (ATMega328), this sketch with use of: 20 | // 21 | // Batista TM1638 library : size 4992b 462b mem 22 | // ---------------------------- 23 | // This library, 24 | // o With normal devide or multiply: 25 | // - Portmode (AVR only) : size 2812b 104b mem (AVR direct port manipulation (=much faster)) 26 | // - Arduino compatible mode : size 3104b 104b mem (with digitalwrite etc) 27 | // - Arduino compatible mode \ 28 | // without PROGMEM font table : size 3098b 174b mem 29 | // o With ShiftDivide mode: 30 | // - Portmode (AVR only) : size 3018b 116b mem (AVR direct port manipulation (=much faster)) 31 | // - Arduino compatible mode : size 3310b 116b mem (with digitalwrite etc) 32 | // - Arduino compatible mode \ 33 | // without PROGMEM font table : size 3304b 186b mem 34 | // 35 | // NOTICE: 36 | // o See also "ARDUINO COMPATIBLE MODE EXPLAINED" in ino file for more info. 37 | // o Compile sizes and memory usage may vary between different versions of the Arduino IDE and library. 38 | 39 | // Stress test with Arduino Nano (ATMega328), count-up to 100.000 test): 40 | // --------------------------------------------------------------------- 41 | // Mode: Score: Minutes: no-leds score: no-leds minutes: 42 | // Batista TM1638 library 448676ms 7.48 minutes(!) 363178ms 6.05 minutes(!) 43 | // ---------- xtm1638 library: 44 | // Register+Multiply 94177ms 1.57 minutes 72077ms 1.20 minutes 45 | // Register+Divide 86952ms 1.44 minutes 64852ms 1.08 minutes 46 | // Register+ShiftDivide 76404ms 1.2734 minutes 58314ms 0.972 minutes(!) 47 | // Register+ShiftDivide-PROGMEM 76374ms 1.2729 minutes 58283ms 0.971 minutes(!) 48 | // Compatible Mode+Multiply 321876ms 5.36 minutes 232628ms 3.88 minutes 49 | // Compatible Mode+Divide 314652ms 5.25 minutes 226404ms 3.77 minutes 50 | // Compatible Mode+ShiftDivide 304093ms 5.07 minutes 219866ms 3.66 minutes 51 | // 52 | // 26 jan 2019 update: New records with caching (buffering)! 53 | // Register+ShiftDivide+caching 39435ms 0.65725 minutes 22512ms 0.375 minutes(!) - speed gain: 1.9x/2.5x 54 | // Compatible Mode+ShiftDivide+ 142392ms 2.3732 minutes 59284ms 0.988 minutes(!) - speed gain: 2.1x/3.7x 55 | // caching 56 | // 57 | // 29 jan 2019 update (1): New records by using assembler (ASM)! 58 | // ASM+ShiftDivide 37125ms 0.618 minutes(!) 30239ms 0.504 minutes(!) - speed gain: 2.1x/1.9x 59 | // ASM+ShiftDivide+caching 23889ms 0.398 minutes(!) 17004ms 0.283 minutes(!) - speed gain: 3.1x/3.4x 60 | // 61 | // 29 jan 2019 update (2): New records by using assembler (ASM) + font-table in dynamic memory! 62 | // ASM+ShiftDivide 37095ms 0.617 minutes(!) 30209ms 0.503 minutes(!) - speed gain: 2.1x/1.9x 63 | // ASM+ShiftDivide+caching 23860ms 0.397 minutes(!) 16974ms 0.282 minutes(!) - speed gain: 3.2x/3.5x 64 | // 65 | // 30 jan 2019: 66 | // Another gain can be reached by using new define (which is default now), XTM_AVR_SHIFTWISE_DIVIDE_ASM. 67 | // I don't have the time right now to update the tables above again. Fastest speed is now 12756ms = 68 | // 0.213 minutes = 12.756 seconds to count up to 100000 and display it. 28x faster than Batista's library. 69 | 70 | // Conclusion: 71 | // And the OVERALL winner is..... ;-) 72 | 73 | 74 | // Uncomment this define if you want to test with R. Batista library 75 | // (if you have it installed). You need to recompile this sketch. 76 | //#define LIB_1638_RBATISTA 77 | 78 | #include "xtm1638Example04.h" 79 | 80 | bool isButtonPressed( const uint8_t iPos, uint8_t iButtons ) 81 | { 82 | if( iButtons == 0 ) 83 | { iButtons = ledandkey.getButtons(); 84 | if( iButtons == 0 ) 85 | { return false; } 86 | } 87 | return (iButtons & (0x1 << iPos))?true:false; 88 | } 89 | 90 | 91 | uint8_t getButtonPressed() 92 | { 93 | uint8_t iButtons = ledandkey.getButtons(); 94 | uint8_t i = 0; 95 | 96 | while( i < 8 ) 97 | { 98 | if( isButtonPressed(i++, iButtons ) ) 99 | { return (i-1); } 100 | } 101 | 102 | return XTM_NOBUTTON; 103 | } 104 | 105 | void startCounterDemo(bool bWithoutsetLed = false) // Count up to 100.000 106 | { 107 | clearLEDs(); 108 | setLed(0); 109 | setLed(3); 110 | setLed(4); 111 | setLed(7); 112 | 113 | printStr( "COUNTER" ); 114 | delay(2000); 115 | clearLED(0); 116 | printStr( "SPEED" ); 117 | delay(2000); 118 | clearLED(7); 119 | printStr( "} DEMO" ); 120 | delay(2000); 121 | clearLED(3); 122 | printStr( bWithoutsetLed?"NO LEDS":"100000x " ); 123 | delay(2000); 124 | clearLED(4); 125 | cls(); 126 | 127 | unsigned long i = 0; 128 | uint8_t x = 0; 129 | uint8_t y = 0; 130 | unsigned long iBench = millis(); 131 | 132 | if( bWithoutsetLed ) 133 | { 134 | while( ++i < 100001UL ) 135 | { printNum(i); } 136 | } 137 | else { 138 | while( ++i < 100001UL ) 139 | { 140 | clearLED(y); 141 | setLed(x); 142 | y = x; 143 | ++x; 144 | if( x > 7 ) 145 | { x = 0; } 146 | printNum(i); 147 | } 148 | } 149 | 150 | iBench = millis()-iBench; 151 | printStr( "DONE" ); 152 | delay(2000); 153 | clearLEDs(); 154 | printStr( "SCORE" ); 155 | delay(2000); 156 | 157 | cls(); 158 | printNum( iBench ); 159 | delay(4000); 160 | } 161 | 162 | 163 | void setup() 164 | { 165 | Serial.begin( 9600 ); 166 | printStr("WhatsUp?"); 167 | delay(2000); 168 | printStr("Stress"); 169 | delay(2000); 170 | printStr("Test"); 171 | delay(2000); 172 | printStr("Library"); 173 | delay(2000); 174 | } 175 | 176 | bool bIsDirty = true; 177 | 178 | void loop() 179 | { 180 | delay(300); // Give some time, be gentle 181 | 182 | if( bIsDirty ) // avoid flicker 183 | { printStr("PressBtn"); } 184 | 185 | bIsDirty=false; 186 | 187 | uint8_t iButton = getButtonPressed(); 188 | if( iButton != XTM_NOBUTTON ) 189 | { 190 | if( iButton == XTM_BUTTON1 ) 191 | { 192 | bIsDirty=true; 193 | startCounterDemo(); 194 | } 195 | else 196 | 197 | if( iButton == XTM_BUTTON2 ) 198 | { 199 | bIsDirty=true; 200 | startCounterDemo(true); 201 | } 202 | } 203 | } 204 | -------------------------------------------------------------------------------- /xtm1638.config.h: -------------------------------------------------------------------------------- 1 | #pragma no-cache 2 | 3 | /* xtm1638 library compiler options 4 | Software related info in: xtm1638.h & xtml1638.h (please read it first) 5 | 6 | CHANGE THIS FILE ONLY WHEN THERE ARE ISSUES ON: 7 | o PORT SETTINGS 8 | o AUTO DETECTION FEATURES (FAILING) 9 | o COMPATIBILITY 10 | o SPEED OR GLOBAL PERFORMANCE 11 | o PROGRAM VERSUS DYNAMIC MEMORY USAGE 12 | 13 | 14 | PERFORMANCE STATS (Arduino Nano stress test - see xtm1638example04.ino) 15 | ----------------------------------------------------------------------- 16 | Mode: Score: Minutes: no-leds score: no-leds minutes: 17 | Register+Multiply 94177ms 1.57 minutes 72077ms 1.20 minutes 18 | Register+Divide 86952ms 1.44 minutes 64852ms 1.08 minutes 19 | Register+ShiftDivide 76404ms 1.2734 minutes 58314ms 0.972 minutes(!) 20 | Register+ShiftDivide-PROGMEM 76374ms 1.2729 minutes 58283ms 0.971 minutes(!) 21 | Compatible Mode+Multiply 321876ms 5.36 minutes 232628ms 3.88 minutes 22 | Compatible Mode+Divide 314652ms 5.25 minutes 226404ms 3.77 minutes 23 | Compatible Mode+ShiftDivide 304093ms 5.07 minutes 219866ms 3.66 minutes 24 | 25 | ---- 26 | 27 | 26 jan 2019 update: New records with caching (buffering)! 28 | Register+ShiftDivide+caching 39435ms 0.65725 minutes 22512ms 0.375 minutes(!) - speed gain: 1.9x/2.5x 29 | Compatible Mode+ShiftDivide+ 142392ms 2.3732 minutes 59284ms 0.988 minutes(!) - speed gain: 2.1x/3.7x 30 | caching 31 | 32 | 29 jan 2019 update (1): New records by using assembler (ASM)! 33 | ASM+ShiftDivide 37125ms 0.618 minutes(!) 30239ms 0.504 minutes(!) - speed gain: 2.1x/1.9x 34 | ASM+ShiftDivide+caching 23889ms 0.398 minutes(!) 17004ms 0.283 minutes(!) - speed gain: 3.1x/3.4x 35 | 36 | 29 jan 2019 update (2): New records by using assembler (ASM) + font-table in dynamic memory! 37 | ASM+ShiftDivide 37095ms 0.617 minutes(!) 30209ms 0.503 minutes(!) - speed gain: 2.1x/1.9x 38 | ASM+ShiftDivide+caching 23860ms 0.397 minutes(!) 16974ms 0.282 minutes(!) - speed gain: 3.2x/3.5x 39 | 40 | 41 | 30 jan 2019: 42 | Another gain can be reached by using new define (which is default now), XTM_AVR_SHIFTWISE_DIVIDE_ASM. 43 | At this moment I don't have the time to update the tables above again. Fastest speed is 12756ms = 44 | 0.213 minutes = 12.756 seconds to count up to 100000 and display it. To achieve this, use fastest 45 | config settings specified below. 46 | 47 | ---- 48 | 49 | FASTEST CONFIG (highest mem usage) AVERAGE CONFIG (fast) SLOWEST CONFIG (very slow compared to all others) 50 | # XTM_AVR_ASM_MODE # XTM_APPLY_CACHED_SEGMENTS # XTM_ARDUINO_COMPATIBLE 51 | # XTM_APPLY_CACHED_SEGMENTS # XTM_SHIFTWISE_DIVIDE # XTM_ARITHMETIC_MULTIPLY 52 | # XTM_NOPROGMEM 53 | # XTM_SHIFTWISE_DIVIDE 54 | # XTM_AVR_SHIFTWISE_DIVIDE_ASM 55 | 56 | NOTICE: Compile sizes and memory usage may vary between different versions of the Arduino IDE and library. 57 | */ 58 | 59 | /* XTM_ARDUINO_COMPATIBLE: (default = disabled) 60 | ------------------------- 61 | By enabling this (uncomment it), forces the xtm1638 class to use Arduino 62 | compatible mode (disables auto detection), which means it performs the 63 | same on any Andruino library supported board/processor. 64 | Enable this when there are (detection) problems or for testing proposal 65 | /debugging. When enabling this for AVR's, drop in performance will be noticeable. 66 | NOTICE: You can improve performance significantly by enabling the define 67 | XTM_APPLY_CACHED_SEGMENTS below. 68 | */ 69 | //#define XTM_ARDUINO_COMPATIBLE 70 | 71 | // Used when enabled and creating object without parameters: 72 | #ifdef XTM_ARDUINO_COMPATIBLE 73 | #define XTM_ARD_AUTO_PIN_DATAIO 8 74 | #define XTM_ARD_AUTO_PIN_CLOCK 9 75 | #define XTM_ARD_AUTO_PIN_STROBE 10 76 | #endif 77 | 78 | 79 | /* XTM_AVR_ASM_MODE: (default = disabled) 80 | ------------------- 81 | By enabling this (uncomment it), forces the xtm1638 class to use optimized 82 | assembler instructions to manipulate pins (AVR only). Overall the 83 | fastest method of all to use, however, requires an extra library called 84 | FastGPIO (by Pololu Corporation) and it requires to configure static pin 85 | assignments below (not configurable via constructor). 86 | Reason: The FastGPIO::Pin class provides static functions for manipulating pins. 87 | This class can only be used if the pin number is known at compile time, which 88 | means it does not come from a variable that might change and it does not come 89 | from the result of a complicated calculation. 90 | You can download the FastGPIO library from here: 91 | - https://github.com/pololu/fastgpio-arduino 92 | */ 93 | #define XTM_AVR_ASM_MODE 94 | 95 | #ifdef XTM_AVR_ASM_MODE 96 | #define XTM_AVR_ASM_PIN_DATAIO 8 97 | #define XTM_AVR_ASM_PIN_CLOCK 9 98 | #define XTM_AVR_ASM_PIN_STROBE 10 99 | #endif 100 | 101 | 102 | /* XTM_APPLY_CACHED_SEGMENTS: (default = enabled) 103 | ---------------------------- 104 | By enabling this (uncommented state), enables the xtm1638 class to use 105 | a 8 byte buffer to cache the display content. Which means it performs 106 | about 1.9x up to 3.7x faster (see also updated PERFORMANCE STATS table on top 107 | of this file). 108 | The class doesn't write to the bus when a char is already send once. This is 109 | a very effective way to avoid unnecessary delays and to unstress the MCU. 110 | Disable this only when almost out of memory, when you want to save some 111 | memory (8 bytes of dynamic memory), your project doesn't need SUPERB 112 | performance or you discovered some bug. 113 | */ 114 | #define XTM_APPLY_CACHED_SEGMENTS 115 | 116 | 117 | /* XTM_NOPROGMEM: (default = disabled) 118 | ---------------- 119 | By enabling this (uncomment it), forces the xtm1638 class to use program/ 120 | dynamic memory, instead of storage flash memory, it disables the auto 121 | detection feature. Disabling or enabling this feature does not effect or will 122 | suit any performance gain at general speed configurations (ASM_MODE and cache 123 | enabled slightly will). However, if capacity is low on storage memory but not 124 | on dynamic memory, you can decide to enable this option permanently. 125 | */ 126 | //#define XTM_NOPROGMEM 127 | 128 | 129 | /* XTM_ARITHMETIC_MULTIPLY: (default = disabled) 130 | -------------------------- 131 | By enabling this (uncomment it), forces the xtm1638 class to use standard 132 | multiply arithmetic operations, it disables the "XTM_SHIFTWISE_DIVIDE" option 133 | (see below). Enable this when there are performance/compatibility problems 134 | or for testing proposal/debugging only. 135 | 136 | Enabling this is most of the time an overall PERFORMANCE DROP. 137 | */ 138 | //#define XTM_ARITHMETIC_MULTIPLY 139 | 140 | 141 | /* XTM_SHIFTWISE_DIVIDE: (default = enabled) 142 | XTM_AVR_SHIFTWISE_DIVIDE_ASM: (default = enabled) 143 | ------------------------------- 144 | By disabling this (comment it), forces the xtm1638 class to use standard 145 | multiply or divide arithmetic operations, it can be overridden by the previous 146 | listed "XTM_ARITHMETIC_MULTIPLY" option. Disable this when there are 147 | performance/compatibility problems or for testing proposal/debugging only. 148 | 149 | Disabling this is most of the time an overall PERFORMANCE DROP. 150 | 151 | To make it perform a little faster, you can decide to enable the assembler 152 | version of this (AVR only) by uncomment the XTM_AVR_SHIFTWISE_DIVIDE_ASM 153 | option below. 154 | */ 155 | #define XTM_SHIFTWISE_DIVIDE 156 | #define XTM_AVR_SHIFTWISE_DIVIDE_ASM 157 | 158 | 159 | /* DEFINE A PORT TO USE (NOT REQUIRED WHEN COMPATIBLE MODE OR ASM MODE): 160 | ------------------------------------------------------------------------ 161 | NOTE: All three pins used must be bits on the same PORT 162 | register (ex. PORTB). 163 | ------------------------------------------------------------------------ 164 | PORTB (XTM_PORTB) PORTD (XTM_PORTD) PORTC (XTM_PORTC) 165 | PB0 D8 PD0 D0/RX PC0 A0 166 | PB1 D9 PD1 D1/TX PC1 A1 167 | PB2 D10 PD2 D2 PC2 A2 168 | PB3 D11/MOSI PD3 D3 PC3 A3 169 | PB4 D12/MISO PD4 D4 PC4 A4 170 | PB5 D13/SCK PD5 D5 PC5 A5 171 | PD6 D6 172 | PD7 D7 173 | 174 | Just uncomment only one port option: 175 | */ 176 | #define XTM_PORT XTM_PORTB 177 | //#define XTM_PORT XTM_PORTC 178 | //#define XTM_PORT XTM_PORTD 179 | 180 | 181 | // Used when creating object without parameters: 182 | #define XTM_REG_DEF_PIN_DATAIO PB0 183 | #define XTM_REG_DEF_PIN_CLOCK PB1 184 | #define XTM_REG_DEF_PIN_STROBE PB2 185 | 186 | 187 | /* SOME DEFINES FOR YOU CONVENIENCE: 188 | ----------------------------------- 189 | Disable, enable or add what you like 190 | */ 191 | #define PB4 4 //D12 192 | #define PB3 3 //D11 193 | #define PB2 2 //D10 194 | #define PD5 5 //D5 195 | #define PD4 4 //D4 196 | #define PD3 3 //D3 197 | 198 | -------------------------------------------------------------------------------- /Examples/xtm1638Example02/xtm1638Example02.ino: -------------------------------------------------------------------------------- 1 | // Project: xtm1638 usage examples, nr. 2 2 | // Author : codebeat - Erwin Haantjes - http://codebeat.nl 3 | // Source : https://github.com/codebeat-nl/xtm1638 4 | // Date : 17 may 2017 5 | // --------------------------------- 6 | // WHAT IT DOES 7 | // Sample program, a simple basic clock (with hours, minutes and seconds), 8 | // to show you how to work with the xtm1638 class, this example includes: 9 | // o Handle compatible mode (so you can use the library on any 'Arduino') 10 | // o Read and use buttons and how to handle button combinations 11 | // o Using a timer to time share operation without interuption 12 | // o Change the orientation of board operation 13 | // 14 | // NOTICE 15 | // o This is an example, this clock might not that accurate for daily use. 16 | // You can fine-tune it yourself if you want to. 17 | // o RTC is missing so there is no time back-up, power loss = reset. 18 | // 19 | // BUTTON ASSIGMENTS (LEFT TO RIGHT) 20 | // o Button #1 - SET Hour button 21 | // |__ + Button #6 - Hour up 22 | // |__ + Button #7 - Hour down 23 | // o Button #2 - SET minute button 24 | // |__ + Button #6 - Hour up 25 | // |__ + Button #7 - Hour down 26 | // o Button #3 - RESET seconds button 27 | // |__ + Button #6 - Reset seconds to 00 28 | // |__ + Button #7 - Reset seconds to 00 29 | // o Button #4 - Set brightness of display 30 | // |__ + Button #6 - Brightness up 31 | // |__ + Button #7 - Brightness down 32 | // o Button #5 - Toggle blinky "dot" on/off 33 | // o Button #8 - Switch orientation of the board 34 | // NOTICE: All functionality is swapped, is upside down, also buttons! 35 | 36 | 37 | // Include the library first 38 | #include 39 | #include "Timer.h" 40 | 41 | 42 | // ARDUINO COMPATIBLE MODE EXPLAINED 43 | // --------------------------------- 44 | // If the code detects it is not compiled on an ATMEL AVR, it will switch to Arduino 45 | // compatible mode, which means the code doesn't use port registers directly (Arduino's 46 | // library handles this). In this case you need to specify (digital) PIN numbers. 47 | // Overall, compatible mode is slower than the direct port access methods the code 48 | // provides, but when your application is not time critical, it just works fine. 49 | // Also, in compatible mode, the code consumes more memory, a few hundred bytes more. 50 | 51 | #ifdef XTM_ARDUINO_COMPATIBLE 52 | // Specify (digital) PINS as described on the board 53 | #define PIN_DIG_LEDKEY_DATAIO 8 54 | #define PIN_DIG_LEDKEY_CLOCK 9 55 | #define PIN_DIG_LEDKEY_STROBE 10 56 | #else 57 | // Specify register port PINS specified in the documentation of the board 58 | #define PIN_REG_LEDKEY_DATAIO PB0 59 | #define PIN_REG_LEDKEY_CLOCK PB1 60 | #define PIN_REG_LEDKEY_STROBE PB2 61 | #endif 62 | 63 | // Create the class object 64 | #ifdef XTM_ARDUINO_COMPATIBLE 65 | // Specify (digital) PINS as described on the board 66 | static xtm1638 ledandkey( PIN_DIG_LEDKEY_DATAIO, 67 | PIN_DIG_LEDKEY_CLOCK, 68 | PIN_DIG_LEDKEY_STROBE 69 | ); 70 | #else 71 | // Specify register port PINS specified in the documentation of the board 72 | static xtm1638 ledandkey( PIN_REG_LEDKEY_DATAIO, 73 | PIN_REG_LEDKEY_CLOCK, 74 | PIN_REG_LEDKEY_STROBE 75 | ); 76 | 77 | // Read the xtm1638.config.h file to find more options 78 | #endif 79 | 80 | 81 | // Timers for clock update and button processing 82 | static TTimer tSec = TTimer( 1000, (bool)true, (bool)false ); 83 | static TTimer tButton = TTimer( 200, (bool)true, (bool)false ); 84 | 85 | // Clock data 86 | static uint8_t iSec = 0; 87 | static uint8_t iMin = 0; 88 | static uint8_t iHour = 0; 89 | static bool bBlink = false; 90 | 91 | // Board settings 92 | static bool bOrient = XTM_ORIENT_NORMAL; 93 | static uint8_t iBrightness = 7; 94 | 95 | // Operation states 96 | static bool bBlinkEnabled = true; 97 | static bool bButtonPressed = false; 98 | static uint8_t iMsgTimeOut = false; 99 | 100 | 101 | // Updates a digit leading zero 102 | void updateDigit( uint8_t iPos, uint8_t iValue ) 103 | { 104 | if( iValue < 10 ) 105 | { 106 | ledandkey.setByte(iPos, 0x3F ); 107 | ledandkey.setDigit(iPos+1, iValue); 108 | } 109 | else { ledandkey.setNumber(iValue, iPos+1 ); } 110 | } 111 | 112 | // Updates time settings and changes 113 | void updateTime(bool bTimeUpdate = false, bool bDisplay = true ) 114 | { 115 | if( bTimeUpdate ) 116 | { ++iSec; } 117 | 118 | if( iSec > 59 ) 119 | { iSec = 0; 120 | if( bTimeUpdate ) 121 | { ++iMin; } 122 | } 123 | 124 | if( iMin > 59 ) 125 | { 126 | if( bTimeUpdate ) 127 | { 128 | iSec = iMin = 0; 129 | ++iHour; 130 | } 131 | else { iMin = 0; } 132 | } 133 | 134 | if( iHour > 23 ) 135 | { 136 | if( bTimeUpdate ) 137 | { 138 | iHour = iSec = iMin = 0; 139 | } else { iHour = 0; } 140 | } 141 | 142 | if( bDisplay ) 143 | { 144 | iMsgTimeOut=0; 145 | if( bTimeUpdate ) 146 | { bBlink=!bBlink; } 147 | 148 | updateDigit(0, iHour ); 149 | updateDigit(3, iMin ); 150 | updateDigit(6, iSec ); 151 | 152 | ledandkey.setByte(2, (bBlink && bBlinkEnabled)?XTM_EQUALS:XTM_OFF ); 153 | ledandkey.setByte(5, (bBlink && bBlinkEnabled)?XTM_EQUALS:XTM_OFF ); 154 | } 155 | } 156 | 157 | // Shows a message on display for a period of seconds 158 | void showMessage(char* sMessage, uint8_t iSecs = 2 ) 159 | { 160 | iMsgTimeOut=iSecs; 161 | ledandkey.setChars( sMessage ); 162 | } 163 | 164 | void setup() 165 | { 166 | // Nothing special here, just say hello 167 | showMessage(" =Hello="); 168 | } 169 | 170 | void loop() 171 | { 172 | if( tSec.isTime() ) 173 | { 174 | updateTime(true, (iMsgTimeOut == 0)); 175 | if( iMsgTimeOut > 0 ) 176 | { --iMsgTimeOut; } 177 | } 178 | 179 | if( tButton.isTime() ) 180 | { 181 | // Check if button pressed 182 | uint8_t iButton = ledandkey.getButtonPressed(); 183 | 184 | 185 | // Button press detected? 186 | if( iButton != XTM_NOBUTTON ) 187 | { 188 | bButtonPressed = true; 189 | bool bBtUp = ledandkey.isButtonPressed( XTM_BUTTON6 ); 190 | bool bBtDown = !bBtUp?ledandkey.isButtonPressed( XTM_BUTTON7 ):false; 191 | 192 | // User want to change hours? 193 | if( iButton == XTM_BUTTON1 ) 194 | { 195 | //char values[] = { 1, 2, 4, 8, 16, 32, 64, 128 }; 196 | //ledandkey.setBytes(values, 0); 197 | //return; 198 | 199 | ledandkey.setLED(0); 200 | ledandkey.setLED(1); 201 | 202 | // Up? 203 | if( bBtUp ) 204 | { ++iHour; 205 | updateTime(); 206 | } 207 | else if( bBtDown ) 208 | { 209 | iHour = (iHour > 0)?(iHour-1):23; 210 | updateTime(); 211 | } 212 | } 213 | else 214 | 215 | // User want to change minutes? 216 | if( iButton == XTM_BUTTON2 ) 217 | { 218 | ledandkey.setLED(3); 219 | ledandkey.setLED(4); 220 | 221 | // Up? 222 | if( bBtUp ) 223 | { ++iMin; 224 | updateTime(); 225 | } 226 | else if( bBtDown ) 227 | { 228 | iMin = (iMin > 0)?(iMin-1):59; 229 | updateTime(); 230 | } 231 | } 232 | else 233 | 234 | // User want to reset seconds? 235 | if( iButton == XTM_BUTTON3 ) 236 | { 237 | ledandkey.setLED(6); 238 | ledandkey.setLED(7); 239 | 240 | // Up? 241 | if( bBtUp || bBtDown ) 242 | { iSec = 0; 243 | updateTime(); 244 | } 245 | } 246 | else 247 | 248 | // User want to change brightness? 249 | if( iButton == XTM_BUTTON4 ) 250 | { 251 | showMessage("Brightns", 1 ); 252 | if( bBtUp || bBtDown ) 253 | { 254 | if( bBtUp ) 255 | { 256 | ++iBrightness; 257 | if( iBrightness > 7 ) 258 | { iBrightness = 0; } 259 | } 260 | else { 261 | if( iBrightness == 0 ) 262 | { iBrightness = 7; } 263 | else { --iBrightness; } 264 | } 265 | 266 | ledandkey.setDisplay( true, iBrightness ); 267 | } 268 | 269 | uint8_t i = 8; 270 | while( i-- ) 271 | { if( i <= iBrightness ) 272 | { ledandkey.setLED(i); } 273 | else { ledandkey.clearLED(i); } 274 | } 275 | } 276 | else 277 | 278 | // User want to change blinky character on/off? 279 | if( iButton == XTM_BUTTON5 ) 280 | { 281 | if( iMsgTimeOut == 0 ) 282 | { 283 | bBlinkEnabled=!bBlinkEnabled; 284 | showMessage( bBlinkEnabled?"Blnk On":"Blnk Off" ); 285 | } 286 | } 287 | else { ledandkey.clearLEDs(); } 288 | 289 | // User want to change orientation? 290 | if( iButton == XTM_BUTTON8 ) 291 | { 292 | if( iMsgTimeOut == 0 ) 293 | { 294 | ledandkey.setOrientation( bOrient ); 295 | bOrient=!bOrient; 296 | showMessage(bOrient?"= Normal":"= Upside" ); 297 | } 298 | } 299 | //else { ledandkey.clearLEDs(); } 300 | } 301 | else { 302 | if( bButtonPressed ) 303 | { 304 | bButtonPressed = false; 305 | ledandkey.clearLEDs(); 306 | } 307 | } 308 | } 309 | } 310 | -------------------------------------------------------------------------------- /Examples/xtm1638Example05/xtm1638Example05.ino: -------------------------------------------------------------------------------- 1 | // Project: xtm1638 usage examples, nr. 5 2 | // Author : codebeat - Erwin Haantjes - http://codebeat.nl 3 | // Source : https://github.com/codebeat-nl/xtm1638 4 | // Date : 30 jan 2019 5 | // --------------------------------- 6 | // WHAT IT DOES 7 | // Sample program to show you how to work with the object, this example includes: 8 | // o Handle compatible mode (so you can use the library on any 'Arduino') 9 | // o Read and use buttons, handle button combinations 10 | // o The gauge functionality and it possibilities 11 | // o Change the orientation of operation 12 | // o Battery indicator and animation demo 13 | // 14 | // 15 | // BUTTON ASSIGMENTS (LEFT TO RIGHT) 16 | // o Button #1 - Select gauge type 17 | // o Button #2 - Select gauge style 18 | // o Button #3 - Gauge peakhold on/off 19 | // o Button #4 - Switch Single or Dual (average) mode of gauge 20 | // |__ + Button #2 - Shows a message that two buttons are pressed 21 | // o Button #5 - Battery indicator demo 22 | // o Button #6 - Simple animation demo 23 | // o Button #7 - Not in use 24 | // o Button #8 - Switch orientation of the board 25 | // NOTICE: All functionality is swapped, is upside down, also buttons! 26 | 27 | 28 | // Include the library first 29 | #include 30 | 31 | // ARDUINO COMPATIBLE MODE EXPLAINED 32 | // --------------------------------- 33 | // If the code detects it is not compiled on an ATMEL AVR, it will switch to Arduino 34 | // compatible mode, which means the code doesn't use port registers directly (Arduino's 35 | // library handles this). In this case you need to specify (digital) PIN numbers. 36 | // Overall, compatible mode is slower than the direct port access methods the code 37 | // provides, but when your application is not time critical, it just works fine. 38 | // Also, in compatible mode, the code consumes more memory, a few hundred bytes more. 39 | 40 | #ifdef XTM_ARDUINO_COMPATIBLE 41 | // Specify (digital) PINS as described on the board 42 | #define PIN_DIG_LEDKEY_DATAIO 8 43 | #define PIN_DIG_LEDKEY_CLOCK 9 44 | #define PIN_DIG_LEDKEY_STROBE 10 45 | #else 46 | // Specify register port PINS specified in the documentation of the board 47 | #define PIN_REG_LEDKEY_DATAIO PB0 48 | #define PIN_REG_LEDKEY_CLOCK PB1 49 | #define PIN_REG_LEDKEY_STROBE PB2 50 | 51 | // Read the xtm1638.config.h file to find more options 52 | #endif 53 | 54 | // Create the class object 55 | #ifdef XTM_ARDUINO_COMPATIBLE 56 | // Specify (digital) PINS as described on the board 57 | static xtm1638 ledandkey( PIN_DIG_LEDKEY_DATAIO, 58 | PIN_DIG_LEDKEY_CLOCK, 59 | PIN_DIG_LEDKEY_STROBE 60 | ); 61 | #else 62 | // Specify register port PINS specified in the documentation of the board 63 | static xtm1638 ledandkey( PIN_REG_LEDKEY_DATAIO, 64 | PIN_REG_LEDKEY_CLOCK, 65 | PIN_REG_LEDKEY_STROBE 66 | ); 67 | #endif 68 | 69 | #define iMinStyle XTM_GAUGE_STYLE_PIPE 70 | #define iMaxStyle XTM_GAUGE_STYLE_LED 71 | #define iMinSubStyle XTM_GAUGE_SUBSTYLE_NORMAL 72 | #define iMaxSubStyle XTM_GAUGE_SUBSTYLE_INBOUND 73 | 74 | // Global vars, setting holders 75 | static bool bOrient = XTM_ORIENT_NORMAL; 76 | static bool bAudioInput = true; 77 | static uint8_t iFirstPerc = 0; // Percents % 78 | static uint8_t iSecondPerc = 100; // Percents % 79 | static uint8_t iPeakHold = 0; // Percents % 80 | static uint8_t bPeakEnabled = false; 81 | static uint8_t bDualPerc = true; 82 | static uint8_t iStyle = iMaxStyle; 83 | static uint8_t iSubStyle = iMinSubStyle; 84 | 85 | // Names and settings of gauge type 86 | static const char* aStyleNames[] = {"! Pipe", "! Stripe", "!BullTop", "!BullBtm", "!CentrLn", "{ LEDS }"}; 87 | 88 | // Names and settings of gauge style 89 | static const char* aSubStyleNames[] = {"Normal", "Center" /* = outbound */, "Inbound"}; 90 | 91 | 92 | uint8_t getAnalogAudio( uint8_t iChannel ) 93 | { 94 | uint16_t iResult = analogRead( iChannel ); 95 | 96 | if( iResult > 0 ) 97 | { 98 | iResult = round(iResult * (100.0 / 1024.0)); 99 | } 100 | 101 | return ( iResult > 100 )?100:iResult; 102 | } 103 | 104 | 105 | void setup() 106 | { 107 | // Nothing special here 108 | Serial.begin( 9600 ); 109 | 110 | // Dim the display a bit, it is very, very bright at default 111 | ledandkey.setDisplay(true,2); 112 | 113 | pinMode( A1, INPUT ); 114 | pinMode( A2, INPUT ); 115 | 116 | ledandkey.setChars( "TOUCH" ); 117 | delay(1500); 118 | ledandkey.setChars( "PIN A1" ); 119 | delay(1500); 120 | ledandkey.setChars( "OR AND" ); 121 | delay(1500); 122 | ledandkey.setChars( "PIN A2" ); 123 | delay(1500); 124 | ledandkey.setChars( "WITH" ); 125 | delay(1500); 126 | ledandkey.setChars( " YOUR" ); 127 | delay(1500); 128 | ledandkey.setChars( "FINGER" ); 129 | delay(1500); 130 | ledandkey.clear(); 131 | } 132 | 133 | 134 | void loop() 135 | { 136 | // Check if button pressed 137 | uint8_t iButton = ledandkey.getButtonPressed(); 138 | 139 | // Button press detected? 140 | if( iButton != XTM_NOBUTTON ) 141 | { 142 | delay(200); 143 | // User want to change gauge type? 144 | if( iButton == XTM_BUTTON1 ) 145 | { 146 | ++iStyle; 147 | if( iStyle > iMaxStyle ) 148 | { iStyle = iMinStyle; } 149 | 150 | ledandkey.setChars( aStyleNames[iStyle-iMinStyle] ); 151 | } 152 | else 153 | 154 | // User want to change style? 155 | if( iButton == XTM_BUTTON2 ) 156 | { 157 | ++iSubStyle; 158 | if( iSubStyle > iMaxSubStyle ) 159 | { iSubStyle = iMinSubStyle; } 160 | 161 | ledandkey.setChars( aSubStyleNames[iSubStyle-iMinSubStyle] ); 162 | } 163 | else 164 | 165 | // User want to turn peakhold ON or OFF? 166 | if( iButton == XTM_BUTTON3 ) 167 | { 168 | bPeakEnabled=!bPeakEnabled; 169 | ledandkey.setChars( bPeakEnabled?"Peak on":"Peak off" ); 170 | } 171 | else 172 | 173 | // User want to change gauge dual(average)/single mode? 174 | if( iButton == XTM_BUTTON4 ) 175 | { 176 | // Or when the user pressed two buttons, show this 177 | if( ledandkey.isButtonPressed( XTM_BUTTON2 )) 178 | { 179 | ledandkey.setChars( "YOU" ); 180 | delay(2000); 181 | ledandkey.setChars( "pressed" ); 182 | delay(2000); 183 | ledandkey.setChars( "two" ); 184 | delay(2000); 185 | ledandkey.setChars( "buttons" ); 186 | } 187 | else { 188 | bDualPerc=!bDualPerc; 189 | ledandkey.setChars(bDualPerc?"DualAVG":"Single" ); 190 | } 191 | delay(200); 192 | } 193 | else 194 | 195 | if( iButton == XTM_BUTTON5 ) 196 | { 197 | // Fake battery indicator 198 | ledandkey.gauge( 0, 0, 80, iStyle, XTM_GAUGE_SUBSTYLE_INBOUND ); 199 | ledandkey.setChars( "BATT", 0, false ); 200 | delay( 4000 ); 201 | ledandkey.setChars( "BATT" ); 202 | ledandkey.setNumber( 80 ); 203 | delay( 4000 ); 204 | ledandkey.clear(); 205 | ledandkey.clearLEDs(); 206 | } 207 | else 208 | 209 | 210 | if( iButton == XTM_BUTTON6 ) 211 | { 212 | // Simple 'Wave' animation 213 | 214 | // Wait until no key pressed 215 | while( ledandkey.getButtonPressed() != XTM_NOBUTTON ); 216 | 217 | uint8_t i; 218 | bool b = true; // waveflag 219 | 220 | // Do this while no key is pressed 221 | while( ledandkey.getButtonPressed() == XTM_NOBUTTON ) 222 | { 223 | i=8; 224 | while( i-- ) 225 | { 226 | ledandkey.setByte( i, b?0x23:0x1C ); 227 | if( i > 0 ) 228 | { b=!b; } 229 | delay(10); 230 | } 231 | delay(100); 232 | } 233 | 234 | ledandkey.clear(); 235 | ledandkey.clearLEDs(); 236 | } 237 | else 238 | 239 | if( iButton == XTM_BUTTON7 ) 240 | { 241 | // do something you want here 242 | } 243 | else 244 | 245 | // User want to change orientation? 246 | if( iButton == XTM_BUTTON8 ) 247 | { 248 | ledandkey.setOrientation( bOrient ); 249 | bOrient=!bOrient; 250 | ledandkey.setChars(bOrient?"= Normal":"= Upside" ); 251 | } 252 | else { return; } 253 | 254 | if( iStyle == XTM_GAUGE_STYLE_LED ) 255 | { delay(2000); 256 | ledandkey.clear(); 257 | } 258 | else { 259 | ledandkey.clearLEDs(); 260 | delay(2000); 261 | } 262 | 263 | 264 | // Wait for release 265 | ledandkey.waitForNoButtonPressed(); 266 | } 267 | 268 | if( bAudioInput ) 269 | { 270 | iFirstPerc = getAnalogAudio(A1); 271 | iSecondPerc = getAnalogAudio(A2); 272 | } 273 | else { 274 | if( iSecondPerc > 0 ) 275 | { iSecondPerc-=2; } 276 | else { iSecondPerc = 100; } 277 | 278 | iFirstPerc+=2; 279 | if( iFirstPerc > 100 ) 280 | { iFirstPerc = 0; } 281 | 282 | delay(100); 283 | } 284 | 285 | 286 | // Apply 287 | ledandkey.gauge( bPeakEnabled?(bDualPerc?iPeakHold:iFirstPerc):0, 288 | iFirstPerc, 289 | bDualPerc?iSecondPerc:XTM_GAUGE_SINGLE, 290 | iStyle, 291 | iSubStyle ); 292 | 293 | iPeakHold = iFirstPerc>iSecondPerc?iFirstPerc:iSecondPerc; 294 | 295 | } 296 | -------------------------------------------------------------------------------- /Examples/xtm1638Example03/xtm1638Example03.ino: -------------------------------------------------------------------------------- 1 | // Project: xtm1638 usage examples, nr. 3 2 | // Author : codebeat - Erwin Haantjes - http://codebeat.nl 3 | // Source : https://github.com/codebeat-nl/xtm1638 4 | // Date : 17 may 2017 5 | // --------------------------------- 6 | // WHAT IT DOES 7 | // Sample program to show you how to work with the object, this example includes: 8 | // o Handle compatible mode (so you can use the library on any 'Arduino') 9 | // o Read and use buttons, handle button combinations 10 | // o The gauge functionality and it possibilities 11 | // o Change the orientation of operation 12 | // o Character demo 13 | // o Performance test 14 | // 15 | // 16 | // BUTTON ASSIGMENTS (LEFT TO RIGHT) 17 | // o Button #1 - Switch Single or Dual (average) mode of gauge 18 | // |__ + Button #2 - Shows a message that two buttons are pressed 19 | // o Button #2 - Select gauge type 20 | // o Button #3 - Gauge peakhold on/off 21 | // o Button #4 - Select gauge style 22 | // o Button #5 - Character demo, shows all characters possible 23 | // o Button #6 - Performance test, count up to 100.000, report you a score in milliseconds 24 | // o Button #7 - Performance test, same as #6 but without flickering LEDS 25 | // o Button #8 - Switch orientation of the board 26 | // NOTICE: All functionality is swapped, is upside down, also buttons! 27 | 28 | 29 | // Include the library first 30 | #include 31 | 32 | // ARDUINO COMPATIBLE MODE EXPLAINED 33 | // --------------------------------- 34 | // If the code detects it is not compiled on an ATMEL AVR, it will switch to Arduino 35 | // compatible mode, which means the code doesn't use port registers directly (Arduino's 36 | // library handles this). In this case you need to specify (digital) PIN numbers. 37 | // Overall, compatible mode is slower than the direct port access methods the code 38 | // provides, but when your application is not time critical, it just works fine. 39 | // Also, in compatible mode, the code consumes more memory, a few hundred bytes more. 40 | 41 | #ifdef XTM_ARDUINO_COMPATIBLE 42 | // Specify (digital) PINS as described on the board 43 | #define PIN_DIG_LEDKEY_DATAIO 8 44 | #define PIN_DIG_LEDKEY_CLOCK 9 45 | #define PIN_DIG_LEDKEY_STROBE 10 46 | #else 47 | // Specify register port PINS specified in the documentation of the board 48 | #define PIN_REG_LEDKEY_DATAIO PB0 49 | #define PIN_REG_LEDKEY_CLOCK PB1 50 | #define PIN_REG_LEDKEY_STROBE PB2 51 | #endif 52 | 53 | // Create the class object 54 | #ifdef XTM_ARDUINO_COMPATIBLE 55 | // Specify (digital) PINS as described on the board 56 | static xtm1638 ledandkey( PIN_DIG_LEDKEY_DATAIO, 57 | PIN_DIG_LEDKEY_CLOCK, 58 | PIN_DIG_LEDKEY_STROBE 59 | ); 60 | #else 61 | // Specify register port PINS specified in the documentation of the board 62 | static xtm1638 ledandkey( PIN_REG_LEDKEY_DATAIO, 63 | PIN_REG_LEDKEY_CLOCK, 64 | PIN_REG_LEDKEY_STROBE 65 | ); 66 | #endif 67 | 68 | #define iMinStyle XTM_GAUGE_STYLE_PIPE 69 | #define iMaxStyle XTM_GAUGE_STYLE_LED 70 | #define iMinSubStyle XTM_GAUGE_SUBSTYLE_NORMAL 71 | #define iMaxSubStyle XTM_GAUGE_SUBSTYLE_INBOUND 72 | 73 | // Global vars, setting holders 74 | static bool bOrient = XTM_ORIENT_NORMAL; 75 | static uint8_t iFirstPerc = 0; // Percents % 76 | static uint8_t iSecondPerc = 100; // Percents % 77 | static uint8_t iPeakHold = 0; // Percents % 78 | static uint8_t bPeakEnabled = true; 79 | static uint8_t bDualPerc = true; 80 | static uint8_t iStyle = iMaxStyle; 81 | static uint8_t iSubStyle = iMinSubStyle; 82 | 83 | // Names and settings of gauge type 84 | static const char* aStyleNames[] = {"! Pipe", "! Stripe", "!BullTop", "!BullBtm", "!CentrLn", "{ LEDS }"}; 85 | 86 | // Names and settings of gauge style 87 | static const char* aSubStyleNames[] = {"Normal", "Center", "inbound"}; 88 | 89 | 90 | void startCharDemo() 91 | { 92 | // NOTICE: 93 | // I use a macro of the class to get characters, forget it, it's low level stuff and 94 | // only used here to show all the characters available. Normally you don't need this. 95 | 96 | uint8_t i = 0; 97 | uint8_t x; 98 | bool b; 99 | bool bWantEscape = false; 100 | 101 | ledandkey.clearLEDs(); 102 | ledandkey.setLED(0); 103 | ledandkey.setLED(3); 104 | ledandkey.setLED(4); 105 | ledandkey.setLED(7); 106 | 107 | ledandkey.setChars( "CHR DEMO" ); 108 | delay(2000); 109 | ledandkey.clearLED(0); 110 | ledandkey.setChars( "HOLD DWN" ); 111 | delay(2000); 112 | ledandkey.clearLED(7); 113 | ledandkey.setChars( "BUTTON 5" ); 114 | delay(2000); 115 | ledandkey.clearLED(3); 116 | ledandkey.setChars( "TO ESCPE" ); 117 | delay(2000); 118 | ledandkey.clearLED(4); 119 | 120 | // Wait for release, if not show user message and wait again.... 121 | while( !ledandkey.waitForNoButtonPressed( XTM_BUTTON5 ) ) 122 | { 123 | ledandkey.setChars( "RELEASE" ); 124 | delay( 2000 ); 125 | ledandkey.setChars( "BUTTON" ); 126 | delay( 2000 ); 127 | ledandkey.setChars( "NOW" ); 128 | delay( 2000 ); 129 | ledandkey.clear(); 130 | } 131 | 132 | 133 | while( !bWantEscape && (i+7 < XTM_SPECIAL_CHAR_OFFSET) ) 134 | { 135 | if( ledandkey.getButtonPressed() == XTM_BUTTON5 ) 136 | { bWantEscape = true; } 137 | 138 | for( x=0; x < 8; ++x ) 139 | { ledandkey.setByte( x, XTM_GET_DIGIT(i+x) ); } 140 | ++i; 141 | delay(200); 142 | } 143 | 144 | if( !bWantEscape ) 145 | { 146 | delay(1000); 147 | ledandkey.setChars( "SPECIAL" ); 148 | delay(2000); 149 | ledandkey.setChars( " CHARS" ); 150 | delay(2000); 151 | ledandkey.setChars( " {CHRS" ); 152 | 153 | i = XTM_SPECIAL_CHAR_OFFSET; 154 | while( !bWantEscape && (XTM_GET_DIGIT(i)) ) 155 | { 156 | if( ledandkey.getButtonPressed() == XTM_BUTTON5 ) 157 | { bWantEscape = true; } 158 | 159 | if( !bWantEscape ) 160 | { 161 | b = false; 162 | for( x=0; x < 8; ++x ) 163 | { 164 | ledandkey.setByte( 0, (b=!b)?XTM_GET_DIGIT(i+1):0x00 ); 165 | delay(100); 166 | } 167 | 168 | delay(200); 169 | } 170 | i+=2; 171 | } 172 | } 173 | 174 | ledandkey.setChars( "END DEMO" ); 175 | delay(1000); 176 | 177 | } 178 | 179 | 180 | void startCounterDemo(bool bWithoutSetLed = false) // Count up to 100.000 181 | { 182 | ledandkey.clearLEDs(); 183 | ledandkey.setLED(0); 184 | ledandkey.setLED(3); 185 | ledandkey.setLED(4); 186 | ledandkey.setLED(7); 187 | 188 | ledandkey.setChars( "COUNTER" ); 189 | delay(2000); 190 | ledandkey.clearLED(0); 191 | ledandkey.setChars( "SPEED" ); 192 | delay(2000); 193 | ledandkey.clearLED(7); 194 | ledandkey.setChars( "} DEMO" ); 195 | delay(2000); 196 | ledandkey.clearLED(3); 197 | ledandkey.setChars( bWithoutSetLed?"NO LEDS":"100000x " ); 198 | delay(2000); 199 | ledandkey.clearLED(4); 200 | ledandkey.clear(); 201 | 202 | unsigned long i = 0; 203 | uint8_t x = 0; 204 | uint8_t y = 0; 205 | unsigned long iBench = millis(); 206 | 207 | if( bWithoutSetLed ) 208 | { 209 | while( ++i < 100001UL ) 210 | { ledandkey.setNumber(i); } 211 | } 212 | else { 213 | while( ++i < 100001UL ) 214 | { 215 | ledandkey.clearLED(y); 216 | ledandkey.setLED(x); 217 | y = x; 218 | ++x; 219 | if( x > 7 ) 220 | { x = 0; } 221 | ledandkey.setNumber(i); 222 | } 223 | } 224 | 225 | iBench = millis()-iBench; 226 | ledandkey.setChars( "DONE" ); 227 | delay(2000); 228 | ledandkey.clearLEDs(); 229 | ledandkey.setChars( "SCORE" ); 230 | delay(2000); 231 | 232 | ledandkey.clear(); 233 | ledandkey.setNumber( iBench ); 234 | delay(4000); 235 | } 236 | 237 | 238 | void setup() 239 | { 240 | // Nothing special here 241 | Serial.begin( 9600 ); 242 | ledandkey.setDisplay(true,2); 243 | } 244 | 245 | 246 | void loop() 247 | { 248 | // Check if button pressed 249 | uint8_t iButton = ledandkey.getButtonPressed(); 250 | 251 | // Button press detected? 252 | if( iButton != XTM_NOBUTTON ) 253 | { 254 | // User want to change gauge dual(average)/single mode? 255 | if( iButton == XTM_BUTTON1 ) 256 | { 257 | delay(200); 258 | // Or when the user pressed two buttons, show this 259 | if( ledandkey.isButtonPressed( XTM_BUTTON2 )) 260 | { 261 | ledandkey.setChars( "YOU" ); 262 | delay(2000); 263 | ledandkey.setChars( "pressed" ); 264 | delay(2000); 265 | ledandkey.setChars( "two" ); 266 | delay(2000); 267 | ledandkey.setChars( "buttons" ); 268 | } 269 | else { 270 | bDualPerc=!bDualPerc; 271 | ledandkey.setChars(bDualPerc?"DualAVG":"Single" ); 272 | delay(2000); 273 | } 274 | } 275 | else 276 | // User want to change gauge type? 277 | if( iButton == XTM_BUTTON2 ) 278 | { 279 | ++iStyle; 280 | if( iStyle > iMaxStyle ) 281 | { iStyle = iMinStyle; } 282 | 283 | ledandkey.setChars( aStyleNames[iStyle-iMinStyle] ); 284 | } 285 | else 286 | 287 | // User want to turn peakhold ON or OFF? 288 | if( iButton == XTM_BUTTON3 ) 289 | { 290 | bPeakEnabled=!bPeakEnabled; 291 | ledandkey.setChars( bPeakEnabled?"Peak on":"Peak off" ); 292 | } 293 | else 294 | 295 | 296 | // User want to change style? 297 | if( iButton == XTM_BUTTON4 ) 298 | { 299 | ++iSubStyle; 300 | if( iSubStyle > iMaxSubStyle ) 301 | { iSubStyle = iMinSubStyle; } 302 | 303 | ledandkey.setChars( aSubStyleNames[iSubStyle-iMinSubStyle] ); 304 | } 305 | else 306 | 307 | // User want to show character demo? 308 | if( iButton == XTM_BUTTON5 ) 309 | { startCharDemo(); } 310 | else 311 | 312 | // User want to show speed demo? 313 | if( iButton == XTM_BUTTON6 ) 314 | { startCounterDemo(); } 315 | else 316 | 317 | // User want to show speed demo without LEDs? 318 | if( iButton == XTM_BUTTON7 ) 319 | { startCounterDemo(true); } 320 | else 321 | 322 | // User want to change orientation? 323 | if( iButton == XTM_BUTTON8 ) 324 | { 325 | ledandkey.setOrientation( bOrient ); 326 | bOrient=!bOrient; 327 | ledandkey.setChars(bOrient?"= Normal":"= Upside" ); 328 | } 329 | else { return; } 330 | 331 | if( iStyle == XTM_GAUGE_STYLE_LED ) 332 | { ledandkey.clear(); } 333 | else { ledandkey.clearLEDs(); } 334 | 335 | delay(2000); 336 | // Wait for release 337 | ledandkey.waitForNoButtonPressed(); 338 | } 339 | 340 | 341 | if( iSecondPerc > 0 ) 342 | { iSecondPerc-=2; } 343 | else { iSecondPerc = 100; } 344 | 345 | iFirstPerc+=2; 346 | if( iFirstPerc > 100 ) 347 | { iFirstPerc = 0; } 348 | 349 | 350 | // Apply 351 | ledandkey.gauge( bPeakEnabled?(bDualPerc?iPeakHold:iFirstPerc):0, 352 | iFirstPerc, 353 | bDualPerc?iSecondPerc:XTM_GAUGE_SINGLE, 354 | iStyle, 355 | iSubStyle ); 356 | 357 | iPeakHold = iFirstPerc>iSecondPerc?iFirstPerc:iSecondPerc; 358 | delay(100); 359 | } 360 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Fastest library to control TM1638 chip (for example: "LED AND KEY") based modules 2 | ## updated 30-jan-2019 3 | 4 | ## Category 5 | - Arduino C++ 6 | - Library, class xtm1638 7 | - TM1638 8 | - LED and KEY 9 | 10 | 11 | ## About 12 | The TM1638 can be found in combination of 8 digit, 7-segment LED displays which also incorporate 8 buttons as well as bi-colored red and green LEDs. This library allows you to control the module with ease, using MCU's optimal direct port access on AVR's or in "Arduino library compatible mode" on any other 'Arduino'. Usage demo sketches included. 13 | 14 | ![Example Led and Key Module](https://raw.githubusercontent.com/codebeat-nl/xtm1638/master/ledandkey-256.png) 15 | 16 | *Image above: The cheapest and most common LED and KEY module* 17 | 18 | 19 | ## Features 20 | - Significantly faster than existing implementations (see also benchmark results below) 21 | - Low latency 22 | - Configurable ports and pins 23 | - Optimized arithmic operations (ASM for AVR), shiftdivide for C++ 24 | - SuperB speed on ATMEL/MicroChip AVR by using direct port access and assembler 25 | - Optional 8 byte segment cache 26 | - FastGPIO compatibility (see also xtm1638.config.h) 27 | - Auto detection when compiling for optimal configuration 28 | - Cool gauge feature, gauge functionality to be able to display bars, (battery) status, VU-meter, etc 29 | - Runs on any Arduino with auto compatible mode 30 | - Easy to use class methods 31 | - Light weight, small memory footprint (can vary by used compile settings, Arduino IDE and library) 32 | - Compiler configuration messages when compiling 33 | - Configuration file included (no need to change class files) 34 | - 5 Examples, usage sketches/demo's included 35 | 36 | 37 | ## Application usage cases 38 | - Counters which requires low latency, for example gear control 39 | - Clocks 40 | - Battery equipment - indicator 41 | - Amplifiers, general display and/or VU-meter indicator 42 | - Power supplies 43 | - Measurement tools 44 | - Or anything else ;-) 45 | 46 | 47 | ## Benefits 48 | - Ideal to performing other time critical tasks on the same MCU 49 | - Use it on a low memory capacity MCU such as ATTiny85 50 | 51 | 52 | ## How to install 53 | - Download zip 54 | - Unpack it in the libraries folder 55 | - Rename the folder to xtm1638 56 | - Restart IDE 57 | - Open example by using the example and search for xtml1638 58 | - Open xtm1638Example01 59 | - Try to compile it (no errors = installed) 60 | 61 | 62 | ## Included examples 63 | - xtm1638Example01.ino - Hello world example 64 | - xtm1638Example02.ino - Clock example with hours, minutes, seconds and user interface 65 | - xtm1638Example03.ino - Characterset, gauge functionality (bars and indicators), orientation change 66 | - xtm1638Example04.ino - Dedicated to benchmarks (see benchmarks below) 67 | - xtm1638Example05.ino - Gauge, battery indicator, animation, orientation change 68 | . 69 | ## Gauge examples 70 | You can use the class it's gauge() function to display bars as indicators. Gauges can represent a single value or two values (balanced). There are 5 different types, each gauge type has 3 modes, normal, center or inbound. Values can be 0..100% (for each 'channel'). 71 | 72 | ![Gauge examples](https://raw.githubusercontent.com/codebeat-nl/xtm1638/master/xtm1638_gauge_modes-16.png) 73 | 74 | 75 | 76 | ## Benchmarks 77 | 78 | ``` 79 | ### PERFORMANCE STATS 80 | Running sketch: tm1386example04.ino (included stress test example) 81 | Device/MCU : Arduino Nano/ATMega328 82 | [Link](http://www.atmel.com/images/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet_Complete.pdf) 83 | ---------------------------------------------------------------------- 84 | Mode: Score: Minutes: no-leds score: no-leds minutes: 85 | Register+Multiply 94177ms 1.57 minutes 72077ms 1.20 minutes 86 | Register+Divide 86952ms 1.44 minutes 64852ms 1.08 minutes 87 | Register+ShiftDivide 76404ms 1.2734 minutes 58314ms 0.972 minutes(!) 88 | Register+ShiftDivide-PROGMEM 76374ms 1.2729 minutes 58283ms 0.971 minutes(!) 89 | Compatible Mode+Multiply 321876ms 5.36 minutes 232628ms 3.88 minutes 90 | Compatible Mode+Divide 314652ms 5.25 minutes 226404ms 3.77 minutes 91 | Compatible Mode+ShiftDivide 304093ms 5.07 minutes 219866ms 3.66 minutes 92 | 93 | ---- 94 | 95 | 26 jan 2019 update: New records with caching (buffering)! 96 | Register+ShiftDivide+caching 39435ms 0.65725 minutes 22512ms 0.375 minutes(!) - speed gain: 1.9x/2.5x 97 | Compatible Mode+ShiftDivide+ 142392ms 2.3732 minutes 59284ms 0.988 minutes(!) - speed gain: 2.1x/3.7x 98 | caching 99 | 100 | 29 jan 2019 update (1): New records by using assembler (ASM)! 101 | ASM+ShiftDivide 37125ms 0.618 minutes(!) 30239ms 0.504 minutes(!) - speed gain: 2.1x/1.9x 102 | ASM+ShiftDivide+caching 23889ms 0.398 minutes(!) 17004ms 0.283 minutes(!) - speed gain: 3.1x/3.4x 103 | 104 | 29 jan 2019 update (2): New records by using assembler (ASM) + font-table in dynamic memory! 105 | ASM+ShiftDivide 37095ms 0.617 minutes(!) 30209ms 0.503 minutes(!) - speed gain: 2.1x/1.9x 106 | ASM+ShiftDivide+caching 23860ms 0.397 minutes(!) 16974ms 0.282 minutes(!) - speed gain: 3.2x/3.5x 107 | 108 | 109 | 30 jan 2019: 110 | Another gain can be reached by using new define (which is default now), XTM_AVR_SHIFTWISE_DIVIDE_ASM. 111 | At this moment I don't have the time to update the tables above again. Fastest speed is 12756ms = 112 | 0.213 minutes = 12.756 seconds to count up to 100000 and display it. To achieve this, use fastest 113 | 114 | ---- 115 | 116 | FASTEST CONFIG (highest mem usage) AVERAGE CONFIG (fast) SLOWEST CONFIG (very slow compared to all others) 117 | # XTM_AVR_ASM_MODE # XTM_APPLY_CACHED_SEGMENTS # XTM_ARDUINO_COMPATIBLE 118 | # XTM_APPLY_CACHED_SEGMENTS # XTM_SHIFTWISE_DIVIDE # XTM_ARITHMETIC_MULTIPLY 119 | # XTM_NOPROGMEM 120 | # XTM_SHIFTWISE_DIVIDE 121 | # XTM_AVR_SHIFTWISE_DIVIDE_ASM 122 | 123 | ``` 124 | 125 | 126 | ## Compile size 127 | 128 | Example tm1386example04.ino: 129 | ``` 130 | NOTICE: Results can vary by used MCU-type, compile settings, Arduino IDE and library 131 | 132 | Compiled size on an Arduino Nano (ATMega328), this sketch with use of: 133 | 134 | Ricardo Batista TM1638 library : size 4992b 462b mem 135 | ---------------------------------- 136 | This xtm1638 library, 137 | o With normal devide or multiply: 138 | - Portmode (AVR only) : size 2812b 104b mem (AVR direct port manipulation (=much faster)) 139 | - Arduino compatible mode : size 3104b 104b mem (with digitalwrite etc) 140 | - Arduino compatible mode \ 141 | without PROGMEM font table : size 3098b 174b mem 142 | 143 | o With ShiftDivide mode: 144 | - Portmode (AVR only) : size 3018b 116b mem (AVR direct port manipulation (=much faster)) 145 | - Arduino compatible mode : size 3310b 116b mem (with digitalwrite etc) 146 | - Arduino compatible mode \ 147 | without PROGMEM font table : size 3304b 186b mem 148 | 149 | 30 jan 2019: 150 | Since there a few major changes, this table needs an update however I don't have the time right now to 151 | update the tables above again. At least it compiles about 1K smaller than Batista's library. 152 | ``` 153 | 154 | 155 | ## Usage 156 | 157 | Example for AVR's 158 | ``` 159 | #include 160 | static xtm1638 ledandkey( PB0, PB1, PB2 ); 161 | ledandkey.setChars("Hello"); 162 | 163 | ``` 164 | 165 | Example for the other Arduino's (compatible mode) 166 | ``` 167 | #include 168 | static xtm1638 ledandkey( 8, 9, 10 ); 169 | ledandkey.setChars("Hello"); 170 | ``` 171 | 172 | 173 | 174 | ## Extended usage example 175 | ``` 176 | // On top of your sketch 177 | #include 178 | 179 | #ifdef XTM_ARDUINO_COMPATIBLE 180 | // Specify (digital) PINS as described on the board 181 | #define PIN_LEDKEY_DATAIO 8 182 | #define PIN_LEDKEY_CLOCK 9 183 | #define PIN_LEDKEY_STROBE 10 184 | #else 185 | // Specify register port PINS specified in the documentation of the board 186 | #define PIN_LEDKEY_DATAIO PB0 187 | #define PIN_LEDKEY_CLOCK PB1 188 | #define PIN_LEDKEY_STROBE PB2 189 | #endif 190 | 191 | // Create the class object 192 | static xtm1638 ledandkey( PIN_LEDKEY_DATAIO, 193 | PIN_LEDKEY_CLOCK, 194 | PIN_LEDKEY_STROBE 195 | ); 196 | 197 | ....... 198 | void setup() 199 | { 200 | ledandkey.setChars("Hello"); 201 | delay(2000); 202 | } 203 | 204 | void loop() 205 | { 206 | ledandkey.setChars("Input?"); 207 | 208 | // Be gentle 209 | delay(300); 210 | 211 | if( ledandkey.getButtonPressed() == XTM_BUTTON1 ) 212 | { 213 | ledandkey.setChars("Hello"); 214 | delay(2000); 215 | ledandkey.setChars("Again"); 216 | delay(2000); 217 | } 218 | 219 | ...... etc 220 | } 221 | 222 | ``` 223 | 224 | 225 | ## Compiler messages 226 | 227 | When compiling the code, the compiler informs you about current applied configuration. For example, when using port register on AVR's, you will see something like this: 228 | 229 | ``` 230 | libraries/xtm1638/xtm1638.h:85:74: note: #pragma message: Compiling 1638 H file: Port manipulation mode - PORTB 231 | 232 | ``` 233 | However, when compiling in compatible mode, you will see something like this: 234 | 235 | ``` 236 | libraries/xtm1638\xtm1638.h:123:75: note: #pragma message: Compiling 1638 H file: Arduino library compatible mode. 237 | 238 | ``` 239 | 240 | 241 | ## Config file 242 | 243 | You are able to change the behaviour of the class by using the xtm1386.config.h file. Change this file only when there are issues on: 244 | - Port settings 245 | - Auto detection features (failing) 246 | - Compatibility 247 | - Speed or global performance 248 | - Program versus dynamic memory usage 249 | 250 | 251 | ## Port configuration 252 | 253 | When using the class with direct port access on ATMEL AVR/MCU, you need to specify register port names of the same register port. At default the register port is **PORTB** *(XTM_PORTB)*. If you want to use another port register, you must change the port option available in the included xtm1386.config.h file. 254 | 255 | ``` 256 | /* DEFINE A PORT TO USE (NOT REQUIRED WHEN COMPATIBLE MODE): 257 | ----------------------------------------------------------- 258 | NOTE: All three pins used must be bits on the same PORT 259 | register (ex. PORTB). 260 | ------------------------------------------------------------------------ 261 | PORTB (XTM_PORTB) PORTD (XTM_PORTD) PORTC (XTM_PORTC) 262 | PB0 D8 PD0 D0/RX PC0 A0 263 | PB1 D9 PD1 D1/TX PC1 A1 264 | PB2 D10 PD2 D2 PC2 A2 265 | PB3 D11/MOSI PD3 D3 PC3 A3 266 | PB4 D12/MISO PD4 D4 PC4 A4 267 | PB5 D13/SCK PD5 D5 PC5 A5 268 | PD6 D6 269 | PD7 D7 270 | 271 | Just uncomment only one port option: 272 | */ 273 | #define XTM_PORT XTM_PORTB 274 | //#define XTM_PORT XTM_PORTC 275 | //#define XTM_PORT XTM_PORTD 276 | 277 | ``` 278 | **NOTICE:** *When the class detects is not possible to use a configurated register port, it will switch to compatible mode!* 279 | 280 | 281 | ## If you don't want to deal with port registers at all 282 | 283 | You can completely switch off the use of port registers in the included xtm1386.config.h file, however, you will not able to enjoy optimal performance. 284 | ``` 285 | /* XTM_ARDUINO_COMPATIBLE: 286 | ------------------------- 287 | By enabling this (uncomment it), you force the xtm1638 class to use Arduino 288 | compatible mode always (disable auto detection), which means it performs the 289 | same on any Andruino library supported board/processor. Enable this when there 290 | are (detection) problems or for testing proposal/debugging. 291 | Enabling this, also for ATMEL AVR's, can address a significantly instantly 292 | drop in performance for any code compatible ATMEL device. If you want instant 293 | performance for any ATMEL device, you better don't enable this. 294 | */ 295 | #define XTM_ARDUINO_COMPATIBLE 296 | ``` 297 | 298 | ## Notes and version history 299 | 300 | If you have questions or found some serious issues, please post your findings at the issues tab on this project. 301 | 302 | ### The why of this library 303 | This software is based upon the AVR-Only TM1638 "library" of IronCreek Software. Although the original library is suitable to use, it lacks some flexibility, compatibility and features. 304 | 305 | I have used the 'standard' [TM1638 library](https://github.com/rjbatista/tm1638-library) of Ricardo Batista, but isn't able to use direct port access. It is written for functionality but not for performance. Especially on low end devices or allot connected peripherals, this can be a serious issue. 306 | 307 | This xtm1638 library can be classified as the best of both worlds, the performance of the improved IronCreek library with the portability of the Batista library. 308 | 309 | ``` 310 | VERSION history: 311 | - Date : 20-may-2017 (v2.00) 312 | updated : 31-dec-2018 (v2.01) 313 | updated : 30-jan-2019 (v2.02) 314 | 315 | Original by IronCreek Software, available here: 316 | Source: https://github.com/int2str/TM1638 317 | Topic : https://forum.arduino.cc/index.php?topic=190472.0 318 | 319 | v2.02 320 | - Added caching method for segments, SUPERB performance! Compatible mode 321 | has been also improved because of this. See also XTM_APPLY_CACHED_SEGMENTS in 322 | changed xtm1638.config.h file; 323 | - Added more optimizations in assembler for AVR, see also 324 | XTM_AVR_SHIFTWISE_DIVIDE_ASM in changed xtm1638.config.h file; 325 | - Added FastGPIO support for AVR, requires third-party library by Pololu 326 | Corporation, see also XTM_AVR_ASM_MODE in changed xtm1638.config.h file; 327 | - Add new constructor without parameters that use defaults specified in 328 | xtm1638.config.h file; 329 | - Fix compatibility issue with ARM MCU's; 330 | - Removed ARDUINO <= 100 IDE support, sorry, time to upgrade; 331 | - Fix position and layout problems with XTM_GAUGE_STYLE_PIPE style parameter 332 | at gauge function. Most of code rewritten; 333 | - Added a new gauge style XTM_GAUGE_STYLE_CENTER_LINE; 334 | - Updated stats however not all latest performance updates; 335 | - Added extra example, no 5. 336 | 337 | v2.01 338 | - Added setSignedNumber() function; 339 | - Fix non display of null/zero (0) value in number functions; 340 | - Changed 2 characters, i and +. 341 | 342 | 343 | V2.00 (v1.01 IMPROVEMENTS by codebeat) 344 | --------------------------------- 345 | o Change layout of class and some names, macros and many other things; 346 | o Optional constructor parameters, no need to change library; 347 | o Adding many auto detection device/MCU defines; 348 | o Compatible Arduino mode, however, register/port manipulation is faster but 349 | only possible on AVR's (Atmel) boards/devices/MCU's. Still, because of this 350 | compatible mode, it is possible to use the code on non AVR models such as 351 | NodeMCU, ESP8266, etc; 352 | o Adding display settings functionality; 353 | o Adding orientation functionality (normal use or upside down use); 354 | o Replaced font method; 355 | o Adding more font characters; 356 | o Adding better divide/math methods (in namespace), overall performance improvement; 357 | o It is small and lightweight but a little heavier in size compared to previous 358 | version (approx +670 bytes) because of changes, improvements. Still much, 359 | MUCH, smaller comparing to rjbatista tm1638-library and MUCH more less 360 | MCU intensive; 361 | o Adding gauge functionality to be able to display bars, (battery) status etc; 362 | o Adding library config file; 363 | o Adding several (useful) examples with extended docu info; 364 | 365 | v1.01 366 | - Added divmod10_asm() (<---????? NOT there!) 367 | - Un-rolled send loop 368 | - Switch to toggeling output ports 369 | 370 | v1.00 371 | - Initial release 372 | ``` 373 | 374 | 375 | 376 | -------------------------------------------------------------------------------- /xtm1638.h: -------------------------------------------------------------------------------- 1 | #pragma no-cache 2 | /* 3 | XTM1638 Library v2.02 (a TM1638 Led & Key library based upon AVR 4 | TM1638 "Library" v1.02 of IronCreek Software. 5 | 6 | Very fast library to control TM1638 chip (for example: "LED AND KEY") based 7 | modules, using (optional) direct port access on ATMEL (now MicroChip) MCU's. 8 | 9 | CopyLight (c) 2017-2019 codebeat, Erwin Haantjes, http://codebeat.nl 10 | Featuring some essential parts: Copyright (c) 2013 IronCreek Software 11 | 12 | Redistribution and use in source and binary forms, with or without 13 | modification, are permitted provided that the following conditions are met: 14 | 15 | 1. Redistributions of source code must retain the above copyright notice, this 16 | list of conditions and the following disclaimer. 17 | 2. Redistributions in binary form must reproduce the above copyright notice, 18 | this list of conditions and the following disclaimer in the documentation 19 | and/or other materials provided with the distribution. 20 | 21 | Just a WARNING: 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 23 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 26 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | 33 | VERSION history: 34 | - Date : 20-may-2017 (v2.00) 35 | updated : 31-dec-2018 (v2.01) 36 | updated : 30-jan-2019 (v2.02) 37 | 38 | Original by IronCreek Software, available here: 39 | Source: https://github.com/int2str/TM1638 40 | Topic : https://forum.arduino.cc/index.php?topic=190472.0 41 | 42 | v2.02 43 | - Added caching method for segments, SUPERB performance! Compatible mode 44 | has been also improved because of this. See also XTM_APPLY_CACHED_SEGMENTS in 45 | changed xtm1638.config.h file; 46 | - Added more optimizations in assembler for AVR, see also 47 | XTM_AVR_SHIFTWISE_DIVIDE_ASM in changed xtm1638.config.h file; 48 | - Added FastGPIO support for AVR, requires third-party library by Pololu 49 | Corporation, see also XTM_AVR_ASM_MODE in changed xtm1638.config.h file; 50 | - Add new constructor without parameters that use defaults specified in 51 | xtm1638.config.h file; 52 | - Fix compatibility issue with ARM MCU's; 53 | - Removed ARDUINO <= 100 IDE support, sorry, time to upgrade; 54 | - Fix position and layout problems with XTM_GAUGE_STYLE_PIPE style parameter 55 | at gauge function. Most of code rewritten; 56 | - Added a new gauge style XTM_GAUGE_STYLE_CENTER_LINE; 57 | - Updated stats however not all latest performance updates; 58 | - Added extra example, no 5. 59 | 60 | v2.01 61 | - Added setSignedNumber() function; 62 | - Fix non display of null/zero (0) value in number functions; 63 | - Changed 2 characters, i and +. 64 | 65 | 66 | V2.00 (v1.01 IMPROVEMENTS by codebeat) 67 | --------------------------------- 68 | o Change layout of class and some names, macros and many other things; 69 | o Optional constructor parameters, no need to change library; 70 | o Adding many auto detection device/MCU defines; 71 | o Compatible Arduino mode, however, register/port manipulation is faster but 72 | only possible on AVR's (Atmel) boards/devices/MCU's. Still, because of this 73 | compatible mode, it is possible to use the code on non AVR models such as 74 | NodeMCU, ESP8266, etc; 75 | o Adding display settings functionality; 76 | o Adding orientation functionality (normal use or upside down use); 77 | o Replaced font method; 78 | o Adding more font characters; 79 | o Adding better divide/math methods (in namespace), overall performance improvement; 80 | o It is small and lightweight but a little heavier in size compared to previous 81 | version (approx +670 bytes) because of changes, improvements. Still much, 82 | MUCH, smaller comparing to rjbatista tm1638-library and MUCH more less 83 | MCU intensive; 84 | o Adding gauge functionality to be able to display bars, (battery) status etc; 85 | o Adding library config file; 86 | o Adding several (useful) examples with extended docu info; 87 | 88 | v1.01 89 | - Added divmod10_asm() (<---????? NOT there!) 90 | - Un-rolled send loop 91 | - Switch to toggeling output ports 92 | 93 | v1.00 94 | - Initial release 95 | 96 | SUPPORT ME ON PATREON! 97 | ---------------------- 98 | Support the effort/time (many hours) spend to create this software solution, 99 | if you like it, enjoyed it, appreciate it, discovering benefits by using it. 100 | Support on Patreon if you are able to: 101 | https://www.patreon.com/codebeat 102 | 103 | If you do, you help and/or stimulate: 104 | - To spend more time to continue to create free open-source software like this; 105 | - Providing improvements and bug fixes; 106 | - Time to deliver support or to write documentation; 107 | - Develop new projects; 108 | - Pay bills, hosting costs, rent, energy, costs of life; 109 | - Time to create (educational) video's (on my channel) and such. 110 | 111 | Other ways to support me: 112 | ------------------------- 113 | By sharing name, by sharing links to projects, by adopting or improving, 114 | by contacting, by using thumbs up buttons, by subscribing, 115 | by visiting/reading blog, etc. 116 | 117 | codebeat channels: 118 | - http://www.codebeat.nl ; Main website 119 | - http://blog.codebeat.nl ; Blog website (projects and more) 120 | - http://youtube.codebeat.nl ; YouTube channel (shortcut) 121 | - http://patreon.codebeat.nl ; Patreon support channel (shortcut) 122 | - http://github.codebeat.nl ; Github (projects) 123 | 124 | Thank you for supporting if you do! 125 | 126 | Happy coding, greetz, 127 | Erwin Haantjes (codebeat) 128 | */ 129 | 130 | #ifndef XTM1638AE_H 131 | #define XTM1638AE_H 132 | 133 | #define XTM_OBJECT_VER 2.02 134 | #define XTM_PORTB 0x00 135 | #define XTM_PORTC 0x01 136 | #define XTM_PORTD 0x02 137 | 138 | // Change this config file instead of changing this file if you want to change 139 | // something: 140 | #include "xtm1638.config.h" 141 | 142 | #ifdef __AVR__ 143 | #include 144 | #include 145 | #else 146 | #include 147 | 148 | //#ifndef _delay_us 149 | // #define _delay_us(x) 150 | //#endif 151 | #endif 152 | 153 | 154 | #ifndef XTM_ARDUINO_COMPATIBLE 155 | #if !defined(__AVR__) || (!defined( PORTB ) && !defined( PORTC ) && !defined( PORTD )) 156 | #define XTM_ARDUINO_COMPATIBLE 157 | #else 158 | #ifndef XTM_PORT 159 | #error "1638 H file: XTM_PORT define is missing!" 160 | #endif 161 | 162 | #if XTM_PORT == XTM_PORTB && !defined(PORTB) 163 | #define XTM_ARDUINO_COMPATIBLE 164 | #pragma message("WARNING: PORTB not available on this device, switched to compatible mode!") 165 | 166 | #elif XTM_PORT == XTM_PORTC && !defined(PORTC) 167 | #define XTM_ARDUINO_COMPATIBLE 168 | #pragma message("WARNING: PORTC not available on this device, switched to compatible mode!") 169 | 170 | #elif XTM_PORT == XTM_PORTD && !defined(PORTD) 171 | #define XTM_ARDUINO_COMPATIBLE 172 | #pragma message("WARNING: PORTD not available on this device, switched to compatible mode!") 173 | #endif 174 | 175 | #endif 176 | #endif 177 | 178 | #ifdef XTM_ARDUINO_COMPATIBLE 179 | #include "xtm1638.config.h" 180 | 181 | #ifndef __AVR__ 182 | #ifdef XTM_AVR_ASM_MODE 183 | #undef XTM_AVR_ASM_MODE 184 | #endif 185 | #ifdef XTM_AVR_SHIFTWISE_DIVIDE_ASM 186 | #undef XTM_AVR_SHIFTWISE_DIVIDE_ASM 187 | #endif 188 | #endif 189 | #endif 190 | 191 | 192 | #if defined(XTM_NOPROGMEM) || !defined(PROGMEM) || !defined(pgm_read_byte) 193 | #pragma message("Compiling 1638 H file: PROGMEM off, font table in memory") 194 | #ifndef XTM_NOPROGMEM 195 | #define XTM_NOPROGMEM 196 | #endif 197 | #define XTM_MEM_ALLOC_TYPE 198 | #define XTM_GET_DIGIT(x) XTM_DIGITS[x] 199 | #else 200 | #define XTM_MEM_ALLOC_TYPE PROGMEM 201 | #define XTM_GET_DIGIT(x) pgm_read_byte( &XTM_DIGITS[x] ) 202 | #endif 203 | 204 | 205 | #ifndef XTM_ARDUINO_COMPATIBLE 206 | 207 | #ifdef XTM_AVR_ASM_MODE 208 | 209 | // Assembler mode 210 | 211 | // FastGPIO: If you don't have this lib, download it from here: 212 | // https://github.com/pololu/fastgpio-arduino 213 | #include 214 | 215 | #pragma message("Compiling 1638 H file: Assembler mode.") 216 | 217 | #define XTM_CLK_LOW() FastGPIO::Pin::setOutput(0); 218 | #define XTM_CLK_HIGH() FastGPIO::Pin::setOutput(1); 219 | #define XTM_STB_LOW() FastGPIO::Pin::setOutput(0); 220 | #define XTM_STB_HIGH() FastGPIO::Pin::setOutput(1); 221 | #define XTM_DAT_LOW() FastGPIO::Pin::setOutput(0); 222 | #define XTM_DAT_HIGH() FastGPIO::Pin::setOutput(1); 223 | 224 | #define XTM_SETUP() FastGPIO::Pin::setOutput(0); \ 225 | FastGPIO::Pin::setOutput(0); \ 226 | FastGPIO::Pin::setOutput(0); 227 | #define XTM_INIT() XTM_STB_HIGH(); XTM_CLK_HIGH(); 228 | #define XTM_START_RECEIVE() FastGPIO::Pin::setInput(); XTM_DAT_HIGH(); 229 | #define XTM_STOP_RECEIVE() FastGPIO::Pin::setOutput(0); XTM_DAT_LOW(); 230 | #define XTM_COMPARE_RECEIVED() (uint8_t)FastGPIO::Pin::isInputHigh() 231 | 232 | #else 233 | 234 | // Port manipulation mode 235 | #if XTM_PORT == XTM_PORTB 236 | #pragma message("Compiling 1638 H file: Port manipulation mode - PORTB") 237 | #define XTM_OUT_REG PORTB 238 | #define XTM_IN_REG PINB 239 | #define XTM_DDR_REG DDRB 240 | #elif XTM_PORT == XTM_PORTC 241 | #pragma message("Compiling 1638 H file: Port manipulation mode - PORTC") 242 | #define XTM_OUT_REG PORTC 243 | #define XTM_IN_REG PINC 244 | #define XTM_DDR_REG DDRC 245 | #elif XTM_PORT == XTM_PORTD 246 | #pragma message("Compiling 1638 H file: Port manipulation mode - PORTD") 247 | #define XTM_OUT_REG PORTD 248 | #define XTM_IN_REG PIND 249 | #define XTM_DDR_REG DDRD 250 | #else 251 | #error "1638 H file: Something is wrong with configuration, cannot determine XTM_PORT settings!" 252 | #endif 253 | 254 | #define XTM_BIT_DAT _BV(_pinDataIO) 255 | #define XTM_BIT_CLK _BV(_pinClock) 256 | #define XTM_BIT_STB _BV(_pinStrobe) 257 | 258 | #define XTM_CLK_LOW() (XTM_OUT_REG &= ~XTM_BIT_CLK) 259 | #define XTM_CLK_HIGH() (XTM_OUT_REG |= XTM_BIT_CLK) 260 | #define XTM_STB_LOW() (XTM_OUT_REG &= ~XTM_BIT_STB) 261 | #define XTM_STB_HIGH() (XTM_OUT_REG |= XTM_BIT_STB) 262 | #define XTM_DAT_LOW() (XTM_OUT_REG &= ~XTM_BIT_DAT) 263 | #define XTM_DAT_HIGH() (XTM_OUT_REG |= XTM_BIT_DAT) 264 | 265 | 266 | #define XTM_SETUP() XTM_DDR_REG |= XTM_BIT_STB | XTM_BIT_CLK | \ 267 | XTM_BIT_DAT; XTM_OUT_REG |= XTM_BIT_STB | \ 268 | XTM_BIT_CLK 269 | #define XTM_INIT() 270 | #define XTM_START_RECEIVE() XTM_DDR_REG &= ~XTM_BIT_DAT; XTM_DAT_HIGH(); 271 | #define XTM_STOP_RECEIVE() XTM_DDR_REG |= XTM_BIT_DAT; XTM_DAT_LOW(); 272 | #define XTM_COMPARE_RECEIVED() XTM_IN_REG & XTM_BIT_DAT 273 | #endif // XTM_AVR_ASM_MODE 274 | 275 | #else 276 | // Compatible mode 277 | #pragma message("Compiling 1638 H file: Arduino library compatible mode.") 278 | 279 | #include 280 | 281 | #define XTM_CLK_LOW() digitalWrite( _pinClock , LOW ); 282 | #define XTM_CLK_HIGH() digitalWrite( _pinClock , HIGH ); 283 | #define XTM_STB_LOW() digitalWrite( _pinStrobe, LOW ); 284 | #define XTM_STB_HIGH() digitalWrite( _pinStrobe, HIGH ); 285 | #define XTM_DAT_LOW() digitalWrite( _pinDataIO, LOW ); 286 | #define XTM_DAT_HIGH() digitalWrite( _pinDataIO, HIGH ); 287 | 288 | #define XTM_SETUP() pinMode( _pinClock, OUTPUT ); \ 289 | pinMode( _pinStrobe, OUTPUT ); \ 290 | pinMode( _pinDataIO, OUTPUT ); 291 | #define XTM_INIT() XTM_STB_HIGH(); XTM_CLK_HIGH(); 292 | #define XTM_START_RECEIVE() pinMode( _pinDataIO, INPUT ); XTM_DAT_HIGH(); 293 | #define XTM_STOP_RECEIVE() pinMode( _pinDataIO, OUTPUT ); XTM_DAT_LOW(); 294 | #define XTM_COMPARE_RECEIVED() digitalRead( _pinDataIO ) 295 | 296 | #endif 297 | 298 | 299 | // Register mappings 300 | #define XTM_REG_MAX 0x0F 301 | #define XTM_REG_LED_OFFSET 1 302 | #define XTM_LED_TO_REG(p) (XTM_REG_LED_OFFSET + (p << 1)) 303 | 304 | // Instructions 305 | #define XTM_DATA_CMD 0x40 306 | #define XTM_DISP_CTRL 0x80 307 | #define XTM_ADDR_CMD 0xC0 308 | 309 | // Data command set 310 | #define XTM_WRITE_DISP 0x00 311 | #define XTM_READ_KEYS 0x02 312 | #define XTM_FIXED_ADDR 0x04 313 | 314 | // Display control command 315 | #define XTM_DISP_PWM_MASK 0x07 // First 3 bits are brightness (PWM controlled) 316 | #define XTM_DISP_ENABLE 0x08 317 | 318 | 319 | // ---------------------------------------------------------------------------- 320 | // Functions and parameters 321 | // ---------------------------------------------------------------------------- 322 | 323 | // Parameters for setLED() 324 | #define XTM_OFF 0x0 325 | #define XTM_GREEN 0x1 326 | #define XTM_RED 0x2 327 | 328 | // Parameters for setNumber() 329 | #define XTM_RIGHT 0x01 330 | #define XTM_LEFT 0x00 331 | 332 | // Parameters for setNumberPad() 333 | #define XTM_PAD_SPACE 0x00 334 | #define XTM_PAD_0 0x3F 335 | 336 | // Parameters for setOrientation() 337 | #define XTM_ORIENT_NORMAL true 338 | #define XTM_ORIENT_UPSIDEDOWN false 339 | 340 | // Return values getButtonPressed() 341 | #define XTM_NOBUTTON 0x40 342 | #define XTM_BUTTON1 0x00 343 | #define XTM_BUTTON2 0x01 344 | #define XTM_BUTTON3 0x02 345 | #define XTM_BUTTON4 0x03 346 | #define XTM_BUTTON5 0x04 347 | #define XTM_BUTTON6 0x05 348 | #define XTM_BUTTON7 0x06 349 | #define XTM_BUTTON8 0x07 350 | 351 | 352 | // Parameters for gauge(), see also included xtm1638Example03 and xtm1638Example05 353 | // how to use the gauge() function. 354 | #define XTM_GAUGE_SINGLE 0x7F // Single is default, use both segments 355 | // up and lower segments 356 | #define XTM_GAUGE_STYLE_PIPE 0x6A // Pipe is default, shows something 357 | // like this: |||||||||||||||| 358 | #define XTM_GAUGE_STYLE_STRIPE 0x6B // Shows something like this: ======== 359 | // peak in middle as: - 360 | #define XTM_GAUGE_STYLE_BULLET_TOP 0x6C // Shows something like this: oooooooo 361 | // (on segment top), peak on bottom as _ 362 | #define XTM_GAUGE_STYLE_BULLET_BOTTOM 0x6D // Shows something like this: oooooooo 363 | // (on segment top), peak on top as - 364 | #define XTM_GAUGE_STYLE_CENTER_LINE 0x6E // Shows something like this: |------- 365 | #define XTM_GAUGE_STYLE_LED 0x6F // Use the LEDs on top of the device, 366 | // Green is gauge, red is peak 367 | 368 | #define XTM_GAUGE_SUBSTYLE_NORMAL 0x8A // Default, show normal 369 | 370 | #define XTM_GAUGE_SUBSTYLE_CENTER 0x8B // Center = 0% 100% ----|---- 100% 371 | // 0% 372 | 373 | #define XTM_GAUGE_SUBSTYLE_INBOUND 0x8C // Side = 0% 0% ----|---- 0% 374 | // 100% 375 | 376 | 377 | #define XTM_DOT 0x80 378 | 379 | #define XTM_CHAR_ERR 0x49 380 | 381 | // Special characters 382 | #define XTM_MINUS 0x40 383 | #define XTM_PLUS 0x46 384 | #define XTM_BLANK 0x00 385 | #define XTM_DEGREES 0x63 386 | #define XTM_UNDERSCORE 0x08 387 | #define XTM_EQUALS 0x48 388 | #define XTM_LEFT 0x70 389 | #define XTM_RIGHT 0x46 390 | #define XTM_BRACKET_LEFT 0x39 391 | #define XTM_BRACKET_RIGHT 0x0F 392 | #define XTM_GREATER_LEFT 0x46 393 | #define XTM_GREATER_RIGHT 0x70 394 | #define XTM_ACCOLADE_LEFT XTM_GREATER_LEFT 395 | #define XTM_ACCOLADE_RIGHT XTM_GREATER_RIGHT 396 | #define XTM_COLON 0x09 397 | #define XTM_PARENTHESIS_LEFT XTM_BRACKET_LEFT 398 | #define XTM_PARENTHESIS_RIGHT XTM_BRACKET_RIGHT 399 | #define XTM_QUESTION_MARK 0x53+0x80 400 | 401 | #define XTM_SPECIAL_CHAR_OFFSET 0x24 402 | 403 | 404 | // Bits: Hex: 405 | // -- 0 -- -- 01 -- 406 | // | | | | 407 | // 5 1 20 02 408 | // | | | | 409 | // -- 6 -- -- 40 -- 410 | // | | | | 411 | // 4 2 10 04 412 | // | | | | 413 | // -- 3 -- .7 -- 08 -- .80 414 | // 415 | // How to 'calculate' a new character? 416 | // ----------------------------------- 417 | // Just add the segments you want, for example: 418 | // 01h + 02h + 40h + 04h + 08h = 4Fh => 0x4F == an E in opposite direction. 419 | 420 | 421 | 422 | XTM_MEM_ALLOC_TYPE const uint8_t XTM_DIGITS[] = 423 | { 424 | // Numbers 425 | // 0 1 2 3 4 5 6 7 8 9 426 | 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, //(10) 427 | 428 | // Hex 429 | // A b C D E F 430 | 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71, //(06) 431 | 432 | // Letters ( * = same as K ) 433 | // G h I J K L M n O P Q r 434 | 0x3D, 0x74, 0x06, 0x1F, 0x76, 0x38, 0x15, 0x54, 0x3F, 0x73, 0x67, 0x50, //(12) 435 | 436 | // S t U V W *X Y Z 437 | 0x6D, 0x78, 0x1C, 0x3E, 0x2A, 0x76, 0x6E, 0x5B, //(08) 438 | 439 | // Special chars, offset = 0x24 (36) 440 | (uint8_t)'-', XTM_MINUS, 441 | (uint8_t)'+', XTM_PLUS, 442 | (uint8_t)' ', XTM_BLANK, 443 | (uint8_t)'^', XTM_DEGREES, 444 | (uint8_t)'_', XTM_UNDERSCORE, 445 | (uint8_t)'=', XTM_EQUALS, 446 | (uint8_t)'[', XTM_BRACKET_LEFT, 447 | (uint8_t)']', XTM_BRACKET_RIGHT, 448 | (uint8_t)'<', XTM_GREATER_LEFT, 449 | (uint8_t)'>', XTM_GREATER_RIGHT, 450 | (uint8_t)'{', XTM_ACCOLADE_LEFT, 451 | (uint8_t)'}', XTM_ACCOLADE_RIGHT, 452 | (uint8_t)':', XTM_COLON, 453 | (uint8_t)'(', XTM_PARENTHESIS_LEFT, 454 | (uint8_t)')', XTM_PARENTHESIS_RIGHT, 455 | (uint8_t)'?', XTM_QUESTION_MARK, 456 | 0x00 457 | }; 458 | 459 | 460 | #ifdef XTM_ARITHMETIC_MULTIPLY 461 | #define XTM_16_MULDIV_NUMBER_BY_10 number *= 0.1 462 | #define XTM_32_MULDIV_NUMBER_BY_10 number *= 0.1 463 | #define XTM_32_MODDIV_NUMBER_BY_10 number % 10 464 | #else 465 | #ifdef XTM_SHIFTWISE_DIVIDE 466 | #define XTM_16_MULDIV_NUMBER_BY_10 _bit16Div10(&number) 467 | #define XTM_32_MULDIV_NUMBER_BY_10 _bit32Div10(&number) 468 | #define XTM_32_MODDIV_NUMBER_BY_10 _bit32Mod10(&number) 469 | #else 470 | #define XTM_16_MULDIV_NUMBER_BY_10 number /= 10 471 | #define XTM_32_MULDIV_NUMBER_BY_10 number /= 10 472 | #define XTM_32_MODDIV_NUMBER_BY_10 number % 10 473 | #endif 474 | #endif 475 | 476 | #ifdef XTM_APPLY_CACHED_SEGMENTS 477 | #pragma message("Compiling 1638 H file: Cached segments enabled.") 478 | #endif 479 | 480 | // Class to control a 8x 7-segment, 8x dual/single-color LED, 8x button module 481 | // powered by the TM1638 chipset. 482 | class xtm1638 483 | { 484 | public: 485 | // Constructor: Construct a TM1638 object and initialize all the pins used. 486 | // About PIN / PORT configuration: 487 | // o All three pins used must be bits on the same PORT register (ex. PORTB) 488 | // See also: https://www.arduino.cc/en/Reference/PortManipulation and also 489 | // search for the pinouts of your Arduino. 490 | // If your bits are not in the same register or you using a non-AVR 491 | // device, code switch automaticly to compatible mode. using digital pins 492 | // compatible with the Arduino programming library or you have 493 | // to change the header file to keep speed and save a couple of 494 | // hundred bytes. 495 | xtm1638( uint8_t iDataIoPin, uint8_t iClockPin, uint8_t iStrobePin ); 496 | 497 | // Constructor: Construct object without parameters, uses the defaults specified 498 | // in xtm1638.config.h file for specific mode. To view the defaults, take a 499 | // look at this file. 500 | xtm1638(); 501 | 502 | // Resets and initialize the device 503 | void reset(); 504 | 505 | // Clears the 7-segment displays (only) 506 | void clear(); 507 | 508 | // Set the display (digits and leds) enabled or disabled or change the 509 | // brightness of the display and leds. 510 | void setDisplay( bool bEnabled, uint8_t iBrightness = 7 ); 511 | 512 | // Set the orientation of your device, normally keys are below the display 513 | // however you can change this to use the device with keys on top of the display 514 | // (180 degrees turn). All text, leds and knobs will be interpreted upside down 515 | // so it fits the orientation and expectations of the user and programmer. 516 | void setOrientation( bool bUpsideDown ); 517 | 518 | // Rotates a byte, in fact a character 180 degrees. 519 | uint8_t rotateByte( uint8_t value ); 520 | 521 | // Set a single 7-segment display to the given byte value. 522 | // This allows direct control of the elements to do spinning animations etc. 523 | void setByte(uint8_t pos, uint8_t value); 524 | 525 | // For example: 526 | // Set a 7-segments display to the given byte values at offset. 527 | // char values[] = { 1, 2, 4, 8, 16, 32, 64, 128 }; 528 | // setBytes(values); 529 | void setBytes(const char* value, uint8_t offset = 0); 530 | 531 | 532 | // Display a single digit at the given position. 533 | // Position is left-to-right, starting at 0. 534 | void setDigit(uint8_t pos, uint8_t value); 535 | 536 | // Display an unsigned number at a given offset and alignment. 537 | void setNumber(uint32_t number, uint8_t offset = 7, uint8_t align = XTM_RIGHT); 538 | 539 | // Display a signed number (negative and positive) at a given offset and alignment. 540 | void setSignedNumber(int32_t number, bool bShowPlusSign = false, uint8_t offset = 7, uint8_t align = XTM_RIGHT ); 541 | 542 | // Display an unsigned number at a given offset and pad it with 0's or 543 | // spaces to a desired width. This function is helpful when the numbers can 544 | // fluctuate in length (ex. 100, 5, 1000) to avoid flickering and shifting segments. 545 | void setNumberPad(uint32_t number, uint8_t offset, uint8_t width, uint8_t pad = XTM_PAD_SPACE); 546 | 547 | // Display an unsigned number at a given offset and adds zero('s) before number 548 | // when the length (width) is less than required specified nDigits. 549 | // NOTICE: Offset must be higher or egual to nDigits. 550 | void setLzNumber(uint32_t number, uint8_t offset, uint8_t nDigits = 2); 551 | 552 | // Display an unsigned number in hex format at a given offset and pad it 553 | // with 0's or spaces to a desired width. 554 | void setNumberHex(uint32_t number, uint8_t offset, uint8_t width, uint8_t pad = XTM_PAD_SPACE); 555 | 556 | // Draw a character at a given position. 557 | // Not all characters are supported, check declaration for an overview. 558 | // If you want to draw custom characters (or animations), you better use 559 | // setByte() function. 560 | void setChar(uint8_t pos, const char value ); 561 | 562 | // Display a string starting at a given offset. 563 | void setChars(const char* value, uint8_t offset = 0, bool bClrScr = true ); 564 | 565 | // Display a string starting at a given alignment 566 | void setAlignedChars(const char* value, uint8_t align = XTM_RIGHT, bool bClrScr = true ); 567 | 568 | // Set which "dots" should be enabled. 569 | // Mask is mapped right to left (ex. 0x01 = right-most dot) 570 | void setDots(uint8_t mask); 571 | 572 | // Turn off a single LED 573 | void clearLED(uint8_t pos); 574 | 575 | // Turn off all LEDs 576 | void clearLEDs(); 577 | 578 | // Set an LED at the given position to the specified color. 579 | // LEDs are numbered 0-7, left to right 580 | void setLED(uint8_t pos, uint8_t color = XTM_GREEN ); 581 | 582 | // Bitmask setting up the color of all LEDs at once. 583 | // Examples: 584 | // setLEDs(0xFF, 0x00); <-- All green 585 | // setLEDs(0x00, 0xFF); <-- All red 586 | // setLEDs(0xFF, 0xFF); <-- All green+red 587 | // setLEDs(0xF0, 0x0F); <-- Right half green, left half red 588 | void setLEDs(uint8_t green, uint8_t red); 589 | 590 | // Returns a bitmask containing all button states. 591 | uint8_t getButtons(); 592 | 593 | // Test if a button is pressed, returns true when iPos button is 594 | // pressed. 595 | bool isButtonPressed( uint8_t iPos, uint8_t iButtons = 0 ); 596 | 597 | // Function to avoid/requirement include of strings.h, result is same as strlen() 598 | uint8_t getStrLen( const char* value ); 599 | 600 | // Returns a value <> XTM_NOBUTTON when a button is pressed. 601 | uint8_t getButtonPressed(); 602 | 603 | // Wait for buttonpress, if no iTimeOutSec is specified the timeout 604 | // is infinite. if iTimeOutSec > 0 and seconds specified is reached, 605 | // function returns false, otherwise it returns true. 606 | bool waitForButtonPressed( uint8_t iButton, uint8_t iTimeOutSec = 0 ); 607 | 608 | // Wait for NO buttonpress, if no iTimeOutSec is specified the timeout 609 | // is infinite. if iTimeOutSec > 0 and seconds specified is reached, 610 | // function returns false, otherwise it returns true. 611 | bool waitForNoButtonPressed( uint8_t iTimeOutSec = 0 ); 612 | 613 | // Cool gauge functionality with peak indicator. Useful for VU-meter, 614 | // battery indicator, settings, measurement, etc. 615 | // Values are in percent, see also gauge options on top of this file. 616 | // Or refer to the demo's sketches provided with this library. 617 | // Examples: 618 | // gauge( 0, 50 ); <-- 50% gauge 619 | // gauge( 0, 50, 20 ); <-- First = 50%, second = 20% 620 | // gauge( 80, 50, 20 ); <-- Peak = 80%, First = 50%, second = 20% 621 | void gauge( uint8_t iPeakPerc, uint8_t iFirstPerc, 622 | uint8_t iSecondPerc = XTM_GAUGE_SINGLE, 623 | uint8_t iStyle = XTM_GAUGE_STYLE_PIPE, 624 | uint8_t iSubStyle = XTM_GAUGE_SUBSTYLE_NORMAL 625 | ); 626 | 627 | 628 | 629 | protected: 630 | uint8_t _dotMask = 0; 631 | bool _orient = XTM_ORIENT_NORMAL; 632 | 633 | #ifdef XTM_APPLY_CACHED_SEGMENTS 634 | uint8_t _segbuff[8]; // consumes 8 bytes of dynamic memory 635 | #endif 636 | 637 | #if defined(XTM_ARDUINO_COMPATIBLE) || !defined(XTM_AVR_ASM_MODE) 638 | // Variables set with use of constructor 639 | uint8_t _pinStrobe; // for example: PB0 640 | uint8_t _pinClock; // for example: PB1 641 | uint8_t _pinDataIO; // for example: PB2 642 | #endif 643 | 644 | // Internally used, applies parameters 645 | void setup( uint8_t iDataIoPin, uint8_t iClockPin, uint8_t iStrobePin ); 646 | 647 | void send(uint8_t b); 648 | void sendCommand(uint8_t cmd); 649 | void sendData(uint8_t addr, uint8_t data); 650 | 651 | uint8_t receive(); 652 | uint8_t getOffsetDigits(uint32_t number); 653 | }; 654 | 655 | #endif 656 | -------------------------------------------------------------------------------- /xtm1638.cpp: -------------------------------------------------------------------------------- 1 | #pragma no-cache 2 | /* 3 | XTM1638 Library v2.02 (a TM1638 Led & Key library based upon AVR 4 | TM1638 "Library" v1.02 of IronCreek Software. 5 | 6 | Very fast library to control TM1638 chip (for example: "LED AND KEY") based 7 | modules, using (optional) direct port access on ATMEL (now MicroChip) MCU's. 8 | 9 | CopyLight (c) 2017-2019 codebeat, Erwin Haantjes, http://codebeat.nl 10 | Featuring some essential parts: Copyright (c) 2013 IronCreek Software 11 | 12 | Redistribution and use in source and binary forms, with or without 13 | modification, are permitted provided that the following conditions are met: 14 | 15 | 1. Redistributions of source code must retain the above copyright notice, this 16 | list of conditions and the following disclaimer. 17 | 2. Redistributions in binary form must reproduce the above copyright notice, 18 | this list of conditions and the following disclaimer in the documentation 19 | and/or other materials provided with the distribution. 20 | 21 | Just a WARNING: 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 23 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 26 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | 33 | VERSION history: 34 | - Date : 20-may-2017 (v2.00) 35 | updated : 31-dec-2018 (v2.01) 36 | updated : 30-jan-2019 (v2.02) 37 | 38 | Original by IronCreek Software, available here: 39 | Source: https://github.com/int2str/TM1638 40 | Topic : https://forum.arduino.cc/index.php?topic=190472.0 41 | 42 | v2.02 43 | - Added caching method for segments, SUPERB performance! Compatible mode 44 | has been also improved because of this. See also XTM_APPLY_CACHED_SEGMENTS in 45 | changed xtm1638.config.h file; 46 | - Added more optimizations in assembler for AVR, see also 47 | XTM_AVR_SHIFTWISE_DIVIDE_ASM in changed xtm1638.config.h file; 48 | - Added FastGPIO support for AVR, requires third-party library by Pololu 49 | Corporation, see also XTM_AVR_ASM_MODE in changed xtm1638.config.h file; 50 | - Add new constructor without parameters that use defaults specified in 51 | xtm1638.config.h file; 52 | - Fix compatibility issue with ARM MCU's; 53 | - Removed ARDUINO <= 100 IDE support, sorry, time to upgrade; 54 | - Fix position and layout problems with XTM_GAUGE_STYLE_PIPE style parameter 55 | at gauge function. Most of code rewritten; 56 | - Added a new gauge style XTM_GAUGE_STYLE_CENTER_LINE; 57 | - Updated stats however not all latest performance updates; 58 | - Added extra example, no 5. 59 | 60 | v2.01 61 | - Added setSignedNumber() function; 62 | - Fix non display of null/zero (0) value in number functions; 63 | - Changed 2 characters, i and +. 64 | 65 | 66 | V2.00 (v1.01 IMPROVEMENTS by codebeat) 67 | --------------------------------- 68 | o Change layout of class and some names, macros and many other things; 69 | o Optional constructor parameters, no need to change library; 70 | o Adding many auto detection device/MCU defines; 71 | o Compatible Arduino mode, however, register/port manipulation is faster but 72 | only possible on AVR's (Atmel) boards/devices/MCU's. Still, because of this 73 | compatible mode, it is possible to use the code on non AVR models such as 74 | NodeMCU, ESP8266, etc; 75 | o Adding display settings functionality; 76 | o Adding orientation functionality (normal use or upside down use); 77 | o Replaced font method; 78 | o Adding more font characters; 79 | o Adding better divide/math methods (in namespace), overall performance improvement; 80 | o It is small and lightweight but a little heavier in size compared to previous 81 | version (approx +670 bytes) because of changes, improvements. Still much, 82 | MUCH, smaller comparing to rjbatista tm1638-library and MUCH more less 83 | MCU intensive; 84 | o Adding gauge functionality to be able to display bars, (battery) status etc; 85 | o Adding library config file; 86 | o Adding several (useful) examples with extended docu info; 87 | 88 | v1.01 89 | - Added divmod10_asm() (<---????? NOT there!) 90 | - Un-rolled send loop 91 | - Switch to toggeling output ports 92 | 93 | v1.00 94 | - Initial release 95 | 96 | SUPPORT ME ON PATREON! 97 | ---------------------- 98 | Support the effort/time (many hours) spend to create this software solution, 99 | if you like it, enjoyed it, appreciate it, discovering benefits by using it. 100 | Support on Patreon if you are able to: 101 | https://www.patreon.com/codebeat 102 | 103 | If you do, you help and/or stimulate: 104 | - To spend more time to continue to create free open-source software like this; 105 | - Providing improvements and bug fixes; 106 | - Time to deliver support or to write documentation; 107 | - Develop new projects; 108 | - Pay bills, hosting costs, rent, energy, costs of life; 109 | - Time to create (educational) video's (on my channel) and such. 110 | 111 | Other ways to support me: 112 | ------------------------- 113 | By sharing name, by sharing links to projects, by adopting or improving, 114 | by contacting, by using thumbs up buttons, by subscribing, 115 | by visiting/reading blog, etc. 116 | 117 | codebeat channels: 118 | - http://www.codebeat.nl ; Main website 119 | - http://blog.codebeat.nl ; Blog website (projects and more) 120 | - http://youtube.codebeat.nl ; YouTube channel (shortcut) 121 | - http://patreon.codebeat.nl ; Patreon support channel (shortcut) 122 | - http://github.codebeat.nl ; Github (projects) 123 | 124 | Thank you for supporting if you do! 125 | 126 | Happy coding, greetz, 127 | Erwin Haantjes (codebeat) 128 | */ 129 | 130 | #include "xtm1638.h" 131 | 132 | 133 | #ifdef XTM_SHIFTWISE_DIVIDE 134 | namespace 135 | { 136 | 137 | uint32_t ___q; 138 | 139 | #if defined(__AVR__) && defined(XTM_AVR_SHIFTWISE_DIVIDE_ASM) 140 | uint8_t ___x; 141 | 142 | //void divmod10(uint32_t in, uint32_t &div, uint8_t &mod) __attribute__((noinline)); 143 | void _avrdivmod10(uint32_t in, uint32_t &div, uint8_t &mod) //__attribute__((noinline)) 144 | { 145 | //assumes that div/mod pointers arrive in r18:r19 and r20:r21 pairs (doesn't matter which way around) 146 | //and that in arrives in r22:r25 quad 147 | asm volatile( 148 | "movw r30, %2 \n\t" //uint32_t* divPtr = ÷ 149 | "movw r26, %1 \n\t" //uint32_t* modPtr = &mod; 150 | 151 | "mov r0, %A0 \n\t" //byte temp = in 152 | "movw r18, %A0 \n\t" //uint32_t q = in; 153 | "movw r20, %C0 \n\t" 154 | "ori r18, 0x01 \n\t" //q |= 1; 155 | 156 | "lsr r25 \n\t" //x = in >> 2 //note: x reuses registers of 'in', as 'in' was backed up in r0 157 | "ror r24 \n\t" 158 | "ror r23 \n\t" 159 | "ror r22 \n\t" 160 | "lsr r25 \n\t" 161 | "ror r24 \n\t" 162 | "ror r23 \n\t" 163 | "ror r22 \n\t" 164 | 165 | "sub r18, r22 \n\t" //q = q - x; 166 | "sbc r19, r23 \n\t" 167 | "sbc r20, r24 \n\t" 168 | "sbc r21, r25 \n\t" 169 | 170 | "movw r22, r18 \n\t" //x = q; 171 | "movw r24, r20 \n\t" 172 | "lsr r25 \n\t" //x = x >> 4; 173 | "ror r24 \n\t" 174 | "ror r23 \n\t" 175 | "ror r22 \n\t" 176 | "lsr r25 \n\t" 177 | "ror r24 \n\t" 178 | "ror r23 \n\t" 179 | "ror r22 \n\t" 180 | "lsr r25 \n\t" 181 | "ror r24 \n\t" 182 | "ror r23 \n\t" 183 | "ror r22 \n\t" 184 | "lsr r25 \n\t" 185 | "ror r24 \n\t" 186 | "ror r23 \n\t" 187 | "ror r22 \n\t" 188 | 189 | "add r22, r18 \n\t" //x = x + q 190 | "adc r23, r19 \n\t" 191 | "adc r24, r20 \n\t" 192 | "adc r25, r21 \n\t" 193 | 194 | "movw r18, r22 \n\t" //q = x 195 | "movw r20, r24 \n\t" 196 | "add r18, r23 \n\t" //q = q + (x >> 8) 197 | "adc r19, r24 \n\t" 198 | "adc r20, r25 \n\t" 199 | "adc r21, r1 \n\t" 200 | 201 | "movw r18, r20 \n\t" //q = q >> 16 202 | "eor r20, r20 \n\t" 203 | "eor r21, r21 \n\t" 204 | "add r18, r23 \n\t" //q = q + (x>>8) 205 | "adc r19, r24 \n\t" 206 | "adc r20, r25 \n\t" 207 | "adc r21, r1 \n\t" //NOTE: r1 is a known 0. 208 | "add r18, r22 \n\t" //q = q + x 209 | "adc r19, r23 \n\t" 210 | "adc r20, r24 \n\t" 211 | "adc r21, r25 \n\t" 212 | 213 | "mov r18, r19 \n\t" //q = q >> 8 214 | "mov r19, r20 \n\t" 215 | "mov r20, r21 \n\t" 216 | "eor r21, r21 \n\t" 217 | "add r18, r22 \n\t" //q = q + x 218 | "adc r19, r23 \n\t" 219 | "adc r20, r24 \n\t" 220 | "adc r21, r25 \n\t" 221 | 222 | "andi r18, 0xF8 \n\t" //q = q & ~0x7 223 | 224 | "sub r0, r18 \n\t" //in = in - q 225 | 226 | "lsr r21 \n\t" //q = q >> 2 227 | "ror r20 \n\t" 228 | "ror r19 \n\t" 229 | "ror r18 \n\t" 230 | "lsr r21 \n\t" 231 | "ror r20 \n\t" 232 | "ror r19 \n\t" 233 | "ror r18 \n\t" 234 | 235 | "sub r0, r18 \n\t" //in = in - q 236 | "st X, r0 \n\t" //mod = in; 237 | 238 | "lsr r21 \n\t" //q = q >> 1 239 | "ror r20 \n\t" 240 | "ror r19 \n\t" 241 | "ror r18 \n\t" 242 | 243 | "st Z, r18 \n\t" //div = q 244 | "std Z+1, r19 \n\t" 245 | "std Z+2, r20 \n\t" 246 | "std Z+3, r21 \n\t" 247 | 248 | : 249 | : "r" (in), "r" (&mod), "r" (&div) 250 | : "r0", "r26", "r27", "r31", "r31" 251 | ); 252 | } 253 | 254 | 255 | #else 256 | uint32_t ___t; 257 | uint32_t ___x; 258 | #endif 259 | 260 | // This cannot be optimized in C++, so don't try it, you are wasting your time, sure ;-) 261 | 262 | void _bit32Div10(uint32_t* div) 263 | { 264 | #if defined(__AVR__) && defined(XTM_AVR_SHIFTWISE_DIVIDE_ASM) 265 | _avrdivmod10( *div, *div, ___x ); 266 | #else 267 | ___x= (*div|1) - (*div>>2); // div = in/10 <~~> div = (0.75*in) >> 3 268 | ___q= (___x>>4) + ___x; 269 | ___x= ___q; 270 | ___q= (___q>>8) + ___x; 271 | ___q= (___q>>8) + ___x; 272 | ___q= (___q>>8) + ___x; 273 | ___q= (___q>>8) + ___x; 274 | 275 | *div = (___q >> 3); 276 | #endif 277 | } 278 | 279 | uint8_t _bit32Mod10(uint32_t* mod) 280 | { 281 | #if defined(__AVR__) && defined(XTM_AVR_SHIFTWISE_DIVIDE_ASM) 282 | _avrdivmod10(*mod, ___q, ___x ); 283 | return ___x; 284 | #else 285 | _bit32Div10(&(___t= *mod)); 286 | return (uint8_t)(*mod - (((___t << 2) + ___t) << 1)); 287 | #endif 288 | } 289 | 290 | } 291 | #endif 292 | 293 | xtm1638::xtm1638( uint8_t iDataIoPin, uint8_t iClockPin, uint8_t iStrobePin ) 294 | { 295 | #ifdef XTM_AVR_ASM_MODE 296 | #pragma message("WARNING: Calling constructor with parameters make no sense when compiled in ASM mode!") 297 | reset(); 298 | #else 299 | setup( iDataIoPin, iClockPin, iStrobePin ); 300 | #endif 301 | } 302 | 303 | xtm1638::xtm1638() 304 | { 305 | #ifdef XTM_ARDUINO_COMPATIBLE 306 | 307 | // #pragma message("NOTICE: Compiled with defaults pins specified in xtm1638.config.h") 308 | setup( XTM_ARD_AUTO_PIN_DATAIO, 309 | XTM_ARD_AUTO_PIN_CLOCK, 310 | XTM_ARD_AUTO_PIN_STROBE 311 | ); 312 | #else 313 | #ifndef XTM_AVR_ASM_MODE 314 | // #pragma message("NOTICE: Compiled with default register pins specified in xtm1638.config.h") 315 | setup( XTM_REG_DEF_PIN_DATAIO, 316 | XTM_REG_DEF_PIN_CLOCK, 317 | XTM_REG_DEF_PIN_STROBE 318 | ); 319 | 320 | #else 321 | #ifdef XTM_AVR_ASM_MODE 322 | #pragma message("NOTICE: Compiled with default ASM pins specified in xtm1638.config.h") 323 | #endif 324 | reset(); 325 | #endif 326 | 327 | #endif 328 | } 329 | 330 | 331 | void xtm1638::setup( uint8_t iDataIoPin, uint8_t iClockPin, uint8_t iStrobePin ) 332 | { 333 | #ifndef XTM_AVR_ASM_MODE 334 | _pinDataIO = iDataIoPin; 335 | _pinClock = iClockPin; 336 | _pinStrobe = iStrobePin; 337 | #endif 338 | 339 | reset(); 340 | } 341 | 342 | 343 | void xtm1638::reset() 344 | { 345 | XTM_SETUP(); 346 | 347 | sendCommand(XTM_DATA_CMD | XTM_WRITE_DISP); 348 | sendCommand(XTM_DISP_CTRL | XTM_DISP_ENABLE | XTM_DISP_PWM_MASK); 349 | 350 | XTM_INIT(); 351 | 352 | clear(); 353 | clearLEDs(); 354 | } 355 | 356 | 357 | void xtm1638::send(uint8_t b) 358 | { 359 | for (uint8_t i = 8; i; --i, b >>= 1) 360 | { 361 | XTM_CLK_LOW(); 362 | if (b & 1) 363 | { XTM_DAT_HIGH(); } 364 | else { XTM_DAT_LOW(); } 365 | XTM_CLK_HIGH(); 366 | } 367 | } 368 | 369 | void xtm1638::sendCommand(uint8_t cmd) 370 | { 371 | XTM_STB_LOW(); 372 | send(cmd); 373 | XTM_STB_HIGH(); 374 | } 375 | 376 | void xtm1638::sendData(uint8_t addr, uint8_t data) 377 | { 378 | sendCommand(XTM_DATA_CMD | XTM_FIXED_ADDR); 379 | XTM_STB_LOW(); 380 | send(XTM_ADDR_CMD | addr); 381 | send(data); 382 | XTM_STB_HIGH(); 383 | } 384 | 385 | uint8_t xtm1638::receive() 386 | { 387 | uint8_t rc = 0; 388 | 389 | // Change DAT pin to INPUT and enable pull-up 390 | XTM_START_RECEIVE(); 391 | 392 | for (uint8_t i = 8, b = 1; i; --i, b <<= 1) 393 | { 394 | XTM_CLK_LOW(); 395 | 396 | // Not required when in compatible mode 397 | #ifndef XTM_ARDUINO_COMPATIBLE 398 | // Must wait tWAIT for CLK transition 399 | _delay_us(1); 400 | #endif 401 | 402 | if( XTM_COMPARE_RECEIVED() ) 403 | { rc |= b; } 404 | 405 | XTM_CLK_HIGH(); 406 | } 407 | 408 | // Disable pull-up and reset pin 409 | XTM_STOP_RECEIVE(); 410 | 411 | return rc; 412 | } 413 | 414 | void xtm1638::clear() 415 | { 416 | int8_t i = XTM_REG_MAX+1; 417 | while( i >= 0 ) 418 | { 419 | if( i > 0 ) 420 | { sendData(i, 0x00); } 421 | #ifdef XTM_APPLY_CACHED_SEGMENTS 422 | if( i < 8 ) 423 | { _segbuff[i] = _segbuff[i+1] = 0x00; } 424 | #endif 425 | i-=2; 426 | } 427 | } 428 | 429 | void xtm1638::setDisplay( bool bEnabled, uint8_t iBrightness ) 430 | { 431 | XTM_STB_HIGH(); 432 | XTM_CLK_HIGH(); 433 | 434 | sendCommand( XTM_DATA_CMD ); 435 | sendCommand( XTM_DISP_CTRL | ( bEnabled ? 8 : 0) | ((iBrightness >= 0 && iBrightness < 8)?iBrightness:7) ); 436 | 437 | XTM_STB_LOW(); 438 | } 439 | 440 | void xtm1638::setOrientation( bool bUpsideDown ) 441 | { 442 | _orient = bUpsideDown?XTM_ORIENT_UPSIDEDOWN:XTM_ORIENT_NORMAL; 443 | } 444 | 445 | 446 | uint8_t xtm1638::rotateByte( uint8_t value ) 447 | { 448 | return ( value & 0xC0 | (value & 0x07) << 3 | (value & 0x38) >> 3 ); 449 | } 450 | 451 | void xtm1638::setByte(uint8_t pos, uint8_t value) 452 | { 453 | if( _orient == XTM_ORIENT_UPSIDEDOWN ) 454 | { 455 | pos = 7-pos; 456 | if( value > 0 ) 457 | { value = rotateByte( value ); } 458 | } 459 | 460 | 461 | if( pos < 8 ) 462 | #ifdef XTM_APPLY_CACHED_SEGMENTS 463 | if( _segbuff[pos] != value ) 464 | { 465 | _segbuff[pos] = value | (_dotMask & (1 << pos) ? XTM_DOT : 0); 466 | sendData(pos << 1, _segbuff[pos] ); 467 | } 468 | #else 469 | sendData(pos << 1, value | (_dotMask & (1 << pos) ? XTM_DOT : 0) ); 470 | #endif 471 | } 472 | 473 | void xtm1638::setBytes(const char* value, uint8_t offset) 474 | { 475 | while (*value && offset < 8 ) 476 | { setByte(offset++, *value++); } 477 | 478 | while( offset < 8 ) 479 | { setByte(offset++, 0x00 ); } 480 | } 481 | 482 | 483 | 484 | uint8_t xtm1638::getOffsetDigits(uint32_t number) 485 | { 486 | uint8_t digits = 0; 487 | while (number >= 10) 488 | { 489 | XTM_32_MULDIV_NUMBER_BY_10; 490 | 491 | ++digits; 492 | } 493 | return digits; 494 | } 495 | 496 | 497 | void xtm1638::setDigit(uint8_t pos, uint8_t value) 498 | { 499 | setByte(pos, XTM_GET_DIGIT(value & (uint8_t)0xF) ); 500 | } 501 | 502 | void xtm1638::setNumber(uint32_t number, uint8_t offset, uint8_t align) 503 | { 504 | if( number == 0 ) 505 | { 506 | setDigit( offset, 0 ); 507 | return; 508 | } 509 | 510 | if (align == XTM_LEFT) 511 | offset += getOffsetDigits(number); 512 | 513 | while (number && offset != (uint8_t)0xFF) 514 | { 515 | 516 | #ifdef XTM_32_MODMULDIV_NUMBER_BY_10 517 | setDigit(offset--, XTM_32_MODMULDIV_NUMBER_BY_10 ); 518 | #else 519 | setDigit(offset--, XTM_32_MODDIV_NUMBER_BY_10 ); 520 | 521 | XTM_32_MULDIV_NUMBER_BY_10; 522 | #endif 523 | } 524 | } 525 | 526 | void xtm1638::setSignedNumber(int32_t number, bool bShowPlusSign, uint8_t offset, uint8_t align) 527 | { 528 | uint8_t iSignChar = XTM_PLUS; 529 | 530 | if( number < 0 ) 531 | { 532 | iSignChar = XTM_MINUS; 533 | bShowPlusSign = true; 534 | number*=-1; 535 | } 536 | 537 | if( bShowPlusSign && number > 0 ) 538 | { 539 | if( align == XTM_LEFT ) 540 | { ++offset; } 541 | } 542 | 543 | setNumber( number, offset, align ); 544 | 545 | if( bShowPlusSign && number > 0 ) 546 | { 547 | if( align == XTM_LEFT ) 548 | { setByte( offset-1, iSignChar ); } 549 | else { setByte( offset-getOffsetDigits(number)-1, iSignChar ); } 550 | } 551 | } 552 | 553 | 554 | void xtm1638::setNumberPad(uint32_t number, uint8_t offset, uint8_t width, uint8_t pad) 555 | { 556 | while (number && width-- && offset != (uint8_t)0xFF) 557 | { 558 | #ifdef XTM_32_MODMULDIV_NUMBER_BY_10 559 | setDigit(offset--, XTM_32_MODMULDIV_NUMBER_BY_10 ); 560 | #else 561 | setDigit(offset--, XTM_32_MODDIV_NUMBER_BY_10 ); 562 | 563 | XTM_32_MULDIV_NUMBER_BY_10; 564 | #endif 565 | } 566 | 567 | while (width-- && offset != (uint8_t)0xFF) 568 | setByte(offset--, pad); 569 | } 570 | 571 | void xtm1638::setLzNumber(uint32_t number, uint8_t offset, uint8_t nDigits) 572 | { 573 | setNumberPad(number, offset, nDigits, XTM_PAD_0 ); 574 | } 575 | 576 | void xtm1638::setNumberHex(uint32_t number, uint8_t offset, uint8_t width, uint8_t pad) 577 | { 578 | while (number && width-- && offset != (uint8_t)0xFF) 579 | { 580 | setDigit(offset--, number & 0x0F); 581 | number >>= 4; 582 | } 583 | 584 | while (width-- && offset != (uint8_t)0xFF) 585 | setByte(offset--, pad); 586 | } 587 | 588 | void xtm1638::setChar(uint8_t pos, const char value) 589 | { 590 | uint8_t i = (uint8_t)0xFF; 591 | char c = 0; 592 | 593 | if (value >= '0' && value <= '9') 594 | { i = value - '0'; } 595 | else { 596 | if (value >= 'a' && value <= 'z') 597 | { i = value - 'a' + 10; } 598 | else { 599 | if(value >= 'A' && value <= 'Z') 600 | { i = value - 'A' + 10; } 601 | } 602 | } 603 | 604 | if( i != (uint8_t)0xFF ) 605 | { setByte(pos, XTM_GET_DIGIT(i) ); } 606 | else { 607 | i = XTM_SPECIAL_CHAR_OFFSET; 608 | 609 | while( ((c = XTM_GET_DIGIT(i))!=value) && c > 0 ) 610 | { i+=2; } 611 | 612 | setByte( pos, c?XTM_GET_DIGIT(i+1):XTM_CHAR_ERR ); 613 | } 614 | } 615 | 616 | void xtm1638::setChars(const char* value, uint8_t offset, bool bClrScr ) 617 | { 618 | while (*value && offset < 8 ) 619 | setChar(offset++, *value++); 620 | 621 | if( bClrScr ) 622 | while( offset < 8 ) 623 | { setByte(offset++, 0x00 ); } 624 | } 625 | 626 | void xtm1638::setAlignedChars(const char* value, uint8_t align, bool bClrScr ) 627 | { 628 | if( align == XTM_LEFT ) 629 | { setChars( value, 0 ); } 630 | else { 631 | if( *value ) 632 | { setChars( value, 8-getStrLen(value), bClrScr ); } 633 | } 634 | } 635 | 636 | void xtm1638::setDots(uint8_t mask) 637 | { 638 | _dotMask = mask; 639 | } 640 | 641 | void xtm1638::clearLED(uint8_t pos) 642 | { 643 | if( _orient == XTM_ORIENT_UPSIDEDOWN ) 644 | { pos=7-pos; } 645 | 646 | sendData(XTM_REG_LED_OFFSET+(pos*2), 0x00); 647 | } 648 | 649 | void xtm1638::clearLEDs() 650 | { 651 | for (uint8_t a = XTM_REG_LED_OFFSET; a <= XTM_REG_MAX; a += 2) 652 | sendData(a, 0x00); 653 | } 654 | 655 | void xtm1638::setLED(uint8_t pos, uint8_t color) 656 | { 657 | if( _orient == XTM_ORIENT_UPSIDEDOWN ) 658 | { pos=7-pos; } 659 | 660 | 661 | sendData(XTM_LED_TO_REG(pos), color); 662 | } 663 | 664 | void xtm1638::setLEDs(uint8_t green, uint8_t red) 665 | { 666 | for (uint8_t a = XTM_REG_LED_OFFSET, b = 1; a <= XTM_REG_MAX; a += 2, green >>= 1, red >>= 1) 667 | sendData(a, green & 1 | ((red & 1) << 1)); 668 | } 669 | 670 | uint8_t xtm1638::getButtons() 671 | { 672 | uint8_t iButtons = 0; 673 | 674 | XTM_STB_LOW(); 675 | send(XTM_DATA_CMD | XTM_READ_KEYS); 676 | 677 | for(uint8_t bytes = 0; bytes != 4; ++bytes) 678 | iButtons |= receive() << bytes; 679 | 680 | XTM_STB_HIGH(); 681 | 682 | if( _orient == XTM_ORIENT_UPSIDEDOWN ) 683 | { 684 | // swap each other 685 | iButtons = (iButtons & 0b01010101) << 1 | (iButtons & 0b10101010) >> 1; 686 | 687 | // swap each pair 688 | iButtons = (iButtons & 0b00110011) << 2 | (iButtons & 0b11001100) >> 2; 689 | 690 | // swap each quad 691 | iButtons = (iButtons & 0b00001111) << 4 | (iButtons & 0b11110000) >> 4; 692 | } 693 | 694 | return iButtons; 695 | } 696 | 697 | bool xtm1638::isButtonPressed( uint8_t iPos, uint8_t iButtons ) 698 | { 699 | if( iButtons == 0 ) 700 | { iButtons = getButtons(); 701 | if( iButtons == 0 ) 702 | { return false; } 703 | } 704 | return (iButtons & (0x1 << iPos))?true:false; 705 | } 706 | 707 | uint8_t xtm1638::getStrLen( const char* value ) 708 | { 709 | uint8_t iResult = 0; 710 | while (*value && iResult++ < 8 ) { *value++; } 711 | return iResult; 712 | } 713 | 714 | 715 | uint8_t xtm1638::getButtonPressed() 716 | { 717 | uint8_t iButtons = getButtons(); 718 | uint8_t i = 0; 719 | 720 | while( i < 8 ) 721 | { 722 | if( isButtonPressed(i++, iButtons ) ) 723 | { return (i-1); } 724 | } 725 | 726 | return XTM_NOBUTTON; 727 | } 728 | 729 | bool xtm1638::waitForButtonPressed( uint8_t iButton, uint8_t iTimeOutSec ) 730 | { 731 | uint8_t iWaitState = 0; 732 | 733 | if( iTimeOutSec > 0 ) 734 | { 735 | if( iTimeOutSec > 50 ) 736 | { iTimeOutSec = 50; } 737 | 738 | iTimeOutSec*=5; 739 | } 740 | 741 | while( getButtonPressed() != iButton ) 742 | { 743 | #ifndef XTM_ARDUINO_COMPATIBLE 744 | _delay_us( 200000UL ); 745 | #else 746 | delay( 200 ); 747 | #endif 748 | 749 | if( iTimeOutSec > 0 ) 750 | { ++iWaitState; 751 | if( iWaitState > (uint8_t)254 || iWaitState > iTimeOutSec ) 752 | { return false; } 753 | } 754 | } 755 | 756 | return true; 757 | } 758 | 759 | bool xtm1638::waitForNoButtonPressed( uint8_t iTimeOutSec ) 760 | { return waitForButtonPressed( XTM_NOBUTTON, iTimeOutSec ); } 761 | 762 | 763 | 764 | void xtm1638::gauge( uint8_t iPeakPerc, uint8_t iFirstPerc, uint8_t iSecondPerc, 765 | uint8_t iStyle, uint8_t iSubStyle ) 766 | { 767 | bool bDivide = ( iSubStyle != XTM_GAUGE_SUBSTYLE_NORMAL ); 768 | bool bDivideInCenter = bDivide?( iSubStyle != XTM_GAUGE_SUBSTYLE_INBOUND ):false; 769 | bool bPipe = (iStyle == XTM_GAUGE_STYLE_PIPE); 770 | uint8_t iSegs = bDivide?(bPipe?8:4):(bPipe?16:8); 771 | uint8_t iDivSegs = bDivide?(bPipe?4:iSegs):iSegs; 772 | bool bTwoSegs = bPipe || (iStyle == XTM_GAUGE_STYLE_STRIPE); 773 | bool bBulletTop = bTwoSegs?false:(iStyle == XTM_GAUGE_STYLE_BULLET_TOP); 774 | bool bCenterLine = bTwoSegs?false:(iStyle == XTM_GAUGE_STYLE_CENTER_LINE); 775 | 776 | if( iSecondPerc == XTM_GAUGE_SINGLE ) 777 | { iSecondPerc = iFirstPerc; } 778 | 779 | if( iFirstPerc > 100 ) 780 | { iFirstPerc = 100; } 781 | if( iSecondPerc > 100 ) 782 | { iSecondPerc = 100; } 783 | if( iPeakPerc > 100 ) 784 | { iPeakPerc = 100; } 785 | 786 | #ifdef XTM_ARITHMETIC_MULTIPLY 787 | iFirstPerc = (iFirstPerc>0)?((uint8_t)round((iSegs * 0.01) * iFirstPerc )):0; 788 | iSecondPerc = (iSecondPerc>0)?((uint8_t)round((iSegs * 0.01) * iSecondPerc )):0; 789 | iPeakPerc = (iPeakPerc>0)?((uint8_t)round((iSegs * 0.01) * iPeakPerc )):0; 790 | #else 791 | iFirstPerc = (iFirstPerc>0)?((uint8_t)round((iSegs / 100.0) * iFirstPerc )):0; 792 | iSecondPerc = (iSecondPerc>0)?((uint8_t)round((iSegs / 100.0) * iSecondPerc )):0; 793 | iPeakPerc = (iPeakPerc>0)?((uint8_t)round((iSegs / 100.0) * iPeakPerc )):0; 794 | #endif 795 | 796 | uint8_t i = 0; 797 | uint8_t iPos = 0; 798 | uint8_t c; 799 | uint8_t iLeft; 800 | uint8_t iRight; 801 | bool bLeft; 802 | bool bRight; 803 | 804 | //clearLEDs(); 805 | 806 | while( ++i <= iSegs ) 807 | { 808 | bLeft = ( iFirstPerc && iFirstPerc >= i ); 809 | bRight = ( iSecondPerc && iSecondPerc >= i ); 810 | 811 | if( bDivide ) 812 | { 813 | if( bPipe ) 814 | { 815 | // 27-jan-2019: Quick fix of position 816 | iLeft = (bDivideInCenter?(3-iPos):iPos); 817 | iRight = (bDivideInCenter?(4+iPos):7-iPos); 818 | } 819 | else { 820 | iLeft = (bDivideInCenter?(iDivSegs-i):(i-1)); 821 | iRight = (bDivideInCenter?(iDivSegs+i-1):((iDivSegs*2)-i)); 822 | } 823 | } 824 | else { iLeft = i-1; } 825 | 826 | if( iStyle == XTM_GAUGE_STYLE_LED ) 827 | { 828 | if( bLeft || bRight || (i == iPeakPerc) ) 829 | { 830 | if( bDivide ) 831 | { 832 | if( bLeft ) 833 | { setLED( iLeft ); } 834 | else { clearLED( iLeft ); } 835 | if( bRight ) 836 | { setLED( iRight ); } 837 | else { clearLED( iRight ); } 838 | } 839 | else { setLED( iLeft ); } 840 | } 841 | else { 842 | clearLED( iLeft ); 843 | if( bDivide ) 844 | { clearLED( iRight ); } 845 | } 846 | 847 | continue; 848 | } 849 | 850 | c = (uint8_t)0x00; 851 | 852 | if( bTwoSegs ) // Stripe or Pipe character? 853 | { 854 | // inbound: bDivide && !bDivideInCenter 855 | // center : bDivide && bDivideInCenter 856 | 857 | 858 | if( !bDivide ) // Two channels in one character? 859 | { 860 | if( bLeft ) 861 | { c+=bPipe?(uint8_t)0x20:(uint8_t)0x01; } // Add | left-top or -- at top 862 | 863 | if( bRight ) 864 | { c+=bPipe?(uint8_t)0x10:(uint8_t)0x08; } // Add | left-bottom or -- at bottom 865 | } 866 | else { 867 | if( (bLeft && bRight) || bLeft || bRight ) 868 | { 869 | c+=bPipe?(uint8_t)0x30:(uint8_t)0x09; // Add | left-top or -- at top 870 | // Add | left-bottom or -- at bottom 871 | } 872 | } 873 | 874 | // Pipe can hold two values (doubled resolution) in one character, get and set next one 875 | if( bPipe && (bLeft || bRight) ) 876 | { 877 | // Set peak value 878 | if( iPeakPerc == i ) 879 | { c+=0x80; } 880 | 881 | // Get next 882 | ++i; 883 | 884 | if( !bDivide ) // Two channels in one character? 885 | { 886 | if( iFirstPerc && iFirstPerc >= i ) 887 | { c+=(uint8_t)0x02; } // Add | right-top 888 | if( iSecondPerc && iSecondPerc >= i) 889 | { c+=(uint8_t)0x04; } // Add | right-bottom 890 | } 891 | else { 892 | if( iFirstPerc && iFirstPerc >= i || iSecondPerc && iSecondPerc >= i ) 893 | { 894 | c+=(uint8_t)0x06; // Add | right-top && add | right-bottom 895 | } 896 | } 897 | } 898 | 899 | // Set peak value 900 | if( iPeakPerc == i && c < 0x40 ) 901 | { c+=bPipe?(uint8_t)0x80:0x40; } 902 | 903 | if( bDivide ) 904 | { 905 | setByte( iLeft , bLeft ?((bPipe && bDivideInCenter)?rotateByte(c):c):0x00 ); 906 | setByte( iRight, bRight?((bPipe && !bDivideInCenter)?rotateByte(c):c):0x00 ); 907 | } 908 | else { setByte( iPos, c ); } 909 | } 910 | else { 911 | uint8_t iChar = bCenterLine?0x40:(bBulletTop?(uint8_t)0x63:(uint8_t)0x5C); 912 | uint8_t iPeakChar = bCenterLine?0x80:(bBulletTop?(uint8_t)0x08:(uint8_t)0x01); 913 | 914 | if( bDivide ) 915 | { 916 | // Center line and first one? 917 | if( bCenterLine && iPos == 0 ) 918 | { 919 | c+=(bDivideInCenter)?(uint8_t)0x06:(uint8_t)0x30; // Add | 920 | } 921 | 922 | if( bLeft ) 923 | { c+=iChar+(( iPeakPerc == i )?iPeakChar:0); } 924 | 925 | setByte( iLeft , c ); 926 | c=0x00; 927 | 928 | // Center line and first one? 929 | if( bCenterLine && iPos == 0 ) 930 | { 931 | c+=(bDivideInCenter)?(uint8_t)0x30:(uint8_t)0x06; // Add | 932 | } 933 | 934 | if( bRight ) 935 | { c+=iChar+(( iPeakPerc == i )?iPeakChar:0); } 936 | 937 | 938 | setByte( iRight , c ); 939 | } 940 | else { 941 | // Center line and first one? 942 | if( bCenterLine && iPos == 0 ) 943 | { 944 | c+=(uint8_t)0x30; // Add | 945 | } 946 | 947 | if( bLeft || bRight ) 948 | { c+=iChar+(( iPeakPerc == i )?iPeakChar:0); } 949 | 950 | setByte( iPos, c ); 951 | } 952 | } 953 | 954 | ++iPos; 955 | } 956 | 957 | } 958 | 959 | 960 | 961 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | --------------------------------------------------------------------------------