sd functions read, write, delete, add
This commit is contained in:
1 parent
770aae092d
commit
8215f50bd9
1 file changed
+110
@@ -0,0 +1,110 @@
|
||||
#include <SPI.h>
|
||||
#include <SD.h>
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <MCUFRIEND_kbv.h>
|
||||
|
||||
MCUFRIEND_kbv tft;
|
||||
const int chipSelect = 10;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
uint16_t ID = tft.readID();
|
||||
tft.begin(ID);
|
||||
tft.setRotation(1);
|
||||
tft.fillScreen(0x0000);
|
||||
tft.setTextSize(2);
|
||||
tft.setTextColor(0xFFFF);
|
||||
tft.setCursor(0, 0);
|
||||
|
||||
if (!SD.begin(chipSelect)) {
|
||||
tft.println("SD Initialisierung fehlgeschlagen!");
|
||||
return;
|
||||
}
|
||||
tft.println("SD bereit!");
|
||||
delay(1000);
|
||||
tft.fillScreen(0x0000);
|
||||
tft.setCursor(0, 0);
|
||||
|
||||
// --- ANWENDUNGSBEISPIELE ---
|
||||
|
||||
// 1. Datei neu schreiben (überschreibt alten Inhalt)
|
||||
schreibeDatei("TEST.TXT", "Das ist die erste Zeile.");
|
||||
|
||||
// 2. Etwas an die Datei anhängen
|
||||
anhaengenDatei("TEST.TXT", "Hier kommt ein Nachtrag.");
|
||||
|
||||
// 3. Datei auslesen und auf dem Display anzeigen
|
||||
leseDatei("TEST.TXT");
|
||||
|
||||
// 4. Datei löschen (auskommentiert zum Testen)
|
||||
// loescheDatei("TEST.TXT");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Hier passiert nichts
|
||||
}
|
||||
|
||||
// --- FUNKTIONEN ---
|
||||
|
||||
// SCHREIBEN: Erstellt Datei neu oder überschreibt existierenden Inhalt
|
||||
void schreibeDatei(const char* pfad, const char* inhalt) {
|
||||
if (SD.exists(pfad)) {
|
||||
SD.remove(pfad);
|
||||
}
|
||||
File f = SD.open(pfad, FILE_WRITE);
|
||||
if (f) {
|
||||
f.println(inhalt);
|
||||
f.close();
|
||||
Serial.println("Erfolgreich geschrieben.");
|
||||
} else {
|
||||
Serial.println("Schreibfehler!");
|
||||
}
|
||||
}
|
||||
|
||||
// ANHÄNGEN: Fügt Text am Ende hinzu, ohne den alten Inhalt zu löschen
|
||||
void anhaengenDatei(const char* pfad, const char* inhalt) {
|
||||
File f = SD.open(pfad, FILE_WRITE); // FILE_WRITE springt automatisch ans Ende
|
||||
if (f) {
|
||||
f.println(inhalt);
|
||||
f.close();
|
||||
Serial.println("Erfolgreich angehaengt.");
|
||||
} else {
|
||||
Serial.println("Fehler beim Anhaengen!");
|
||||
}
|
||||
}
|
||||
|
||||
// LESEN: Zeigt den Inhalt auf dem Display und im Serial Monitor an
|
||||
void leseDatei(const char* pfad) {
|
||||
File f = SD.open(pfad);
|
||||
if (f) {
|
||||
tft.setTextColor(0x07FF); // Cyan für gelesenen Text
|
||||
tft.println("Inhalt von ");
|
||||
tft.println(pfad);
|
||||
tft.println("-----");
|
||||
|
||||
while (f.available()) {
|
||||
char c = f.read();
|
||||
tft.print(c);
|
||||
Serial.print(c);
|
||||
}
|
||||
f.close();
|
||||
tft.println("\n-----");
|
||||
tft.setTextColor(0xFFFF);
|
||||
} else {
|
||||
tft.println("Datei konnte nicht geoeffnet werden.");
|
||||
}
|
||||
}
|
||||
|
||||
// LÖSCHEN: Entfernt die Datei komplett von der Karte
|
||||
void loescheDatei(const char* pfad) {
|
||||
if (SD.exists(pfad)) {
|
||||
if (SD.remove(pfad)) {
|
||||
Serial.println("Datei geloescht.");
|
||||
tft.println("Datei geloescht.");
|
||||
} else {
|
||||
Serial.println("Loeschfehler!");
|
||||
}
|
||||
} else {
|
||||
Serial.println("Datei existiert nicht.");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user