From b2578512e5b8476021d0a5eb6392d1bfc6c691b5 Mon Sep 17 00:00:00 2001 From: Gabriel Marcano Date: Sun, 22 Dec 2024 22:54:38 -0800 Subject: Update Cart_Reader.ino - Remove incomingByte global variable, mark all uses as local variables. This leads to a tiny (yet measurable) decrease in global variable use, and no change in sketch memory use. Of course, the variable now resides in the stack, but only while the functions using it exist, which isn't always. --- Cart_Reader/Cart_Reader.ino | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Cart_Reader/Cart_Reader.ino b/Cart_Reader/Cart_Reader.ino index 2ee979c..1dc7f44 100644 --- a/Cart_Reader/Cart_Reader.ino +++ b/Cart_Reader/Cart_Reader.ino @@ -267,11 +267,6 @@ boolean holdEventPast2 = false; // whether or not the hold event happened a boolean longholdEventPast2 = false; // whether or not the long hold event happened already #endif -#ifdef ENABLE_SERIAL -// For incoming serial data -int incomingByte; -#endif - // Variables for the menu int choice = 0; // Temporary array that holds the menu option read out of progmem @@ -2955,7 +2950,7 @@ byte questionBox_Serial(const __FlashStringHelper* question __attribute__((unuse } // Read the incoming byte: - incomingByte = Serial.read() - 48; + int incomingByte = Serial.read() - 48; // Page up (u) if (incomingByte == 69) { @@ -3197,7 +3192,7 @@ void checkUpdater() { uint8_t checkButton() { while (Serial.available() == 0) { } - incomingByte = Serial.read() - 48; + int incomingByte = Serial.read() - 48; //Next if (incomingByte == 52) { @@ -3223,7 +3218,7 @@ void wait_serial() { } while (Serial.available() == 0) { } - incomingByte = Serial.read() - 48; + int incomingByte = Serial.read() - 48; /* if ((incomingByte == 53) && (fileName[0] != '\0')) { // Open file on sd card sd.chdir(folder); -- cgit v1.2.3 From 62b8182dee2e4c0bc326c1b8a23a1f15365fa65c Mon Sep 17 00:00:00 2001 From: Gabriel Marcano Date: Sun, 22 Dec 2024 23:16:14 -0800 Subject: Update Cart_Reader.ino - Don't use `Serial.read() - 48` to extract chars. Just treat the data as chars. This is more legible, and actually reduced the program storage space utilization slightly. --- Cart_Reader/Cart_Reader.ino | 54 +++++++++++---------------------------------- 1 file changed, 13 insertions(+), 41 deletions(-) diff --git a/Cart_Reader/Cart_Reader.ino b/Cart_Reader/Cart_Reader.ino index 1dc7f44..f83ea2d 100644 --- a/Cart_Reader/Cart_Reader.ino +++ b/Cart_Reader/Cart_Reader.ino @@ -2949,11 +2949,11 @@ byte questionBox_Serial(const __FlashStringHelper* question __attribute__((unuse while (Serial.available() == 0) { } - // Read the incoming byte: - int incomingByte = Serial.read() - 48; + // Read the incoming byte (can't be -1, as there must be data available) + char incomingByte = Serial.read(); // Page up (u) - if (incomingByte == 69) { + if (incomingByte == 'u') { if (currPage > 1) { lastPage = currPage; currPage--; @@ -2963,7 +2963,7 @@ byte questionBox_Serial(const __FlashStringHelper* question __attribute__((unuse } // Page down (d) - else if (incomingByte == 52) { + else if (incomingByte == 'd') { if (numPages > currPage) { lastPage = currPage; currPage++; @@ -2971,14 +2971,14 @@ byte questionBox_Serial(const __FlashStringHelper* question __attribute__((unuse } // Execute choice - else if ((incomingByte >= 0) && (incomingByte < 7)) { + else if ((incomingByte >= '0') && (incomingByte < '7')) { numPages = 0; } // Print the received byte for validation e.g. in case of a different keyboard mapping //Serial.println(incomingByte); //Serial.println(FS(FSTRING_EMPTY)); - return incomingByte; + return incomingByte - '0'; } #endif @@ -3192,20 +3192,21 @@ void checkUpdater() { uint8_t checkButton() { while (Serial.available() == 0) { } - int incomingByte = Serial.read() - 48; + // read() can't return -1 since there's data available. + char incomingByte = Serial.read(); //Next - if (incomingByte == 52) { + if (incomingByte == 'd') { return 1; } //Previous - else if (incomingByte == 69) { + else if (incomingByte == 'u') { return 2; } //Selection - else if ((incomingByte == 240) || (incomingByte == -16) || (incomingByte == 0)) { + else if ((incomingByte == ' ') || (incomingByte == '0')) { return 3; } @@ -3218,37 +3219,8 @@ void wait_serial() { } while (Serial.available() == 0) { } - int incomingByte = Serial.read() - 48; - /* if ((incomingByte == 53) && (fileName[0] != '\0')) { - // Open file on sd card - sd.chdir(folder); - if (myFile.open(fileName, O_READ)) { - // Get rom size from file - fileSize = myFile.fileSize(); - - // Send filesize - char tempStr[16]; - sprintf(tempStr, "%d", fileSize); - Serial.write(tempStr); - - // Wait for ok - while (Serial.available() == 0) { - } - - // Send file - for (unsigned long currByte = 0; currByte < fileSize; currByte++) { - // Blink led - if (currByte % 1024 == 0) - blinkLED(); - Serial.write(myFile.read()); - } - // Close the file: - myFile.close(); - } - else { - print_FatalError(open_file_STR); - } - }*/ + // Result is ignored, so don't even bother putting it in a variable + Serial.read(); } #endif -- cgit v1.2.3 From 9f92f6614d45e8ca5cd6bb1e4c1acacce6488750 Mon Sep 17 00:00:00 2001 From: Gabriel Marcano Date: Sun, 22 Dec 2024 23:30:38 -0800 Subject: Update Cart_Reader.ino - Add support for simulating a long press for the serial interface by seinding `l` or `L`. This lets someone actually run the SNES clock calibration using the serial interface. --- Cart_Reader/Cart_Reader.ino | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Cart_Reader/Cart_Reader.ino b/Cart_Reader/Cart_Reader.ino index f83ea2d..8ff5c1c 100644 --- a/Cart_Reader/Cart_Reader.ino +++ b/Cart_Reader/Cart_Reader.ino @@ -3210,6 +3210,11 @@ uint8_t checkButton() { return 3; } + //Long Press (simulate) + else if ((incomingByte == 'l') || (incomingByte == 'L')) { + return 4; + } + return 0; } -- cgit v1.2.3