Skip to content

Commit

Permalink
Merge pull request #59 from PaulFreund/feature/runtimeFeatureSettings
Browse files Browse the repository at this point in the history
Community feature settings in main settings menu
  • Loading branch information
jamiefaye committed Jun 27, 2023
2 parents 74856ca + 3acfe50 commit 203b128
Show file tree
Hide file tree
Showing 8 changed files with 514 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/Deluge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
#include "SaveSongUI.h"
#include "oled.h"
#include "ContextMenuOverwriteBootloader.h"
#include "RuntimeFeatureSettings.h"
#include "Deluge.h"

#if AUTOMATED_TESTER_ENABLED
Expand Down Expand Up @@ -801,6 +802,9 @@ extern "C" int main2(void) {
FlashStorage::writeSettings();
}

new (&runtimeFeatureSettings) RuntimeFeatureSettings;
runtimeFeatureSettings.readSettingsFromFile();

usbLock = 1;
openUSBHost();

Expand Down
67 changes: 67 additions & 0 deletions src/MenuItemRuntimeFeatureSetting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright © 2021-2023 Synthstrom Audible Limited
*
* This file is part of The Synthstrom Audible Deluge Firmware.
*
* The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/

#include <MenuItemRuntimeFeatureSetting.h>
#include "RuntimeFeatureSettings.h"
#include "soundeditor.h"

MenuItemRuntimeFeatureSetting runtimeFeatureSettingMenuItem;

MenuItemRuntimeFeatureSetting::MenuItemRuntimeFeatureSetting(char const* newName) : MenuItemSelection(newName) {
}

void MenuItemRuntimeFeatureSetting::readCurrentValue() {
for (uint32_t idx = 0; idx < RUNTIME_FEATURE_SETTING_MAX_OPTIONS; ++idx) {
if (runtimeFeatureSettings.settings[currentSettingIndex].options[idx].value
== runtimeFeatureSettings.settings[currentSettingIndex].value) {
soundEditor.currentValue = idx;
return;
}
}

soundEditor.currentValue = 0;
}

void MenuItemRuntimeFeatureSetting::writeCurrentValue() {
runtimeFeatureSettings.settings[currentSettingIndex].value =
runtimeFeatureSettings.settings[currentSettingIndex].options[soundEditor.currentValue].value;
}

char const** MenuItemRuntimeFeatureSetting::getOptions() {
static char const* options[RUNTIME_FEATURE_SETTING_MAX_OPTIONS] = {0};
uint32_t optionCount = 0;
for (uint32_t idx = 0; idx < RUNTIME_FEATURE_SETTING_MAX_OPTIONS; ++idx) {
if (runtimeFeatureSettings.settings[currentSettingIndex].options[idx].displayName != NULL) {
options[optionCount++] = runtimeFeatureSettings.settings[currentSettingIndex].options[idx].displayName;
}
else {
options[optionCount] = NULL;
break;
}
}
return (char const**)&options;
}

int MenuItemRuntimeFeatureSetting::getNumOptions() {
for (uint32_t idx = 0; idx < RUNTIME_FEATURE_SETTING_MAX_OPTIONS; ++idx) {
if (runtimeFeatureSettings.settings[currentSettingIndex].options[idx].displayName == NULL) {
return idx;
}
}

return 0;
}
38 changes: 38 additions & 0 deletions src/MenuItemRuntimeFeatureSetting.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright © 2021-2023 Synthstrom Audible Limited
*
* This file is part of The Synthstrom Audible Deluge Firmware.
*
* The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef MENUITEMRUNTIMEFEATURESETTING_H_
#define MENUITEMRUNTIMEFEATURESETTING_H_

#include "MenuItemSelection.h"

class MenuItemRuntimeFeatureSetting final : public MenuItemSelection {
public:
MenuItemRuntimeFeatureSetting(char const* newName = NULL);
void readCurrentValue();
void writeCurrentValue();
char const** getOptions();
int getNumOptions();

private:
friend class MenuItemRuntimeFeatureSettings;
uint32_t currentSettingIndex;
};

extern MenuItemRuntimeFeatureSetting runtimeFeatureSettingMenuItem;

#endif /* MENUITEMRUNTIMEFEATURESETTING_H_ */
119 changes: 119 additions & 0 deletions src/MenuItemRuntimeFeatureSettings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright © 2021-2023 Synthstrom Audible Limited
*
* This file is part of The Synthstrom Audible Deluge Firmware.
*
* The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/

