Skip to content

Commit

Permalink
Firmware switching menu reads eligible alt firmwares from disk
Browse files Browse the repository at this point in the history
  • Loading branch information
Dewb committed Sep 14, 2023
1 parent 80ac01e commit 2e32c99
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
17 changes: 17 additions & 0 deletions src/common/core/FirmwareManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <iostream>
#include <stdlib.h>
#include <unordered_set>
#include <ghc/filesystem.hpp>

namespace fs = ghc::filesystem;

extern rack::Plugin* pluginInstance;

Expand Down Expand Up @@ -108,6 +111,14 @@ struct FirmwareManagerImpl

std::string librarySource;
librarySource = rack::asset::plugin(pluginInstance, "res/firmware/" + firmwareName + LIB_EXTENSION);

std::error_code ec;
if (!fs::is_regular_file(fs::status(librarySource, ec)))
{
WARN("Requested firmware not found or invalid");
return false;
}

std::string libraryToLoad = librarySource;

// If we have already loaded this firmware at least once, create a temp copy so it will have its own address space
Expand Down Expand Up @@ -230,12 +241,18 @@ FirmwareManager::~FirmwareManager()
delete impl;
}

const std::string FirmwareManager::getLibExtension()
{
return LIB_EXTENSION;
}

bool FirmwareManager::load(std::string firmwareName)
{
delete impl;
impl = new FirmwareManagerImpl();
if (!impl->load(firmwareName))
{
delete impl;
impl = nullptr;
WARN("Could not load firmware %s", firmwareName.c_str());
return false;
Expand Down
2 changes: 2 additions & 0 deletions src/common/core/FirmwareManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ struct FirmwareManager
FirmwareManager();
~FirmwareManager();

const std::string getLibExtension();

bool load(std::string firmwareName);
void unload();

Expand Down
39 changes: 26 additions & 13 deletions src/common/core/LibAVR32ModuleWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include "SerialOscInterface.hpp"
#include "Screenshot.hpp"

#include <ghc/filesystem.hpp>

namespace fs = ghc::filesystem;

using namespace rack;

struct ConnectGridItem : rack::ui::MenuItem
Expand Down Expand Up @@ -37,15 +41,27 @@ struct SwitchFirmwareItem : rack::ui::MenuItem

ui::Menu* createChildMenu() override
{
ui::Menu* menu = new ui::Menu;
// duplicate filenames that may be left in res folder by prior versions
std::vector<std::string> ignoreList = {"teletype", "ansible 2", "earthsea 2", "meadowphysics 2", "teletype 2", "whitewhale 2"};
std::vector<std::string> fwNames = {};

// TODO: populate this from looking in the firmware folder for binaries starting with the module basename
std::string tt_names[] = { "teletype4", "teletype5" };
std::string ww_names[] = { "whitewhale", "whitewhale-kria" };
const fs::path fwPath{rack::asset::plugin(pluginInstance, "res/firmware")};
for (auto const& file : fs::directory_iterator{fwPath})
{
auto name = file.path().stem().string();
auto extension = file.path().extension().string();
if (extension == module->firmware.getLibExtension() &&
name.substr(0, module->firmwarePrefix.size()) == module->firmwarePrefix &&
std::find(std::begin(ignoreList), std::end(ignoreList), name) == std::end(ignoreList))
{
fwNames.push_back(name);
}
}

for (int i = 0; i < 2; i++)
ui::Menu* menu = new ui::Menu;

for (auto const& name : fwNames)
{
std::string name = module->firmwarePrefix == "teletype" ? tt_names[i] : ww_names[i];
menu->addChild(createCheckMenuItem(
name,
"",
Expand Down Expand Up @@ -120,13 +136,10 @@ struct FirmwareSubmenuItem : MenuItem

menu->addChild(new MenuSeparator());

// TODO: enable for other modules
if (m->firmwarePrefix == "teletype" || m->firmwarePrefix == "whitewhale") {
menu->addChild(construct<SwitchFirmwareItem>(
&MenuItem::text, "Switch Firmware", &MenuItem::rightText, RIGHT_ARROW,
&SwitchFirmwareItem::module, m
));
}
menu->addChild(construct<SwitchFirmwareItem>(
&MenuItem::text, "Switch Firmware", &MenuItem::rightText, RIGHT_ARROW,
&SwitchFirmwareItem::module, m
));

auto reloadItem = new ReloadFirmwareItem();
reloadItem->text = "Reload & Restart";
Expand Down

0 comments on commit 2e32c99

Please sign in to comment.