#include <Arduino.h>
#include "FS.h"
#include <LittleFS.h>
#include <PNGdec.h>
#include <TFT_eSPI.h>
#include "Logo.h"
#include "Buttons.h"
#include "NotoSansBold15.h"
#include "SPI.h"
#include "Letras.h"
#define FORMAT_LITTLEFS_IF_FAILED true
#define AA_FONT_SMALL NotoSansBold15
#define MAX_IMAGE_WIDTH 240
#define DISP_X 40
#define DISP_Y 22
#define DISP_W 207
#define DISP_H 30
PNG png;
TFT_eSPI tft = TFT_eSPI();
const int btn_Der = 34;
int btn_Estado = 0;
int16_t xpos = 0;
int16_t ypos = 0;
uint8_t Pulsado = false;
uint8_t Nro_Msj = 0;
uint8_t i=0;
uint8_t j=0;
const char* Titulos[100] = {"Vacio","Vacio"};
static uint8_t buf[100];
static uint8_t Contenido[100] = {""};
const char* Qwerty[100] = {"Q","W","E","R","T","Y","U"," I"
,"O","P","A","S","D","F","G","H"," J","K","L","Z","X","C"
,"V","B","N","M"};
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
Serial.printf("Listing directory: %s\r\n", dirname);
File root = fs.open(dirname);
if(!root){
Serial.println("- failed to open directory");
return;
}
if(!root.isDirectory()){
Serial.println(" - not a directory");
return;
}
File file = root.openNextFile();
i=0;
while(file){
if(file.isDirectory()){
Serial.print(" DIR : ");
Serial.println(file.name());
if(levels){
listDir(fs, file.path(), levels -1);
}
} else {
///Serial.print(" FILE: ");
///Serial.print(file.name());
Titulos[i] = file.name();
///Serial.print("\tSIZE: ");
///Serial.println(file.size());
Serial.print(Titulos[i]);
i++;
}
file = root.openNextFile();
}
}
void createDir(fs::FS &fs, const char * path){
Serial.printf("Creating Dir: %s\n", path);
if(fs.mkdir(path)){
Serial.println("Dir created");
} else {
Serial.println("mkdir failed");
}
}
void removeDir(fs::FS &fs, const char * path){
Serial.printf("Removing Dir: %s\n", path);
if(fs.rmdir(path)){
Serial.println("Dir removed");
} else {
Serial.println("rmdir failed");
}
}
void readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\r\n", path);
File file = fs.open(path);
if(!file || file.isDirectory()){
Serial.println("- failed to open file for reading");
return;
}
Serial.println("- read from file:");
while(file.available()){
Serial.write(file.read());
}
file.close();
}
void writeFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Writing file: %s\r\n", path);
File file = fs.open(path, FILE_WRITE);
if(!file){
Serial.println("- failed to open file for writing");
return;
}
if(file.print(message)){
Serial.println("- file written");
} else {
Serial.println("- write failed");
}
file.close();
}
void appendFile(fs::FS &fs, const char * path, const char * message){
Serial.printf("Appending to file: %s\r\n", path);
File file = fs.open(path, FILE_APPEND);
if(!file){
Serial.println("- failed to open file for appending");
return;
}
if(file.print(message)){
Serial.println("- message appended");
} else {
Serial.println("- append failed");
}
file.close();
}
void renameFile(fs::FS &fs, const char * path1, const char * path2){
Serial.printf("Renaming file %s to %s\r\n", path1, path2);
if (fs.rename(path1, path2)) {
Serial.println("- file renamed");
} else {
Serial.println("- rename failed");
}
}
void deleteFile(fs::FS &fs, const char * path){
Serial.printf("Deleting file: %s\r\n", path);
if(fs.remove(path)){
Serial.println("- file deleted");
} else {
Serial.println("- delete failed");
}
}
void testFileIO(fs::FS &fs, const char * path){
Serial.printf("Testing file I/O with %s\r\n", path);
static uint8_t buf[512];
size_t len = 0;
File file = fs.open(path, FILE_WRITE);
if(!file){
Serial.println("- failed to open file for writing");
return;
}
size_t i;
Serial.print("- writing" );
uint32_t start = millis();
for(i=0; i<2048; i++){
if ((i & 0x001F) == 0x001F){
Serial.print(".");
}
file.write(buf, 512);
}
Serial.println("");
uint32_t end = millis() - start;
Serial.printf(" - %u bytes written in %u ms\r\n", 2048 * 512, end);
file.close();
file = fs.open(path);
start = millis();
end = start;
i = 0;
if(file && !file.isDirectory()){
len = file.size();
size_t flen = len;
start = millis();
Serial.print("- reading" );
while(len){
size_t toRead = len;
if(toRead > 512){
toRead = 512;
}
file.read(buf, toRead);
if ((i++ & 0x001F) == 0x001F){
Serial.print(".");
}
len -= toRead;
}
Serial.println("");
end = millis() - start;
Serial.printf("- %u bytes read in %u ms\r\n", flen, end);
file.close();
} else {
Serial.println("- failed to open file for reading");
}
}
void Mostrar_Msj(const char * path) {
File file = LittleFS.open(path, "r");
if (!file) {
Serial.println("Error al abrir el archivo");
return;
}
size_t size = file.size();
char* buffer = new char[size + 1]; // +1 para el carácter nulo
file.readBytes(buffer, size);
buffer[size] = '\0'; // Añadir el carácter nulo al final
tft.println(buffer);
Serial.println(buffer); // Imprimir el contenido del archivo
delete[] buffer; // Liberar la memoria
file.close();
}
void Fecha(void)
{
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setCursor(0, 0);
Serial.println("Loading font");
tft.loadFont(AA_FONT_SMALL);
tft.println("19/07/24");
}
void Hora(void)
{
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setCursor(240, 0);
Serial.println("Loading font");
tft.loadFont(AA_FONT_SMALL);
tft.println("19:15");
}
void pngDraw(PNGDRAW *pDraw) {
uint16_t lineBuffer[MAX_IMAGE_WIDTH];
png.getLineAsRGB565(pDraw, lineBuffer, PNG_RGB565_BIG_ENDIAN, 0xffffffff);
tft.pushImage(xpos, ypos + pDraw->y, pDraw->iWidth, 1, lineBuffer);
}
void Inicio(void)
{
xpos = 82;
ypos = 41;
int16_t rc = png.openFLASH((uint8_t *)Logo, sizeof(Logo), pngDraw);
if (rc == PNG_SUCCESS) {
Serial.println("Successfully opened png file");
Serial.printf("image specs: (%d x %d), %d bpp, pixel type: %d\n", png.getWidth(), png.getHeight(), png.getBpp(), png.getPixelType());
tft.startWrite();
uint32_t dt = millis();
rc = png.decode(NULL, 0);
Serial.print(millis() - dt); Serial.println("ms");
tft.endWrite();
// png.close(); // not needed for memory->memory decode
}
delay(5000);
tft.fillScreen(TFT_BLACK);
Play();
Edit();
Setting();
Right();
Left();
Hora();
Fecha();
tft.fillRect(DISP_X, DISP_Y, DISP_W, DISP_H, TFT_WHITE);
tft.drawRect(DISP_X, DISP_Y, DISP_W, DISP_H, TFT_WHITE);
tft.setTextColor(TFT_BLACK, TFT_WHITE);
tft.setCursor(45, 30);
Serial.println("Loading font");
tft.loadFont(AA_FONT_SMALL);
tft.println("Prueba");
}
void Play(void)
{
xpos = 95;
ypos = 120;
int16_t rc1 = png.openFLASH((uint8_t *)Play_96, sizeof(Play_96), pngDraw);
if (rc1 == PNG_SUCCESS) {
Serial.println("Successfully opened png file");
Serial.printf("image specs: (%d x %d), %d bpp, pixel type: %d\n", png.getWidth(), png.getHeight(), png.getBpp(), png.getPixelType());
tft.startWrite();
uint32_t dt = millis();
rc1 = png.decode(NULL, 0);
Serial.print(millis() - dt); Serial.println("ms");
tft.endWrite();
}
}
void Stop(void)
{
xpos = 95;
ypos = 140;
int16_t rc1 = png.openFLASH((uint8_t *)Stop_96, sizeof(Stop_96), pngDraw);
if (rc1 == PNG_SUCCESS) {
Serial.println("Successfully opened png file");
Serial.printf("image specs: (%d x %d), %d bpp, pixel type: %d\n", png.getWidth(), png.getHeight(), png.getBpp(), png.getPixelType());
tft.startWrite();
uint32_t dt = millis();
rc1 = png.decode(NULL, 0);
Serial.print(millis() - dt); Serial.println("ms");
tft.endWrite();
}
}
void Edit(void)
{
xpos = 5;
ypos = 150;
int16_t rc1 = png.openFLASH((uint8_t *)Edit_64, sizeof(Edit_64), pngDraw);
if (rc1 == PNG_SUCCESS) {
Serial.println("Successfully opened png file");
Serial.printf("image specs: (%d x %d), %d bpp, pixel type: %d\n", png.getWidth(), png.getHeight(), png.getBpp(), png.getPixelType());
tft.startWrite();
uint32_t dt = millis();
rc1 = png.decode(NULL, 0);
Serial.print(millis() - dt); Serial.println("ms");
tft.endWrite();
}
}
void Setting(void)
{
xpos = 220;
ypos = 150;
int16_t rc1 = png.openFLASH((uint8_t *)Setting_64, sizeof(Setting_64), pngDraw);
if (rc1 == PNG_SUCCESS) {
Serial.println("Successfully opened png file");
Serial.printf("image specs: (%d x %d), %d bpp, pixel type: %d\n", png.getWidth(), png.getHeight(), png.getBpp(), png.getPixelType());
tft.startWrite();
uint32_t dt = millis();
rc1 = png.decode(NULL, 0);
Serial.print(millis() - dt); Serial.println("ms");
tft.endWrite();
}
}
//void Up(void)
//{
//xpos = 240;
//ypos = 20;
//int16_t rc1 = png.openFLASH((uint8_t *)Up_32, sizeof(Up_32), pngDraw);
//if (rc1 == PNG_SUCCESS) {
//Serial.println("Successfully opened png file");
//Serial.printf("image specs: (%d x %d), %d bpp, pixel type: %d\n", png.getWidth(), png.getHeight(), png.getBpp(), png.getPixelType());
//tft.startWrite();
//uint32_t dt = millis();
//rc1 = png.decode(NULL, 0);
//Serial.print(millis() - dt); Serial.println("ms");
//tft.endWrite();
//}
//}
//void Down(void)
//{
//xpos = 20;
//ypos = 20;
//int16_t rc1 = png.openFLASH((uint8_t *)Down_32, sizeof(Down_32), pngDraw);
//if (rc1 == PNG_SUCCESS) {
//Serial.println("Successfully opened png file");
//Serial.printf("image specs: (%d x %d), %d bpp, pixel type: %d\n", png.getWidth(), png.getHeight(), png.getBpp(), png.getPixelType());
//tft.startWrite();
//uint32_t dt = millis();
//rc1 = png.decode(NULL, 0);
//Serial.print(millis() - dt); Serial.println("ms");
//tft.endWrite();
//}
//}
void Right(void)
{
xpos = 250;
ypos = 20;
int16_t rc1 = png.openFLASH((uint8_t *)Right_32, sizeof(Right_32), pngDraw);
if (rc1 == PNG_SUCCESS) {
Serial.println("Successfully opened png file");
Serial.printf("image specs: (%d x %d), %d bpp, pixel type: %d\n", png.getWidth(), png.getHeight(), png.getBpp(), png.getPixelType());
tft.startWrite();
uint32_t dt = millis();
rc1 = png.decode(NULL, 0);
Serial.print(millis() - dt); Serial.println("ms");
tft.endWrite();
}
}
void Left(void)
{
xpos = 5;
ypos = 20;
int16_t rc1 = png.openFLASH((uint8_t *)Left_32, sizeof(Left_32), pngDraw);
if (rc1 == PNG_SUCCESS) {
Serial.println("Successfully opened png file");
Serial.printf("image specs: (%d x %d), %d bpp, pixel type: %d\n", png.getWidth(), png.getHeight(), png.getBpp(), png.getPixelType());
tft.startWrite();
uint32_t dt = millis();
rc1 = png.decode(NULL, 0);
Serial.print(millis() - dt); Serial.println("ms");
tft.endWrite();
}
}
void Pag_2(void)
{
tft.fillScreen(TFT_BLACK);
tft.fillRect(DISP_X, DISP_Y, DISP_W, DISP_H, TFT_WHITE);
tft.fillRect(DISP_X, DISP_Y + 35, DISP_W, DISP_H, TFT_WHITE);
Hora();
Fecha();
int16_t rc1 = png.openFLASH((uint8_t *)Tecla_24, sizeof(Tecla_24), pngDraw);
if(rc1 == PNG_SUCCESS) {
ypos = 140;
tft.setTextColor( TFT_WHITE, 0x753a);
for(uint8_t lin_1 = 0; lin_1 < 10; lin_1++)
{
xpos = 30 * lin_1;
tft.startWrite();
rc1 = png.decode(NULL, 0);
tft.endWrite();
tft.setCursor(6 + xpos, 145);
tft.println(Qwerty[lin_1]);
}
ypos = 170;
for(uint8_t lin_2 = 0; lin_2 < 9; lin_2++)
{
xpos = 12 + 30 * lin_2;
tft.startWrite();
rc1 = png.decode(NULL, 0);
tft.endWrite();
tft.setCursor(6 + xpos, 175);
tft.println(Qwerty[10 + lin_2]);
}
ypos = 200;
for(uint8_t lin_3 = 0; lin_3 < 7; lin_3++)
{
xpos = 45 + 30 * lin_3;
tft.startWrite();
rc1 = png.decode(NULL, 0);
tft.endWrite();
tft.setCursor(6 + xpos, 205);
tft.println(Qwerty[19 + lin_3]);
}
}
}
void Pag_3(void)
{
tft.fillScreen(TFT_BLACK);
tft.setTextColor( TFT_WHITE, TFT_BLACK);
Hora();
Fecha();
tft.setCursor(DISP_X + 85, 30);
tft.println("Retardo");
tft.fillRect(DISP_X + 80, DISP_Y + 30, 75, 30, TFT_WHITE);
tft.setCursor(DISP_X + 80, 80);
tft.println("Velocidad");
tft.fillRect(DISP_X + 80, DISP_Y + 90, 75, 30, TFT_WHITE);
}
void Pag_4(void)
{
tft.fillScreen(TFT_BLACK);
tft.setTextColor( TFT_WHITE, TFT_BLACK);
Hora();
Fecha();
tft.setCursor(6 + xpos, 205);
tft.println("Hora");
tft.fillRect(DISP_X, DISP_Y, 50, 30, TFT_WHITE);
tft.setCursor(6 + xpos, 205);
tft.println("Min.");
tft.fillRect(DISP_X, DISP_Y, 50, 30, TFT_WHITE);
tft.setCursor(6 + xpos, 205);
tft.println("Dia");
tft.fillRect(DISP_X, DISP_Y, 50, 30, TFT_WHITE);
tft.setCursor(6 + xpos, 205);
tft.println("Mes");
tft.fillRect(DISP_X, DISP_Y, 50, 30, TFT_WHITE);
tft.setCursor(6 + xpos, 205);
tft.println("Ano");
tft.fillRect(DISP_X, DISP_Y, 50, 30, TFT_WHITE);
}
//====================================================================================
// Setup
//====================================================================================
void setup(){
Serial.begin(115200);
pinMode(btn_Der, INPUT);
if(!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)){
Serial.println("LittleFS Mount Failed");
return;
}
// Initialise the TFT
tft.begin();
tft.fillScreen(TFT_WHITE);
tft.setRotation(1);
Serial.println("\r\nInitialisation done.");
createDir(LittleFS, "/Mensajes"); // Create a mydir folder
writeFile(LittleFS, "/Mensajes/hello1", "Hello1");
writeFile(LittleFS, "/Mensajes/hello2", "Hello1");
writeFile(LittleFS, "/Mensajes/hello3", "Hello1");
readFile(LittleFS, "/Mensajes/hello1");
listDir(LittleFS, "/", 1);
Inicio();
}
//====================================================================================
// Loop
//====================================================================================
void loop2(){
btn_Estado = digitalRead(btn_Der);
if(btn_Estado == HIGH && Pulsado == false)
{
Pulsado = true;
}
if(btn_Estado == LOW && Pulsado == true)
{
Serial.println("\r\nBoton");
Pulsado = false;
tft.fillRect(DISP_X, DISP_Y, DISP_W, DISP_H, TFT_WHITE);
tft.setTextColor( TFT_BLACK, TFT_WHITE);
tft.setCursor(45, 30);
Serial.println("Loading font por aca");
tft.loadFont(AA_FONT_SMALL);
tft.println(Titulos[Nro_Msj]);
tft.fillRect(DISP_X, DISP_Y + 35, DISP_W, DISP_H, TFT_WHITE);
tft.setTextColor( TFT_BLACK, TFT_WHITE);
tft.setCursor(45, 65);
Mostrar_Msj("/Mensajes/hello2");
Nro_Msj++;
if(Nro_Msj == i)
{
Nro_Msj = 0;
}
}
}
void loop(){
btn_Estado = digitalRead(btn_Der);
if(btn_Estado == HIGH && Pulsado == false)
{
Pulsado = true;
}
if(btn_Estado == LOW && Pulsado == true)
{
Serial.println("\r\nBoton");
Pulsado = false;
Pag_3();
}
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
Loading
ili9341-cap-touch
ili9341-cap-touch