diff options
| author | Louis Burda <quent.burda@gmail.com> | 2022-12-18 14:53:27 +0100 |
|---|---|---|
| committer | Louis Burda <quent.burda@gmail.com> | 2022-12-18 14:53:27 +0100 |
| commit | 4995cfc61e7d2d0c05cf493959456b5bc9a74f19 (patch) | |
| tree | 2c8b21d211340f7e74ae9ceaeb8dbd685051b157 /src/hid.c | |
| parent | 8e8c972cbec56d8de31847981149cde4a8fc16cd (diff) | |
| download | sxkbd-4995cfc61e7d2d0c05cf493959456b5bc9a74f19.tar.gz sxkbd-4995cfc61e7d2d0c05cf493959456b5bc9a74f19.zip | |
Rename matrix to keymat and refactor key matrix access
Diffstat (limited to 'src/hid.c')
| -rw-r--r-- | src/hid.c | 106 |
1 files changed, 90 insertions, 16 deletions
@@ -1,10 +1,11 @@ #include "hid.h" #include "split.h" -#include "matrix.h" +#include "keymat.h" #include "keysym.h" #include "keymap.h" -#include <pico/types.h> +#include "bsp/board.h" +#include "pico/types.h" void hid_init(void) @@ -12,15 +13,6 @@ hid_init(void) } -inline uint -layer_index(uint x, uint y) -{ - if (y < KEY_ROWS) - return y * KEY_COLS + x; - else - return y * KEY_COLS + (KEY_COLS - 1 - x); -} - bool hid_gen_report(uint8_t *report) { @@ -30,12 +22,12 @@ hid_gen_report(uint8_t *report) keycnt = 0; for (y = 0; y < KEY_ROWS * 2; y++) { for (x = 0; x < KEY_COLS; x++) { - if (!state_matrix[y * KEY_COLS + x]) + if (!keymat[y][x]) continue; if (keycnt >= 6) break; DEBUG("PRESS %i %", x, y); - p = layer_index(x, y); - report[keycnt] = TO_CODE(keymap_layers_de[0][p]); + p = y * KEY_COLS + x; + report[keycnt] = TO_CODE(keymap_layers[0][p]); keycnt++; } } @@ -43,9 +35,91 @@ hid_gen_report(uint8_t *report) return keycnt > 0; } +bool +send_keyboard_report(void) +{ + static bool cleared = true; + uint8_t report[6] = { 0 }; + bool any; + + any = hid_gen_report(report); + + if (any) { + tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, report); + cleared = false; + return true; + } else if (!cleared) { + tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL); + cleared = true; + return true; + } + + return false; +} + +bool +send_mouse_report(bool state) +{ + if (state) { + tud_hid_mouse_report(REPORT_ID_MOUSE, 0, 10, 10, 0, 0); + return true; + } + + return false; +} + +bool +send_consumer_control_report(bool state) +{ + static bool cleared = true; + uint16_t report; + + if (state) { + report = HID_USAGE_CONSUMER_VOLUME_DECREMENT; + tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &report, 2); + cleared = false; + return true; + } else if (!cleared) { + report = 0; + tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &report, 2); + cleared = true; + return true; + } + + return false; +} + +bool +send_hid_report(int id) +{ + if (!tud_hid_ready()) return false; + + switch (id) { + case REPORT_ID_KEYBOARD: + return send_keyboard_report(); + case REPORT_ID_MOUSE: + return send_mouse_report(false); + case REPORT_ID_CONSUMER_CONTROL: + return send_consumer_control_report(false); + } + + return false; +} + void -hid_task(void) +tud_hid_report_complete_cb(uint8_t instance, + uint8_t const *report, uint8_t len) { + uint8_t id; - /* assemble hid report */ + for (id = report[0] + 1; id < REPORT_ID_MAX; id++) { + if (send_hid_report(id)) + break; + } +} + +void +hid_task(void) +{ + send_hid_report(REPORT_ID_MIN); } |