#include <MenuItemRuntimeFeatureSettings.h>
#include <MenuItemRuntimeFeatureSetting.h>
#include "RuntimeFeatureSettings.h"
#include "soundeditor.h"
#include "numericdriver.h"
#include <cstdio>

MenuItemRuntimeFeatureSettings runtimeFeatureSettingsMenu;

void MenuItemRuntimeFeatureSettings::beginSession(MenuItem* navigatedBackwardFrom) {
if (!navigatedBackwardFrom) {
lastActiveValue = 0; // Reset last active value
}

soundEditor.currentValue = lastActiveValue; // Restore

#if HAVE_OLED
soundEditor.menuCurrentScroll = soundEditor.currentValue;
#else
drawValue();
#endif
}

void MenuItemRuntimeFeatureSettings::selectEncoderAction(int offset) {
soundEditor.currentValue += offset;
int numOptions = RuntimeFeatureSettingType::MaxElement;

#if HAVE_OLED
if (soundEditor.currentValue > numOptions - 1) soundEditor.currentValue = numOptions - 1;
else if (soundEditor.currentValue < 0) soundEditor.currentValue = 0;
#else
if (soundEditor.currentValue >= numOptions) soundEditor.currentValue -= numOptions;
else if (soundEditor.currentValue < 0) soundEditor.currentValue += numOptions;
#endif

lastActiveValue = soundEditor.currentValue;

#if HAVE_OLED
if (soundEditor.currentValue < soundEditor.menuCurrentScroll)
soundEditor.menuCurrentScroll = soundEditor.currentValue;

if (offset >= 0) {
int d = soundEditor.currentValue;
int numSeen = 1;
while (true) {
d--;
if (d == soundEditor.menuCurrentScroll) break;
numSeen++;
if (numSeen >= OLED_MENU_NUM_OPTIONS_VISIBLE) {
soundEditor.menuCurrentScroll = d;
break;
}
}
}
#endif

drawValue();
}

void MenuItemRuntimeFeatureSettings::drawValue() {
#if HAVE_OLED
renderUIsForOled();
#else
numericDriver.setScrollingText(runtimeFeatureSettings.settings[soundEditor.currentValue].displayName);
#endif
}

MenuItem* MenuItemRuntimeFeatureSettings::selectButtonPress() {
#if HAVE_OLED
runtimeFeatureSettingMenuItem.basicTitle = runtimeFeatureSettings.settings[soundEditor.currentValue]
.displayName; // A bit ugly, but saves us extending a class.
#endif
runtimeFeatureSettingMenuItem.currentSettingIndex = soundEditor.currentValue;
return &runtimeFeatureSettingMenuItem;
}

#if HAVE_OLED

void MenuItemRuntimeFeatureSettings::drawPixelsForOled() {
char const* itemNames[OLED_MENU_NUM_OPTIONS_VISIBLE];

int selectedRow = -1;

int displayRow = soundEditor.menuCurrentScroll;
int row = 0;
while (row < OLED_MENU_NUM_OPTIONS_VISIBLE && displayRow < RuntimeFeatureSettingType::MaxElement) {
itemNames[row] = runtimeFeatureSettings.settings[displayRow].displayName;
if (displayRow == soundEditor.currentValue) {
selectedRow = row;
}
row++;
displayRow++;
}

while (row < OLED_MENU_NUM_OPTIONS_VISIBLE) {
itemNames[row++] = NULL;
}

drawItemsForOled(itemNames, selectedRow);
}

#endif
39 changes: 39 additions & 0 deletions src/MenuItemRuntimeFeatureSettings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright © 2021-2023 Synthstrom Audible Limited
*
* This file is part of The Synthstrom Audible Deluge Firmware.
*
* The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef MENUITEMRUNTIMEFEATURESETTINGS_H_
#define MENUITEMRUNTIMEFEATURESETTINGS_H_

#include "MenuItem.h"

class MenuItemRuntimeFeatureSettings final : public MenuItem {
public:
MenuItemRuntimeFeatureSettings(char const* newName = 0) : MenuItem(newName) {}
void beginSession(MenuItem* navigatedBackwardFrom = NULL);
void selectEncoderAction(int offset);
void drawValue();
MenuItem* selectButtonPress();
void drawPixelsForOled();

private:
uint32_t lastActiveValue = 0;
// getSetting?
};

extern MenuItemRuntimeFeatureSettings runtimeFeatureSettingsMenu;

#endif /* MENUITEMRUNTIMEFEATURESETTINGS_H_ */

0 comments on commit 203b128

Please sign in to comment.