Skip to content

Commit 8a18533

Browse files
author
Eric
committed
v1.0.0
0 parents  commit 8a18533

File tree

8 files changed

+287
-0
lines changed

8 files changed

+287
-0
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Digital Rain Animation for TFT_eSPI
2+
3+
A library that represents Digital Rain Animation on color displays that support TFT_eSPI
4+
5+
Search for this library in the Arduino Library Manager and download it or clone it yourself from Github.
6+
7+
This library is built on TFT_eSPI. TFT_eSPI works properly on color displays among the supported displays.
8+

examples/SimpleDraw/SimpleDraw.ino

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
tft.begin();
11+
tft.setRotation(0);
12+
tft.fillScreen(TFT_BLACK);
13+
digitalRainAnim.init(&tft);
14+
}
15+
16+
void loop()
17+
{
18+
digitalRainAnim.play();
19+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <TFT_eSPI.h>
2+
#include <SPI.h>
3+
#include <DigitalRainAnim.h>
4+
#include "Button2.h";
5+
6+
#define RIGHT_BUTTON_PIN 35
7+
#define LEFT_BUTTON_PIN 0
8+
9+
TFT_eSPI tft = TFT_eSPI();
10+
Button2 rButton = Button2(RIGHT_BUTTON_PIN);
11+
Button2 lButton = Button2(LEFT_BUTTON_PIN);
12+
13+
DigitalRainAnim digitalRainAnim = DigitalRainAnim();
14+
15+
void setup()
16+
{
17+
tft.begin();
18+
tft.setRotation(0);
19+
tft.fillScreen(TFT_BLACK);
20+
digitalRainAnim.init(&tft, 3, 20, 3, 20, 60);
21+
rButton.setClickHandler(rClick);
22+
lButton.setClickHandler(lClick);
23+
}
24+
25+
void loop()
26+
{
27+
digitalRainAnim.play();
28+
rButton.loop();
29+
lButton.loop();
30+
}
31+
32+
void rClick(Button2& btn) {
33+
digitalRainAnim.pause();
34+
}
35+
36+
void lClick(Button2& btn) {
37+
digitalRainAnim.resume();
38+
}

keywords.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
###########################################
2+
# A library that represents Digital Rain Animation on color displays that support TFT_eSPI
3+
###########################################
4+
5+
###########################################
6+
# Datatypes (KEYWORD1)
7+
###########################################
8+
9+
Digital_Rain_Animation_for_TFT_eSPI KEYWORD1
10+
11+
###########################################
12+
# Methods and Functions (KEYWORD2)
13+
###########################################
14+
init KEYWORD2
15+
setParams KEYWORD2
16+
play KEYWORD2
17+
pause KEYWORD2
18+
resume KEYWORD2
19+
prepareAnim KEYWORD2
20+
lineUpdate KEYWORD2
21+
lineAnimation KEYWORD2
22+
getASCIIChar KEYWORD2
23+
setYPos KEYWORD2
24+
getRandomNum KEYWORD2
25+
###########################################
26+
# Constants (LITERAL1)
27+
###########################################
28+
LINE_WIDTH LITERAL1
29+
LETTER_HEIGHT LITERAL1

library.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "Digital Rain Animation for TFT_eSPI",
3+
"keywords": "TFT_eSPI, Animation, Matrix, Digital, Rain",
4+
"description": "We love the movie Matrix. Feel the Digital Rain Animation effect in the movie. You can make the matrix effect on your display easily.",
5+
"repository":
6+
{
7+
"type": "git",
8+
"url": "https://github.com/0015/TP_Arduino_DigitalRain_Anim.git"
9+
},
10+
"version": "1.0",
11+
"frameworks": "arduino",
12+
"platforms": "*"
13+
}

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Digital Rain Animation for TFT_eSPI
2+
version=1.0.0
3+
author=Eric Nam
4+
maintainer=Eric Nam <[email protected]>
5+
sentence=A library that represents Digital Rain Animation on color displays that support TFT_eSPI
6+
paragraph=We love the movie Matrix. Feel the Digital Rain Animation effect in the movie. You can make the matrix effect on your display easily.
7+
category=TFT
8+
url=https://github.com/0015/TP_Arduino_DigitalRain_Anim
9+
architectures=*

