Skip to content

Commit

Permalink
[NFC] extract more menu logic
Browse files Browse the repository at this point in the history
  • Loading branch information
delan committed Jul 15, 2023
1 parent 005cdca commit 84c256e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 35 deletions.
8 changes: 3 additions & 5 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void loop() {
// static int i = 0;
// display.printf("#%d @%lu", i++, t / 1'000);
// display.printf("usb3sun%c", t / 500'000 % 2 == 1 ? '.' : ' ');
if (state.inMenu) {
if (menu.open) {
menu.draw();
} else {
drawStatus(78, 0, "CLK", state.clickEnabled);
Expand Down Expand Up @@ -453,16 +453,14 @@ void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t cons
state.lastModifiers = kreport->modifier;

for (int i = 0; i < selectorChangesLen; i++) {
if (state.inMenu) {
if (menu.open) {
menu.key(selectorChanges[i].usbkSelector, selectorChanges[i].make);
continue;
}

// CtrlR+Space acts like a DV+Sel binding, but opens the settings menu
if (selectorChanges[i].usbkSelector == USBK_SPACE && state.lastModifiers == USBK_CTRL_R) {
state.inMenu = !state.inMenu;
state.selectedMenuItem = 0u;
state.topMenuItem = 0u;
menu.toggle();
continue;
}

Expand Down
55 changes: 28 additions & 27 deletions src/menu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,30 @@
#include "state.h"

template<typename... Args>
static void drawMenuItem(int16_t &marqueeX, size_t i, const char *fmt, Args... args);
static void drawMenuItem(int16_t &marqueeX, size_t i, bool on, const char *fmt, Args... args);

using MenuItemPainter = void (*)(int16_t &marqueeX, size_t i);
using MenuItemPainter = void (*)(int16_t &marqueeX, size_t i, bool on);
static const MenuItemPainter MENU_ITEM_PAINTERS[] = {
[](int16_t &marqueeX, size_t i) {
drawMenuItem(marqueeX, i, "Go back");
[](int16_t &marqueeX, size_t i, bool on) {
drawMenuItem(marqueeX, i, on, "Go back");
},
[](int16_t &marqueeX, size_t i) {
drawMenuItem(marqueeX, i, "Force click: %s",
[](int16_t &marqueeX, size_t i, bool on) {
drawMenuItem(marqueeX, i, on, "Force click: %s",
settings.forceClick() == ForceClick::_::NO ? "no"
: settings.forceClick() == ForceClick::_::OFF ? "off"
: settings.forceClick() == ForceClick::_::ON ? "on"
: "?");
},
[](int16_t &marqueeX, size_t i) {
drawMenuItem(marqueeX, i, "Click duration: %u ms", settings.clickDuration());
[](int16_t &marqueeX, size_t i, bool on) {
drawMenuItem(marqueeX, i, on, "Click duration: %u ms", settings.clickDuration());
},
};

static const size_t MENU_ITEM_COUNT = sizeof(MENU_ITEM_PAINTERS) / sizeof(MENU_ITEM_PAINTERS[0]);

template<typename... Args>
static void drawMenuItem(int16_t &marqueeX, int16_t y, bool on, const char *fmt, Args... args) {
static void drawMenuItem(int16_t &marqueeX, size_t i, bool on, const char *fmt, Args... args) {
int16_t y = 8 * (1 + i);
if (on) {
int width = snprintf(NULL, 0, fmt, args...) * 6;
display.fillRect(4, y, 120, 8, SSD1306_WHITE);
Expand All @@ -56,26 +57,26 @@ static void drawMenuItem(int16_t &marqueeX, int16_t y, bool on, const char *fmt,
}
}

template<typename... Args>
static void drawMenuItem(int16_t &marqueeX, size_t i, const char *fmt, Args... args) {
if (i >= state.topMenuItem && i <= state.topMenuItem + 2)
drawMenuItem(marqueeX, 8 * (1 + i - state.topMenuItem), state.selectedMenuItem == i, fmt, args...);
void Menu::toggle() {
open = !open;
selectedItem = 0u;
topItem = 0u;
}

void Menu::draw() {
marqueeTick = (marqueeTick + 1) % 2;
if (marqueeTick == 0)
marqueeX += 1;

for (size_t i = 0; i < MENU_ITEM_COUNT; i++)
MENU_ITEM_PAINTERS[i](marqueeX, i);
for (size_t i = topItem; i <= topItem + 2 && i < MENU_ITEM_COUNT; i++)
MENU_ITEM_PAINTERS[i](marqueeX, i - topItem, selectedItem == i);
}

void Menu::key(uint8_t usbkSelector, bool make) {
if (make) {
switch (usbkSelector) {
case USBK_RIGHT:
switch (state.selectedMenuItem) {
switch (selectedItem) {
case 1:
++settings.forceClick();
settings.write(settings.forceClick_field);
Expand All @@ -90,7 +91,7 @@ void Menu::key(uint8_t usbkSelector, bool make) {
}
break;
case USBK_LEFT:
switch (state.selectedMenuItem) {
switch (selectedItem) {
case 1:
--settings.forceClick();
settings.write(settings.forceClick_field);
Expand All @@ -105,29 +106,29 @@ void Menu::key(uint8_t usbkSelector, bool make) {
}
break;
case USBK_DOWN:
if (state.selectedMenuItem < MENU_ITEM_COUNT - 1u) {
state.selectedMenuItem += 1u;
if (selectedItem < MENU_ITEM_COUNT - 1u) {
selectedItem += 1u;
marqueeX = 0;
}
if (state.selectedMenuItem - state.topMenuItem > 2u)
state.topMenuItem += 1u;
if (selectedItem - topItem > 2u)
topItem += 1u;
break;
case USBK_UP:
if (state.selectedMenuItem > 0u) {
state.selectedMenuItem -= 1u;
if (selectedItem > 0u) {
selectedItem -= 1u;
marqueeX = 0;
}
if (state.selectedMenuItem < state.topMenuItem)
state.topMenuItem -= 1u;
if (selectedItem < topItem)
topItem -= 1u;
break;
case USBK_RETURN:
case USBK_ENTER:
switch (state.selectedMenuItem) {
switch (selectedItem) {
// case N:
// state.foo = !state.foo;
// break;
}
state.inMenu = false;
toggle();
break;
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/menu.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#ifndef USB3SUN_MENU_H
#define USB3SUN_MENU_H

#include <cstddef>
#include <cstdint>

struct Menu {
bool open = false;
size_t selectedItem = 0u;
size_t topItem = 0u;
int16_t marqueeX = 0;
unsigned marqueeTick = 0;

void toggle();
void draw();
void key(uint8_t usbkSelector, bool make);
};
Expand Down
3 changes: 0 additions & 3 deletions src/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ struct State {
uint8_t lastModifiers;
uint8_t lastKeys[6];
uint8_t lastButtons;
bool inMenu = false;
size_t selectedMenuItem = 0u;
size_t topMenuItem = 0u;
};

extern State state;
Expand Down

0 comments on commit 84c256e

Please sign in to comment.