Skip to content

Commit b5db7d1

Browse files
committed
Adding support for forcing refresh
Added some examples for ESP32
1 parent 266f09d commit b5db7d1

File tree

10 files changed

+416
-95
lines changed

10 files changed

+416
-95
lines changed

README.md

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,62 @@ An Arduino library for drawing letters and numbers using a falling block style a
66

77
Tested using ESP8266 & ESP32 on RGB Led matrixes (using the [PxMatrix library](https://github.com/2dom/PxMatrix/)), but potentially works on any display that uses Adafruit GFX library
88

9+
## Installation & Setup
10+
11+
Search for "Tetris Animation" on the Arduino library manager
12+
13+
If you are using an RGB LED Matrix, Install the "PXMatrix" library from the library manager. This will also require the Adafruit GFX library.
14+
915
## Basic Usage
1016

1117
See examples for more details.
1218

19+
### Intialise library by passing in a display:
20+
1321
```
14-
//Intialise library by passing in a display
15-
PxMATRIX display(64, 32, P_LAT, P_OE, P_A, P_B, P_C, P_D, P_E);
16-
TetrisMatrixDraw tetris(display);
22+
PxMATRIX display(64, 32, P_LAT, P_OE, P_A, P_B, P_C, P_D, P_E); //Intialise any display that makes use of Adafruit GFX
23+
TetrisMatrixDraw tetris(display); //Pass it into the library
24+
25+
```
26+
27+
### Set the value:
1728

18-
// Set the value of the library by using setTime, setNumbers or setText
29+
Set the value of the library by using one of: setTime, setNumbers or setText commands
1930

20-
// Usage: setTime(time_string)
31+
```
32+
// Usage: setTime(time_string, forceRefresh = false)
2133
// time_string = time in the format "21:23"
34+
// forceRefresh: by default, a digit will only redraw if it's value has changed
35+
// (so for a clock the hour digit would only draw once an hour)
36+
// but setting this value to true tells the library to redraw
37+
// all the digits.
2238
tetris.setTime("12:34");
2339
24-
// Usage: setNumbers(num)
40+
// Usage: setNumbers(num, forceRefresh = false)
2541
// num = Integer value. Max: 999999999 (9 digits long) Min: 0 (negative not currently supported).
42+
// forceRefresh: by default, a digit will only redraw if it's value has changed
43+
// (so for a clock the hour digit would only draw once an hour)
44+
// but setting this value to true tells the library to redraw
45+
// all the digits.
2646
tetris.setNumbers(1234);
2747
28-
// Usage: setText(string)
48+
// Usage: setText(string, forceRefresh = false)
2949
// string = regular text string, Use uppercase letters only.
50+
// forceRefresh: by default, a digit will only redraw if it's value has changed
51+
// (so for a clock the hour digit would only draw once an hour)
52+
// but setting this value to true tells the library to redraw
53+
// all the digits.
54+
//
3055
// For full list of characters supported, check the AlphaTest examples.
3156
tetris.setText("HOWDY!");
3257
58+
```
3359

34-
// Draw the value
60+
### Draw the value:
3561

36-
// These will normally be called in a timer or ticker (see any example)
37-
// How often they are called will increase/decrease the speed of which
38-
// the blocks drop.
62+
These will normally be called in a timer or ticker (see any example). How often they are called will increase/decrease the speed of which the blocks drop.
63+
64+
```
3965
4066
// Usage: drawNumbers(x, y, showColon) (for use with setTime or setNumber)
4167
// x = most left Pixel of the text
@@ -54,9 +80,11 @@ tetris.drawNumbers(16,8, true);
5480
// (will return false if there is still falling blocks)
5581
tetris.drawText(16,8);
5682
83+
```
5784

58-
// Scale
85+
### Scale the font:
5986

87+
```
6088
// Usage: scale = 2
6189
// Will scale up the size of the characters
6290
// Can be used with either numbers or text
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*******************************************************************
2+
Example showing writing text using the TetrisAnimation
3+
library for the ESP32.
4+
* *
5+
Written by Brian Lough (witnessmenow on Github)
6+
7+
*******************************************************************/
8+
9+
// ----------------------------
10+
// Standard Libraries - Already Installed if you have ESP32 set up
11+
// ----------------------------
12+
13+
// ----------------------------
14+
// Additional Libraries - each one of these will need to be installed.
15+
// ----------------------------
16+
17+
#include <PxMatrix.h>
18+
// The library for controlling the LED Matrix
19+
// Can be installed from the library manager
20+
// https://github.com/2dom/PxMatrix
21+
22+
// Adafruit GFX library is a dependancy for the PxMatrix Library
23+
// Can be installed from the library manager
24+
// https://github.com/adafruit/Adafruit-GFX-Library
25+
26+
#include <TetrisMatrixDraw.h>
27+
// This library!
28+
// https://github.com/toblum/TetrisAnimation
29+
30+
31+
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
32+
33+
hw_timer_t * displayTimer = NULL;
34+
hw_timer_t * animationTimer = NULL;
35+
36+
// Pins for LED MATRIX to the ESP32
37+
#define P_LAT 22
38+
#define P_A 19
39+
#define P_B 23
40+
#define P_C 18
41+
#define P_D 5
42+
#define P_E 15
43+
#define P_OE 2
44+
45+
// PxMATRIX display(32,16,P_LAT, P_OE,P_A,P_B,P_C);
46+
// PxMATRIX display(64,32,P_LAT, P_OE,P_A,P_B,P_C,P_D);
47+
PxMATRIX display(64, 32, P_LAT, P_OE, P_A, P_B, P_C, P_D, P_E);
48+
49+
// Some standard colors
50+
uint16_t myRED = display.color565(255, 0, 0);
51+
uint16_t myGREEN = display.color565(0, 255, 0);
52+
uint16_t myBLUE = display.color565(0, 0, 255);
53+
uint16_t myWHITE = display.color565(255, 255, 255);
54+
uint16_t myYELLOW = display.color565(255, 255, 0);
55+
uint16_t myCYAN = display.color565(0, 255, 255);
56+
uint16_t myMAGENTA = display.color565(255, 0, 255);
57+
uint16_t myBLACK = display.color565(0, 0, 0);
58+
59+
uint16_t myCOLORS[8] = {myRED, myGREEN, myBLUE, myWHITE, myYELLOW, myCYAN, myMAGENTA, myBLACK};
60+
61+
TetrisMatrixDraw tetris(display);
62+
63+
bool showColon = true;
64+
65+
#define ANIMATION_TIME 50000 // Controls the speed in which the blocks update,
66+
// increase this number to make it slower
67+
68+
void IRAM_ATTR display_updater() {
69+
// Increment the counter and set the time of ISR
70+
portENTER_CRITICAL_ISR(&timerMux);
71+
display.display(70);
72+
portEXIT_CRITICAL_ISR(&timerMux);
73+
}
74+
75+
void animationHandler()
76+
{
77+
portENTER_CRITICAL_ISR(&timerMux);
78+
display.clearDisplay();
79+
tetris.drawText(5, 20);
80+
portEXIT_CRITICAL_ISR(&timerMux);
81+
}
82+
83+
84+
void drawIntro(int x = 0, int y = 0)
85+
{
86+
tetris.drawChar("T", x, y, myCYAN);
87+
tetris.drawChar("e", x + 5, y, myMAGENTA);
88+
tetris.drawChar("t", x + 11, y, myYELLOW);
89+
tetris.drawChar("r", x + 17, y, myGREEN);
90+
tetris.drawChar("i", x + 22, y, myBLUE);
91+
tetris.drawChar("s", x + 27, y, myRED);
92+
93+
tetris.drawChar("L", x - 2, y + 9, myRED);
94+
tetris.drawChar("e", x + 5, y + 9, myMAGENTA);
95+
tetris.drawChar("t", x + 11, y + 9, myYELLOW);
96+
tetris.drawChar("t", x + 17, y + 9, myGREEN);
97+
tetris.drawChar("e", x + 23, y + 9, myBLUE);
98+
tetris.drawChar("r", x + 29, y + 9, myRED);
99+
tetris.drawChar("s", x + 37, y + 9, myMAGENTA);
100+
}
101+
102+
void setup() {
103+
Serial.begin(115200);
104+
display.begin(16);
105+
display.clearDisplay();
106+
107+
displayTimer = timerBegin(0, 80, true);
108+
timerAttachInterrupt(displayTimer, &display_updater, true);
109+
timerAlarmWrite(displayTimer, 2000, true);
110+
timerAlarmEnable(displayTimer);
111+
112+
yield();
113+
display.clearDisplay();
114+
115+
drawIntro(10, 10);
116+
117+
delay(2000);
118+
119+
// Must use upercase characters
120+
// See below for all supported chars
121+
tetris.setText("HELLO!");
122+
123+
animationTimer = timerBegin(1, 80, true);
124+
timerAttachInterrupt(animationTimer, &animationHandler, true);
125+
timerAlarmWrite(animationTimer, ANIMATION_TIME, true);
126+
timerAlarmEnable(animationTimer);
127+
128+
129+
delay(5000);
130+
tetris.setText("!#$%&'()");
131+
132+
delay(5000);
133+
tetris.setText("*+,-./");
134+
135+
delay(5000);
136+
tetris.setText("01234567");
137+
138+
delay(5000);
139+
tetris.setText("89:;<=>?");
140+
141+
delay(5000);
142+
tetris.setText("@ABCDEFG");
143+
144+
delay(5000);
145+
tetris.setText("HIJKLMNO");
146+
147+
delay(5000);
148+
tetris.setText("PQRSTUVW");
149+
150+
delay(5000);
151+
tetris.setText("XYZ");
152+
153+
154+
}
155+
156+
157+
void loop() {
158+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*******************************************************************
2+
Example that displays numbers using an ESP32
3+
* *
4+
Written by Brian Lough (witnessmenow)
5+
https://www.youtube.com/channel/UCezJOfu7OtqGzd5xrP3q6WA
6+
*******************************************************************/
7+
8+
// ----------------------------
9+
// Standard Libraries - Already Installed if you have ESP32 set up
10+
// ----------------------------
11+
12+
// ----------------------------
13+
// Additional Libraries - each one of these will need to be installed.
14+
// ----------------------------
15+
16+
#include <PxMatrix.h>
17+
// The library for controlling the LED Matrix
18+
// Can be installed from the library manager
19+
// https://github.com/2dom/PxMatrix
20+
21+
// Adafruit GFX library is a dependancy for the PxMatrix Library
22+
// Can be installed from the library manager
23+
// https://github.com/adafruit/Adafruit-GFX-Library
24+
25+
#include <TetrisMatrixDraw.h>
26+
// This library!
27+
// https://github.com/toblum/TetrisAnimation
28+
29+
30+
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
31+
32+
hw_timer_t * displayTimer = NULL;
33+
hw_timer_t * animationTimer = NULL;
34+
35+
// Pins for LED MATRIX to the ESP32
36+
#define P_LAT 22
37+
#define P_A 19
38+
#define P_B 23
39+
#define P_C 18
40+
#define P_D 5
41+
#define P_E 15
42+
#define P_OE 2
43+
44+
// PxMATRIX display(32,16,P_LAT, P_OE,P_A,P_B,P_C);
45+
// PxMATRIX display(64,32,P_LAT, P_OE,P_A,P_B,P_C,P_D);
46+
PxMATRIX display(64, 32, P_LAT, P_OE, P_A, P_B, P_C, P_D, P_E);
47+
48+
// Some standard colors
49+
uint16_t myRED = display.color565(255, 0, 0);
50+
uint16_t myGREEN = display.color565(0, 255, 0);
51+
uint16_t myBLUE = display.color565(0, 0, 255);
52+
uint16_t myWHITE = display.color565(255, 255, 255);
53+
uint16_t myYELLOW = display.color565(255, 255, 0);
54+
uint16_t myCYAN = display.color565(0, 255, 255);
55+
uint16_t myMAGENTA = display.color565(255, 0, 255);
56+
uint16_t myBLACK = display.color565(0, 0, 0);
57+
58+
uint16_t myCOLORS[8] = {myRED, myGREEN, myBLUE, myWHITE, myYELLOW, myCYAN, myMAGENTA, myBLACK};
59+
60+
TetrisMatrixDraw tetris(display);
61+
62+
#define ANIMATION_TIME 50000 // Controls the speed in which the blocks update,
63+
// increase this number to make it slower
64+
65+
void IRAM_ATTR display_updater() {
66+
// Increment the counter and set the time of ISR
67+
portENTER_CRITICAL_ISR(&timerMux);
68+
display.display(70);
69+
portEXIT_CRITICAL_ISR(&timerMux);
70+
}
71+
72+
void animationHandler()
73+
{
74+
portENTER_CRITICAL_ISR(&timerMux);
75+
display.clearDisplay();
76+
tetris.drawNumbers(0,24);
77+
portEXIT_CRITICAL_ISR(&timerMux);
78+
}
79+
80+
void drawIntro(int x = 0, int y = 0)
81+
{
82+
tetris.drawChar("T", x, y, myCYAN);
83+
tetris.drawChar("e", x + 5, y, myMAGENTA);
84+
tetris.drawChar("t", x + 11, y, myYELLOW);
85+
tetris.drawChar("r", x + 17, y, myGREEN);
86+
tetris.drawChar("i", x + 22, y, myBLUE);
87+
tetris.drawChar("s", x + 27, y, myRED);
88+
89+
tetris.drawChar("T", x + 6, y + 9, myRED);
90+
tetris.drawChar("i", x + 11, y + 9, myWHITE);
91+
tetris.drawChar("m", x + 16, y + 9, myCYAN);
92+
tetris.drawChar("e", x + 22, y + 9, myMAGENTA);
93+
}
94+
95+
void setup() {
96+
Serial.begin(115200);
97+
display.begin(16);
98+
display.clearDisplay();
99+
100+
displayTimer = timerBegin(0, 80, true);
101+
timerAttachInterrupt(displayTimer, &display_updater, true);
102+
timerAlarmWrite(displayTimer, 2000, true);
103+
timerAlarmEnable(displayTimer);
104+
105+
yield();
106+
display.clearDisplay();
107+
108+
drawIntro(10, 10);
109+
delay(2000);
110+
111+
tetris.setNumbers(123456789);
112+
113+
animationTimer = timerBegin(1, 80, true);
114+
timerAttachInterrupt(animationTimer, &animationHandler, true);
115+
timerAlarmWrite(animationTimer, ANIMATION_TIME, true);
116+
timerAlarmEnable(animationTimer);
117+
118+
delay(10000);
119+
120+
// Scale will make each tetris block bigger
121+
// It needs to be called before you do tetris.setNumbers
122+
// It will persist for future setNumbers
123+
tetris.scale = 2; // Make blocks double the size
124+
125+
tetris.setNumbers(1234);
126+
}
127+
128+
129+
void loop() {
130+
}

0 commit comments

Comments
 (0)