src/DigitalRainAnim.cpp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
DigitalRainAnim.cpp - Library for Digital Rain Animation for TFT_eSPI.
3+
Created by Eric Nam, December 30, 2020.
4+
Released into the public domain.
5+
*/
6+
7+
#include "DigitalRainAnim.h"
8+
9+
DigitalRainAnim::DigitalRainAnim(){}
10+
11+
//initialze with only tft
12+
void DigitalRainAnim::init(TFT_eSPI* tft){
13+
14+
tft_DigitalRainAnim = tft;
15+
line_len_min = 3;
16+
line_len_max = 20;
17+
line_speed_min = 3;
18+
line_speed_max = 15;
19+
timeFrame = 100;
20+
21+
this->prepareAnim();
22+
}
23+
24+
//initialze with only tft and params
25+
void DigitalRainAnim::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)
26+
{
27+
tft_DigitalRainAnim = tft;
28+
line_len_min = new_line_len_min;
29+
line_len_max = new_line_len_max;
30+
line_speed_min = new_line_speed_min;
31+
line_speed_max = new_line_speed_max;
32+
timeFrame = new_timeFrame;
33+
34+
this->prepareAnim();
35+
}
36+
37+
//checking how many lines it can draw from the width of the screen.
38+
//the size of the array is determined by the number of lines.
39+
void DigitalRainAnim::prepareAnim()
40+
{
41+
lastDrawTime = millis() - timeFrame;
42+
width = tft_DigitalRainAnim->width();
43+
height = tft_DigitalRainAnim->height();
44+
45+
numOfline = width / LINE_WIDTH;
46+
47+
for(int i = 0; i < numOfline; i++){
48+
line_length.push_back(this->getRandomNum(line_len_min, line_len_max));
49+
line_pos.push_back(this->setYPos(line_length[i]));
50+
line_speed.push_back(this->getRandomNum(line_speed_min, line_speed_max));
51+
}
52+
53+
isPlaying = true;
54+
}
55+
56+
//updating each line with a new length, Y position, and speed.
57+
void DigitalRainAnim::lineUpdate(int lineNum){
58+
line_length[lineNum] = this->getRandomNum(line_len_min, line_len_max);
59+
line_pos[lineNum] = this->setYPos(line_length[lineNum]);
60+
line_speed[lineNum] = this->getRandomNum(line_speed_min, line_speed_max);
61+
}
62+
63+
//while moving vertically, the color value changes and the character changes as well.
64+
void DigitalRainAnim::lineAnimation(int lineNum)
65+
{
66+
int startX = lineNum * LINE_WIDTH;
67+
tft_DigitalRainAnim->fillRect(startX, 0, 10, 240,TFT_BLACK);
68+
69+
int currentY = 0;
70+
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);
73+
tft_DigitalRainAnim->drawString(this->getASCIIChar(), startX, line_pos[lineNum] + currentY, FONT_SIZE);
74+
currentY = (i * LETTER_HEIGHT);
75+
}
76+
77+
tft_DigitalRainAnim->setTextColor(TFT_WHITE, TFT_BLACK);
78+
tft_DigitalRainAnim->drawString(this->getASCIIChar(), startX, line_pos[lineNum] +currentY, FONT_SIZE);
79+
line_pos[lineNum] += line_speed[lineNum];
80+
81+
if (line_pos[lineNum] >= height){
82+
this->lineUpdate(lineNum);
83+
}
84+
}
85+
86+
//a function where animation continues to run.
87+
void DigitalRainAnim::play(){
88+
uint32_t currentTime = millis();
89+
if (((currentTime - lastDrawTime) < timeFrame)) {
90+
return;
91+
}
92+
93+
if(isPlaying) for(int i = 0; i < numOfline; i++) this->lineAnimation(i);
94+
lastDrawTime = currentTime;
95+
}
96+
97+
//a function to stop animation.
98+
void DigitalRainAnim::pause(){
99+
isPlaying = false;
100+
}
101+
102+
//a function to resume animation.
103+
void DigitalRainAnim::resume(){
104+
isPlaying = true;
105+
}
106+
107+
//a function that gets randomly from ASCII code 33 to 126.
108+
String DigitalRainAnim::getASCIIChar()
109+
{
110+
return String((char)(random(33, 127)));
111+
}
112+
113+
//move the position to start from out of the screen.
114+
int DigitalRainAnim::setYPos(int lineLen){
115+
return lineLen * -20;
116+
}
117+
118+
//the function is to get the random number (including max)
119+
int DigitalRainAnim::getRandomNum(int min, int max){
120+
return random(min, max+1);
121+
}

src/DigitalRainAnim.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
DigitalRainAnim.h - Library for Digital Rain Animation for TFT_eSPI.
3+
Created by Eric Nam, December 30, 2020.
4+
Released into the public domain.
5+
*/
6+
7+
#ifndef DigitalRainAnim_h
8+
#define DigitalRainAnim_h
9+
10+
#include <TFT_eSPI.h>
11+
#include <SPI.h>
12+
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+
17+
class DigitalRainAnim
18+
{
19+
public:
20+
DigitalRainAnim();
21+
void init(TFT_eSPI* tft); //initialization
22+
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
26+
27+
private:
28+
TFT_eSPI* tft_DigitalRainAnim; //target display TFT_eSPI
29+
int line_len_min; //minimum length of characters
30+
int line_len_max; //maximum length of characters
31+
int line_speed_min; //minimum vertical move speed
32+
int line_speed_max; //maximum vertical move speed
33+
int width, height; //width, height of display
34+
int numOfline; //number of calculated row
35+
bool isPlaying; //boolean for play or pause
36+
int timeFrame; //time frame for drawing
37+
uint32_t lastDrawTime; //checking last drawing time
38+
std::vector<int> line_length; //dynamic array for each line of vertical length
39+
std::vector<int> line_pos; //dynamic array for eacg line Y position
40+
std::vector<int> line_speed; //dynamic array for eacg line speed
41+
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+
int getRandomNum(int min, int max); //the function is to get the random number (including max)
48+
};
49+
50+
#endif

0 commit comments

Comments
 (0)