From 6785261d08783efcf93f65dc9353a5918b136983 Mon Sep 17 00:00:00 2001 From: true Date: Mon, 5 Aug 2024 13:04:54 -0700 Subject: [PATCH] buttons operate properly now what the buttons actually _do_ does not necessarily operate properly, except the cursor. that's been fixed --- firmware/retro_tech_fw/user/src/btn.c | 35 +++++++++----- firmware/retro_tech_fw/user/src/btn.h | 5 +- firmware/retro_tech_fw/user/src/led_rgbprog.c | 2 +- firmware/retro_tech_fw/user/src/ui.c | 46 +++++++++---------- 4 files changed, 49 insertions(+), 39 deletions(-) diff --git a/firmware/retro_tech_fw/user/src/btn.c b/firmware/retro_tech_fw/user/src/btn.c index 81f1c1f..76362e1 100644 --- a/firmware/retro_tech_fw/user/src/btn.c +++ b/firmware/retro_tech_fw/user/src/btn.c @@ -19,15 +19,15 @@ void btn_init() // configure GPIO BTN_PORT->BSHR = (BTN1_PUPD << BTN1_PIN) | (BTN2_PUPD << BTN2_PIN); - BTN_PORT->CFGLR &= ~((0xff << (BTN1_PIN*4)) | ((0xff << (BTN2_PIN*4)))); - BTN_PORT->CFGLR |= (0x80 << (BTN1_PIN*4)) | (0x80 << (BTN2_PIN*4)); + BTN_PORT->CFGLR &= ~((0xf << (BTN1_PIN*4)) | ((0xf << (BTN2_PIN*4)))); + BTN_PORT->CFGLR |= (0x8 << (BTN1_PIN*4)) | (0x8 << (BTN2_PIN*4)); // configure default setup for (i = 0; i < BTN_COUNT; i++) { - btn[i]._mask = 0; + btn[i]._mask = BTN_RELEASE; } - btn[0]._pintype = BTN1_PIN | 0x10; + btn[0]._pintype = BTN1_PIN | BTN_ACTIVE_LO; btn[1]._pintype = BTN2_PIN; } @@ -37,19 +37,28 @@ void btn_poll() uint8_t r; uint8_t ignore; + uint8_t pushed; + for (i = 0; i < BTN_COUNT; i++) { + pushed = 0; + ignore = btn[i]._mask & BTN_IGNORE; - r = BTN_PORT->INDR & (1 << (btn[i]._pintype & 0xf)); + r = BTN_PORT->INDR & (1 << (btn[i]._pintype & BTN_PIN_MASK)); - if ((!r && (btn[i]._pintype & 0x10)) || (r && !(btn[i]._pintype & 0x10))) { - // is pushed - if (btn[i]._count < BTN_DEBOUNCE) continue; + // active low type buttons + if (!r && (btn[i]._pintype & BTN_ACTIVE_LO)) pushed = 1; + // active high type buttons + if (r && !(btn[i]._pintype & BTN_ACTIVE_LO)) pushed = 1; + if (pushed) { // hold counter if (btn[i]._count < 0xffff) btn[i]._count++; + // pushed long ennough? + if (btn[i]._count < BTN_DEBOUNCE) continue; + // first push? - if (!btn[i]._mask & BTN_PUSH) { + if (!(btn[i]._mask & BTN_PUSH)) { btn[i]._mask = BTN_PUSH; if (btn[i].cb_push) { btn[i].cb_push(i); @@ -59,7 +68,7 @@ void btn_poll() // held to count limit // if button is not repeatable, do not retrigger - if ((btn[i]._mask && BTN_HOLD) && !btn[i].repeat) continue; + if ((btn[i]._mask & BTN_HOLD) && !btn[i].repeat) continue; btn[i]._mask |= BTN_HOLD; // call callback only if not in ignore state @@ -69,11 +78,13 @@ void btn_poll() } // apply repeat rate to count - btn[i]._count -= btn[i].repeat; + if (btn[i].repeat > btn[i]._count) { + btn[i]._count = 0; + } else btn[i]._count -= btn[i].repeat; } } else { // is not pushed - if (!btn[i]._mask & BTN_RELEASE) { + if (!(btn[i]._mask & BTN_RELEASE)) { btn[i]._mask = BTN_RELEASE; btn[i]._count = 0; // call callback only if not in ignore state diff --git a/firmware/retro_tech_fw/user/src/btn.h b/firmware/retro_tech_fw/user/src/btn.h index 5167b90..688df78 100644 --- a/firmware/retro_tech_fw/user/src/btn.h +++ b/firmware/retro_tech_fw/user/src/btn.h @@ -19,6 +19,9 @@ #define BTN_PULL_UP (1 << 0) #define BTN_PULL_DOWN (1 << 16) +#define BTN_ACTIVE_LO 0x10 +#define BTN_PIN_MASK 0x0f + #define BTN1_PIN 5 #define BTN1_PUPD BTN_PULL_UP @@ -35,7 +38,7 @@ typedef struct Btn { uint8_t _mask; uint8_t _pintype; - uint16_t _count; // held counts + uint16_t _count; // held counts uint16_t hold; // initial hold uint16_t repeat; // repeated hold void (*cb_push)(uint8_t); diff --git a/firmware/retro_tech_fw/user/src/led_rgbprog.c b/firmware/retro_tech_fw/user/src/led_rgbprog.c index bc10fc2..bd56ec3 100644 --- a/firmware/retro_tech_fw/user/src/led_rgbprog.c +++ b/firmware/retro_tech_fw/user/src/led_rgbprog.c @@ -434,7 +434,7 @@ void led_rgb_4_typing(uint8_t preview, uint8_t tick) if (typing_flash_delay <= 8) { w = 8 - typing_flash_delay; cursor[color] >>= w; - if (cursor[color] < 12) cursor[color] = 12; + if (cursor[color] < 12) cursor[color] = 0; // idle value } } diff --git a/firmware/retro_tech_fw/user/src/ui.c b/firmware/retro_tech_fw/user/src/ui.c index fdea3a1..6bb668d 100644 --- a/firmware/retro_tech_fw/user/src/ui.c +++ b/firmware/retro_tech_fw/user/src/ui.c @@ -37,7 +37,7 @@ static uint16_t editor_timeout_timer = 0; static uint8_t target_gc = 0; static uint8_t cursor_flash = 0; -static uint8_t cursor_state = 0xff; +static uint8_t cursor_state = 0; const uint16_t cursor_flash_rates[8] = { // off-to-on flash rates in 1/256 second increments 255, // (0.5 on-off/second) 191, // (0.75 on-off/second) @@ -189,6 +189,9 @@ void ui_btn_release_cb(uint8_t idx) userconf.cursor_color++; userconf.cursor_color &= 0x3; + // force cursor to change immediately + cursor_flash = 0; + config_save_timer = UI_CONF_SAVE_TIMEOUT; break; @@ -302,10 +305,16 @@ static void ui_cursor_flash() switch (mode) { case MODE_RUN: { if (rgb_prog_idx != 4) { - // initial setup - if (cursor_state == 0xff) { - cursor_state = 0; - goto ui_cursor_flash_setup; + if (!cursor_flash) { + // toggle on/off + cursor_state++; + cursor_state &= 1; + + // set new cursor rate + cursor_flash = cursor_flash_rates[flash] >> 1; + + // set all colors off + cursor[0] = cursor[1] = cursor[2] = 0; } // wind down counter @@ -317,8 +326,8 @@ static void ui_cursor_flash() level = (cursor_flash == cursor_flash_rates[flash] - 1) ? 160 : 255; // at final frames, dim - if (cursor_flash < 4) { - level >>= (cursor_flash << 1); + if (cursor_flash <= 4) { + level >>= (4 - cursor_flash); } } @@ -327,32 +336,19 @@ static void ui_cursor_flash() cursor[color] = led_gamma(level); led_is_updated(); } - - if (!cursor_flash) { - // toggle on/off - cursor_state++; - cursor_state &= 1; - -ui_cursor_flash_setup: - // set new cursor rate - cursor_flash = cursor_flash_rates[flash] >> 1; - - // set all colors off - cursor[0] = cursor[1] = cursor[2] = 0; - } } break; } case MODE_PROGRAM: { - // cursor is always on bright - cursor[color] = 255; + // cursor is always off + cursor[color] = 0; break; } case MODE_PARAMETER: { - // cursor is always on dim - cursor[color] = 16; + // cursor is always off + cursor[color] = 0; break; } @@ -490,7 +486,7 @@ void ui_render() } // temp: remove me once buttons are tested and working - rgb_prog_idx = 4; + rgb_prog_idx = 0; led_rgbprog[rgb_prog_idx](LED_RGBPROG_NORMAL, tick); break;