Skip to content

Commit 8f8e4cc

Browse files
author
Eric
committed
v1.1.0
1 parent 89ac6fc commit 8f8e4cc

File tree

6 files changed

+128
-22
lines changed

6 files changed

+128
-22
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
Search for this library in the Arduino Library Manager and download it or clone it yourself from Github.
66

77
This library is built on TFT_eSPI. This works properly on all color displays supported by TFT_eSPI.
8-
8+
9+
# Updates
10+
11+
- v1.1.0
12+
- Added the function to generate a random key
13+
- Added switching to red color for one minute if it has a random key
14+
- Added the example, DEMO_Generating_Random_Key
15+
916
# License
1017

1118
This software is written by Eric Nam and is licensed under The MIT License. Check License.txt for more information.

examples/DEMO/DEMO.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ void setup()
1515

1616
void loop()
1717
{
18-
digitalRainAnim.play();
18+
digitalRainAnim.loop();
1919
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <TFT_eSPI.h>
2+
#include <SPI.h>
3+
#include <DigitalRainAnim.h>
4+
TFT_eSPI tft = TFT_eSPI();
5+
6+
DigitalRainAnim digitalRainAnim = DigitalRainAnim();
7+
8+
void setup()
9+
{
10+
Serial.begin(115200);
11+
Serial.println();
12+
xTaskCreate(task_display,"task_display", 2048, NULL, 1, NULL);
13+
xTaskCreate(task_input, "task_input", 1024, NULL, 1, NULL);
14+
}
15+
16+
void task_display(void *pvParameter)
17+
{
18+
tft.begin();
19+
tft.setRotation(0);
20+
tft.fillScreen(TFT_BLACK);
21+
digitalRainAnim.init(&tft);
22+
23+
while(true)
24+
{
25+
digitalRainAnim.loop();
26+
vTaskDelay(1);
27+
}
28+
}
29+
30+
void task_input(void *pvParameter)
31+
{
32+
while(true){
33+
while(Serial.available() > 0){
34+
String str = Serial.readString();
35+
if(str.indexOf("key?") > -1){
36+
keyMode();
37+
}else{
38+
normalMode();
39+
}
40+
}
41+
vTaskDelay(10);
42+
}
43+
}
44+
45+
void keyMode(){
46+
String newKey = digitalRainAnim.getKey(0).c_str();
47+
Serial.println(newKey);
48+
}
49+
50+
void normalMode(){
51+
digitalRainAnim.resetKey();
52+
}
53+
54+
void loop() {}

examples/DEMOWithButton2/DEMOWithButton2.ino renamed to examples/DEMO_with_Button2/DEMO_with_Button2.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void setup()
2424

2525
void loop()
2626
{
27-
digitalRainAnim.play();
27+
digitalRainAnim.loop();
2828
rButton.loop();
2929
lButton.loop();
3030
}

src/DigitalRainAnim.cpp

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ void DigitalRainAnim::prepareAnim()
5151
}
5252

5353
isPlaying = true;
54+
lastUpdatedKeyTime = millis() - timeFrame;
5455
}
5556

5657
//updating each line with a new length, Y position, and speed.
@@ -61,21 +62,31 @@ void DigitalRainAnim::lineUpdate(int lineNum){
6162
}
6263

