MCPcopy Create free account
hub / github.com/OpenStickCommunity/GP2040-CE / obdMenuRun

Function obdMenuRun

lib/OneBitDisplay/OneBitDisplay.cpp:1085–1149  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1083// time in milliseconds for a long press
1084#define MENU_LONG_PRESS 600
1085int obdMenuRun(SIMPLEMENU *sm)
1086{
1087 uint8_t buttons = 0;
1088 unsigned long ul;
1089 int iDelta, rc = -1;
1090
1091 if (sm->bIsRotary)
1092 { // read the rotary encoder
1093 if (gpio_get(sm->u8Enter) == sm->iPressed)
1094 {
1095 buttons |= 1; // pressed
1096 if (buttons != sm->u8BtnState)
1097 rc = sm->iMenuIndex; // user pressed ENTER, return current menu index
1098 }
1099 else
1100 { // check for rotary encoder activity
1101 iDelta = obdMenuReadRotary(sm);
1102 obdMenuDelta(sm, iDelta);
1103 }
1104 sm->u8BtnState = buttons;
1105 }
1106 else
1107 {
1108 // check the button states
1109 if (gpio_get(sm->u8Up) == sm->iPressed)
1110 buttons |= 1;
1111 if (buttons != sm->u8BtnState) // something changed
1112 {
1113 if (sm->bOneButton) // different logic for a single button system
1114 {
1115 if (sm->u8BtnState == 0 && buttons == 1) // up button just pressed
1116 {
1117 sm->ulPressTime = to_ms_since_boot(get_absolute_time()); // record the press time
1118 }
1119 if (sm->u8BtnState == 1 && buttons == 0) // up button just released
1120 {
1121 ul = to_ms_since_boot(get_absolute_time()) - sm->ulPressTime;
1122 if (ul < MENU_LONG_PRESS) // short press = navigate menu
1123 obdMenuDelta(sm, 1);
1124 else // treat it like a long press
1125 rc = sm->iMenuIndex; // action
1126 }
1127 }
1128 else // 3 button setup (UP/DOWN/ENTER)
1129 {
1130 if (gpio_get(sm->u8Dn) == sm->iPressed)
1131 buttons |= 2;
1132 if (gpio_get(sm->u8Enter) == sm->iPressed)
1133 rc = sm->iMenuIndex; // user pressed ENTER, return current menu index
1134 if ((sm->u8BtnState & 1) == 0 && (buttons & 1) == 1) // Up button pressed
1135 {
1136 obdMenuDelta(sm, -1);
1137 }
1138 if ((sm->u8BtnState & 2) == 0 && (buttons & 2) == 2) // Down button pressed
1139 {
1140 obdMenuDelta(sm, 1);
1141 }
1142 }

Callers

nothing calls this directly

Calls 3

obdMenuReadRotaryFunction · 0.85
obdMenuDeltaFunction · 0.85
obdMenuFlashFunction · 0.85

Tested by

no test coverage detected