Skip to content

Commit

Permalink
Adds busy light - fixes crash on back
Browse files Browse the repository at this point in the history
  • Loading branch information
cgreening committed Oct 13, 2024
1 parent e507698 commit d7b05ac
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 28 deletions.
80 changes: 54 additions & 26 deletions firmware/src/Files/Files.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <dirent.h>
#include <algorithm>
#include <memory>
#include <map>
#include <vector>
#include <string>
#include <sstream>
Expand All @@ -14,23 +15,43 @@
#include <sys/stat.h>
#include <unistd.h>

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
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -201,6 +222,7 @@ class Files
{
private:
FileSystemT *fileSystem;

public:
Files(FileSystemT *fileSystem) : fileSystem(fileSystem)
{
Expand All @@ -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;
Expand All @@ -223,17 +247,19 @@ 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());
}
}

FileLetterCountVector getFileLetters(const char *folder, const std::vector<std::string> &extensions)
{
auto bl = BusyLight();
FileLetterCountVector fileLetters;
if (!fileSystem->isMounted()) {
if (!fileSystem->isMounted())
{
return fileLetters;
}
std::string full_path = std::string(fileSystem->mountPoint()) + folder;
Expand All @@ -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())
{
Expand All @@ -266,8 +292,10 @@ class Files

FileInfoVector getFileStartingWithPrefix(const char *folder, const char *prefix, const std::vector<std::string> &extensions)
{
auto bl = BusyLight();
FileInfoVector files;
if (!fileSystem->isMounted()) {
if (!fileSystem->isMounted())
{
return files;
}
std::string full_path = std::string(fileSystem->mountPoint()) + folder;
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions firmware/src/Screens/EmulatorScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion firmware/src/Screens/PickerScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ class PickerScreen : public Screen
case JOYK_LEFT:
case SPECKEY_5:
{
onBack();
playKeyClick();
isHandled = true;
onBack();
break;
}
case JOYK_FIRE:
Expand All @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions firmware/src/Screens/SaveSnapshotScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion firmware/src/Screens/Screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down

0 comments on commit d7b05ac

Please sign in to comment.