6364
//while moving vertically, the color value changes and the character changes as well.
65+
//if a random key is generated, switch to red.
6466
void DigitalRainAnim::lineAnimation(int lineNum)
6567
{
6668
int startX = lineNum * LINE_WIDTH;
67-
tft_DigitalRainAnim->fillRect(startX, 0, 10, 240,TFT_BLACK);
68-
6969
int currentY = 0;
70+
tft_DigitalRainAnim->fillRect(startX, 0, 10, height, TFT_BLACK);
71+
72+
bool isKeyMode = keyString.length() > 0;
73+
7074
for(int i=0; i<line_length[lineNum]; i++){
71-
int greenVal = map(i, 0, line_length[lineNum], 25, 255);
72-
tft_DigitalRainAnim->setTextColor(tft_DigitalRainAnim->color565(0, greenVal, 0), TFT_BLACK);
75+
int colorVal = map(i, 0, line_length[lineNum], 25, 255);
76+
tft_DigitalRainAnim->setTextColor(isKeyMode ? tft_DigitalRainAnim->color565(colorVal, 0, 0) : tft_DigitalRainAnim->color565(0, colorVal, 0), TFT_BLACK);
7377
tft_DigitalRainAnim->drawString(this->getASCIIChar(), startX, line_pos[lineNum] + currentY, FONT_SIZE);
7478
currentY = (i * LETTER_HEIGHT);
7579
}
7680

7781
tft_DigitalRainAnim->setTextColor(TFT_WHITE, TFT_BLACK);
78-
tft_DigitalRainAnim->drawString(this->getASCIIChar(), startX, line_pos[lineNum] +currentY, FONT_SIZE);
82+
if(keyString.length() > lineNum){
83+
char _char = keyString.at(lineNum);
84+
const char *keyChar = &_char;
85+
tft_DigitalRainAnim->drawString(keyChar, startX, line_pos[lineNum] + currentY, FONT_SIZE);
86+
}else{
87+
tft_DigitalRainAnim->drawString(this->getASCIIChar(), startX, line_pos[lineNum] + currentY, FONT_SIZE);
88+
}
89+
7990
line_pos[lineNum] += line_speed[lineNum];
8091

8192
if (line_pos[lineNum] >= height){
@@ -84,8 +95,12 @@ void DigitalRainAnim::lineAnimation(int lineNum)
8495
}
8596

8697
//a function where animation continues to run.
87-
void DigitalRainAnim::play(){
98+
void DigitalRainAnim::loop(){
8899
uint32_t currentTime = millis();
100+
if (((currentTime - lastUpdatedKeyTime) > KEY_RESET_TIME)) {
101+
this->resetKey();
102+
}
103+
89104
if (((currentTime - lastDrawTime) < timeFrame)) {
90105
return;
91106
}
@@ -110,6 +125,12 @@ String DigitalRainAnim::getASCIIChar()
110125
return String((char)(random(33, 127)));
111126
}
112127

128+
//a function that gets only alphabets from ASCII code.
129+
String DigitalRainAnim::getAbcASCIIChar()
130+
{
131+
return String((char)(random(0,2) == 0 ?random(65, 91) : random(97, 123)));
132+
}
133+
113134
//move the position to start from out of the screen.
114135
int DigitalRainAnim::setYPos(int lineLen){
115136
return lineLen * -20;
@@ -118,4 +139,22 @@ int DigitalRainAnim::setYPos(int lineLen){
118139
//the function is to get the random number (including max)
119140
int DigitalRainAnim::getRandomNum(int min, int max){
120141
return random(min, max+1);
121-
}
142+
}
143+
144+
//the function is to generate a random key with length with a length
145+
std::string DigitalRainAnim::getKey(int key_length){
146+
this->resetKey();
147+
int maxKeyLength =(key_length > 0 ? (key_length > numOfline ? numOfline : key_length) : numOfline);
148+
149+
for(int i = 0; i < maxKeyLength; i++){
150+
keyString.append((this->getAbcASCIIChar()).c_str());
151+
}
152+
153+
return keyString;
154+
}
155+
156+
//the function is to remove the generated key
157+
void DigitalRainAnim::resetKey(){
158+
keyString = "";
159+
lastUpdatedKeyTime = millis();
160+
}

src/DigitalRainAnim.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,22 @@
1010
#include <TFT_eSPI.h>
1111
#include <SPI.h>
1212

13-
#define FONT_SIZE 2 //set font size 2
14-
#define LINE_WIDTH 10 //width for font size 2
15-
#define LETTER_HEIGHT 14 //height for font size 2
13+
#define FONT_SIZE 2 //set font size 2
14+
#define LINE_WIDTH 10 //width for font size 2
15+
#define LETTER_HEIGHT 14 //height for font size 2
16+
#define KEY_RESET_TIME 60 * 1000 //1 Min reset time
1617

1718
class DigitalRainAnim
1819
{
1920
public:
2021
DigitalRainAnim();
21-
void init(TFT_eSPI* tft); //initialization
22+
void init(TFT_eSPI* tft); //initialization
2223
void init(TFT_eSPI* tft, int new_line_len_min, int new_line_len_max, int new_line_speed_min, int new_line_speed_max, int new_timeFrame); //initialization with parameters
23-
void play(); //play animation
24-
void pause(); //pause animation
25-
void resume(); //resume animation
24+
void loop(); //loop animation
25+
void pause(); //pause animation
26+
void resume(); //resume animation
27+
std::string getKey(int key_length); //generating new key
28+
void resetKey(); //make key empty
2629

2730
private:
2831
TFT_eSPI* tft_DigitalRainAnim; //target display TFT_eSPI
@@ -35,15 +38,18 @@ class DigitalRainAnim
3538
bool isPlaying; //boolean for play or pause
3639
int timeFrame; //time frame for drawing
3740
uint32_t lastDrawTime; //checking last drawing time
41+
uint32_t lastUpdatedKeyTime; //checking last generating key time
3842
std::vector<int> line_length; //dynamic array for each line of vertical length
3943
std::vector<int> line_pos; //dynamic array for eacg line Y position
4044
std::vector<int> line_speed; //dynamic array for eacg line speed
45+
std::string keyString; //storing generated key
4146

42-
void prepareAnim(); //the function is to prepare all lines at the beginning
43-
void lineUpdate(int lineNum); //the function is to update each line after disappeared from the screen
44-
void lineAnimation(int lineNum);//the function is to update each line
45-
String getASCIIChar(); //the function is to get the random ASCII char
46-
int setYPos(int lineLen); //the function is to update the position of line
47+
void prepareAnim(); //the function is to prepare all lines at the beginning
48+
void lineUpdate(int lineNum); //the function is to update each line after disappeared from the screen
49+
void lineAnimation(int lineNum); //the function is to update each line
50+
String getASCIIChar(); //the function is to get the random ASCII char
51+
String getAbcASCIIChar(); //the function is to get only alphabet lowercase and uppercase
52+
int setYPos(int lineLen); //the function is to update the position of line
4753
int getRandomNum(int min, int max); //the function is to get the random number (including max)
4854
};
4955

0 commit comments

Comments
 (0)