From 169dae37d9ef14f6f50eaa1e5d74e067475c4901 Mon Sep 17 00:00:00 2001 From: Gavin lyons Date: Mon, 28 Sep 2020 23:59:30 +0100 Subject: [PATCH] Version 1.7.0 --- README.md | 145 +++++++++-------- .../TM1638plus_ADC_Model2.ino} | 2 +- .../TM1638plus_HELLOWORLD_Model1.ino} | 16 +- .../TM1638plus_HELLOWORLD_Model2.ino} | 13 +- .../TM1638plus_MQTT_Model2.ino} | 0 .../TM1638plus_SCROLL_TEST_Model2.ino | 92 ----------- .../TM1638plus_TEST_Model1.ino | 82 ++++++---- .../TM1638plus_TEST_Model2.ino | 153 ++++++++++++------ .../TM1638plus_TEST_Model3.ino | 84 ++++++---- extra/CHANGELOG.md | 15 +- keywords.txt | 5 +- library.properties | 2 +- src/TM1638plus.cpp | 116 ++++++------- src/TM1638plus.h | 68 +++----- src/TM1638plus_Model2.cpp | 114 +++++++------ src/TM1638plus_Model2.h | 71 ++++---- src/TM1638plus_common.cpp | 52 ++++++ src/TM1638plus_common.h | 152 +++++++++++++++++ src/TM1638plus_font.h | 109 ------------- 19 files changed, 674 insertions(+), 617 deletions(-) rename examples/{TM1638plus_ADC_TEST_Model2/TM1638plus_ADC_TEST_Model2.ino => TM1638plus_ADC_Model2/TM1638plus_ADC_Model2.ino} (99%) rename examples/{TM1638plus_BASIC_TEST_Model1/TM1638plus_BASIC_TEST_Model1.ino => TM1638plus_HELLOWORLD_Model1/TM1638plus_HELLOWORLD_Model1.ino} (53%) rename examples/{TM1638plus_BASIC_TEST_Model2/TM1638plus_BASIC_TEST_Model2.ino => TM1638plus_HELLOWORLD_Model2/TM1638plus_HELLOWORLD_Model2.ino} (57%) rename examples/{TM1638plus_MQTT_TEST_Model2/TM1638plus_MQTT_TEST_Model2.ino => TM1638plus_MQTT_Model2/TM1638plus_MQTT_Model2.ino} (100%) delete mode 100644 examples/TM1638plus_SCROLL_TEST_Model2/TM1638plus_SCROLL_TEST_Model2.ino create mode 100644 src/TM1638plus_common.cpp create mode 100644 src/TM1638plus_common.h delete mode 100644 src/TM1638plus_font.h diff --git a/README.md b/README.md index 8cdc220..f23624b 100755 --- a/README.md +++ b/README.md @@ -1,5 +1,19 @@ ![ module pics ](https://github.com/gavinlyonsrepo/TM1638plus/blob/master/extra/images/tm16383.jpg) +Table of contents +--------------------------- + + * [Overview](#overview) + * [Installation](#installation) + * [Features](#features) + * [Model One](#model-one) + * [Model Two](#model-two) + * [Model Three](#model-three) + * [Notes](#notes) + * [Memory](#memory) + * [Pic Ports](#pic-port) + + Overview -------------------------------------------- * Name: TM1638plus @@ -16,31 +30,17 @@ This library supports 3 different models, pictured above from left to right. * Tested on Development platforms: -1. Arduino UNO & NANO . +1. Arduino UNO & NANO v3 . 2. ATtiny85 . 3. ESP32. 4. STM32 STM32F103C8T6 "the blue pill". 5. ESP8266. -6. Teensy 4.0. - -See Issue B in issue section below for potential problems with high frequency MCU's. +6. Teensy 4.0. (may not work at highest frequency see notes section) * History: see CHANGELOG.md in extra folder -* Contributors: [gabormay](https://github.com/gabormay) [centic9](https://github.com/centic9) +* Contributors: [gabormay](https://github.com/gabormay) [centic9](https://github.com/centic9) [wunderbaum](https://github.com/wunderbaum) -Table of contents ---------------------------- - * [Overview](#overview) - * [Installation](#installation) - * [Features](#features) - * [Model One](#model-one) - * [Model Two](#model-two) - * [Model Three](#model-three) - * [Issues](#issues) - * [Memory](#memory) - * [See Also](#see-also) - Installation ------------------------------ @@ -75,18 +75,13 @@ will be named Model 1 ,Model 2 and Model 3. | 2 | TM1638 KEYS, QYF | 0 | 16 | | 3 | TM1638 V1.3 or LKM1638 | 8 bi color, red and green | 8 | -There are two sets of files to support model 1 & 2 . I kept them separate as the models are wired quite different Model 1 address by digit, while Model 2 address by segment. So the code is quite different for many functions. Also helps with hardware testing and modularity. -The same ASCII font file is used all models. -Model 3 uses same code base as Model 1, just different example file and different use of setLED functions. +There are two sets of files to support model 1 & 2 . I kept them separate as the models are wired quite different, Model 1 address by digit, while Model 2 address by segment. So the code is quite different for most functions. Model 3 uses same code as Model 1, just different example file and different use of LED functions. Common settings, data and functions are in the TM1638plus_common.x files. -| Model | Header | Code file | Example files | +| Model | Header | Code file | TEST files | | ------ | ------ | ------ | ------ | -| 1 | TM1638plus.h | TM1638plus.cpp | TM1638plus_BASIC_TEST.ino TM1638plus_TEST.ino | +| 1 | TM1638plus.h | TM1638plus.cpp | TM1638plus_HELLOWORLD_Model1.ino TM1638plus_TEST_Model1.ino | | 3 | Same as model 1 | Same as model 1 | TM1638plus_TEST_Model3.ino | -| 2 | TM1638plus_Model2.h | TM1638plus_Model2.cpp | TM1638plus_BASIC_TEST_Model2 TM1638plus_TEST_Model2.ino TM1638plus_ADC_TEST_Model2.ino TM1638plus_SCROLL_TEST_Model2.ino | - -It is possible to drive multiple modules and share the DIO and CLK lines see issue number 10 -at github for example code. +| 2 | TM1638plus_Model2.h | TM1638plus_Model2.cpp | TM1638plus_HELLOWORLD_Model2 TM1638plus_TEST_Model2.ino | Model One -------------------------------------- @@ -98,19 +93,15 @@ TM1638 Module 8 Push buttons 8 LEDS (LED & KEY) ![ sch ](https://github.com/gavinlyonsrepo/pic_16F18446_projects/blob/master/images/TM1638_2.jpg) This variant consist of an 8-digit seven segment display with decimal points, -8 Leds and 8 Push buttons. - -Two 4 digit 3461AS-1 (.34 inch, 4 digit ,common Cathode, decimal point, RED) are used in this module -giving a total of 8 digits. A TM1638 controller chip drives the unit. +8 Leds and 8 Push buttons. Two 4 digit 3461AS-1 (.34 inch, 4 digit ,common Cathode, decimal point, RED) are used in this module giving a total of 8 digits. A TM1638 controller chip drives the unit. The unit is marked (LED & KEY). -**Model 1 Library Functions and example files** +**Model 1 Library Functions** The commented functions can be found in library header file TM1638plus.h. The library support ASCII ,text ,Hex and allows for setting individual segments, and the decimal point of segment. The TM1638plus_TEST.ino contains a set of tests demonstrating library functions. - For more information see the commented headers in header file. 1. Print an ASCII character. @@ -142,22 +133,15 @@ TM1638 Module 16 Push buttons (16 KEY) (QYF). ![ sch ](https://github.com/gavinlyonsrepo/TM1638plus/blob/master/extra/images/tm16382.jpg) - They consist of an 8-digit seven segment display with decimal points, -and 16 Push buttons. - -Two 4 digit 3461BS-1 (.34 inch, 4 digit ,common Anode, decimal point, RED)are used in this module -giving a total of 8 digits. A TM1638 controller chip drives the unit. +and 16 Push buttons.Two 4 digit 3461BS-1 (.34 inch, 4 digit ,common Anode, decimal point, RED)are used in this module giving a total of 8 digits. A TM1638 controller chip drives the unit. +NB : If your display shows "56781234" for "12345678" see Notes section. Note A. -NB : If your display shows "56781234" for "12345678" see Issues section. issue A. - -**Model 2 Library Functions and example files** +**Model 2 Library Functions** The commented functions can be found in library header file TM1638plus_Model2.h. The library support Strings,decimal ,Hex ,raw ASCII data, setting individual segments, -and the decimal point. - -For more detailed information on functions see commented headers in header file(.h). +and the decimal point. For more detailed information on functions see commented headers in header file(.h). 1. Print a Hexadecimal number with or without leading zeros 2. Print a decimal number with or without leading zeros @@ -166,59 +150,57 @@ For more detailed information on functions see commented headers in header file( 5. Print a text string, dot function supported. 6. Read buttons status. User may want to debounce buttons depending on application. See TM1638plus_ADC_TEST_Model2.ino for debounce button example. +Two different functions to read buttons. 7. Reset and init module functions. 8. Adjust brightness of module. Support 8 degree brightness adjustment. If user wishes to change the default brightness at start-up change, The "DEFAULT_BRIGHTNESS" define in header file. 9. Print raw ASCII data without reference to font file. -10. Example file general contains a set of general function tests: TM1638plus_TEST_model2.ino -11. Example file ADC shows some practical data examples : TM1638plus_ADC_TEST_Model2.ino -12. Example file for one method of scrolling text: TM1638plus_SCROLL_TEST_Model2.ino Model Three ----------------------------------------- There are different PCB's of these modules on market, -This library was tested on version 1.3 below. I think this software will work for all of them +This library was tested on no 3 below. I think this software will work for all of them and the differences in PCB are related to connectors, layout and component placement. - -1. LKM1638 v1.1 -2. LKM1638 v1.2 -3. TM1638 V1.3 - This module is a variant of Model 1. The differences are the LEDs are bigger and bi-color both red and green, The seven segment display is larger and extra connectors are added for Daisy chaining. - Two 4 digit KYX-5461AS-7.3 (.54 inch, 4 digit ,common cathode, decimal point, RED)are used in this module giving a total of 8 digits. +1. LKM1638 v1.1 +2. LKM1638 v1.2 +3. TM1638 V1.3 + ![ module ](https://github.com/gavinlyonsrepo/TM1638plus/blob/master/extra/images/tm16384.jpg) -**Model 3 Library Functions and example files** +**Model 3 Library Functions** -The code is largely the same as model 1 and there is one unique model 3 example file. +The code is the same as model 1 and there is one unique model 3 example file. setLED and setLEDs functions behaviour is the only difference in code base between 1 and 3. - -SetLED: The difference is when you call the setLED function you pass the following to get LEDs to change colour. +SetLED: The difference is when you call the setLED function you pass the following to get LEDs to change colour. For more detailed information on functions see commented headers in header file(.h). | Model | setLED Value | result | | ---- | ---- | ---- | | 1 & 3 | 0 | LED off | -| 3 | 1 | Led green | -| 3 | 2 | LED red | +| 3 | 1 | Led green on | +| 3 | 2 | LED red on | | 1 | 1 | LED on | -SetLEDs: When you pass call the setLEDs function you can pass a word pattern where upper byte is turns LEDs green and lower byte turns LEDs red. Model one ignores lower byte always set to 0x00. +SetLEDs: When you pass call the setLEDs function you can pass a word pattern where upper byte is turns LEDs green on and lower byte turns LEDs red on . Model one ignores lower byte, Set to 0x00 always. 1. Model 3 setLEDs(word) = 0xGGRR 3. Model 1 setLEDs(word) = 0xRR00 -For more detailed information on functions see commented headers in header file(.h). - -Issues +Notes -------------------------- -*Issue A* : Swapped display Issue: Model 2 only +1. Swapped data on Display issue on some Model 2 modules +2. Anomaly's on High frequency micro-controllers. +3. Driving multiple displays. +4. Detecting multiple buttons pressed together. + +*Note A* : Swapped display Issue: Model 2 only For Some users using this library the nibbles in information display byte where swapped around. This is because there are different versions of modules on market with different wiring. See issue #3 on github called Swapped display :: "12345678" becomes "56781234". @@ -230,17 +212,14 @@ Object, set the fourth parameter "swap_nibbles" to True, The default is false. | QYF-TM1638 | default operation | false | | QYF-TM1638 -Ver 1.0 | Swapped display Fix | true | - -*ISSUE B* : High frequency micro-controllers. +*Note B* : High frequency micro-controllers. This library uses a software SPI-like protocol and may not work fully on micro-controllers running at a very high frequency, without some adjustments to timing. Its a SPI-like interface with a single bidirectional data wire DIO. The TM1638 is basically a slow SPI device (< 500kHz) in DIO mode. The clock uses the equivalent of SPI mode 3 (normally high, clocks data on the rising edge). The problem is that the native Arduino shiftIn()/shiftOut() wire functions are simply too fast for this device (technically the clock signalling for the TM1638 is inverted but as it triggers on a rising edge still it is tolerant of that). To make this work with fast devices, the shift clocking is slowed with a small delay (on the order of a microsecond). As of version 1.6 a new parameter *(_HIGH_FREQ)* has been introduced to constructor it is false by default. Set to true for high frequency MCU ~> 100Mhz. This will fix the issue of HF MCU not reading buttons correctly(ESP-Xs). The High_Freq parameter causes a custom shift-in function to be used. - -The Teensy results have been sent to me, I don't have these MCU's them at time of writing. -also this Teensy data results where pre v1.6 so buttons issue should be fixed. In theory changing the shiftout routine's the same way as shiftin changed in v1.6 should fix any display issues here. +The Teensy results have been sent to me, I don't have these MCU's them at time of writing. | IC | frequency | Status | | ------ | ------ | ------ | @@ -251,8 +230,27 @@ also this Teensy data results where pre v1.6 so buttons issue should be fixed. I | ESP8266 | 160Mhz | Working | | ESP 32 | 240 MHz | Working, with high_freq set to true | | Teensy 4.0| 150Mhz | Working model 1, no Data rest of models | -| Teensy 4.0| 396Mhz | buttons not working , display issues on model1 , no Data rest of models | +| Teensy 4.0| 396Mhz | Not working on model1 , no Data rest of models | + +*Note C* : Driving multiple displays. + +It is possible to drive multiple modules. Share the DIO and CLK lines and use a unique +STB line for each device. see issue number 10 at github for example code. + +*Note D* : Detecting multiple buttons pressed together. + +Model 1 and Model 3 CAN detect multiple buttons pressed. + +Model 3 has two different functions: + +1. ReadKey16 returns a byte with decimal value 1-16 this function cannot +detect multiple buttons pressed. +2. ReadKey16Two returns a 16 bit integer where each bit corresponds to the 16 switch's. +However due to the wiring of the module, see SG-X lines on schematic, +Pressing Certain combinations of buttons will cause the data on Seven Segments to +change. So the simultaneous use of multiple key presses and the seven segments display +is problematic. See issue 12 on github for more details. Memory ------------------------------- @@ -270,8 +268,9 @@ Sketch uses 1536 bytes (5%) of program storage space. Global variables use 23 bytes (1%) of dynamic memory. -See Also +Pic Port ------------------- -MicroChip PIC XC8 forks. -I have forked this library to the PIC for the XC8 compiler the forks can be found at [Link](https://github.com/gavinlyonsrepo/pic_16F18446_projects) +MicroChip PIC XC8 port. +I have ported this library to the PIC for the XC8 compiler: +[ Link ](https://github.com/gavinlyonsrepo/pic_16F18446_projects) diff --git a/examples/TM1638plus_ADC_TEST_Model2/TM1638plus_ADC_TEST_Model2.ino b/examples/TM1638plus_ADC_Model2/TM1638plus_ADC_Model2.ino similarity index 99% rename from examples/TM1638plus_ADC_TEST_Model2/TM1638plus_ADC_TEST_Model2.ino rename to examples/TM1638plus_ADC_Model2/TM1638plus_ADC_Model2.ino index 6a60d2e..3d6ff49 100644 --- a/examples/TM1638plus_ADC_TEST_Model2/TM1638plus_ADC_TEST_Model2.ino +++ b/examples/TM1638plus_ADC_Model2/TM1638plus_ADC_Model2.ino @@ -1,6 +1,6 @@ /* Project Name: TM1638 - File: TM1638plus_ADC_TEST_Model2.ino + File: TM1638plus_ADC_Model2.ino Description: demo file library for model 2 TM1638 module(16 KEY 16 pushbuttons). Carries out tests demonstrating arduino library TM1638plus displaying ADC data. diff --git a/examples/TM1638plus_BASIC_TEST_Model1/TM1638plus_BASIC_TEST_Model1.ino b/examples/TM1638plus_HELLOWORLD_Model1/TM1638plus_HELLOWORLD_Model1.ino similarity index 53% rename from examples/TM1638plus_BASIC_TEST_Model1/TM1638plus_BASIC_TEST_Model1.ino rename to examples/TM1638plus_HELLOWORLD_Model1/TM1638plus_HELLOWORLD_Model1.ino index d2f54a3..0e3145d 100644 --- a/examples/TM1638plus_BASIC_TEST_Model1/TM1638plus_BASIC_TEST_Model1.ino +++ b/examples/TM1638plus_HELLOWORLD_Model1/TM1638plus_HELLOWORLD_Model1.ino @@ -1,23 +1,23 @@ /* Project Name: TM1638 - File: TM1638plus_BASIC_TEST.ino - Description: A demo file library for TM1638 module(LED & KEY). - Carries out series most basic test , "hello world" + File: TM1638plus_HELLOWORLD_TEST_Model1.ino + Description: A demo file library for TM1638 module(LED & KEY). Works on Model 1 and 3 + Carries out series most basic test , "hello world" helo wrld Author: Gavin Lyons. Created: feb 2020 URL: https://github.com/gavinlyonsrepo/TM1638plus */ -#include +#include //include the library // GPIO I/O pins on the Arduino connected to strobe, clock, data, //pick on any I/O you want. -#define STROBE_TM 4 -#define CLOCK_TM 6 -#define DIO_TM 7 +#define STROBE_TM 4 // strobe = GPIO connected to strobe line of module +#define CLOCK_TM 6 // clock = GPIO connected to clock line of module +#define DIO_TM 7 // data = GPIO connected to data line of module bool high_freq = false; //default false, If using a high freq CPU > ~100 MHZ set to true. -//Constructor object (GPIO STB , GPIO CLOCK , GPIO DIO, use high freq MCU) +// Constructor object (GPIO STB , GPIO CLOCK , GPIO DIO, use high freq MCU) TM1638plus tm(STROBE_TM, CLOCK_TM , DIO_TM, high_freq); void setup() diff --git a/examples/TM1638plus_BASIC_TEST_Model2/TM1638plus_BASIC_TEST_Model2.ino b/examples/TM1638plus_HELLOWORLD_Model2/TM1638plus_HELLOWORLD_Model2.ino similarity index 57% rename from examples/TM1638plus_BASIC_TEST_Model2/TM1638plus_BASIC_TEST_Model2.ino rename to examples/TM1638plus_HELLOWORLD_Model2/TM1638plus_HELLOWORLD_Model2.ino index 5820b44..6e9d524 100644 --- a/examples/TM1638plus_BASIC_TEST_Model2/TM1638plus_BASIC_TEST_Model2.ino +++ b/examples/TM1638plus_HELLOWORLD_Model2/TM1638plus_HELLOWORLD_Model2.ino @@ -1,7 +1,7 @@ /* - Project Name: TM1638plus - File: TM1638plus_BASIC_TEST_Model2.ino - Description: ABasic test library for model 2, TM1638 module(16 KEY 16 pushbuttons). + Project Name: TM1638plus (arduino library) + File: TM1638plus_HELLOWORLD_Model2.ino + Description: A Basic test file for model 2, TM1638 module(16 KEY 16 pushbuttons). Author: Gavin Lyons. Created Feb 2020 URL: https://github.com/gavinlyonsrepo/TM1638plus @@ -13,8 +13,8 @@ #define STROBE_TM 4 // strobe = GPIO connected to strobe line of module #define CLOCK_TM 6 // clock = GPIO connected to clock line of module #define DIO_TM 7 // data = GPIO connected to data line of module -bool swap_nibbles = false; //Default is false if left out, see note in readme at URL -bool high_freq = false; //default false,, If using a high freq CPU > ~100 MHZ set to true. +bool swap_nibbles = false; //Default is false if left out, see issues section in readme at URL +bool high_freq = false; //default false, If using a high freq CPU > ~100 MHZ set to true. // Constructor object Init the module TM1638plus_Model2 tm(STROBE_TM, CLOCK_TM , DIO_TM, swap_nibbles, high_freq); @@ -25,7 +25,6 @@ void setup() { } void loop() { - - tm.DisplayStr("helowrld", 0); + tm.DisplayStr("helowrld", 0); // Display "helowrld" in seven segments with zero decimal points set. delay(5000); } diff --git a/examples/TM1638plus_MQTT_TEST_Model2/TM1638plus_MQTT_TEST_Model2.ino b/examples/TM1638plus_MQTT_Model2/TM1638plus_MQTT_Model2.ino similarity index 100% rename from examples/TM1638plus_MQTT_TEST_Model2/TM1638plus_MQTT_TEST_Model2.ino rename to examples/TM1638plus_MQTT_Model2/TM1638plus_MQTT_Model2.ino diff --git a/examples/TM1638plus_SCROLL_TEST_Model2/TM1638plus_SCROLL_TEST_Model2.ino b/examples/TM1638plus_SCROLL_TEST_Model2/TM1638plus_SCROLL_TEST_Model2.ino deleted file mode 100644 index 1742617..0000000 --- a/examples/TM1638plus_SCROLL_TEST_Model2/TM1638plus_SCROLL_TEST_Model2.ino +++ /dev/null @@ -1,92 +0,0 @@ -/* - Project Name: TM1638 - File: TM1638plus_SCROLL_TEST_Model2.ino - Description: demo file library for model 2 TM1638 module(16 KEY 16 pushbuutons). - Carries out test demonstrating arduino library TM1638plus. - - Shows ONE possible method of scrolling text and data on the display using the string class. - - Boards Tested: UNO NANO - Author: Gavin Lyons. - Created Jan 2020 - URL: https://github.com/gavinlyonsrepo/TM1638plus -*/ - -#include - - -// GPIO I/O pins on the Arduino connected to strobe, clock, data, pick on any I/O pin you want. -#define STROBE_TM 4 // strobe = GPIO connected to strobe line of module -#define CLOCK_TM 6 // clock = GPIO connected to clock line of module -#define DIO_TM 7 // data = GPIO connected to data line of module -bool swap_nibbles = false; //Default is false if left out, see note in readme at URL -bool high_freq = false; //default false,, If using a high freq CPU > ~100 MHZ set to true. - -// Constructor object Init the module -TM1638plus_Model2 tm(STROBE_TM, CLOCK_TM , DIO_TM, swap_nibbles, high_freq); - -//Control Vars & defines. -#define POT0 A0 -#define POT1 A1 -uint16_t sensorValue0 = 0; -uint16_t sensorValue1 = 0; - -unsigned long previousMillis_display = 0; // will store last time ADC was updated -const long interval_display = 1000; // interval at which to read ADCs (milliseconds) - -String textScroll; - -void setup() { - Serialinit(); - tm.displayBegin(); -} - - -//Function to setup serial called from setup FOR debug -void Serialinit() -{ - Serial.begin(9600); - delay(100); - Serial.println("--Comms UP--TM1638plus_ADC_TEST_Model2.ino--"); -} - -void loop() { - unsigned long currentMillis = millis(); - - // Read the Pots and update string every interval_display delay - if (currentMillis - previousMillis_display >= interval_display) - { - previousMillis_display = currentMillis; - // read the input on analog pin 0 and 1 - sensorValue0 = analogRead(POT0); - sensorValue1 = analogRead(POT1); - - // Send ADC data to serial port DEBUG - Serial.print("POT0 : "); - Serial.println(sensorValue0); - Serial.print("POT1 : "); - Serial.println(sensorValue1); - - textScroll = ""; - textScroll = textScroll + " ADC1 = " + sensorValue1 + " ADC0 = "+ sensorValue0; - } - - ModelTwo_Scroll(); -} - -//Function to scroll data passes back to program when finished. -void ModelTwo_Scroll() -{ - char charbuf[9]; - while(textScroll.length()> 0) - { - - //convert the string object to character array - textScroll.toCharArray(charbuf, 9); - //display the character buffer - tm.DisplayStr(charbuf); - // decrement the string - textScroll.remove(0, 1); - delay(500); - } -} diff --git a/examples/TM1638plus_TEST_Model1/TM1638plus_TEST_Model1.ino b/examples/TM1638plus_TEST_Model1/TM1638plus_TEST_Model1.ino index b5780c2..8a82131 100644 --- a/examples/TM1638plus_TEST_Model1/TM1638plus_TEST_Model1.ino +++ b/examples/TM1638plus_TEST_Model1/TM1638plus_TEST_Model1.ino @@ -1,10 +1,10 @@ /* Project Name: TM1638 - File: TM1638plus_TEST.ino - Description: demo file library for TM1638 module(LED & KEY). + File: TM1638plus_TEST_Model1.ino + Description: demo file library for TM1638 module(LED & KEY). Model 1 Carries out series of tests demonstrating arduino library TM1638plus. - TESTS + TESTS: TEST 0 Reset TEST 1 Brightness TEST 2 ASCII display @@ -30,9 +30,9 @@ // GPIO I/O pins on the Arduino connected to strobe, clock, data, //pick on any I/O you want. -#define STROBE_TM 4 -#define CLOCK_TM 6 -#define DIO_TM 7 +#define STROBE_TM 4 // strobe = GPIO connected to strobe line of module +#define CLOCK_TM 6 // clock = GPIO connected to clock line of module +#define DIO_TM 7 // data = GPIO connected to data line of module bool high_freq = false; //default false,, If using a high freq CPU > ~100 MHZ set to true. //Constructor object (GPIO STB , GPIO CLOCK , GPIO DIO, use high freq MCU) @@ -42,7 +42,7 @@ TM1638plus tm(STROBE_TM, CLOCK_TM , DIO_TM, high_freq); // Some vars and defines for the tests. #define myTestDelay 5000 #define myTestDelay1 1000 -uint8_t testcount = 1; +uint8_t testcount = 0; void setup() @@ -61,7 +61,7 @@ void loop() { case 1: Test1(); break; // Brightness case 2: Test2(); break; // ASCII display - case 3: Test3(); break; // TEST 3 Set a single segment + case 3: Test3(); break; // Set a single segment case 4: Test4(); break; // Hex digits case 5: Test5(); break; // Text String with Decimal point case 6: Test6(); break; // TEXT + ASCII combo @@ -99,12 +99,12 @@ void Test1() { } void Test2() { - //Test 2 ASCII display 2.348 + //Test 2 ASCII , display 2.341 tm.displayASCIIwDot(0, '2'); tm.displayASCII(1, '3'); tm.displayASCII(2, '4'); - tm.displayASCII(3, '8'); + tm.displayASCII(3, '1'); delay(myTestDelay); tm.reset(); } @@ -112,7 +112,7 @@ void Test2() { void Test3() { //TEST 3 single segment (pos, (dp)gfedcba) //In this case segment g (middle dash) of digit position 7 - tm.display7Seg(7, 0b01000000); + tm.display7Seg(7, 0b01000000); // Displays " -" delay(myTestDelay); } @@ -125,8 +125,8 @@ void Test4() { tm.displayHex(4, 5); tm.displayHex(5, 6); tm.displayHex(6, 7); - tm.displayHex(7, 8); - delay(myTestDelay); + tm.displayHex(7, 8); + delay(myTestDelay); // display 12345678 tm.displayHex(0, 8); tm.displayHex(1, 9); @@ -136,7 +136,7 @@ void Test4() { tm.displayHex(5, 13); tm.displayHex(6, 14); tm.displayHex(7, 15); - delay(myTestDelay); + delay(myTestDelay); // display 89ABCDEF } void Test5() { @@ -148,11 +148,11 @@ void Test5() { void Test6() { // Test6 TEXT + ASCII combo - // ADC=.2.548 + // ADC=.2.948 char text1[] = "ADC=."; tm.displayText(text1); tm.displayASCIIwDot(4, '2'); - tm.displayASCII(5, '5'); + tm.displayASCII(5, '9'); tm.displayASCII(6, '4'); tm.displayASCII(7, '8'); delay(myTestDelay); @@ -164,13 +164,13 @@ void Test7() { tm.displayIntNum(45, false); // "45 " delay(myTestDelay); // TEST 7b Integer - tm.displayIntNum(12345, true); // "00012345" + tm.displayIntNum(99991, true); // "00099991" delay(myTestDelay); tm.reset(); // TEST 7b tm.DisplayDecNumNIbble tm.DisplayDecNumNibble(1234, 5678, false); // "12345678" delay(myTestDelay); - tm.DisplayDecNumNibble(123, 678, true); // "01230678" + tm.DisplayDecNumNibble(123, 662, true); // "01230662" delay(myTestDelay); } @@ -221,17 +221,28 @@ void Test11() void Test12() { - //TEST 12 scrolling text, just one possible method using string object. - String textScroll = "123456789AbCdEF" ; - char charbuf[9]; - while (textScroll.length() > 0) + //TEST 12 scrolling text, just one possible method. + char textScroll[17] = " Hello world 123"; + unsigned long previousMillis_display = 0; // will store last time display was updated + const long interval_display = 1000; // interval at which to update display (milliseconds) + + while(1) { - - textScroll.toCharArray(charbuf, 9);// convert the string object to character array - tm.displayText(charbuf); // display the character buffer - textScroll.remove(0, 1); // decrement the string - delay(500); - tm.displayText(" "); // Clear display after each increment + tm.displayText(textScroll); + unsigned long currentMillis = millis(); + + if (currentMillis - previousMillis_display >= interval_display) + { + previousMillis_display = currentMillis; + if (strlen(textScroll) > 0) + { + memmove(textScroll, textScroll+1, strlen(textScroll)); + tm.displayText(" "); //Clear display or last character will drag across screen + }else + { + return; + } + } } } @@ -240,20 +251,23 @@ void Test13() //Test 13 LED display uint8_t LEDposition = 0; - // Test 13A Turn on redleds with setLED + // Test 13A Turn on redleds one by one, left to right, with setLED where 0 is L1 and 7 is L8 (L8 RHS of display) for (LEDposition = 0; LEDposition < 8; LEDposition++) { tm.setLED(LEDposition, 1); delay(500); tm.setLED(LEDposition, 0); } - // TEST 13b test setLEDs function (0xLEDXX) (L0-L7,XX) - // For model 1 just use upper byte , lower byte is is used by model3 for bi-color leds leave at 0x00. - tm.setLEDs(0xFF00); //all red + // TEST 13b test setLEDs function (0xLEDXX) ( L8-L1 , XX ) + // NOTE passed L8-L1 and on display L8 is on right hand side. i.e. 0x01 turns on L1. LXXX XXXX + // For model 1 just use upper byte , lower byte is is used by model3 for bi-color leds leave at 0x00 for model 1. + tm.setLEDs(0xFF00); // all LEDs on + delay(3000); + tm.setLEDs(0x0100); // Displays as LXXX XXXX (L1-L8) , NOTE on display L8 is on right hand side. delay(3000); - tm.setLEDs(0xF000); // L0-L7 XXXXLLLL + tm.setLEDs(0xF000); // Displays as XXXX LLLL (L1-L8) , NOTE on display L8 is on right hand side. delay(3000); - tm.setLEDs(0x0000); //all off + tm.setLEDs(0x0000); // all off delay(3000); } diff --git a/examples/TM1638plus_TEST_Model2/TM1638plus_TEST_Model2.ino b/examples/TM1638plus_TEST_Model2/TM1638plus_TEST_Model2.ino index 8cef75f..68d4833 100644 --- a/examples/TM1638plus_TEST_Model2/TM1638plus_TEST_Model2.ino +++ b/examples/TM1638plus_TEST_Model2/TM1638plus_TEST_Model2.ino @@ -1,33 +1,22 @@ /* Project Name: TM1638 File: TM1638plus_TEST_Model2.ino - Description: demo file library for model 2 TM1638 module(16 KEY 16 pushbuutons). + Description: demo file library for "model 2" TM1638 module(16 KEY 16 pushbuutons). Carries out series of tests demonstrating arduino library TM1638plus. - - Tests - - 0 = Test 0 reset test - -1A = Test 1A decimal numbers - 1B = Test 1B decimal numbers with the DisplayDecNumNibble function - - 2 = Test 2 Hexadecimal number - - 3 Test 3 manually set segments abcdefg(dp) = 01234567 - 3A = Display 1 - 3B = Display 00000005 - 3C = Display scrolling g segment , "-" countup to "--------" - - 4 = Display 4 strings + The tests will increment automatically with exception of test9, to enter press S16 during test8 - 5 = ASCII to segments takes an array of 8 bytes and displays them - without ref to the ASCII font table, direct data to digits to displays. i.e. 3F 3F 3F 6D 3F 3F 3F 6D = 00050005 - (DP)gfedcba = 0011 1111 = 3F = zero https://en.wikipedia.org/wiki/Seven-segment_display - - 6 = Brightness control - - 7 = buttons, no debounce see notes at URL for Example to debounce. - returns 0-16 , 0 for nothing pressed. data sent to serial. + TESTS + + TEST0 = reset function test + TEST1 = decimal numbers + TEST2 = Hexadecimal number + TEST3 = manually set segments + TEST4 = Display strings + TEST5 = ASCII to segments ( no reference to font table) + TEST6 = Brightness control + TEST7 = Scroll text example + TEST8 = Push buttons ReadKey16() buttons function , press 16 to goto test9 + TEST9 = Push buttons ReadKeys16Two() alternate buttons function Author: Gavin Lyons. Created: August 2019 @@ -36,11 +25,8 @@ #include -#define myTestDelay 5000 -uint8_t testcount = 0; - // GPIO I/O pins on the Arduino connected to strobe, clock, data, pick on any I/O pin you want. -#define STROBE_TM 4 // strobe = GPIO connected to strobe line of module +#define STROBE_TM 4 // strobe = GPIO connected to strobe line of module #define CLOCK_TM 6 // clock = GPIO connected to clock line of module #define DIO_TM 7 // data = GPIO connected to data line of module bool swap_nibbles = false; //Default is false if left out, see note in readme at URL @@ -49,10 +35,16 @@ bool high_freq = false; //default false,, If using a high freq CPU > ~100 MHZ se // Constructor object TM1638plus_Model2 tm(STROBE_TM, CLOCK_TM , DIO_TM, swap_nibbles, high_freq); +// For test setup +#define myTestDelay 5000 +#define myTestDelay1 1000 +#define myTestDelay2 250 +uint8_t testcount = 0; + void setup() { - Serialinit(); + Serialinit(); //optional used for buttons test tm.displayBegin(); // Init the module - delay(myTestDelay); + delay(myTestDelay2); // Test 0 reset test tm.reset(); @@ -67,9 +59,11 @@ void loop() { case 2: Test2(); break; // Test 2 Hexadecimal number case 3: Test3(); break; // Test 3a 3b & 3C using DisplaySegments method case 4: Test4(); break; // Test 4 strings - case 5: Test5(); break; // Test 5 ASCIItoSegment method - case 6: Test6(); break; // Test 6 Brightness and reset - case 7: Test7(); break; // Test 7 Buttons + case 5: Test5(); break; // Test 5 ASCIItoSegment method + case 6: Test6(); break; // Test 6 Brightness and reset + case 7: Test7(); break; // Test 7 scroll text + case 8: Test8(); break; // Test 8 Buttons , ReadKey16() returns byte 1-16 decimal, press S16 to goto test9 + case 9: Test9(); break; // Test 9 Buttons , Readkey16Two() alternate buttons function. } } @@ -77,25 +71,25 @@ void loop() { void Serialinit() { Serial.begin(9600); - delay(100); - Serial.println("-------------Comms UP------------"); + delay( myTestDelay); + Serial.println("-- TM1638 M2 test : Comms UP --"); } void Test1(void) { // Test 1 decimal numbers - tm.DisplayDecNum(5, 1 << 6); // 00.000005 + tm.DisplayDecNum(250, 1 << 2, true); // 000002.50 delay(myTestDelay); - tm.DisplayDecNum(99991111, 1 << 4); // 9999.1111 + tm.DisplayDecNum(99991111, 1 << 4, true); // 9999.1111 delay(myTestDelay); - tm.DisplayDecNum(-2888, 0 ); // -0002888 + tm.DisplayDecNum(2888, 0 , true); // 00002888 delay(myTestDelay); - tm.DisplayDecNum(33, 0 ); // 00000033 + tm.DisplayDecNum(331285, 1 <<4 ,false); // "3312.85 " delay(myTestDelay); tm.DisplayDecNum(-33, 0 , false); // "-33 " delay(myTestDelay); // Test 1b decimal numbers with the DisplayDecNumNibble function divides display into two nibbles. - tm.DisplayDecNumNibble(1234 , 78, 1<<4 , true); // "1234.0078" + tm.DisplayDecNumNibble(2134 , 78, 1<<4 , true); // "2134.0078" delay(myTestDelay); } @@ -104,7 +98,7 @@ void Test2(void) // Test 2 Hexadecimal number tm.DisplayHexNum(0x0000, 0x456E, 0x00, true); // 0000456E delay(myTestDelay); - tm.DisplayHexNum(0xABCD, 0xEF23, 0, true); // ABCDEF23 + tm.DisplayHexNum(0xABCD, 0xEF23, 0x00, true); // ABCDEF23 delay(myTestDelay); tm.DisplayHexNum(0x0000, 0x00FF, 1 << 4); // 0000.00FF delay(myTestDelay); @@ -119,7 +113,7 @@ void Test3(void) tm.DisplaySegments(2, 0x01); //c, for c turn on digit one only tm.DisplaySegments(3, 0x00); //d tm.DisplaySegments(4, 0x00); //e - tm.DisplaySegments(5, 0x00); + tm.DisplaySegments(5, 0x00); //f tm.DisplaySegments(6, 0x00); //g tm.DisplaySegments(7, 0x00); //DP delay(myTestDelay); @@ -143,9 +137,9 @@ void Test3(void) for (uint8_t j = 0; j < 8; j++) { - tm.DisplaySegments(6, dashvalue); //g scrolling - dashvalue = (dashvalue*2)+1; //1 to 256 - delay(1000); + tm.DisplaySegments(6, dashvalue); // g scrolling + dashvalue = (dashvalue*2)+1; // 1 to 256 + delay(myTestDelay1); } delay(myTestDelay); @@ -158,7 +152,7 @@ void Test4(void) delay(myTestDelay); tm.DisplayStr(" Hi", 0x08); // " . Hi" delay(myTestDelay); - tm.DisplayStr(" ", 0Xff); // ". . . . . . . ." + tm.DisplayStr(" --- ", 0XE7); // ". . .---. . ." delay(myTestDelay); tm.DisplayStr(" helloU2", 0); // " helloU2" delay(myTestDelay); @@ -186,19 +180,76 @@ void Test6(void) { tm.brightness(brightness); tm.DisplayStr("brightnes", 1); - delay(1000); + delay(myTestDelay1); } + tm.brightness(2); tm.reset(); } +// Just one possible method to scroll text there are many others. void Test7(void) { + char textScroll[17] = " Hello world 123"; + unsigned long previousMillis_display = 0; // will store last time display was updated + const long interval_display = 1000; // interval at which to update display (milliseconds) + + while(1) + { + tm.DisplayStr(textScroll, 0); + unsigned long currentMillis = millis(); + // update data every interval_display delay + if (currentMillis - previousMillis_display >= interval_display) + { + previousMillis_display = currentMillis; + if (strlen(textScroll) > 0) + { + memmove(textScroll, textScroll+1, strlen(textScroll)); // delete first char in array. + }else + { + return; + } + } + } +} + +void Test8(void) +{ + unsigned char buttons; while(1) { - // Test 7 , buttons, no debounce see notes at URL for example to debounce. + // Test 8 , buttons readkey16() function, no debounce see notes at URL for example to debounce. // returns 0-16 , 0 for nothing pressed. - unsigned char buttons = tm.ReadKey16(); + // NOTE: pressing S16 will move to test 9 + buttons = tm.ReadKey16(); + tm.DisplayDecNum(buttons, 0 ,false); Serial.println(buttons); - delay(250); + delay( myTestDelay2); + if (buttons == 16) + { + //pressing 16 moves to test 9 + delay( myTestDelay2); + return; + } } } + +//returns word with binary value of switch. S16 = Bit 15 , S15 = bit 14 etc +void Test9(void) +{ + uint16_t buttons=0; + while(1) + { + // Test 9 buttons Read_key16_two() function + // returns a uint16_t where each bit represents a switch. + // S16S15S14S13S12S11S10S9S8S7S6S5S4S3SS2S1. + // eg S1 = 0x0001 + // eg S16 = 0x8000 + // eg S1 + S16 = 0x8001 + // Can be used to detect multi key presses , see Notes section in readme. + // For issues related to display when pressing multi keys together. + buttons = tm.ReadKey16Two(); + Serial.println(buttons, HEX); + tm.DisplayStr("buttons2", 0); + delay( myTestDelay2); + } +} diff --git a/examples/TM1638plus_TEST_Model3/TM1638plus_TEST_Model3.ino b/examples/TM1638plus_TEST_Model3/TM1638plus_TEST_Model3.ino index 099cb5a..aee3eef 100644 --- a/examples/TM1638plus_TEST_Model3/TM1638plus_TEST_Model3.ino +++ b/examples/TM1638plus_TEST_Model3/TM1638plus_TEST_Model3.ino @@ -1,7 +1,8 @@ /* Project Name: TM1638 File: TM1638plus_TEST.ino - Description: demo file library for TM1638 module(8 bicolour green and red LEDs & 8 pushbuttons). Called Model 3 in this library. This model is labelled LKM1638 or tm1638 v1.3 + Description: demo file library for TM1638 module(8 bicolour green and red LEDs & 8 pushbuttons). + Called Model 3 in this library. This model is labelled LKM1638 or tm1638 v1.3 Carries out series of tests demonstrating arduino library TM1638plus. TESTS @@ -34,9 +35,6 @@ #define CLOCK_TM 6 #define DIO_TM 7 -#define TM_1638_RED_LED 0x02 -#define TM_1638_GREEN_LED 0x01 -#define TM_1638_OFF_LED 0x00 bool high_freq = false; //default false,, If using a high freq CPU > ~100 MHZ set to true. //Constructor object (GPIO STB , GPIO CLOCK , GPIO DIO, use high freq MCU default false) @@ -103,12 +101,12 @@ void Test1() { } void Test2() { - //Test 2 ASCII display 2.348 + //Test 2 ASCII display 2.342 tm.displayASCIIwDot(0, '2'); tm.displayASCII(1, '3'); tm.displayASCII(2, '4'); - tm.displayASCII(3, '8'); + tm.displayASCII(3, '2'); delay(myTestDelay); tm.reset(); } @@ -152,37 +150,37 @@ void Test5() { void Test6() { // Test6 TEXT + ASCII combo - // ADC=.2.548 + // ADC=.2.541 char text1[] = "ADC=."; tm.displayText(text1); tm.displayASCIIwDot(4, '2'); tm.displayASCII(5, '5'); tm.displayASCII(6, '4'); - tm.displayASCII(7, '8'); + tm.displayASCII(7, '1'); delay(myTestDelay); tm.reset(); } void Test7() { // TEST 7a Integer - tm.displayIntNum(45, false); // "45 " + tm.displayIntNum(72, false); // "72 " delay(myTestDelay); // TEST 7b Integer - tm.displayIntNum(12345, true); // "00012345" + tm.displayIntNum(92345, true); // "00092345" delay(myTestDelay); tm.reset(); // TEST 7b tm.DisplayDecNumNIbble tm.DisplayDecNumNibble(1488, 5678, false); // "14885678" delay(myTestDelay); - tm.DisplayDecNumNibble(123, 678, true); // "01230678" + tm.DisplayDecNumNibble(123, 998, true); // "01230998" delay(myTestDelay); } void Test8() { // TEST 8 TEXT STRING + integer SSSSIIII char workStr[11]; - uint16_t data = 234; - sprintf(workStr, "ADC=.%04d", data); // "ADC=.0234" + uint16_t data = 294; + sprintf(workStr, "ADC=.%04d", data); // "ADC=.0294" tm.displayText(workStr); delay(myTestDelay); } @@ -225,18 +223,29 @@ void Test11() void Test12() { - //TEST 12 scrolling text, just one possible method using string object. - String textScroll = "123456789AbCdEF" ; - char charbuf[9]; - while (textScroll.length() > 0) - { + //TEST 12 scrolling text, just one possible method + char textScroll[17] = " Hello world 123"; + unsigned long previousMillis_display = 0; // will store last time display was updated + const long interval_display = 1000; // interval at which to update display (milliseconds) - textScroll.toCharArray(charbuf, 9);// convert the string object to character array - tm.displayText(charbuf); // display the character buffer - textScroll.remove(0, 1); // decrement the string - delay(500); - tm.displayText(" "); // Clear display after each increment + while(1) + { + tm.displayText(textScroll); + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis_display >= interval_display) + { + previousMillis_display = currentMillis; + if (strlen(textScroll) > 0) + { + memmove(textScroll, textScroll+1, strlen(textScroll)); + tm.displayText(" "); //Clear display or last character will drag across screen + }else + { + return; + } + } } + } @@ -247,28 +256,37 @@ void Test13() // Test 13A Turn on green leds with setLED for (LEDposition = 0; LEDposition < 8; LEDposition++) { - tm.setLED(LEDposition, TM_1638_GREEN_LED); + tm.setLED(LEDposition, TM_GREEN_LED); delay(500); - tm.setLED(LEDposition, TM_1638_OFF_LED); + tm.setLED(LEDposition, TM_OFF_LED); } // Test 13b turn on red LEDs with setLED for (LEDposition = 0; LEDposition < 8; LEDposition++) { - tm.setLED(LEDposition, TM_1638_RED_LED); + tm.setLED(LEDposition, TM_RED_LED); delay(500); - tm.setLED(LEDposition, TM_1638_OFF_LED); + tm.setLED(LEDposition, TM_OFF_LED); } - // TEST 13c test setLEDs function (0xgreenred) (L0-L7, L0-L7) - tm.setLEDs(0xE007); //L0-L7 RRRXXGGG + // TEST 13c + // test setLEDs function (0xgreenred) (0xGGRR) (LED8-LED1, LED8-LED1) + // Upper byte switch LED green colour ON, lower byte = switch LED red colour ON + // NB Note on the unit, LED8 is onthe right hand side so result is mirrored. + // Example: + // E0 = green on 07 = red on + // E0 = 1110 0000 , 07 = 0000 0111 = 11100111 = GGGXXRRR = LED8-LED1 + // Shows on display as LED1-LED8 turns on RRRXXGGG as LED 8 is on right hand side. + + tm.setLEDs(0xE007); //L1-L8 turns on RRRXXGGG on display delay(3000); - tm.setLEDs(0xF00F); // L0-L7 RRRRGGGG + + tm.setLEDs(0xF00F); // L1-L8 turns on RRRRGGGG on display delay(3000); - tm.setLEDs(0xFE01); // L0-L7 RGGGGGGG + tm.setLEDs(0xFE01); // L1-L8 turns on RGGGGGGG on display delay(3000); - tm.setLEDs(0x00FF); //all red + tm.setLEDs(0x00FF); //all red RRRRRRR delay(3000); - tm.setLEDs(0xFF00); //all green + tm.setLEDs(0xFF00); //all green GGGGGGG delay(3000); tm.setLEDs(0x0000); //all off delay(3000); diff --git a/extra/CHANGELOG.md b/extra/CHANGELOG.md index b5a333c..555c509 100755 --- a/extra/CHANGELOG.md +++ b/extra/CHANGELOG.md @@ -26,10 +26,7 @@ * ASCII font reduced in size { | } ~ characters removed. * Version 1.4.0 Feb 2020 - * "beginDisplay" method added. This will break backward compatibility, This had to be done as - the pinmodes functions had to be removed from the class constructor and there was no way around this. - The update for 1.4 to files for users is simple, simply add "beginDisplay" function call to "setup" as - per example files. + * "beginDisplay" method added. * Two new example files added. * Tested on Nodemcu ESP-12E ESP2866 , it works. @@ -37,12 +34,18 @@ * Minor update to bring in pull request 7 to deal with github issues 6 and 7. * Display overflow check and dealing with multiple dots. * Changes are for Model 1 only and "displayText" function only. - * Pull request by [gabormay](https://github.com/gabormay) + * Pull request 1 by [gabormay](https://github.com/gabormay) * Version 1.6.0 August 2020 - * Pull request by [centic9](https://github.com/centic9) Model 1 constructor duplicate code removed. + * Pull request 2 by [centic9](https://github.com/centic9) Model 1 constructor duplicate code removed. * Closed Issue 1 and 2 on github by adding support for high frequency MCU, such as ESP32 The constructor of all models has a new parameter if set to true the code uses a custom "shiftin" function * Closed Issue 8 on github, LKM1638 bi-colour LED module support added (named Model 3 for purposes of library ) * New function setLEDs added to turn on all LEDs at once, for model 1 and 3. +* Version 1.7.0 October 2020 + * Added ReadKey16Two function to model 2 to detect multiple key presses see issue 12 on github. + * TM1638_common files created for common code and settings between the different models + * Example file added by [wunderbaum](https://github.com/wunderbaum) see pull request 3. + * Optimisations to example files. + diff --git a/keywords.txt b/keywords.txt index 9bcec1d..ae7e9bb 100644 --- a/keywords.txt +++ b/keywords.txt @@ -8,14 +8,13 @@ TM1638plus_Model2 KEYWORD1 # Methods / functions displayBegin KEYWORD2 -sendCommand KEYWORD2 reset KEYWORD2 brightness KEYWORD2 DisplayDecNumNibble KEYWORD2 readButtons KEYWORD2 setLED KEYWORD2 -setLEDs KEYWORD2 +setLEDs KEYWORD2 displayText KEYWORD2 displayASCIIwDot KEYWORD2 displayASCII KEYWORD2 @@ -29,7 +28,7 @@ DisplayDecNum KEYWORD2 DisplayStr KEYWORD2 ASCIItoSegment KEYWORD2 ReadKey16 KEYWORD2 -sendData KEYWORD2 +ReadKey16Two KEYWORD2 # Constants diff --git a/library.properties b/library.properties index 397a494..8635f3e 100755 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=TM1638plus -version=1.6.0 +version=1.7.0 author=Gavin Lyons maintainer=Gavin Lyons sentence=TM1638plus is an Arduino library to control TM1638 seven segment modules. diff --git a/src/TM1638plus.cpp b/src/TM1638plus.cpp index 4543020..c4076ab 100644 --- a/src/TM1638plus.cpp +++ b/src/TM1638plus.cpp @@ -1,28 +1,28 @@ /* * Project Name: TM1638 * File: TM1638plus.cpp -* Description: source file arduino library for TM1638 module(LED & KEY). +* Description: source file arduino library for TM1638 module(LED & KEY). Model 1 & Model 3 * Author: Gavin Lyons. * Created May 2019 * URL: https://github.com/gavinlyonsrepo/TM1638plus */ #include "TM1638plus.h" -#include "TM1638plus_font.h" + TM1638plus::TM1638plus(uint8_t strobe, uint8_t clock, uint8_t data, bool highfreq) { _STROBE_IO = strobe; _DATA_IO = data; _CLOCK_IO = clock; - _HIGH_FREQ = highfreq; + _HIGH_FREQ = highfreq; } void TM1638plus::displayBegin() { pinMode(_STROBE_IO , OUTPUT); pinMode(_DATA_IO, OUTPUT); pinMode(_CLOCK_IO , OUTPUT); - sendCommand(ACTIVATE_TM); - brightness(DEFAULT_BRIGHTNESS); + sendCommand(TM_ACTIVATE); + brightness(TM_DEFAULT_BRIGHTNESS); reset(); } @@ -30,17 +30,25 @@ void TM1638plus::displayBegin() { void TM1638plus::sendCommand(uint8_t value) { digitalWrite(_STROBE_IO, LOW); - shiftOut(_DATA_IO, _CLOCK_IO, LSBFIRST, value); + sendData(value); digitalWrite(_STROBE_IO, HIGH); } +void TM1638plus::sendData(uint8_t data) +{ + if (_HIGH_FREQ == false) + shiftOut(_DATA_IO, _CLOCK_IO, LSBFIRST, data); + else + TM_common.HighFreqshiftOut(_DATA_IO, _CLOCK_IO, LSBFIRST, data); +} + void TM1638plus::reset() { - sendCommand(WRITE_INC); // set auto increment mode + sendCommand(TM_WRITE_INC); // set auto increment mode digitalWrite(_STROBE_IO, LOW); - shiftOut(_DATA_IO, _CLOCK_IO, LSBFIRST, SEG_ADR); // set starting address to 0 + sendData(TM_SEG_ADR); // set starting address to for (uint8_t i = 0; i < 16; i++) { - shiftOut(_DATA_IO, _CLOCK_IO, LSBFIRST, 0x00); + sendData(0x00); } digitalWrite(_STROBE_IO, HIGH); } @@ -48,80 +56,80 @@ void TM1638plus::reset() { void TM1638plus::setLED(uint8_t position, uint8_t value) { pinMode(_DATA_IO, OUTPUT); - sendCommand(WRITE_LOC); + sendCommand(TM_WRITE_LOC); digitalWrite(_STROBE_IO, LOW); - shiftOut(_DATA_IO, _CLOCK_IO, LSBFIRST, LEDS_ADR + (position << 1)); - shiftOut(_DATA_IO, _CLOCK_IO, LSBFIRST, value); + sendData(TM_LEDS_ADR + (position << 1)); + sendData(value); digitalWrite(_STROBE_IO, HIGH); } -void TM1638plus::setLEDs(word ledvalues) +void TM1638plus::setLEDs(uint16_t ledvalues) { for (uint8_t LEDposition = 0; LEDposition < 8; LEDposition++) { - byte color = 0; + uint8_t colour = 0; if ((ledvalues & (1 << LEDposition)) != 0) { - color |= TM1638_RED_LED; //scan lower byte, set Red if one + colour |= TM_RED_LED; //scan lower byte, set Red if one } if ((ledvalues & (1 << (LEDposition + 8))) != 0) { - color |= TM1638_GREEN_LED; //scan upper byte, set green if one + colour |= TM_GREEN_LED; //scan upper byte, set green if one } - setLED(LEDposition, color); + setLED(LEDposition, colour); } } + void TM1638plus::displayIntNum(unsigned long number, boolean leadingZeros) { - char values[DISPLAY_SIZE + 1]; - snprintf(values, DISPLAY_SIZE + 1, leadingZeros ? "%08ld" : "%ld", number); + char values[TM_DISPLAY_SIZE + 1]; + snprintf(values, TM_DISPLAY_SIZE + 1, leadingZeros ? "%08ld" : "%ld", number); displayText(values); } void TM1638plus::DisplayDecNumNibble(uint16_t numberUpper, uint16_t numberLower, boolean leadingZeros) { - char valuesUpper[DISPLAY_SIZE + 1]; - char valuesLower[DISPLAY_SIZE/2 + 1]; - snprintf(valuesUpper, DISPLAY_SIZE/2 + 1, leadingZeros ? "%04d" : "%d", numberUpper); - snprintf(valuesLower, DISPLAY_SIZE/2 + 1, leadingZeros ? "%04d" : "%d", numberLower); + char valuesUpper[TM_DISPLAY_SIZE + 1]; + char valuesLower[TM_DISPLAY_SIZE/2 + 1]; + snprintf(valuesUpper, TM_DISPLAY_SIZE/2 + 1, leadingZeros ? "%04d" : "%d", numberUpper); + snprintf(valuesLower, TM_DISPLAY_SIZE/2 + 1, leadingZeros ? "%04d" : "%d", numberLower); strcat(valuesUpper ,valuesLower); displayText(valuesUpper); } void TM1638plus::displayText(const char *text) { char c, pos; - pos = 0; - while ((c = (*text++)) && pos < DISPLAY_SIZE ) { - if (*text == '.' && c != '.') { - displayASCIIwDot(pos++, c); + while ((c = (*text++)) && pos < TM_DISPLAY_SIZE) { + if (*text == '.' && c != '.') { + displayASCIIwDot(pos++, c); - text++; - } else { - displayASCII(pos++, c); - } - } + text++; + } else { + displayASCII(pos++, c); + } + } } void TM1638plus::displayASCIIwDot(uint8_t position, uint8_t ascii) { // add 128 or 0x080 0b1000000 to turn on decimal point/dot in seven seg - display7Seg(position, pgm_read_byte(&SevenSeg[ascii- ASCII_OFFSET]) + DOT_MASK_DEC); + display7Seg(position, pgm_read_byte(&SevenSeg[ascii- TM_ASCII_OFFSET]) + TM_DOT_MASK_DEC); } void TM1638plus::display7Seg(uint8_t position, uint8_t value) { // call 7-segment - sendCommand(WRITE_LOC); + sendCommand(TM_WRITE_LOC); digitalWrite(_STROBE_IO, LOW); - shiftOut(_DATA_IO, _CLOCK_IO, LSBFIRST, SEG_ADR + (position << 1)); - shiftOut(_DATA_IO, _CLOCK_IO, LSBFIRST, value); + sendData(TM_SEG_ADR + (position << 1)); + sendData(value); digitalWrite(_STROBE_IO, HIGH); } void TM1638plus::displayASCII(uint8_t position, uint8_t ascii) { - display7Seg(position, pgm_read_byte(&SevenSeg[ascii - ASCII_OFFSET])); + display7Seg(position, pgm_read_byte(&SevenSeg[ascii - TM_ASCII_OFFSET])); } void TM1638plus::displayHex(uint8_t position, uint8_t hex) @@ -129,7 +137,7 @@ void TM1638plus::displayHex(uint8_t position, uint8_t hex) uint8_t offset = 0; if (hex <= 9) { - display7Seg(position, pgm_read_byte(&SevenSeg[hex + HEX_OFFSET])); + display7Seg(position, pgm_read_byte(&SevenSeg[hex + TM_HEX_OFFSET])); // 16 is offset in reduced ASCII table for 0 }else if ((hex >= 10) && (hex <=15)) { @@ -143,7 +151,7 @@ void TM1638plus::displayHex(uint8_t position, uint8_t hex) case 14: offset = 'E'; break; case 15: offset = 'F'; break; } - display7Seg(position, pgm_read_byte(&SevenSeg[offset-ASCII_OFFSET])); + display7Seg(position, pgm_read_byte(&SevenSeg[offset-TM_ASCII_OFFSET])); } } @@ -155,7 +163,7 @@ uint8_t TM1638plus::readButtons() uint8_t v =0; digitalWrite(_STROBE_IO, LOW); - shiftOut(_DATA_IO, _CLOCK_IO, LSBFIRST, BUTTONS_MODE); + sendData(TM_BUTTONS_MODE); pinMode(_DATA_IO, INPUT); for (uint8_t i = 0; i < 4; i++) @@ -164,7 +172,7 @@ uint8_t TM1638plus::readButtons() if (_HIGH_FREQ == false) v = shiftIn(_DATA_IO, _CLOCK_IO, LSBFIRST) << i; else - v = HighFreqshiftin(_DATA_IO, _CLOCK_IO, LSBFIRST) << i; + v = TM_common.HighFreqshiftin(_DATA_IO, _CLOCK_IO, LSBFIRST) << i; buttons |= v; } @@ -178,28 +186,6 @@ uint8_t TM1638plus::readButtons() void TM1638plus::brightness(uint8_t brightness) { uint8_t value = 0; - value = BRIGHT_ADR + (BRIGHT_MASK & brightness); - digitalWrite(_STROBE_IO, LOW); - shiftOut(_DATA_IO, _CLOCK_IO, LSBFIRST, value); - digitalWrite(_STROBE_IO, HIGH); -} - - -uint8_t TM1638plus::HighFreqshiftin(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) -{ - uint8_t value = 0; - uint8_t i = 0; - - for(i = 0; i < 8; ++i) { - if(bitOrder == LSBFIRST) - value |= digitalRead(dataPin) << i; - else - value |= digitalRead(dataPin) << (7 - i); - - digitalWrite(clockPin, HIGH); - delayMicroseconds(1); - digitalWrite(clockPin, LOW); - delayMicroseconds(1); - } - return value; + value = TM_BRIGHT_ADR + (TM_BRIGHT_MASK & brightness); + sendCommand(value); } diff --git a/src/TM1638plus.h b/src/TM1638plus.h index dae5e68..6809a11 100644 --- a/src/TM1638plus.h +++ b/src/TM1638plus.h @@ -1,15 +1,15 @@ /* * Project Name: TM1638plus * File: TM1638plus.h -* Description: TM1638plus.h header file arduino library for TM1638 module(LED & KEY). +* Description: TM1638plus.h header file arduino library for TM1638 module(LED & KEY). Model 1 & Model 3 * Author: Gavin Lyons. * Created May 2019 * URL: https://github.com/gavinlyonsrepo/TM1638plus */ -#ifndef TM1638plus_h -#define TM1638plus_h +#ifndef TM1638PLUS_H +#define TM1638PLUS_H #if (ARDUINO >=100) #include "Arduino.h" @@ -17,25 +17,7 @@ #include "WProgram.h" #endif - -#define ACTIVATE_TM 0x8F // Start up -#define BUTTONS_MODE 0x42 // Buttons mode -#define WRITE_LOC 0x44 // Write to a location -#define WRITE_INC 0x40 // Incremental write -#define SEG_ADR 0XC0 // leftmost segment Address C0 C2 C4 C6 C8 CA CC CE -#define LEDS_ADR 0xC1 // Leftmost LED address C1 C3 C5 C7 C9 CB CD CF -#define BRIGHT_ADR 0x88 // Brightness address -#define BRIGHT_MASK 0x07 // Brightness mask -#define DEFAULT_BRIGHTNESS 0x02 //can be 0x00 to 0x07 -#define DISPLAY_SIZE 8 //size of display - -#define ASCII_OFFSET 32 // Ascii table offset to jump over first missing 32 chars -#define HEX_OFFSET 16 // Ascii table offset to reach number position -#define DOT_MASK_DEC 128 // 0x80 Mask to switch on decimal point in seven seg. - -#define TM1638_RED_LED 0x02 -#define TM1638_GREEN_LED 0x01 -#define TM1638_OFF_LED 0x00 +#include "TM1638plus_common.h" class TM1638plus { @@ -52,15 +34,10 @@ class TM1638plus { // Methods - // Begin method , sets pinmodes , Call in setup - void displayBegin(); + void displayBegin(); // Begin method , sets pinmodes , Call in setup + + void reset(void); // Reset / Clear module - // Send a command to module - void sendCommand(uint8_t value); - - // Reset module - void reset(void); - //Sets the brightness level on a scale of brightness = 0 to 7. //0 is not turned off, it's just the lowest brightness. //If user wishes to change the default brightness at start-up change. @@ -74,9 +51,6 @@ class TM1638plus { // for de-bonce example. uint8_t readButtons(void); - // Set an LED, pass it LED position 0-7 and value 0 or 1 - void setLED(uint8_t position, uint8_t value); - // Send Text to Seven segments, passed char array pointer // dots are removed from string and dot on preceding digit switched on // "abc.def" will be shown as "abcdef" with c decimal point turned on. @@ -104,25 +78,27 @@ class TM1638plus { //and leading zeros optional void DisplayDecNumNibble(uint16_t numberUpper, uint16_t numberLower, boolean leadingZeros = true); - // Set the LEDs. passed a word. - // For model 3 the word contains: - //MSB byte for the green LEDs, LSB for the red LEDs (0xgreenred) - //ie. 0xE007 1110 0000 0000 0111 results in L8-L0 GGGXXRRR where L8 is RHS - // for model 1 the word contains : - // MSB for LED LSB n/a (0xledsXX) - //i.e 0xF100 1111 0001 L8-L0 RRRRXXXR L8 is RHS - void setLEDs(word led); + // Set the LEDs. passed one 16bit integer. + // MODEL 3: + //MSB byte for the green LEDs, LS byte for the red LEDs (0xgreenred) + //ie. 0xE007 1110 0000 0000 0111 results in L8-L0 GGGX XRRR, NOTE L8 is RHS on display + // MODEL 1: + // MSB byte 1 for red LED , LSB byte n/a set to 0x00 (0xleds, 0xXX) + //i.e 0xF100 1111 0000 L8-L0 RRRRXXX0 NOTE L8 is RHS on display + void setLEDs(uint16_t greenred); + // Set an LED, pass it LED position 0-7 and value 0 or 1 , L1-L8 + void setLED(uint8_t position, uint8_t value); + private: uint8_t _STROBE_IO; uint8_t _DATA_IO; uint8_t _CLOCK_IO; - - // Used instead of arduino function "shiftin" when _HIGH_FREQ is set to true - uint8_t HighFreqshiftin(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) ; - - //This is used when running high freq CPU because of issues with button function. + void sendCommand(uint8_t value); + void sendData(uint8_t data); + //This is used when running high freq CPU bool _HIGH_FREQ = false; + TM1638plus_common TM_common; }; #endif diff --git a/src/TM1638plus_Model2.cpp b/src/TM1638plus_Model2.cpp index b3bd119..7ac43bb 100644 --- a/src/TM1638plus_Model2.cpp +++ b/src/TM1638plus_Model2.cpp @@ -8,7 +8,6 @@ */ #include "TM1638plus_Model2.h" -#include "TM1638plus_font.h" TM1638plus_Model2::TM1638plus_Model2(uint8_t strobe, uint8_t clock, uint8_t data, bool swap_nibbles, bool high_freq) { _STROBE_IO = strobe; @@ -23,8 +22,8 @@ void TM1638plus_Model2::displayBegin(void) pinMode(_STROBE_IO, OUTPUT); pinMode(_DATA_IO, OUTPUT); pinMode(_CLOCK_IO, OUTPUT); - sendCommand(ACTIVATE_TM); - brightness(DEFAULT_BRIGHTNESS); + sendCommand(TM_ACTIVATE); + brightness(TM_DEFAULT_BRIGHTNESS); reset(); } @@ -35,15 +34,18 @@ void TM1638plus_Model2::sendCommand(uint8_t value) digitalWrite(_STROBE_IO, HIGH); } -void TM1638plus_Model2::sendData(byte data) +void TM1638plus_Model2::sendData(uint8_t data) { - shiftOut(_DATA_IO, _CLOCK_IO, LSBFIRST, data); + if (_HIGH_FREQ == false) + shiftOut(_DATA_IO, _CLOCK_IO, LSBFIRST, data); + else + TM_common.HighFreqshiftOut(_DATA_IO, _CLOCK_IO, LSBFIRST, data); } void TM1638plus_Model2::reset() { - sendCommand(WRITE_INC); // set auto increment mode + sendCommand(TM_WRITE_INC); // set auto increment mode digitalWrite(_STROBE_IO, LOW); - sendData(SEG_ADR); // set starting address to 0 + sendData(TM_SEG_ADR); // set starting address to 0 for (uint8_t position = 0; position < 16; position++) { sendData(0x00); //clear all segments @@ -62,9 +64,9 @@ void TM1638plus_Model2::DisplaySegments(byte segment, byte digit) } segment = (segment<<1); - sendCommand(WRITE_LOC); + sendCommand(TM_WRITE_LOC); digitalWrite(_STROBE_IO, LOW); - sendData(SEG_ADR | segment); + sendData(TM_SEG_ADR | segment); sendData(digit); digitalWrite(_STROBE_IO, HIGH); } @@ -72,55 +74,53 @@ void TM1638plus_Model2::DisplaySegments(byte segment, byte digit) void TM1638plus_Model2::brightness(uint8_t brightness) { uint8_t value = 0; - value = BRIGHT_ADR + (BRIGHT_MASK & brightness); - digitalWrite(_STROBE_IO, LOW); - shiftOut(_DATA_IO, _CLOCK_IO, LSBFIRST, value); - digitalWrite(_STROBE_IO, HIGH); + value = TM_BRIGHT_ADR + (TM_BRIGHT_MASK & brightness); + sendCommand(value); } void TM1638plus_Model2::DisplayHexNum(uint16_t numberUpper, uint16_t numberLower, byte dots, boolean leadingZeros) { - char valuesUpper[DISPLAY_SIZE + 1]; - char valuesLower[DISPLAY_SIZE/2 + 1]; - snprintf(valuesUpper, DISPLAY_SIZE/2 + 1, leadingZeros ? "%04X" : "%X", numberUpper); - snprintf(valuesLower, DISPLAY_SIZE/2 + 1, leadingZeros ? "%04X" : "%X", numberLower); + char valuesUpper[TM_DISPLAY_SIZE + 1]; + char valuesLower[TM_DISPLAY_SIZE/2 + 1]; + snprintf(valuesUpper, TM_DISPLAY_SIZE/2 + 1, leadingZeros ? "%04X" : "%X", numberUpper); + snprintf(valuesLower, TM_DISPLAY_SIZE/2 + 1, leadingZeros ? "%04X" : "%X", numberLower); strcat(valuesUpper ,valuesLower); DisplayStr(valuesUpper, dots); } void TM1638plus_Model2::DisplayDecNum(unsigned long number, byte dots, boolean leadingZeros) { - char values[DISPLAY_SIZE + 1]; - snprintf(values, DISPLAY_SIZE + 1, leadingZeros ? "%08ld" : "%ld", number); + char values[TM_DISPLAY_SIZE + 1]; + snprintf(values, TM_DISPLAY_SIZE + 1, leadingZeros ? "%08ld" : "%ld", number); DisplayStr(values, dots); } void TM1638plus_Model2::DisplayDecNumNibble(uint16_t numberUpper, uint16_t numberLower, byte dots, boolean leadingZeros) { - char valuesUpper[DISPLAY_SIZE + 1]; - char valuesLower[DISPLAY_SIZE/2 + 1]; - snprintf(valuesUpper, DISPLAY_SIZE/2 + 1, leadingZeros ? "%04d" : "%d", numberUpper); - snprintf(valuesLower, DISPLAY_SIZE/2 + 1, leadingZeros ? "%04d" : "%d", numberLower); + char valuesUpper[TM_DISPLAY_SIZE + 1]; + char valuesLower[TM_DISPLAY_SIZE/2 + 1]; + snprintf(valuesUpper, TM_DISPLAY_SIZE/2 + 1, leadingZeros ? "%04d" : "%d", numberUpper); + snprintf(valuesLower, TM_DISPLAY_SIZE/2 + 1, leadingZeros ? "%04d" : "%d", numberLower); strcat(valuesUpper ,valuesLower); DisplayStr(valuesUpper, dots); } void TM1638plus_Model2::DisplayStr(const char* string, const word dots) { - byte values[DISPLAY_SIZE]; + byte values[TM_DISPLAY_SIZE]; boolean done = false; uint8_t Result = 0; - memset(values, 0, DISPLAY_SIZE * sizeof(byte)); + memset(values, 0, TM_DISPLAY_SIZE * sizeof(byte)); - for (uint8_t i = 0; i < DISPLAY_SIZE; i++) + for (uint8_t i = 0; i < TM_DISPLAY_SIZE; i++) { if (!done && string[i] != '\0') { if (dots >> (7-i) & 1){ //if dots bit is set for that position apply the mask to turn on dot(0x80). - Result = pgm_read_byte(&SevenSeg[string[i] - ASCII_OFFSET]); - values[i] = (Result | DOT_MASK_DEC); //apply the Dot bitmask to value extracted from ASCII table + Result = pgm_read_byte(&SevenSeg[string[i] - TM_ASCII_OFFSET]); + values[i] = (Result | TM_DOT_MASK_DEC); //apply the Dot bitmask to value extracted from ASCII table } else - values[i] = pgm_read_byte(&SevenSeg[string[i] - ASCII_OFFSET]) ; + values[i] = pgm_read_byte(&SevenSeg[string[i] - TM_ASCII_OFFSET]) ; } else { done = true; @@ -133,10 +133,10 @@ void TM1638plus_Model2::DisplayStr(const char* string, const word dots) void TM1638plus_Model2::ASCIItoSegment(const byte values[]) { - for (uint8_t segment = 0; segment < DISPLAY_SIZE; segment++) { + for (uint8_t segment = 0; segment < TM_DISPLAY_SIZE; segment++) { uint8_t SegmentValue = 0; - for (uint8_t j = 0; j < DISPLAY_SIZE; j++) { - SegmentValue |= ((values[j] >> segment) & 1) << (DISPLAY_SIZE - j - 1); + for (uint8_t j = 0; j < TM_DISPLAY_SIZE; j++) { + SegmentValue |= ((values[j] >> segment) & 1) << (TM_DISPLAY_SIZE - j - 1); } DisplaySegments(segment , SegmentValue); } @@ -146,7 +146,7 @@ unsigned char TM1638plus_Model2::ReadKey16() { unsigned char c[4], i, key_value=0; digitalWrite(_STROBE_IO, LOW); - shiftOut(_DATA_IO, _CLOCK_IO, MSBFIRST, BUTTONS_MODE); + sendData(TM_BUTTONS_MODE); pinMode(_DATA_IO, INPUT); for (i = 0; i < 4; i++) { @@ -154,7 +154,7 @@ unsigned char TM1638plus_Model2::ReadKey16() if (_HIGH_FREQ == false) c[i] = shiftIn(_DATA_IO, _CLOCK_IO, LSBFIRST); else - c[i] = HighFreqshiftin(_DATA_IO, _CLOCK_IO, LSBFIRST); + c[i] = TM_common.HighFreqshiftin(_DATA_IO, _CLOCK_IO, LSBFIRST); if (c[i] == 0x04) key_value = 1 + (2*i); //00000100 4 0x04 if (c[i] == 0x40) key_value = 2 + (2*i); //01000000 64 0x40 @@ -169,21 +169,35 @@ unsigned char TM1638plus_Model2::ReadKey16() // 8,16 7,15 6,14 5,13 4,12 3,11 2,10 1,9 :button value } -uint8_t TM1638plus_Model2::HighFreqshiftin(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) + uint16_t TM1638plus_Model2::ReadKey16Two() { - uint8_t value = 0; - uint8_t i = 0; - - for(i = 0; i < 8; ++i) { - if(bitOrder == LSBFIRST) - value |= digitalRead(dataPin) << i; - else - value |= digitalRead(dataPin) << (7 - i); - - digitalWrite(clockPin, HIGH); - delayMicroseconds(1); - digitalWrite(clockPin, LOW); - delayMicroseconds(1); - } - return value; + + uint16_t key_value = 0; + uint8_t Datain ,i = 0; + digitalWrite(_STROBE_IO, LOW); + sendData(TM_BUTTONS_MODE); + pinMode(_DATA_IO, INPUT); + for (i = 0; i < 4; i++) + { + + if (_HIGH_FREQ == false) + Datain = shiftIn(_DATA_IO, _CLOCK_IO, LSBFIRST); + else + Datain = TM_common.HighFreqshiftin(_DATA_IO, _CLOCK_IO, LSBFIRST); + + // turn Datain ABCDEFGI = 0BC00FG0 into 00CG00BF see matrix below + Datain = (((Datain & 0x40) >> 3 | (Datain & 0x04)) >> 2) | (Datain & 0x20) | (Datain & 0x02) << 3; + // i = 0 Datain = 00,10,9,0021 // i = 1 Datain = 00,12,11,0043 + // i = 2 Datain = 00 ,14,13,0065 // i = 3 Datain = 00,16,15,0087 + // key_value = 16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1. + key_value |= ((Datain & 0x000F) << (2*i)) | (((Datain & 0x00F0) << 4) << (2*i)); + } + pinMode(_DATA_IO, OUTPUT); + digitalWrite(_STROBE_IO, HIGH); + + return (key_value); + + // Data matrix for read key_value. c = datain + // c3 0110 0110 c2 0110 0110 c1 0110 0110 c0 0110 0110 :bytes read + // 8,16 7,15 6,14 5,13 4,12 3,11 2,10 1,9 :button value } diff --git a/src/TM1638plus_Model2.h b/src/TM1638plus_Model2.h index aaeb9e1..e79c412 100644 --- a/src/TM1638plus_Model2.h +++ b/src/TM1638plus_Model2.h @@ -8,8 +8,8 @@ */ -#ifndef TM1638plus_Model2_h -#define TM1638plus_Model2_h +#ifndef TM1638PLUS_MODEL2_H +#define TM1638PLUS_MODEL2_H #if (ARDUINO >=100) #include "Arduino.h" @@ -17,18 +17,8 @@ #include "WProgram.h" #endif -#define ACTIVATE_TM 0x8F // Start up -#define BUTTONS_MODE 0x42 // Buttons mode -#define WRITE_LOC 0x44 // Write to a location -#define WRITE_INC 0x40 // Incremental write -#define SEG_ADR 0xC0 // leftmost segment Address C0 C2 C4 C6 C8 CA CC CE -#define BRIGHT_ADR 0x88 // Brightness address -#define BRIGHT_MASK 0x07 // Brightness mask -#define DEFAULT_BRIGHTNESS 0x02 //can be 0x00 to 0x07 - -#define DISPLAY_SIZE 8 //size of display -#define ASCII_OFFSET 32 // Ascii table offset to jump over first missing 32 chars -#define DOT_MASK_DEC 128 // 0x80 Mask to switch on decimal point in seven seg. +#include "TM1638plus_common.h" + class TM1638plus_Model2 { @@ -48,30 +38,35 @@ class TM1638plus_Model2 { // Begin method , sets pinmodes , Call in setup void displayBegin(void); - - // Send a command to module - void sendCommand(uint8_t value); - // Send data to the module - void sendData(unsigned char data); + void reset(void); // Reset / Clear module - // Reset / Clear module - void reset(void); - - //Sets the brightness level on a scale of brightness = 0 to 7. - //0 is not turned off, it's just the lowest brightness. - //If user wishes to change the default brightness at start-up change. - //The DEFAULT_BRIGHTNESS define in header file. + // Sets the brightness level on a scale of brightness = 0 to 7. + // 0 is not turned off, it's just the lowest brightness. + // If user wishes to change the default brightness at start-up change. + // The DEFAULT_BRIGHTNESS define in header file. void brightness(uint8_t brightness); - //Read buttons returns a byte with value of buttons 1-16 (1 for S1 2 for S2 ... 16 for S16 ) - // Returns zero if no button pressed. - //User may have to deb-ounce buttons depending on application. - //See [URL LINK](https://github.com/gavinlyonsrepo/Arduino_Clock_3) - //For de-bounce practical example(model 1) - //model 2 example here in ADC file TM1638plus_ADC_TEST_Model2.ino. - unsigned char ReadKey16(void); + // Function Desc:Read push buttons function + // User may have to deb-ounce buttons depending on application. + // See [URL LINK](https://github.com/gavinlyonsrepo/Arduino_Clock_3) + // For de-bounce practical example(model 1) + // model 2 example here in ADC file TM1638plus_ADC_TEST_Model2.ino. + // Note there are two different read button functions See Also ReadKeyTwo + // Returns: zero if no button pressed. + // Returns: a byte with decimal value of buttons 1-16 (1 for S1 2 for S2 ... 16 for S16 ). + uint8_t ReadKey16(void); + // Function Desc: Reads the push buttons: alternate function to ReadKeys16 + // Can detect multiple key presses , See notes section in readme regarding + // Problems with seven segment display when pressing certain keys in combination + // Returns: A 16 bit integer where each bit correpsond to a switch + // Eg: + // S1 = 0x0001 + // S16 = 0x8000 + // S16 + S1 together = 0x8001 + uint16_t ReadKey16Two(void); + // Send seven segment value module // pass segment 0-7, byte of data corresponding to segments abcdefg(dp) 01234567. // a is 0 , dp is 7 , segmentValue is which segments are off or on for each digit. d7d6d5d54d3d2d1 @@ -111,16 +106,16 @@ class TM1638plus_Model2 { private: + + void sendCommand(uint8_t value); + void sendData(uint8_t data); uint8_t _STROBE_IO; uint8_t _DATA_IO; uint8_t _CLOCK_IO; - bool _SWAP_NIBBLES; - - // Used instead of arduino function "shiftin" when _HIGH_FREQ is set to true - uint8_t HighFreqshiftin(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) ; - + bool _SWAP_NIBBLES= false; //This is used when running high freq CPU because of issues with button function. bool _HIGH_FREQ = false; + TM1638plus_common TM_common; }; #endif diff --git a/src/TM1638plus_common.cpp b/src/TM1638plus_common.cpp new file mode 100644 index 0000000..f0accc1 --- /dev/null +++ b/src/TM1638plus_common.cpp @@ -0,0 +1,52 @@ +/* +* Project Name: TM1638plus +* File: TM1638plus_common +* Description: cpp file for common data and functions between model 1 and 2 classes +* Arduino library TM1638plus +* Author: Gavin Lyons. +* URL: https://github.com/gavinlyonsrepo/TM1638plus +*/ + +#include "TM1638plus_common.h" + + +TM1638plus_common::TM1638plus_common() +{ + // Blank constructor +} + +uint8_t TM1638plus_common::HighFreqshiftin(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) +{ + uint8_t value = 0; + uint8_t i = 0; + + for(i = 0; i < 8; ++i) { + if(bitOrder == LSBFIRST) + value |= digitalRead(dataPin) << i; + else + value |= digitalRead(dataPin) << (7 - i); + + digitalWrite(clockPin, HIGH); + delayMicroseconds(1); + digitalWrite(clockPin, LOW); + delayMicroseconds(1); + } + return value; +} + +void TM1638plus_common::HighFreqshiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val) +{ + uint8_t i; + + for (i = 0; i < 8; i++) { + if (bitOrder == LSBFIRST) + digitalWrite(dataPin, !!(val & (1 << i))); + else + digitalWrite(dataPin, !!(val & (1 << (7 - i)))); + + digitalWrite(clockPin, HIGH); + delayMicroseconds(1); + digitalWrite(clockPin, LOW); + delayMicroseconds(1); + } +} diff --git a/src/TM1638plus_common.h b/src/TM1638plus_common.h new file mode 100644 index 0000000..36a7d54 --- /dev/null +++ b/src/TM1638plus_common.h @@ -0,0 +1,152 @@ +/* +* Project Name: TM1638plus +* File: TM1638plus_common.h +* Description: header file for common data and functions between model 1 and 2 classes +* Arduino library TM1638plus +* Author: Gavin Lyons. +* URL: https://github.com/gavinlyonsrepo/TM1638plus +*/ + +#ifndef TM1638PLUS_COMMON_H +#define TM1638PLUS_COMMON_H + +#if (ARDUINO >=100) + #include "Arduino.h" +#else + #include "WProgram.h" +#endif + +#define TM_ACTIVATE 0x8F // Start up +#define TM_BUTTONS_MODE 0x42 // Buttons mode +#define TM_WRITE_LOC 0x44 // Write to a location +#define TM_WRITE_INC 0x40 // Incremental write +#define TM_SEG_ADR 0xC0 // leftmost segment Address C0 C2 C4 C6 C8 CA CC CE +#define TM_LEDS_ADR 0xC1 // Leftmost LED address C1 C3 C5 C7 C9 CB CD CF +#define TM_BRIGHT_ADR 0x88 // Brightness address +#define TM_BRIGHT_MASK 0x07 // Brightness mask +#define TM_DEFAULT_BRIGHTNESS 0x02 //can be 0x00 to 0x07 +#define TM_DISPLAY_SIZE 8 //size of display + +#define TM_ASCII_OFFSET 32 // Ascii table offset to jump over first missing 32 chars +#define TM_HEX_OFFSET 16 // Ascii table offset to reach number position +#define TM_DOT_MASK_DEC 128 // 0x80 Mask to switch on decimal point in seven seg. + +#define TM_RED_LED 0x02 // Model 3 +#define TM_GREEN_LED 0x01 // Model 3 +#define TM_OFF_LED 0x00 + +// font , map of ASCII values/table to 7-segment, offset to position 32. +const PROGMEM unsigned char SevenSeg[] = { + 0x00, /* (space) */ + 0x86, /* ! */ + 0x22, /* " */ + 0x7E, /* # */ + 0x6D, /* $ */ + 0xD2, /* % */ + 0x46, /* & */ + 0x20, /* ' */ + 0x29, /* ( */ + 0x0B, /* ) */ + 0x21, /* * */ + 0x70, /* + */ + 0x10, /* , */ + 0x40, /* - */ + 0x80, /* . */ + 0x52, /* / */ + 0x3F, /* 0 */ + 0x06, /* 1 */ + 0x5B, /* 2 */ + 0x4F, /* 3 */ + 0x66, /* 4 */ + 0x6D, /* 5 */ + 0x7D, /* 6 */ + 0x07, /* 7 */ + 0x7F, /* 8 */ + 0x6F, /* 9 */ + 0x09, /* : */ + 0x0D, /* ; */ + 0x61, /* < */ + 0x48, /* = */ + 0x43, /* > */ + 0xD3, /* ? */ + 0x5F, /* @ */ + 0x77, /* A */ + 0x7C, /* B */ + 0x39, /* C */ + 0x5E, /* D */ + 0x79, /* E */ + 0x71, /* F */ + 0x3D, /* G */ + 0x76, /* H */ + 0x30, /* I */ + 0x1E, /* J */ + 0x75, /* K */ + 0x38, /* L */ + 0x15, /* M */ + 0x37, /* N */ + 0x3F, /* O */ + 0x73, /* P */ + 0x6B, /* Q */ + 0x33, /* R */ + 0x6D, /* S */ + 0x78, /* T */ + 0x3E, /* U */ + 0x3E, /* V */ + 0x2A, /* W */ + 0x76, /* X */ + 0x6E, /* Y */ + 0x5B, /* Z */ + 0x39, /* [ */ + 0x64, /* \ */ + 0x0F, /* ] */ + 0x23, /* ^ */ + 0x08, /* _ */ + 0x02, /* ` */ + 0x5F, /* a */ + 0x7C, /* b */ + 0x58, /* c */ + 0x5E, /* d */ + 0x7B, /* e */ + 0x71, /* f */ + 0x6F, /* g */ + 0x74, /* h */ + 0x10, /* i */ + 0x0C, /* j */ + 0x75, /* k */ + 0x30, /* l */ + 0x14, /* m */ + 0x54, /* n */ + 0x5C, /* o */ + 0x73, /* p */ + 0x67, /* q */ + 0x50, /* r */ + 0x6D, /* s */ + 0x78, /* t */ + 0x1C, /* u */ + 0x1C, /* v */ + 0x14, /* w */ + 0x76, /* x */ + 0x6E, /* y */ + 0x5B, /* z */ + // Note : Removed last 4 characters to reduce program size as of v 1.3.0 +// 0x46, /* { */ +// 0x30, /* | */ +// 0x70, /* } */ +// 0x01, /* ~ */ +}; + +// Class for some common functions +class TM1638plus_common{ + +public: + // Constructor + TM1638plus_common(); + + // Used instead of arduino function "shiftin" when _HIGH_FREQ is set to true + uint8_t HighFreqshiftin(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) ; + // Used instead of arduino function "shiftOut" when _HIGH_FREQ is set to true + void HighFreqshiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val); + +}; + +#endif diff --git a/src/TM1638plus_font.h b/src/TM1638plus_font.h deleted file mode 100644 index 831cd4c..0000000 --- a/src/TM1638plus_font.h +++ /dev/null @@ -1,109 +0,0 @@ -/* -* Project Name: TM1638plus -* File: TM1638plus_font.h -* Description:font file for Arduino library TM1638plus -* Author: Gavin Lyons. -* URL: https://github.com/gavinlyonsrepo/TM1638plus -*/ - -// Note : Removed last 4 characters to reduce program size as of v 1.3.0 -// 0x46, /* { */ -// 0x30, /* | */ -// 0x70, /* } */ -// 0x01, /* ~ */ - -// map of ASCII values/table to 7-segment, offset to position 32. -const PROGMEM unsigned char SevenSeg[] = { - 0x00, /* (space) */ - 0x86, /* ! */ - 0x22, /* " */ - 0x7E, /* # */ - 0x6D, /* $ */ - 0xD2, /* % */ - 0x46, /* & */ - 0x20, /* ' */ - 0x29, /* ( */ - 0x0B, /* ) */ - 0x21, /* * */ - 0x70, /* + */ - 0x10, /* , */ - 0x40, /* - */ - 0x80, /* . */ - 0x52, /* / */ - 0x3F, /* 0 */ - 0x06, /* 1 */ - 0x5B, /* 2 */ - 0x4F, /* 3 */ - 0x66, /* 4 */ - 0x6D, /* 5 */ - 0x7D, /* 6 */ - 0x07, /* 7 */ - 0x7F, /* 8 */ - 0x6F, /* 9 */ - 0x09, /* : */ - 0x0D, /* ; */ - 0x61, /* < */ - 0x48, /* = */ - 0x43, /* > */ - 0xD3, /* ? */ - 0x5F, /* @ */ - 0x77, /* A */ - 0x7C, /* B */ - 0x39, /* C */ - 0x5E, /* D */ - 0x79, /* E */ - 0x71, /* F */ - 0x3D, /* G */ - 0x76, /* H */ - 0x30, /* I */ - 0x1E, /* J */ - 0x75, /* K */ - 0x38, /* L */ - 0x15, /* M */ - 0x37, /* N */ - 0x3F, /* O */ - 0x73, /* P */ - 0x6B, /* Q */ - 0x33, /* R */ - 0x6D, /* S */ - 0x78, /* T */ - 0x3E, /* U */ - 0x3E, /* V */ - 0x2A, /* W */ - 0x76, /* X */ - 0x6E, /* Y */ - 0x5B, /* Z */ - 0x39, /* [ */ - 0x64, /* \ */ - 0x0F, /* ] */ - 0x23, /* ^ */ - 0x08, /* _ */ - 0x02, /* ` */ - 0x5F, /* a */ - 0x7C, /* b */ - 0x58, /* c */ - 0x5E, /* d */ - 0x7B, /* e */ - 0x71, /* f */ - 0x6F, /* g */ - 0x74, /* h */ - 0x10, /* i */ - 0x0C, /* j */ - 0x75, /* k */ - 0x30, /* l */ - 0x14, /* m */ - 0x54, /* n */ - 0x5C, /* o */ - 0x73, /* p */ - 0x67, /* q */ - 0x50, /* r */ - 0x6D, /* s */ - 0x78, /* t */ - 0x1C, /* u */ - 0x1C, /* v */ - 0x14, /* w */ - 0x76, /* x */ - 0x6E, /* y */ - 0x5B, /* z */ -}; -