From d7b05ac36dc638796b4204f2290d4c4cae0dfc7a Mon Sep 17 00:00:00 2001 From: Chris Greening Date: Sun, 13 Oct 2024 19:52:42 +0100 Subject: [PATCH] Adds busy light - fixes crash on back --- firmware/src/Files/Files.h | 80 +++++++++++++++-------- firmware/src/Screens/EmulatorScreen.cpp | 2 + firmware/src/Screens/PickerScreen.h | 3 +- firmware/src/Screens/SaveSnapshotScreen.h | 1 + firmware/src/Screens/Screen.h | 3 +- 5 files changed, 61 insertions(+), 28 deletions(-) diff --git a/firmware/src/Files/Files.h b/firmware/src/Files/Files.h index 31981c4..1e768a4 100644 --- a/firmware/src/Files/Files.h +++ b/firmware/src/Files/Files.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -14,23 +15,43 @@ #include #include -std::string upcase(const std::string &str) +class StringUtils { - std::string result = str; - std::transform(result.begin(), result.end(), result.begin(), - [](unsigned char c) - { return std::toupper(c); }); - return result; -} - -std::string downcase(const std::string &str) +public: + static std::string upcase(const std::string &str) + { + std::string result = str; + std::transform(result.begin(), result.end(), result.begin(), + [](unsigned char c) + { return std::toupper(c); }); + return result; + } + + static std::string downcase(const std::string &str) + { + std::string result = str; + std::transform(result.begin(), result.end(), result.begin(), + [](unsigned char c) + { return std::tolower(c); }); + return result; + } +}; + +class BusyLight { - std::string result = str; - std::transform(result.begin(), result.end(), result.begin(), - [](unsigned char c) - { return std::tolower(c); }); - return result; -} +public: + BusyLight() + { + pinMode(GPIO_NUM_1, OUTPUT); + digitalWrite(GPIO_NUM_1, HIGH); + Serial.printf("Busy light on: %ld\n", millis()); + } + ~BusyLight() + { + digitalWrite(GPIO_NUM_1, LOW); + Serial.printf("Busy light off: %ld\n", millis()); + } +}; // Iterator for directory entries with optional file extension and prefix filter class DirectoryIterator @@ -124,13 +145,13 @@ class DirectoryIterator return false; // Hidden file } - std::string lowerCaseFilename = downcase(filename); + std::string lowerCaseFilename = StringUtils::downcase(filename); if (extensions.size() > 0) { bool validExtension = false; for (const std::string &extension : extensions) { - std::string lowerCaseExtension = downcase(extension); + std::string lowerCaseExtension = StringUtils::downcase(extension); if (lowerCaseFilename.length() >= lowerCaseExtension.length() && lowerCaseFilename.substr(lowerCaseFilename.length() - lowerCaseExtension.length()) == lowerCaseExtension) { @@ -145,7 +166,7 @@ class DirectoryIterator } if (prefix != nullptr) { - std::string lowerCasePrefix = downcase(prefix); + std::string lowerCasePrefix = StringUtils::downcase(prefix); if (lowerCaseFilename.length() < lowerCasePrefix.length() || lowerCaseFilename.substr(0, lowerCasePrefix.length()) != lowerCasePrefix) { @@ -201,6 +222,7 @@ class Files { private: FileSystemT *fileSystem; + public: Files(FileSystemT *fileSystem) : fileSystem(fileSystem) { @@ -213,7 +235,9 @@ class Files void createDirectory(const char *folder) { - if (!fileSystem->isMounted()) { + auto bl = BusyLight(); + if (!fileSystem->isMounted()) + { return; } std::string full_path = std::string(fileSystem->mountPoint()) + folder; @@ -223,8 +247,8 @@ class Files { Serial.printf("Creating folder %s\n", full_path.c_str()); mkdir(full_path.c_str(), 0777); - } - else + } + else { Serial.printf("Folder %s already exists\n", full_path.c_str()); } @@ -232,8 +256,10 @@ class Files FileLetterCountVector getFileLetters(const char *folder, const std::vector &extensions) { + auto bl = BusyLight(); FileLetterCountVector fileLetters; - if (!fileSystem->isMounted()) { + if (!fileSystem->isMounted()) + { return fileLetters; } std::string full_path = std::string(fileSystem->mountPoint()) + folder; @@ -244,9 +270,9 @@ class Files for (DirectoryIterator it(full_path, nullptr, extensions); it != DirectoryIterator(); ++it) { std::string filename = it->d_name; - std::string lowerCaseFilename = downcase(filename); + std::string lowerCaseFilename = StringUtils::downcase(filename); // get the first letter - auto name = upcase(filename); + auto name = StringUtils::upcase(filename); auto letter = name.substr(0, 1); if (fileCountByLetter.find(letter) == fileCountByLetter.end()) { @@ -266,8 +292,10 @@ class Files FileInfoVector getFileStartingWithPrefix(const char *folder, const char *prefix, const std::vector &extensions) { + auto bl = BusyLight(); FileInfoVector files; - if (!fileSystem->isMounted()) { + if (!fileSystem->isMounted()) + { return files; } std::string full_path = std::string(fileSystem->mountPoint()) + folder; @@ -279,7 +307,7 @@ class Files for (DirectoryIterator it(full_path, prefix, extensions); it != DirectoryIterator(); ++it) { - files.push_back(FileInfoPtr(new FileInfo(upcase(it->d_name), full_path + "/" + it->d_name))); + files.push_back(FileInfoPtr(new FileInfo(StringUtils::upcase(it->d_name), full_path + "/" + it->d_name))); } // sort the files - is this needed? Maybe they are already alphabetically sorted std::sort(files.begin(), files.end(), [](FileInfoPtr a, FileInfoPtr b) diff --git a/firmware/src/Screens/EmulatorScreen.cpp b/firmware/src/Screens/EmulatorScreen.cpp index 15c3f83..8e660b0 100644 --- a/firmware/src/Screens/EmulatorScreen.cpp +++ b/firmware/src/Screens/EmulatorScreen.cpp @@ -3,6 +3,7 @@ #include "../Emulator/spectrum.h" #include "../Emulator/snaps.h" #include "../AudioOutput/AudioOutput.h" +#include "../Files/Files.h" #include "EmulatorScreen.h" #include "NavigationStack.h" #include "SaveSnapshotScreen.h" @@ -213,6 +214,7 @@ EmulatorScreen::EmulatorScreen(TFTDisplay &tft, AudioOutput *audioOutput) : Scre void EmulatorScreen::run(std::string snaPath) { + auto bl = BusyLight(); // audioFile = fopen("/fs/audio.raw", "wb"); memset(pixelBuffer, 0, screenWidth * 8 * sizeof(uint16_t)); memset(screenBuffer, 0, 6192); diff --git a/firmware/src/Screens/PickerScreen.h b/firmware/src/Screens/PickerScreen.h index c9b7ec7..ee69bfe 100644 --- a/firmware/src/Screens/PickerScreen.h +++ b/firmware/src/Screens/PickerScreen.h @@ -83,9 +83,9 @@ class PickerScreen : public Screen case JOYK_LEFT: case SPECKEY_5: { - onBack(); playKeyClick(); isHandled = true; + onBack(); break; } case JOYK_FIRE: @@ -99,6 +99,7 @@ class PickerScreen : public Screen // does the speckey map onto a letter - look in the mapping table if (specKeyToLetter.find(key) != specKeyToLetter.end()) { + playKeyClick(); char letter = specKeyToLetter.at(key); if (millis() - lastSearchPrefix > 500) { diff --git a/firmware/src/Screens/SaveSnapshotScreen.h b/firmware/src/Screens/SaveSnapshotScreen.h index 1fade21..6490ecb 100644 --- a/firmware/src/Screens/SaveSnapshotScreen.h +++ b/firmware/src/Screens/SaveSnapshotScreen.h @@ -34,6 +34,7 @@ class SaveSnapshotScreen : public Screen case JOYK_FIRE: case SPECKEY_ENTER: if (filename.length() > 0) { + auto bl = BusyLight(); drawBusy(); saveZ80(machine, ("/fs/snapshots/" + filename + ".Z80").c_str()); playSuccessBeep(); diff --git a/firmware/src/Screens/Screen.h b/firmware/src/Screens/Screen.h index 78f5e16..7c937bb 100644 --- a/firmware/src/Screens/Screen.h +++ b/firmware/src/Screens/Screen.h @@ -13,6 +13,8 @@ class TFTDisplay; class AudioOutput; class NavigationStack; +const uint8_t click[] = { 50, 50, 50, 0 }; + class Screen { protected: NavigationStack *m_navigationStack = nullptr; @@ -24,7 +26,6 @@ class Screen { virtual void updatekey(SpecKeys key, uint8_t state) {}; virtual void pressKey(SpecKeys key) {}; void playKeyClick() { - uint8_t click[] = { 50, 50, 50, 0 }; m_audioOutput->write(click, 4); } void playErrorBeep() {