Sto riscontrando problemi sempre. Se provo a mettere uno sketch normale che scrive soltanto, lo fa, ma il mio sketch no. Ecco il codice:
#include <Elegoo_GFX.h> // Core graphics library
#include <Elegoo_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h> // Touch screen driver
#include <EEPROM.h> // RAM Storage
#include <SD.h>
#include <SPI.h>
#include "tft_fundamental.h" // Qui stanno tutti i riferimenti sui pin.
File sdf;
Elegoo_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
byte hexStringToByte(const String& hexString) {
// Use strtol to convert the hexadecimal string to a long integer
long intValue = strtol(hexString.c_str(), NULL, 16);
// Ensure the value is within the valid byte range (0x00 to 0x7F)
if (intValue >= 0 && intValue <= 0x7F) {
return static_cast<byte>(intValue);
} else {
// Handle the case where the value is out of range
// You can choose to return a specific value or handle the error as needed.
// Here, we return 0 to indicate an error.
return 0;
}
}
void setup() {
tft.begin(0x9341);
Serial.begin(9600);
tft.setRotation(3);
tft.fillScreen(BLACK);
tft.setTextSize(2);
if (EEPROM.read(0) != 0x7C) {
tft.println("Wrong boot signature!");
tft.println("Change it and restart.");
tft.println("ROMWRITE v1.0");
tft.println(" ");
while (!Serial.available()) {}
String biteofeigthyseven = Serial.readStringUntil('\n');
byte eightyseven = hexStringToByte(biteofeigthyseven);
EEPROM.write(0, eightyseven);
tft.print("Changed to: 0x");
tft.println(eightyseven, HEX);
while (true) {
}
}
tft.println("READY");
int usedBytes = 0; // Initialize to 0
for (int i = 0; i < EEPROM.length(); i++) {
byte val = EEPROM.read(i);
if (val != 0) {
usedBytes++;
}
}
int freeBytes = EEPROM.length() - usedBytes;
// Print the free EEPROM memory
if (freeBytes >= 512) {
tft.print("EEPROM Free: ");
tft.print(freeBytes);
tft.println("B");
} else {
tft.print("EEPROM Free: ");
tft.print(freeBytes);
tft.println("B");
tft.println(" ");
tft.println("WARNING! EEPROM is almost full!");
tft.println("Run eeprom.format to\nformat it.");
}
curGet();
}
void loop() {
if (Serial.available() > 0) {
String command = Serial.readStringUntil('\n'); // Read the incoming serial data until newline character
curReply(command);
if (command == "shape.wave") {
tft.println("WAVE mode");
} else if (command == "shape.sawtooth") {
tft.println("SAWTOOTH mode");
} else if (command == "shape.sawtooth") {
tft.println("SAWTOOTH mode");
} else if (command == "eeprom.format") {
float timer = millis() / 1000;
tft.println("Formatting all bytes...");
for (int i = 0; i < EEPROM.length(); i++) {
EEPROM.write(i, 0);
}
float timed = millis() / 1000;
tft.fillScreen(BLACK);
tft.setCursor(0, 0);
tft.println(" ");
tft.print("Formatted! Time: ");
tft.print(timed - timer, 1);
tft.println(" sec");
tft.print("New EEPROM Free: ");
int usedBytes = 0; // Initialize to 0
for (int i = 0; i < EEPROM.length(); i++) {
byte val = EEPROM.read(i);
if (val != 0) {
usedBytes++;
}
}
int freeBytes = EEPROM.length() - usedBytes;
tft.print(freeBytes);
tft.println("B");
} else if (command == "cls") {
tft.fillScreen(BLACK);
tft.setCursor(0, 0);
tft.println(" ");
} else if (command == "eeprom.detect") {
bool detectedByte = false;
int bits = 0;
tft.fillScreen(BLACK);
tft.setCursor(0, 0);
tft.println(" ");
for (int i = 0; i < EEPROM.length(); i++) {
byte val = EEPROM.read(i);
if (val != 0) {
detectedByte = true;
bits++;
tft.print("Sector ");
tft.print(i);
tft.print(": ");
tft.write(val);
tft.println(" ");
}
}
if (detectedByte) {
tft.println("Bytes found: " + String(bits));
} else {
tft.println("Nothing detected.");
}
} else if (command == "eeprom.detect.hex") {
bool detectedByte = false;
int bits = 0;
tft.fillScreen(BLACK);
tft.setCursor(0, 0);
tft.println(" ");
for (int i = 0; i < EEPROM.length(); i++) {
byte val = EEPROM.read(i);
if (val != 0) {
detectedByte = true;
bits++;
tft.print("Sector ");
tft.print(i);
tft.print(": 0x");
tft.println(val, HEX);
}
}
if (detectedByte) {
tft.println("Bytes found: " + String(bits));
} else {
tft.println("Nothing detected.");
}
} else if (command == "eeprom.detect.dec") {
bool detectedByte = false;
int bits = 0;
tft.fillScreen(BLACK);
tft.setCursor(0, 0);
tft.println(" ");
for (int i = 0; i < EEPROM.length(); i++) {
byte val = EEPROM.read(i);
if (val != 0) {
detectedByte = true;
bits++;
tft.print("Sector ");
tft.print(i);
tft.print(": ");
tft.println(val, DEC);
}
}
if (detectedByte) {
tft.println("Bytes found: " + String(bits));
} else {
tft.println("Nothing detected.");
}
} else if (command == "eeprom.diagram") {
tft.fillScreen(BLACK);
tft.setCursor(0, 0);
tft.println(" ");
tft.drawRect(30, 30, tft.width() - 60, 30, WHITE);
int usedBytes = 0;
for (int i = 0; i < EEPROM.length(); i++) {
byte val = EEPROM.read(i);
if (val != 0) {
usedBytes++;
}
}
usedBytes = map(usedBytes, 0, EEPROM.length(), 1, tft.width() - 62);
tft.fillRect(31, 31, tft.width() - 62, 28, BLUE);
tft.fillRect(31, 31, usedBytes, 28, GREEN);
tft.setCursor(30, 70);
tft.setTextColor(GREEN);
tft.println("Memory Used");
tft.setCursor(30, 90);
tft.setTextColor(BLUE);
tft.println("Memory Free");
tft.setTextColor(WHITE);
usedBytes = 0;
for (int i = 0; i < EEPROM.length(); i++) {
byte val = EEPROM.read(i);
if (val != 0) {
usedBytes++;
}
}
tft.setCursor(30, 110);
tft.print("Sectors used: ");
tft.print(usedBytes);
tft.print("/");
tft.print(EEPROM.length());
} else if (command == "eeprom.write") {
tft.println("ROMWRITE v1.0");
while (true) {
if (Serial.available() > 0) {
// Read the input from the serial monitor
String input = Serial.readStringUntil('\n');
// Check if the input is "q,q" and execute writeallzeros() if true
if (input == "q,q") {
break;
} else {
// Parse the input to extract the sector and value
int sector, value;
sscanf(input.c_str(), "%d,%d", §or, &value);
// Check if the sector is within valid bounds (0 to EEPROM size - 1)
if (sector >= 0 && sector < EEPROM.length()) {
// Write the value to the specified sector in EEPROM
EEPROM.write(sector, value);
tft.println("Writed.");
} else {
tft.println("Invalid sector.");
}
}
}
}
} else if (command == "eeprom.fill") {
tft.print("Enter start sector: ");
while (!Serial.available()) {} // Wait for user input
int startSector = Serial.readStringUntil('\n').toInt();
tft.println(startSector);
tft.print("Enter end sector: ");
while (!Serial.available()) {} // Wait for user input
int endSector = Serial.readStringUntil('\n').toInt();
tft.println(endSector);
tft.print("Enter value to fill: ");
while (!Serial.available()) {} // Wait for user input
int fillValue = Serial.readStringUntil('\n').toInt();
tft.println(fillValue);
tft.println(" ");
tft.println("Filling sectors...");
unsigned long startTime = millis(); // Record the start time
// Fill the EEPROM with the specified value within the specified range
for (int i = startSector; i <= endSector && i < EEPROM.length(); i++) {
EEPROM.write(i, fillValue);
}
unsigned long endTime = millis(); // Record the end time
float elapsedTime = (endTime - startTime) / 1000; // Calculate the elapsed time
tft.print("Filled! Time: ");
tft.print(elapsedTime, 1);
tft.println(" sec");
} else if (command == "bootsig") {
tft.print("bootsig=0x");
tft.println(EEPROM.read(0), HEX);
} else if (command == "sd.mount") {
sdmnt(10);
} else if (command == "sd.read.eeprom") {
tft.fillScreen(BLACK);
tft.setCursor(0, 0);
sdf = SD.open("/eeprom.txt");
if (!sdf) {
tft.println("Error.");
}
while (sdf.available()) {
tft.write(sdf.read());
}
sdf.close();
} else if (command == "eeprom.to-sd.mix") {
sdmnt(10);
sdf = SD.open("/eeprom.txt", FILE_WRITE);
if (!sdf) {
tft.println("Error while opening.");
}
tft.println("Method: DEC > ASCII");
tft.println("Transferring data...");
int startTime = millis() / 1000;
for (int i = 0; i < EEPROM.length(); i++) {
byte value = EEPROM.read(i);
if (value == 0) {
break; // Stop writing when a '0' is encountered
}
sdf.write(value); // Convert and write as ASCII
}
sdf.close();
int endTime = millis() / 1000;
float timer = endTime - startTime;
tft.print("Transfer time: ");
tft.print(timer, 1);
tft.println(" sec");
} else {
tft.println("Invalid command");
}
curGet();
}
}
void curGet() {
tft.println(" ");
tft.print(">");
}
void curReply(String msg) {
tft.println(msg);
tft.print("<");
}
void sdmnt(int num) {
pinMode(10, OUTPUT);
delay(1000);
if (!SD.begin(num)) {
tft.println("Error while mounting.");
} else {
tft.println("SD mounted.");
}
}