-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Eric
committed
Dec 30, 2020
0 parents
commit 8a18533
Showing
8 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Digital Rain Animation for TFT_eSPI | ||
|
||
A library that represents Digital Rain Animation on color displays that support TFT_eSPI | ||
|
||
Search for this library in the Arduino Library Manager and download it or clone it yourself from Github. | ||
|
||
This library is built on TFT_eSPI. TFT_eSPI works properly on color displays among the supported displays. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <TFT_eSPI.h> | ||
#include <SPI.h> | ||
#include <DigitalRainAnim.h> | ||
TFT_eSPI tft = TFT_eSPI(); | ||
|
||
DigitalRainAnim digitalRainAnim = DigitalRainAnim(); | ||
|
||
void setup() | ||
{ | ||
tft.begin(); | ||
tft.setRotation(0); | ||
tft.fillScreen(TFT_BLACK); | ||
digitalRainAnim.init(&tft); | ||
} | ||
|
||
void loop() | ||
{ | ||
digitalRainAnim.play(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <TFT_eSPI.h> | ||
#include <SPI.h> | ||
#include <DigitalRainAnim.h> | ||
#include "Button2.h"; | ||
|
||
#define RIGHT_BUTTON_PIN 35 | ||
#define LEFT_BUTTON_PIN 0 | ||
|
||
TFT_eSPI tft = TFT_eSPI(); | ||
Button2 rButton = Button2(RIGHT_BUTTON_PIN); | ||
Button2 lButton = Button2(LEFT_BUTTON_PIN); | ||
|
||
DigitalRainAnim digitalRainAnim = DigitalRainAnim(); | ||
|
||
void setup() | ||
{ | ||
tft.begin(); | ||
tft.setRotation(0); | ||
tft.fillScreen(TFT_BLACK); | ||
digitalRainAnim.init(&tft, 3, 20, 3, 20, 60); | ||
rButton.setClickHandler(rClick); | ||
lButton.setClickHandler(lClick); | ||
} | ||
|
||
void loop() | ||
{ | ||
digitalRainAnim.play(); | ||
rButton.loop(); | ||
lButton.loop(); | ||
} | ||
|
||
void rClick(Button2& btn) { | ||
digitalRainAnim.pause(); | ||
} | ||
|
||
void lClick(Button2& btn) { | ||
digitalRainAnim.resume(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
########################################### | ||
# A library that represents Digital Rain Animation on color displays that support TFT_eSPI | ||
########################################### | ||
|
||
########################################### | ||
# Datatypes (KEYWORD1) | ||
########################################### | ||
|
||
Digital_Rain_Animation_for_TFT_eSPI KEYWORD1 | ||
|
||
########################################### | ||
# Methods and Functions (KEYWORD2) | ||
########################################### | ||
init KEYWORD2 | ||
setParams KEYWORD2 | ||
play KEYWORD2 | ||
pause KEYWORD2 | ||
resume KEYWORD2 | ||
prepareAnim KEYWORD2 | ||
lineUpdate KEYWORD2 | ||
lineAnimation KEYWORD2 | ||
getASCIIChar KEYWORD2 | ||
setYPos KEYWORD2 | ||
getRandomNum KEYWORD2 | ||
########################################### | ||
# Constants (LITERAL1) | ||
########################################### | ||
LINE_WIDTH LITERAL1 | ||
LETTER_HEIGHT LITERAL1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "Digital Rain Animation for TFT_eSPI", | ||
"keywords": "TFT_eSPI, Animation, Matrix, Digital, Rain", | ||
"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.", | ||
"repository": | ||
{ | ||
"type": "git", | ||
"url": "https://github.com/0015/TP_Arduino_DigitalRain_Anim.git" | ||
}, | ||
"version": "1.0", | ||
"frameworks": "arduino", | ||
"platforms": "*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
name=Digital Rain Animation for TFT_eSPI | ||
version=1.0.0 | ||
author=Eric Nam | ||
maintainer=Eric Nam <[email protected]> | ||
sentence=A library that represents Digital Rain Animation on color displays that support TFT_eSPI | ||
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. | ||
category=TFT | ||
url=https://github.com/0015/TP_Arduino_DigitalRain_Anim | ||
architectures=* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
DigitalRainAnim.cpp - Library for Digital Rain Animation for TFT_eSPI. | ||
Created by Eric Nam, December 30, 2020. | ||
Released into the public domain. | ||
*/ | ||
|
||
#include "DigitalRainAnim.h" | ||
|
||
DigitalRainAnim::DigitalRainAnim(){} | ||
|
||
//initialze with only tft | ||
void DigitalRainAnim::init(TFT_eSPI* tft){ | ||
|
||
tft_DigitalRainAnim = tft; | ||
line_len_min = 3; | ||
line_len_max = 20; | ||
line_speed_min = 3; | ||
line_speed_max = 15; | ||
timeFrame = 100; | ||
|
||
this->prepareAnim(); | ||
} | ||
|
||
//initialze with only tft and params | ||
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) | ||
{ | ||
tft_DigitalRainAnim = tft; | ||
line_len_min = new_line_len_min; | ||
line_len_max = new_line_len_max; | ||
line_speed_min = new_line_speed_min; | ||
line_speed_max = new_line_speed_max; | ||
timeFrame = new_timeFrame; | ||
|
||
this->prepareAnim(); | ||
} | ||
|
||
//checking how many lines it can draw from the width of the screen. | ||
//the size of the array is determined by the number of lines. | ||
void DigitalRainAnim::prepareAnim() | ||
{ | ||
lastDrawTime = millis() - timeFrame; | ||
width = tft_DigitalRainAnim->width(); | ||
height = tft_DigitalRainAnim->height(); | ||
|
||
numOfline = width / LINE_WIDTH; | ||
|
||
for(int i = 0; i < numOfline; i++){ | ||
line_length.push_back(this->getRandomNum(line_len_min, line_len_max)); | ||
line_pos.push_back(this->setYPos(line_length[i])); | ||
line_speed.push_back(this->getRandomNum(line_speed_min, line_speed_max)); | ||
} | ||
|
||
isPlaying = true; | ||
} | ||
|
||
//updating each line with a new length, Y position, and speed. | ||
void DigitalRainAnim::lineUpdate(int lineNum){ | ||
line_length[lineNum] = this->getRandomNum(line_len_min, line_len_max); | ||
line_pos[lineNum] = this->setYPos(line_length[lineNum]); | ||
line_speed[lineNum] = this->getRandomNum(line_speed_min, line_speed_max); | ||
} | ||
|
||
//while moving vertically, the color value changes and the character changes as well. | ||
void DigitalRainAnim::lineAnimation(int lineNum) | ||
{ | ||
int startX = lineNum * LINE_WIDTH; | ||
tft_DigitalRainAnim->fillRect(startX, 0, 10, 240,TFT_BLACK); | ||
|
||
int currentY = 0; | ||
for(int i=0; i<line_length[lineNum]; i++){ | ||
int greenVal = map(i, 0, line_length[lineNum], 25, 255); | ||
tft_DigitalRainAnim->setTextColor(tft_DigitalRainAnim->color565(0, greenVal, 0), TFT_BLACK); | ||
tft_DigitalRainAnim->drawString(this->getASCIIChar(), startX, line_pos[lineNum] + currentY, FONT_SIZE); | ||
currentY = (i * LETTER_HEIGHT); | ||
} | ||
|
||
tft_DigitalRainAnim->setTextColor(TFT_WHITE, TFT_BLACK); | ||
tft_DigitalRainAnim->drawString(this->getASCIIChar(), startX, line_pos[lineNum] +currentY, FONT_SIZE); | ||
line_pos[lineNum] += line_speed[lineNum]; | ||
|
||
if (line_pos[lineNum] >= height){ | ||
this->lineUpdate(lineNum); | ||
} | ||
} | ||
|
||
//a function where animation continues to run. | ||
void DigitalRainAnim::play(){ | ||
uint32_t currentTime = millis(); | ||
if (((currentTime - lastDrawTime) < timeFrame)) { | ||
return; | ||
} | ||
|
||
if(isPlaying) for(int i = 0; i < numOfline; i++) this->lineAnimation(i); | ||
lastDrawTime = currentTime; | ||
} | ||
|
||
//a function to stop animation. | ||
void DigitalRainAnim::pause(){ | ||
isPlaying = false; | ||
} | ||
|
||
//a function to resume animation. | ||
void DigitalRainAnim::resume(){ | ||
isPlaying = true; | ||
} | ||
|
||
//a function that gets randomly from ASCII code 33 to 126. | ||
String DigitalRainAnim::getASCIIChar() | ||
{ | ||
return String((char)(random(33, 127))); | ||
} | ||
|
||
//move the position to start from out of the screen. | ||
int DigitalRainAnim::setYPos(int lineLen){ | ||
return lineLen * -20; | ||
} | ||
|
||
//the function is to get the random number (including max) | ||
int DigitalRainAnim::getRandomNum(int min, int max){ | ||
return random(min, max+1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
DigitalRainAnim.h - Library for Digital Rain Animation for TFT_eSPI. | ||
Created by Eric Nam, December 30, 2020. | ||
Released into the public domain. | ||
*/ | ||
|
||
#ifndef DigitalRainAnim_h | ||
#define DigitalRainAnim_h | ||
|
||
#include <TFT_eSPI.h> | ||
#include <SPI.h> | ||
|
||
#define FONT_SIZE 2 //set font size 2 | ||
#define LINE_WIDTH 10 //width for font size 2 | ||
#define LETTER_HEIGHT 14 //height for font size 2 | ||
|
||
class DigitalRainAnim | ||
{ | ||
public: | ||
DigitalRainAnim(); | ||
void init(TFT_eSPI* tft); //initialization | ||
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 | ||
void play(); //play animation | ||
void pause(); //pause animation | ||
void resume(); //resume animation | ||
|
||
private: | ||
TFT_eSPI* tft_DigitalRainAnim; //target display TFT_eSPI | ||
int line_len_min; //minimum length of characters | ||
int line_len_max; //maximum length of characters | ||
int line_speed_min; //minimum vertical move speed | ||
int line_speed_max; //maximum vertical move speed | ||
int width, height; //width, height of display | ||
int numOfline; //number of calculated row | ||
bool isPlaying; //boolean for play or pause | ||
int timeFrame; //time frame for drawing | ||
uint32_t lastDrawTime; //checking last drawing time | ||
std::vector<int> line_length; //dynamic array for each line of vertical length | ||
std::vector<int> line_pos; //dynamic array for eacg line Y position | ||
std::vector<int> line_speed; //dynamic array for eacg line speed | ||
|
||
void prepareAnim(); //the function is to prepare all lines at the beginning | ||
void lineUpdate(int lineNum); //the function is to update each line after disappeared from the screen | ||
void lineAnimation(int lineNum);//the function is to update each line | ||
String getASCIIChar(); //the function is to get the random ASCII char | ||
int setYPos(int lineLen); //the function is to update the position of line | ||
int getRandomNum(int min, int max); //the function is to get the random number (including max) | ||
}; | ||
|
||
#endif |