Refactor handle rotate to make the code more reusable and readable.
This commit is contained in:
@ -31,7 +31,7 @@ struct Channel {
|
|||||||
struct AppState {
|
struct AppState {
|
||||||
bool refresh_screen = true;
|
bool refresh_screen = true;
|
||||||
bool editing_param = false;
|
bool editing_param = false;
|
||||||
byte selected_param = 0;
|
int selected_param = 0;
|
||||||
byte selected_channel = 0; // 0=tempo, 1-6=output channel
|
byte selected_channel = 0; // 0=tempo, 1-6=output channel
|
||||||
Source selected_source = SOURCE_INTERNAL;
|
Source selected_source = SOURCE_INTERNAL;
|
||||||
Channel channel[OUTPUT_COUNT];
|
Channel channel[OUTPUT_COUNT];
|
||||||
@ -43,6 +43,12 @@ enum ParamsMainPage {
|
|||||||
PARAM_MAIN_SOURCE,
|
PARAM_MAIN_SOURCE,
|
||||||
PARAM_MAIN_LAST,
|
PARAM_MAIN_LAST,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const char* MAIN_PAGE_MENU[PARAM_MAIN_LAST] = {
|
||||||
|
"Tempo",
|
||||||
|
"Source",
|
||||||
|
};
|
||||||
|
|
||||||
enum ParamsChannelPage {
|
enum ParamsChannelPage {
|
||||||
PARAM_CH_MOD,
|
PARAM_CH_MOD,
|
||||||
PARAM_CH_PROB,
|
PARAM_CH_PROB,
|
||||||
@ -51,6 +57,13 @@ enum ParamsChannelPage {
|
|||||||
PARAM_CH_LAST,
|
PARAM_CH_LAST,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const char* CHANNEL_PAGE_MENU[PARAM_CH_LAST] = {
|
||||||
|
"Mod",
|
||||||
|
"Probability",
|
||||||
|
"Duty Cycle",
|
||||||
|
"Offset",
|
||||||
|
};
|
||||||
|
|
||||||
// The number of clock mod options, hepls validate choices and pulses arrays are the same size.
|
// The number of clock mod options, hepls validate choices and pulses arrays are the same size.
|
||||||
const int MOD_CHOICE_SIZE = 21;
|
const int MOD_CHOICE_SIZE = 21;
|
||||||
// Negative for multiply, positive for divide.
|
// Negative for multiply, positive for divide.
|
||||||
@ -166,60 +179,59 @@ void HandleEncoderPressed() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void HandleRotate(Direction dir, int val) {
|
void HandleRotate(Direction dir, int val) {
|
||||||
// Select a prameter when not in edit mode.
|
|
||||||
if (!app.editing_param) {
|
if (!app.editing_param) {
|
||||||
// Main Global Settings Page.
|
// Navigation Mode
|
||||||
if (app.selected_channel == 0) {
|
const int max_param = (app.selected_channel == 0) ? PARAM_MAIN_LAST : PARAM_CH_LAST;
|
||||||
if (app.selected_param == 0 && val < 0) {
|
updateSelection(app.selected_param, val, max_param);
|
||||||
app.selected_param = PARAM_MAIN_LAST - 1;
|
|
||||||
} else {
|
} else {
|
||||||
app.selected_param = (app.selected_param + val) % PARAM_MAIN_LAST;
|
// Editing Mode
|
||||||
}
|
|
||||||
}
|
|
||||||
// Selected Output Channels 1-6 Settings.
|
|
||||||
else {
|
|
||||||
if (app.selected_param == 0 && val < 0) {
|
|
||||||
app.selected_param = PARAM_CH_LAST - 1;
|
|
||||||
} else {
|
|
||||||
app.selected_param = (app.selected_param + val) % PARAM_CH_LAST;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Edit selected param.
|
|
||||||
else {
|
|
||||||
// Main Global Settings Page.
|
|
||||||
if (app.selected_channel == 0) {
|
if (app.selected_channel == 0) {
|
||||||
|
editMainParameter(val);
|
||||||
|
} else {
|
||||||
|
editChannelParameter(dir, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
app.refresh_screen = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void HandlePressedRotate(Direction dir, int val) {
|
||||||
|
if (dir == DIRECTION_INCREMENT && app.selected_channel < OUTPUT_COUNT) {
|
||||||
|
app.selected_channel++;
|
||||||
|
} else if (dir == DIRECTION_DECREMENT && app.selected_channel > 0) {
|
||||||
|
app.selected_channel--;
|
||||||
|
}
|
||||||
|
app.selected_param = 0;
|
||||||
|
app.refresh_screen = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void editMainParameter(int val) {
|
||||||
switch (static_cast<ParamsMainPage>(app.selected_param)) {
|
switch (static_cast<ParamsMainPage>(app.selected_param)) {
|
||||||
case PARAM_MAIN_TEMPO:
|
case PARAM_MAIN_TEMPO:
|
||||||
if (gravity.clock.ExternalSource()) {
|
if (gravity.clock.ExternalSource()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
gravity.clock.SetTempo(gravity.clock.Tempo() + val);
|
gravity.clock.SetTempo(gravity.clock.Tempo() + val);
|
||||||
app.refresh_screen = true;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case PARAM_MAIN_SOURCE:
|
case PARAM_MAIN_SOURCE: {
|
||||||
if (static_cast<Source>(app.selected_source) == 0 && val < 0) {
|
int source = static_cast<int>(app.selected_source);
|
||||||
app.selected_source = static_cast<Source>(SOURCE_LAST - 1);
|
updateSelection(source, val, SOURCE_LAST);
|
||||||
} else {
|
app.selected_source = static_cast<Source>(source);
|
||||||
app.selected_source = static_cast<Source>((app.selected_source + val) % SOURCE_LAST);
|
|
||||||
}
|
|
||||||
|
|
||||||
gravity.clock.SetSource(app.selected_source);
|
gravity.clock.SetSource(app.selected_source);
|
||||||
app.refresh_screen = true;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Selected Output Channel Settings.
|
}
|
||||||
else {
|
|
||||||
auto& ch = GetSelectedChannel();
|
|
||||||
|
|
||||||
|
void editChannelParameter(Direction dir, int val) {
|
||||||
|
auto& ch = GetSelectedChannel();
|
||||||
switch (static_cast<ParamsChannelPage>(app.selected_param)) {
|
switch (static_cast<ParamsChannelPage>(app.selected_param)) {
|
||||||
case PARAM_CH_MOD:
|
case PARAM_CH_MOD:
|
||||||
if (dir == DIRECTION_INCREMENT && ch.clock_mod_index < MOD_CHOICE_SIZE - 1) {
|
if (dir == DIRECTION_INCREMENT && ch.clock_mod_index < MOD_CHOICE_SIZE - 1) {
|
||||||
ch.clock_mod_index += 1;
|
ch.clock_mod_index++;
|
||||||
} else if (dir == DIRECTION_DECREMENT && ch.clock_mod_index > 0) {
|
} else if (dir == DIRECTION_DECREMENT && ch.clock_mod_index > 0) {
|
||||||
ch.clock_mod_index -= 1;
|
ch.clock_mod_index--;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case PARAM_CH_PROB:
|
case PARAM_CH_PROB:
|
||||||
@ -232,22 +244,16 @@ void HandleRotate(Direction dir, int val) {
|
|||||||
ch.offset = constrain(ch.offset + val, 0, 99);
|
ch.offset = constrain(ch.offset + val, 0, 99);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update this channel's parameters based on new values.
|
||||||
uint32_t mod_pulses = clock_mod_pulses[ch.clock_mod_index];
|
uint32_t mod_pulses = clock_mod_pulses[ch.clock_mod_index];
|
||||||
ch.duty_cycle_pulses = max((int)((mod_pulses * (100L - ch.duty_cycle)) / 100L), 1);
|
ch.duty_cycle_pulses = max((int)((mod_pulses * (100L - ch.duty_cycle)) / 100L), 1);
|
||||||
ch.offset_pulses = (int)(mod_pulses * (100L - ch.offset) / 100L);
|
ch.offset_pulses = (int)(mod_pulses * (100L - ch.offset) / 100L);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
app.refresh_screen = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void HandlePressedRotate(Direction dir, int val) {
|
void updateSelection(int& param, int change, int maxValue) {
|
||||||
if (dir == DIRECTION_INCREMENT && app.selected_channel < OUTPUT_COUNT) {
|
// This formula correctly handles positive and negative wrapping.
|
||||||
app.selected_channel++;
|
param = (param + change % maxValue + maxValue) % maxValue;
|
||||||
} else if (dir == DIRECTION_DECREMENT && app.selected_channel > 0) {
|
|
||||||
app.selected_channel--;
|
|
||||||
}
|
|
||||||
app.selected_param = 0;
|
|
||||||
app.refresh_screen = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -336,8 +342,7 @@ void DisplayMainPage() {
|
|||||||
drawCenteredText(subText, SUB_TEXT_Y, TEXT_FONT);
|
drawCenteredText(subText, SUB_TEXT_Y, TEXT_FONT);
|
||||||
|
|
||||||
// Draw Main Page menu items
|
// Draw Main Page menu items
|
||||||
const char* menu_items[PARAM_MAIN_LAST] = {"Tempo", "Source"};
|
drawMenuItems(MAIN_PAGE_MENU, PARAM_MAIN_LAST);
|
||||||
drawMenuItems(menu_items);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DisplayChannelPage() {
|
void DisplayChannelPage() {
|
||||||
@ -380,9 +385,7 @@ void DisplayChannelPage() {
|
|||||||
drawCenteredText(subText, SUB_TEXT_Y, TEXT_FONT);
|
drawCenteredText(subText, SUB_TEXT_Y, TEXT_FONT);
|
||||||
|
|
||||||
// Draw Channel Page menu items
|
// Draw Channel Page menu items
|
||||||
const char* menu_items[PARAM_CH_LAST] = {
|
drawMenuItems(CHANNEL_PAGE_MENU, PARAM_CH_LAST);
|
||||||
"Mod", "Probability", "Duty Cycle", "Offset"};
|
|
||||||
drawMenuItems(menu_items);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void DisplaySelectedChannel() {
|
void DisplaySelectedChannel() {
|
||||||
@ -417,13 +420,13 @@ void DisplaySelectedChannel() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawMenuItems(const char* menu_items[]) {
|
void drawMenuItems(const char* menu_items[], int menu_size) {
|
||||||
// Draw menu items
|
// Draw menu items
|
||||||
gravity.display.setFont(TEXT_FONT);
|
gravity.display.setFont(TEXT_FONT);
|
||||||
|
|
||||||
// Draw selected menu item box
|
// Draw selected menu item box
|
||||||
int selectedBoxY = 0;
|
int selectedBoxY = 0;
|
||||||
if (app.selected_param == PARAM_CH_LAST - 1) {
|
if (menu_size >= VISIBLE_MENU_ITEMS && app.selected_param == menu_size - 1) {
|
||||||
selectedBoxY = MENU_ITEM_HEIGHT * min(2, app.selected_param);
|
selectedBoxY = MENU_ITEM_HEIGHT * min(2, app.selected_param);
|
||||||
} else if (app.selected_param > 0) {
|
} else if (app.selected_param > 0) {
|
||||||
selectedBoxY = MENU_ITEM_HEIGHT;
|
selectedBoxY = MENU_ITEM_HEIGHT;
|
||||||
@ -440,13 +443,13 @@ void drawMenuItems(const char* menu_items[]) {
|
|||||||
|
|
||||||
// Draw the visible menu items
|
// Draw the visible menu items
|
||||||
int start_index = 0;
|
int start_index = 0;
|
||||||
if (app.selected_param == PARAM_CH_LAST - 1) {
|
if (menu_size >= VISIBLE_MENU_ITEMS && app.selected_param == menu_size - 1) {
|
||||||
start_index = PARAM_CH_LAST - VISIBLE_MENU_ITEMS;
|
start_index = menu_size - VISIBLE_MENU_ITEMS;
|
||||||
} else if (app.selected_param > 0) {
|
} else if (app.selected_param > 0) {
|
||||||
start_index = app.selected_param - 1;
|
start_index = app.selected_param - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < VISIBLE_MENU_ITEMS; ++i) {
|
for (int i = 0; i < min(menu_size, VISIBLE_MENU_ITEMS); ++i) {
|
||||||
int idx = start_index + i;
|
int idx = start_index + i;
|
||||||
drawRightAlignedText(menu_items[idx], MENU_ITEM_HEIGHT * (i + 1));
|
drawRightAlignedText(menu_items[idx], MENU_ITEM_HEIGHT * (i + 1));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user