From 236359d7e225098d2b21586c4c8cf8989a3f8b62 Mon Sep 17 00:00:00 2001 From: true Date: Sat, 26 Oct 2024 20:22:42 -0700 Subject: [PATCH] buttons remap when oled is rotated the oled display rotates when the addon is physically rotated, so that menus are upright. now the user buttons will remap as well. this was done in the old code...or so I thought, but I can't find the code that did it. --- nametag8_CH592/user/main.c | 12 +++++++++++ nametag8_CH592/user/ui/btn.c | 34 +++++++++++++++++++++++-------- nametag8_CH592/user/user_config.h | 1 + 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/nametag8_CH592/user/main.c b/nametag8_CH592/user/main.c index 5721902..0d87106 100644 --- a/nametag8_CH592/user/main.c +++ b/nametag8_CH592/user/main.c @@ -140,6 +140,7 @@ __HIGH_CODE void oled_update_done() { int8_t rot; + uint8_t oldsys; // reset oled callback, clear screen, and set default pixel mode and font size oled.callback = 0; @@ -153,6 +154,7 @@ void oled_update_done() // orientation / flipping flags rot = accel_get_rotation(&accel); + oldsys = sysflags; if ((rot > (96+4)) || (rot < (32-4))) { sysflags &= ~SYS_OLED_ROTATE_X; } else if ((rot > (32+4)) && (rot < (96-4))) { @@ -165,6 +167,10 @@ void oled_update_done() sysflags |= SYS_OLED_ROTATE_Y; } + if (sysflags != oldsys) { + sysflags |= SYS_OLED_ROTATE_CHANGED; + } + if ((rot < 21) || (rot > (64 + 24))) { sysflags &= ~SYS_OLED_REVERSE_CHARS; } else if (rot > 24 && rot < (64 + 21)){ @@ -240,6 +246,12 @@ void lowprio_task() { ch32sub_intr_defaults(); btn_commit_hold(); ch32sub_rgb_hwen(1); + } else { + // send button holds any time the orientation of the OLED changes + if (sysflags & SYS_OLED_ROTATE_CHANGED) { + sysflags &= ~SYS_OLED_ROTATE_CHANGED; + btn_commit_hold(); + } } // process sub MCU interrupt, if pending diff --git a/nametag8_CH592/user/ui/btn.c b/nametag8_CH592/user/ui/btn.c index c279f9d..a8ce240 100644 --- a/nametag8_CH592/user/ui/btn.c +++ b/nametag8_CH592/user/ui/btn.c @@ -11,6 +11,7 @@ #include "btn.h" #include "hw/ch32sub.h" +#include "user_config.h" @@ -21,7 +22,8 @@ uint8_t btn_held; -static uint8_t btn_map[BTN_COUNT] = {2, 1, 3, 0, 4}; // bot left, top left, bot right, top right +static uint8_t btn_map_upr[BTN_COUNT] = {3, 0, 1, 2, 4}; // bot left, top left, bot right, top right +static uint8_t btn_map_rot[BTN_COUNT] = {2, 1, 3, 0, 4}; @@ -56,10 +58,18 @@ void btn_commit_hold() uint8_t i, x; uint8_t val[BTN_COUNT*2]; + uint8_t *map; + + if (sysflags & SYS_OLED_ROTATE_X) { + map = btn_map_rot; + } else { + map = btn_map_upr; + } + x = 0; for (i = 0; i < BTN_COUNT; i++) { - val[x + 0x00] = btn[btn_map[i]].hold >> 8; - val[x + 0x01] = btn[btn_map[i]].hold & 0xff; + val[x + 0x00] = btn[map[i]].hold >> 8; + val[x + 0x01] = btn[map[i]].hold & 0xff; x += 2; } @@ -67,8 +77,8 @@ void btn_commit_hold() x = 0; for (i = 0; i < BTN_COUNT; i++) { - val[x + 0x00] = btn[btn_map[i]].repeat >> 8; - val[x + 0x01] = btn[btn_map[i]].repeat & 0xff; + val[x + 0x00] = btn[map[i]].repeat >> 8; + val[x + 0x01] = btn[map[i]].repeat & 0xff; x += 2; } @@ -81,7 +91,13 @@ void btn_intr() { uint8_t i; uint8_t btn_state[4]; // [9]; - // uint8_t *btn_mask = &btn_state[4]; + uint8_t *map; + + if (sysflags & SYS_OLED_ROTATE_X) { + map = btn_map_rot; + } else { + map = btn_map_upr; + } // get button data. ch32sub_read(REG_BTN_PUSHED_LATCHED, btn_state, sizeof(btn_state)); @@ -93,13 +109,13 @@ void btn_intr() // process callbacks for new events for (i = 0; i < BTN_COUNT; i++) { if (btn_state[1] & (1 << i)) { - btn_push_cb(btn_map[i]); + btn_push_cb(map[i]); } if (btn_state[2] & (1 << i)) { - btn_hold_cb(btn_map[i]); + btn_hold_cb(map[i]); } if (btn_state[3] & (1 << i)) { - btn_release_cb(btn_map[i]); + btn_release_cb(map[i]); } } } diff --git a/nametag8_CH592/user/user_config.h b/nametag8_CH592/user/user_config.h index 24fc6ce..162fe0f 100644 --- a/nametag8_CH592/user/user_config.h +++ b/nametag8_CH592/user/user_config.h @@ -25,6 +25,7 @@ #define SYS_OLED_ROTATE_X (1 << 0) #define SYS_OLED_ROTATE_Y (1 << 1) #define SYS_OLED_REVERSE_CHARS (1 << 2) // todo: figure out wtf this is +#define SYS_OLED_ROTATE_CHANGED (1 << 7) // user configuration stuff #define UCONF_VER 0x04