buttons operate properly now

what the buttons actually _do_ does not necessarily operate properly, except the cursor. that's been fixed
This commit is contained in:
true 2024-08-05 13:04:54 -07:00
parent 9d00b8daa1
commit 6785261d08
4 changed files with 49 additions and 39 deletions

View File

@ -19,15 +19,15 @@ void btn_init()
// configure GPIO // configure GPIO
BTN_PORT->BSHR = (BTN1_PUPD << BTN1_PIN) | (BTN2_PUPD << BTN2_PIN); 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 &= ~((0xf << (BTN1_PIN*4)) | ((0xf << (BTN2_PIN*4))));
BTN_PORT->CFGLR |= (0x80 << (BTN1_PIN*4)) | (0x80 << (BTN2_PIN*4)); BTN_PORT->CFGLR |= (0x8 << (BTN1_PIN*4)) | (0x8 << (BTN2_PIN*4));
// configure default setup // configure default setup
for (i = 0; i < BTN_COUNT; i++) { 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; btn[1]._pintype = BTN2_PIN;
} }
@ -37,19 +37,28 @@ void btn_poll()
uint8_t r; uint8_t r;
uint8_t ignore; uint8_t ignore;
uint8_t pushed;
for (i = 0; i < BTN_COUNT; i++) { for (i = 0; i < BTN_COUNT; i++) {
pushed = 0;
ignore = btn[i]._mask & BTN_IGNORE; 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))) { // active low type buttons
// is pushed if (!r && (btn[i]._pintype & BTN_ACTIVE_LO)) pushed = 1;
if (btn[i]._count < BTN_DEBOUNCE) continue; // active high type buttons
if (r && !(btn[i]._pintype & BTN_ACTIVE_LO)) pushed = 1;
if (pushed) {
// hold counter // hold counter
if (btn[i]._count < 0xffff) btn[i]._count++; if (btn[i]._count < 0xffff) btn[i]._count++;
// pushed long ennough?
if (btn[i]._count < BTN_DEBOUNCE) continue;
// first push? // first push?
if (!btn[i]._mask & BTN_PUSH) { if (!(btn[i]._mask & BTN_PUSH)) {
btn[i]._mask = BTN_PUSH; btn[i]._mask = BTN_PUSH;
if (btn[i].cb_push) { if (btn[i].cb_push) {
btn[i].cb_push(i); btn[i].cb_push(i);
@ -59,7 +68,7 @@ void btn_poll()
// held to count limit // held to count limit
// if button is not repeatable, do not retrigger // 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; btn[i]._mask |= BTN_HOLD;
// call callback only if not in ignore state // call callback only if not in ignore state
@ -69,11 +78,13 @@ void btn_poll()
} }
// apply repeat rate to count // 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 { } else {
// is not pushed // is not pushed
if (!btn[i]._mask & BTN_RELEASE) { if (!(btn[i]._mask & BTN_RELEASE)) {
btn[i]._mask = BTN_RELEASE; btn[i]._mask = BTN_RELEASE;
btn[i]._count = 0; btn[i]._count = 0;
// call callback only if not in ignore state // call callback only if not in ignore state

View File

@ -19,6 +19,9 @@
#define BTN_PULL_UP (1 << 0) #define BTN_PULL_UP (1 << 0)
#define BTN_PULL_DOWN (1 << 16) #define BTN_PULL_DOWN (1 << 16)
#define BTN_ACTIVE_LO 0x10
#define BTN_PIN_MASK 0x0f
#define BTN1_PIN 5 #define BTN1_PIN 5
#define BTN1_PUPD BTN_PULL_UP #define BTN1_PUPD BTN_PULL_UP
@ -35,7 +38,7 @@
typedef struct Btn { typedef struct Btn {
uint8_t _mask; uint8_t _mask;
uint8_t _pintype; uint8_t _pintype;
uint16_t _count; // held counts uint16_t _count; // held counts
uint16_t hold; // initial hold uint16_t hold; // initial hold
uint16_t repeat; // repeated hold uint16_t repeat; // repeated hold
void (*cb_push)(uint8_t); void (*cb_push)(uint8_t);

View File

@ -434,7 +434,7 @@ void led_rgb_4_typing(uint8_t preview, uint8_t tick)
if (typing_flash_delay <= 8) { if (typing_flash_delay <= 8) {
w = 8 - typing_flash_delay; w = 8 - typing_flash_delay;
cursor[color] >>= w; cursor[color] >>= w;
if (cursor[color] < 12) cursor[color] = 12; if (cursor[color] < 12) cursor[color] = 0; // idle value
} }
} }

View File

@ -37,7 +37,7 @@ static uint16_t editor_timeout_timer = 0;
static uint8_t target_gc = 0; static uint8_t target_gc = 0;
static uint8_t cursor_flash = 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 const uint16_t cursor_flash_rates[8] = { // off-to-on flash rates in 1/256 second increments
255, // (0.5 on-off/second) 255, // (0.5 on-off/second)
191, // (0.75 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++;
userconf.cursor_color &= 0x3; userconf.cursor_color &= 0x3;
// force cursor to change immediately
cursor_flash = 0;
config_save_timer = UI_CONF_SAVE_TIMEOUT; config_save_timer = UI_CONF_SAVE_TIMEOUT;
break; break;
@ -302,10 +305,16 @@ static void ui_cursor_flash()
switch (mode) { switch (mode) {
case MODE_RUN: { case MODE_RUN: {
if (rgb_prog_idx != 4) { if (rgb_prog_idx != 4) {
// initial setup if (!cursor_flash) {
if (cursor_state == 0xff) { // toggle on/off
cursor_state = 0; cursor_state++;
goto ui_cursor_flash_setup; 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 // wind down counter
@ -317,8 +326,8 @@ static void ui_cursor_flash()
level = (cursor_flash == cursor_flash_rates[flash] - 1) ? 160 : 255; level = (cursor_flash == cursor_flash_rates[flash] - 1) ? 160 : 255;
// at final frames, dim // at final frames, dim
if (cursor_flash < 4) { if (cursor_flash <= 4) {
level >>= (cursor_flash << 1); level >>= (4 - cursor_flash);
} }
} }
@ -327,32 +336,19 @@ static void ui_cursor_flash()
cursor[color] = led_gamma(level); cursor[color] = led_gamma(level);
led_is_updated(); 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; break;
} }
case MODE_PROGRAM: { case MODE_PROGRAM: {
// cursor is always on bright // cursor is always off
cursor[color] = 255; cursor[color] = 0;
break; break;
} }
case MODE_PARAMETER: { case MODE_PARAMETER: {
// cursor is always on dim // cursor is always off
cursor[color] = 16; cursor[color] = 0;
break; break;
} }
@ -490,7 +486,7 @@ void ui_render()
} }
// temp: remove me once buttons are tested and working // 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); led_rgbprog[rgb_prog_idx](LED_RGBPROG_NORMAL, tick);
break; break;