Kod
#include <TFT_eSPI.h>
#include <SD.h>
#include <SPIFFS.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <FS.h>
TFT_eSPI tft = TFT_eSPI();
#define SD_CS_PIN 5
// Zmienna do przechowywania błędów
String errorHistory = "";
void displayText(String text);
void handleCommand(String command);
void listFiles(fs::FS &fs, String path);
void openFile(fs::FS &fs, String filePath);
void copyFile(fs::FS &srcFS, String srcPath, fs::FS &dstFS, String dstPath);
void logError(String errorMessage);
void displayErrorHistory();
void clearErrorHistory();
void showMemoryUsage();
void setup() {
Serial.begin(115200);
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
// Inicjalizacja SPIFFS
if (!SPIFFS.begin(true)) {
logError("Błąd SPIFFS");
displayText("Błąd SPIFFS");
while (true);
}
// Inicjalizacja SD
if (SD.begin(SD_CS_PIN)) {
displayText("Witaj\nŚcieżka: A:/");
} else {
logError("Błąd SD");
displayText("Błąd SD");
}
// Inicjalizacja WiFi
WiFi.begin("TP-Link_A698", "33660196"); // Wprowadź swoje dane WiFi
while (WiFi.status() != WL_CONNECTED) {
delay(500);
tft.print(".");
}
}
void loop() {
if (Serial.available()) {
String command = Serial.readStringUntil('\n');
command.trim();
handleCommand(command);
}
}
void displayText(String text) {
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextSize(2);
tft.setCursor(0, 0);
tft.println(text);
}
void logError(String errorMessage) {
errorHistory += errorMessage + "\n"; // Zapisz błąd do historii
}
void displayErrorHistory() {
displayText("Błędy:\n" + errorHistory); // Wyświetl zapisane błędy
}
void clearErrorHistory() {
errorHistory = ""; // Wyczyść historię błędów
displayText("Historia błędów usunięta.");
}
void showMemoryUsage() {
// Pokazanie użycia pamięci na dyskach A (SD) i C (SPIFFS)
long freeSD = SD.totalBytes() - SD.usedBytes();
long freeSPIFFS = SPIFFS.totalBytes() - SPIFFS.usedBytes();
String memoryInfo = "Pamięć A:/\nZajęte: " + String(SD.usedBytes()) + "B\nPozostało: " + String(freeSD) + "B";
memoryInfo += "\n\nPamięć C:/\nZajęte: " + String(SPIFFS.usedBytes()) + "B\nPozostało: " + String(freeSPIFFS) + "B";
displayText(memoryInfo);
}
void handleCommand(String command) {
if (command.startsWith("info ")) {
// Wyświetlenie listy plików
String drive = command.substring(5);
if (drive == "c") {
displayText("Pliki na C:/");
listFiles(SPIFFS, "/");
} else if (drive == "a") {
displayText("Pliki na A:/");
listFiles(SD, "/");
} else {
displayText("Nieprawidłowy dysk: " + drive);
}
} else if (command.startsWith("del ")) {
// Usunięcie pliku
String filePath = command.substring(4); // Poprawienie ścieżki od "del"
bool isSPIFFS = filePath.startsWith("c:/");
String file = filePath.substring(3); // Usunięcie "c:/"
if (isSPIFFS) {
if (SPIFFS.remove("/" + file)) {
displayText("Usunięto plik: c:/" + file);
} else {
logError("Nie znaleziono pliku: c:/" + file);
displayText("Nie znaleziono pliku: c:/" + file);
}
} else {
if (SD.remove("/" + file)) {
displayText("Usunięto plik: a:/" + file);
} else {
logError("Nie znaleziono pliku: a:/" + file);
displayText("Nie znaleziono pliku: a:/" + file);
}
}
} else if (command.startsWith("cd ")) {
// Otworzenie pliku i wyświetlenie jego zawartości
String filePath = command.substring(3); // Zajmujemy się częścią po "cd"
bool isSPIFFS = filePath.startsWith("c:/");
String file = filePath.substring(3); // Usunięcie "c:/"
if (isSPIFFS) {
openFile(SPIFFS, "/" + file);
} else {
openFile(SD, "/" + file);
}
} else if (command.startsWith("cop ")) {
// Kopiowanie pliku
String paths = command.substring(4);
int separator = paths.indexOf(' ');
if (separator == -1) {
logError("Błąd składni: cop src dst");
displayText("Błąd składni: cop src dst");
return;
}
String srcPath = paths.substring(0, separator);
String dstPath = paths.substring(separator + 1);
bool srcSPIFFS = srcPath.startsWith("c:/");
bool dstSPIFFS = dstPath.startsWith("c:/");
String srcFilePath = srcPath.substring(3); // Usunięcie "c:/"
String dstFilePath = dstPath.substring(3); // Usunięcie "c:/"
fs::FS *srcFS;
fs::FS *dstFS;
// Wybór odpowiedniego systemu plików
if (srcSPIFFS) {
srcFS = &SPIFFS;
} else {
srcFS = &SD;
}
if (dstSPIFFS) {
dstFS = &SPIFFS;
} else {
dstFS = &SD;
}
copyFile(*srcFS, "/" + srcFilePath, *dstFS, "/" + dstFilePath);
} else if (command == "guw") {
// Wyświetlanie czarnego tła i napisu "Witaj"
displayText("Witaj na ekranie!");
} else if (command == "history error") {
displayErrorHistory(); // Wyświetl historię błędów
} else if (command == "history error remove") {
clearErrorHistory(); // Wyczyść historię błędów
} else if (command == "pam") {
showMemoryUsage(); // Pokaż użycie pamięci
} else {
logError("Nieznane polecenie: " + command);
displayText("Nieznane polecenie");
}
}
void openFile(fs::FS &fs, String filePath) {
File file = fs.open(filePath, "r");
if (!file) {
logError("Nie udało się otworzyć pliku: " + filePath);
displayText("Nie udało się otworzyć pliku");
return;
}
String fileContent = "";
while (file.available()) {
fileContent += (char)file.read();
}
file.close();
displayText(fileContent);
}
void listFiles(fs::FS &fs, String path) {
File root = fs.open(path);
if (!root) {
logError("Błąd otwarcia folderu: " + path);
displayText("Błąd otwarcia folderu");
return;
}
if (!root.isDirectory()) {
logError("To nie jest folder: " + path);
displayText("To nie jest folder");
return;
}
File file = root.openNextFile();
while (file) {
tft.println(file.name());
file = root.openNextFile();
}
}
void copyFile(fs::FS &srcFS, String srcPath, fs::FS &dstFS, String dstPath) {
File srcFile = srcFS.open(srcPath, "r");
if (!srcFile) {
logError("Nie udało się otworzyć pliku do kopiowania: " + srcPath);
displayText("Błąd kopiowania");
return;
}
File dstFile = dstFS.open(dstPath, FILE_WRITE);
if (!dstFile) {
logError("Nie udało się otworzyć pliku docelowego: " + dstPath);
displayText("Błąd kopiowania");
srcFile.close();
return;
}
while (srcFile.available()) {
dstFile.write(srcFile.read());
}
srcFile.close();
dstFile.close();
displayText("Skopiowano: " + srcPath + " -> " + dstPath);
}
Wymagane do projektu esp32-2432S028 i Arduino IDE i kabel USB C
Podpowiedź:
Możesz usunąć tę informację włączając Plan Premium