Move menu item values to pointers pointing to the variable to be changed

This commit is contained in:
Jan-Henrik 2020-02-23 14:25:36 +01:00
parent 8315de5d05
commit 5182a9b495
6 changed files with 80 additions and 65 deletions

View file

@ -16,25 +16,25 @@ template <class T>
class MenuItem : public AbstractMenuItem {
private:
const char* label;
T value;
T* value;
T step;
char stringRepresentation[24];
protected:
MenuItem(const char* _label, T _initialValue)
MenuItem(const char* _label, T* _value)
: label(_label)
, value(_initialValue) {};
, value(_value) {};
virtual void to_string(char* buf) = 0;
void set_value(T value)
{
this->value = value;
*this->value = value;
this->to_string(stringRepresentation);
};
public:
T get_value()
T* get_value_ptr()
{
return value;
}
@ -60,8 +60,8 @@ class NumberMenuItem : public MenuItem<T> {
T step;
protected:
NumberMenuItem(const char* _label, T _initialValue, T _minimumValue, T _maximumValue, T _step)
: MenuItem<T>(_label, _initialValue)
NumberMenuItem(const char* _label, T* _value, T _minimumValue, T _maximumValue, T _step)
: MenuItem<T>(_label, _value)
, minimumValue(_minimumValue)
, maximumValue(_maximumValue)
, step(_step) {};
@ -70,24 +70,24 @@ class NumberMenuItem : public MenuItem<T> {
void to_string(char* buf)
{
sprintf(buf, this->get_format_string(), this->get_value());
sprintf(buf, this->get_format_string(), *this->get_value_ptr());
}
public:
void increase()
{
if (this->get_value() + step <= maximumValue && this->get_value() + step >= minimumValue)
this->set_value(this->get_value() + step);
if (*this->get_value_ptr() + step <= maximumValue && *this->get_value_ptr() + step >= minimumValue)
this->set_value(*this->get_value_ptr() + step);
};
void decrease()
{
if (this->get_value() - step >= minimumValue && this->get_value() - step <= maximumValue)
this->set_value(this->get_value() - step);
if (*this->get_value_ptr() - step >= minimumValue && *this->get_value_ptr() - step <= maximumValue)
this->set_value(*this->get_value_ptr() - step);
};
};
class UIntMenuItem : public NumberMenuItem<uint32_t> {
class UInt32MenuItem : public NumberMenuItem<uint32_t> {
private:
protected:
const char* get_format_string()
@ -96,11 +96,11 @@ class UIntMenuItem : public NumberMenuItem<uint32_t> {
}
public:
UIntMenuItem(const char* _label, uint32_t _initialValue, uint32_t _minimumValue, uint32_t _maximumValue, uint32_t _step)
: NumberMenuItem(_label, _initialValue, _minimumValue, _maximumValue, _step) {};
UInt32MenuItem(const char* _label, uint32_t* _value, uint32_t _minimumValue, uint32_t _maximumValue, uint32_t _step)
: NumberMenuItem(_label, _value, _minimumValue, _maximumValue, _step) {};
};
class IntMenuItem : public NumberMenuItem<int32_t> {
class Int32MenuItem : public NumberMenuItem<int32_t> {
private:
protected:
const char* get_format_string()
@ -109,8 +109,8 @@ class IntMenuItem : public NumberMenuItem<int32_t> {
}
public:
IntMenuItem(const char* _label, int32_t _initialValue, int32_t _minimumValue, int32_t _maximumValue, int32_t _step)
: NumberMenuItem(_label, _initialValue, _minimumValue, _maximumValue, _step) {};
Int32MenuItem(const char* _label, int32_t* _value, int32_t _minimumValue, int32_t _maximumValue, int32_t _step)
: NumberMenuItem(_label, _value, _minimumValue, _maximumValue, _step) {};
};
class FloatMenuItem : public NumberMenuItem<float> {
@ -122,8 +122,8 @@ class FloatMenuItem : public NumberMenuItem<float> {
}
public:
FloatMenuItem(const char* _label, float _initialValue, float _minimumValue, float _maximumValue, float _step)
: NumberMenuItem(_label, _initialValue, _minimumValue, _maximumValue, _step) {};
FloatMenuItem(const char* _label, float* _value, float _minimumValue, float _maximumValue, float _step)
: NumberMenuItem(_label, _value, _minimumValue, _maximumValue, _step) {};
};
class BoolMenuItem : public NumberMenuItem<bool> {
@ -135,7 +135,7 @@ class BoolMenuItem : public NumberMenuItem<bool> {
protected:
const char* get_format_string()
{
bool value = this->get_value();
bool value = *this->get_value_ptr();
if (value)
return this->on_string;
@ -144,25 +144,25 @@ class BoolMenuItem : public NumberMenuItem<bool> {
}
public:
BoolMenuItem(const char* _label, bool _initialValue, const char* _on_string, const char* _off_string)
: NumberMenuItem(_label, _initialValue, 0, 1, 1)
BoolMenuItem(const char* _label, bool* _value, const char* _on_string, const char* _off_string)
: NumberMenuItem(_label, _value, 0, 1, 1)
, on_string(_on_string)
, off_string(_off_string) {};
};
class StringListMenuItem : public NumberMenuItem<uint32_t> {
class StringListMenuItem : public NumberMenuItem<uint8_t> {
private:
const char** string_labels;
protected:
const char* get_format_string()
{
return this->string_labels[this->get_value()];
return this->string_labels[*this->get_value_ptr()];
}
public:
StringListMenuItem(const char* _label, uint32_t _initialValue, const char** _stringLabels, size_t _itemCount)
: NumberMenuItem(_label, _initialValue, 0, _itemCount - 1, 1)
StringListMenuItem(const char* _label, uint8_t* _value, const char** _stringLabels, size_t _itemCount)
: NumberMenuItem(_label, _value, 0, _itemCount - 1, 1)
, string_labels(_stringLabels) {};
};
@ -177,7 +177,7 @@ class MidiNoteMenuItem : public NumberMenuItem<uint8_t> {
public:
char* get_string_representation()
{
uint8_t currentNote = this->get_value();
uint8_t currentNote = *this->get_value_ptr();
int note = currentNote % 12;
int octave = (currentNote / 12) - 1;
@ -186,8 +186,8 @@ class MidiNoteMenuItem : public NumberMenuItem<uint8_t> {
return this->string_buffer;
}
MidiNoteMenuItem(const char* _label, uint8_t _initialValue)
: NumberMenuItem(_label, _initialValue, 0, 127, 1)
MidiNoteMenuItem(const char* _label, uint8_t* _value)
: NumberMenuItem(_label, _value, 0, 127, 1)
{
note_strings[0] = "C";
note_strings[1] = "C#";

View file

@ -5,41 +5,49 @@
#define TOTAL_COLUMN_COUNT 4
typedef enum { MIDI_THRU_OFF = 0,
typedef enum : uint8_t {
MIDI_THRU_OFF = 0,
MIDI_THRU_ON = 1,
MIDI_THRU_POLYCHAIN = 2 } MIDIThruMode_t;
MIDI_THRU_POLYCHAIN = 2
} MIDIThruMode_t;
typedef enum {
typedef enum : uint8_t {
MIDI_INPUT_OMNI = 0,
MIDI_INPUT_MIDI = 1,
MIDI_INPUT_USB = 2
} MIDIInput_t;
typedef enum : uint32_t {
BI_OFF = 0,
BI_PITCH_UNI,
BI_PITCH_BI
BI_PITCH_UNI = 1,
BI_PITCH_BI = 2
} BiOutputType_t;
typedef enum {
typedef enum : uint32_t {
UNI_OFF = 0,
UNI_PITCH,
UNI_VELOCITY,
UNI_MODULATION,
UNI_AFTERTOUCH,
UNI_BREATH,
UNI_EXP,
UNI_GATE
UNI_PITCH = 1,
UNI_VELOCITY = 2,
UNI_MODULATION = 3,
UNI_AFTERTOUCH = 4,
UNI_BREATH = 5,
UNI_EXP = 6,
UNI_GATE = 7
} UniOutputType_t;
typedef enum {
typedef enum : uint32_t {
GATE_OFF = 0,
GATE_GATE,
GATE_TRIGGER
GATE_GATE = 2,
GATE_TRIGGER = 3
} GateOutputType_t;
typedef enum {
typedef enum : uint32_t {
VOICE_COUNT_1 = 1,
VOICE_COUNT_2 = 2,
VOICE_COUNT_3 = 3,
VOICE_COUNT_4 = 4
} PartVoiceCount_t;
typedef enum {
typedef enum : uint32_t {
VOICE_DETAIL_S = 0,
VOICE_DETAIL_M = 1,
VOICE_DETAIL_L = 2,
@ -68,13 +76,13 @@ class Part {
uint8_t RequiredColumns();
private:
PartVoiceCount_t part_voice_count;
PartVoiceDetail_t part_voice_detail;
bool midi_filter_channel_enabled;
uint8_t midi_filter_channel;
uint8_t midi_filter_lowest_note;
uint8_t midi_filter_highest_note;
MIDIInput_t midi_filter_input;
MIDIThruMode_t midi_thru_mode;
BiOutputType_t output_type_row_0[TOTAL_COLUMN_COUNT];
UniOutputType_t output_type_row_1[TOTAL_COLUMN_COUNT];

View file

@ -13,7 +13,9 @@ using namespace stmlib;
#define HEADER_HEIGHT 14
const uint32_t kEncoderLongPressTime = 600;
MainMenu mainMenu;
Part parts[4];
Part* part_pointers[4] = {&parts[0], &parts[1], &parts[2], &parts[3]};
MainMenu mainMenu(part_pointers);
void UI::Init()
{

View file

@ -13,8 +13,9 @@ class MainMenu {
void render(U8G2* u8g2, int x, int y, int width, int height);
MainMenu()
: activePartMenu(0)
MainMenu(Part** parts)
: partMenus({ parts[0], parts[1], parts[2], parts[3] })
, activePartMenu(0)
, selectedPart(0) {};
private:

View file

@ -7,18 +7,19 @@ static const char* kVoiceDetailStrings[] = { "S", "M", "L", "XL" };
static const char* kMidiChannelStrings[] = { "omni", "1", "2", "3", "4", "5", "6",
"7", "8", "9", "10", "11", "12", "13",
"14", "15", "16" };
static const char* kMidiInputStrings[] = { "omni", "usb", "midi" };
static const char* kMidiThruStrings[] = {"off", "on", "polychain"};
static const char* kMidiInputStrings[] = { "omni", "midi", "usb" };
static const char* kMidiThruStrings[] = { "off", "on", "polychain" };
PartMenu::PartMenu()
: item_voice_count("voice count", 1, 1, 4, 1)
, item_voice_detail("voice detail", 0, kVoiceDetailStrings, 4)
, item_midi_filter_enabled("MIDI filter", 1, "on", "off")
, item_midi_channel("MIDI channel", 0, kMidiChannelStrings, 17)
, item_midi_input("MIDI input", 0, kMidiInputStrings, 3)
, item_midi_lowest_note("MIDI lowest", 0)
, item_midi_highest_note("MIDI highest", 127)
, item_midi_thru_mode("MIDI thru", 0, kMidiThruStrings, 3)
PartMenu::PartMenu(Part* _part)
: part(_part)
, item_voice_count("voice count", (uint32_t*)&_part->part_voice_count, 1, 4, 1)
, item_voice_detail("voice detail", (uint8_t*)&_part->part_voice_detail, kVoiceDetailStrings, 4)
, item_midi_filter_enabled("MIDI filter", &_part->midi_filter_channel_enabled, "on", "off")
, item_midi_channel("MIDI channel", (uint8_t*)&_part->midi_filter_channel, kMidiChannelStrings, 17)
, item_midi_input("MIDI input", (uint8_t*)&_part->midi_filter_input, kMidiInputStrings, 3)
, item_midi_lowest_note("MIDI lowest", (uint8_t*)&_part->midi_filter_lowest_note)
, item_midi_highest_note("MIDI highest", (uint8_t*)&_part->midi_filter_highest_note)
, item_midi_thru_mode("MIDI thru", (uint8_t*)&_part->midi_thru_mode, kMidiThruStrings, 3)
{
this->menu.add_item(&this->item_voice_count);
this->menu.add_item(&this->item_voice_detail);

View file

@ -2,11 +2,12 @@
#include "../menu/menu.h"
#include "../menu/menu_items.h"
#include "../part.h"
#include <U8g2lib.h>
class PartMenu {
public:
PartMenu();
PartMenu(Part* _part);
bool enter();
bool back();
@ -16,8 +17,10 @@ class PartMenu {
void render(U8G2* u8g2, int x, int y, int width, int height);
private:
Part* part;
Menu menu;
UIntMenuItem item_voice_count;
UInt32MenuItem item_voice_count;
StringListMenuItem item_voice_detail;
BoolMenuItem item_midi_filter_enabled;
StringListMenuItem item_midi_channel;