[Keyboard] Overhaul usb_to_usb converter

[Keyboard] Fixup config for usb-usb
This commit is contained in:
Drashna Jael're 2024-01-06 01:38:52 -08:00
parent bf92d0ef70
commit d57b4ba37b
No known key found for this signature in database
GPG Key ID: DBA1FD3A860D1B11
9 changed files with 697 additions and 572 deletions

View File

@ -1,3 +0,0 @@
#pragma once
#define NO_ACTION_ONESHOT

View File

@ -7,6 +7,7 @@
"lto": true
},
"features":{
"bluetooth": true
"bluetooth": true,
"extrakey": true
}
}

View File

@ -17,23 +17,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#pragma once
/* size of virtual matrix */
#define MATRIX_ROWS 16
#define MATRIX_ROWS 9
#define MATRIX_COLS 16
/*
* Feature disable options
* These options are also useful to firmware size reduction.
*/
/* disable debug print */
//#define NO_DEBUG
/* disable print */
//#define NO_PRINT
/* disable action features */
//#define NO_ACTION_LAYER
//#define NO_ACTION_TAPPING
//#define NO_ACTION_ONESHOT
// power saving during suspended without remote wakeup disabled
#define UHS2_POWER_SAVING

View File

@ -20,6 +20,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
// USB HID host
#include "Usb.h"
#include "suspend.h"
#include "usb_device_state.h"
#include "usbhub.h"
#include "hid.h"
#include "hidboot.h"
@ -35,40 +37,79 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "host.h"
#include "keyboard.h"
/* KEY CODE to Matrix
*
* HID keycode(1 byte):
* Higher 5 bits indicates ROW and lower 3 bits COL.
*
* 7 6 5 4 3 2 1 0
* +---------------+
* | ROW | COL |
* +---------------+
*
* Matrix space(16 * 16):
* r\c0123456789ABCDEF
* 0 +----------------+
* : | |
* : | |
* 16 +----------------+
extern "C" {
#include "quantum.h"
}
/* Maps value encoding a (row, col) index in a scan matrix
val = ((row) << 4) + (col))
to a 1-byte HID keycode. There are only 142 HID keycodes of interest in the LAYOUT macro,
so a 9x16 (144-element) array is sufficient.
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 Esc F13 F14 F15 F16 F17 F18 F19 F20 F21 F22 F23 F24 F1 F2 F3
1 F4 F5 F6 F7 F8 F9 F10 F11 F12 Prn Scr Pau VDn VUp Mut Pwr
2 Hlp ` 1 2 3 4 5 6 7 8 9 0 - = JPY Bsp
3 Ins Hom PgU NmL / * - Stp Agn Tab Q W E R T Y
4 U I O P [ ] \ Del End PgD 7 8 9 + Mnu Und
5 CLk A S D F G H J K L ; : # Ent 4 5
6 6 KP, Sel Cpy < Z X C V B N M , . / R0
7 Up 1 2 3 KP= Exe Pst MHN HNJ Spc H/E HNK KAN App Lft Dn
8 LCt LSh LAl LGu Rct RSh Ral RGu Rt 0 . Ent Fnd Cut
*/
static const uint8_t hidindex[144] PROGMEM = {
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
0x29, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x3A, 0x3B, 0x3C, /* 0 */
0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x81, 0x80, 0x7F, 0x66, /* 1 */
0x75, 0x35, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x2D, 0x2E, 0x89, 0x2A, /* 2 */
0x49, 0x4A, 0x4B, 0x53, 0x54, 0x55, 0x56, 0x78, 0x79, 0x2B, 0x14, 0x1A, 0x08, 0x15, 0x17, 0x1C, /* 3 */
0x18, 0x0C, 0x12, 0x13, 0x2F, 0x30, 0x31, 0x4C, 0x4D, 0x4E, 0x5F, 0x60, 0x61, 0x57, 0x76, 0x7A, /* 4 */
0x39, 0x04, 0x16, 0x07, 0x09, 0x0A, 0x0B, 0x0D, 0x0E, 0x0F, 0x33, 0x34, 0x32, 0x28, 0x5C, 0x5D, /* 5 */
0x5E, 0x85, 0x77, 0x7C, 0x64, 0x1D, 0x1B, 0x06, 0x19, 0x05, 0x11, 0x10, 0x36, 0x37, 0x38, 0x87, /* 6 */
0x52, 0x59, 0x5A, 0x5B, 0x67, 0x74, 0x7D, 0x8B, 0x91, 0x2C, 0x90, 0x8A, 0x88, 0x65, 0x50, 0x51, /* 7 */
0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0x4F, 0x62, 0x63, 0x58, 0x7E, 0x7B, 0x00, 0x00 /* 8 */
};
/* Maps 1-byte HID keycode to a value encoding a (row, col) index in a 9x16 scan matrix:
val = ((row) << 4) + (col)) */
#define XBAD 0xFF
static const uint8_t matrixindex[256] PROGMEM = {
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
XBAD, XBAD, XBAD, XBAD, 0x51, 0x69, 0x67, 0x53, 0x3C, 0x54, 0x55, 0x56, 0x41, 0x57, 0x58, 0x59, /* 0 */
0x6B, 0x6A, 0x42, 0x43, 0x3A, 0x3D, 0x52, 0x3E, 0x40, 0x68, 0x3B, 0x66, 0x3F, 0x65, 0x22, 0x23, /* 1 */
0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x5D, 0x00, 0x2F, 0x39, 0x79, 0x2C, 0x2D, 0x44, /* 2 */
0x45, 0x46, 0x5C, 0x5A, 0x5B, 0x21, 0x6C, 0x6D, 0x6E, 0x50, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, /* 3 */
0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x30, 0x31, 0x32, 0x47, 0x48, 0x49, 0x88, /* 4 */
0x7E, 0x7F, 0x70, 0x33, 0x34, 0x35, 0x36, 0x4D, 0x8B, 0x71, 0x72, 0x73, 0x5E, 0x5F, 0x60, 0x4A, /* 5 */
0x4B, 0x4C, 0x89, 0x8A, 0x64, 0x7D, 0x1F, 0x74, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, /* 6 */
0x09, 0x0A, 0x0B, 0x0C, 0x75, 0x20, 0x4E, 0x62, 0x37, 0x38, 0x4F, 0x8D, 0x63, 0x76, 0x8C, 0x1E, /* 7 */
0x1D, 0x1C, XBAD, XBAD, XBAD, 0x61, XBAD, 0x6F, 0x7C, 0x2E, 0x7B, 0x77, XBAD, XBAD, XBAD, XBAD, /* 8 */
0x7A, 0x78, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, /* 9 */
XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, /* A */
XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, /* B */
XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, /* C */
XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, /* D */
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, /* E */
XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, XBAD, /* F */
};
#define ROW_MASK 0xF0
#define COL_MASK 0x0F
#define CODE(row, col) (((row) << 4) | (col))
#define ROW(code) (((code) & ROW_MASK) >> 4)
#define COL(code) ((code) & COL_MASK)
#define CODE(row, col) (pgm_read_byte(&hidindex[(((row) << 4) | (col))]))
#define ROW(code) ((pgm_read_byte(&matrixindex[(code)]) & ROW_MASK) >> 4)
#define COL(code) (pgm_read_byte(&matrixindex[(code)]) & COL_MASK)
#define ROW_BITS(code) (1 << COL(code))
// Integrated key state of all keyboards
static report_keyboard_t local_keyboard_report;
static bool matrix_is_mod = false;
/*
* USB Host Shield HID keyboards
* This supports two cascaded hubs and four keyboards
*/
USB usb_host;
USBHub hub1(&usb_host);
USBHub hub2(&usb_host);
HIDBoot<HID_PROTOCOL_KEYBOARD> kbd1(&usb_host);
HIDBoot<HID_PROTOCOL_KEYBOARD> kbd2(&usb_host);
HIDBoot<HID_PROTOCOL_KEYBOARD> kbd3(&usb_host);
@ -77,6 +118,8 @@ KBDReportParser kbd_parser1;
KBDReportParser kbd_parser2;
KBDReportParser kbd_parser3;
KBDReportParser kbd_parser4;
USBHub hub1(&usb_host);
USBHub hub2(&usb_host);
extern "C" {
uint8_t matrix_rows(void) { return MATRIX_ROWS; }
@ -126,7 +169,6 @@ extern "C" {
}
uint8_t matrix_scan(void) {
bool changed = false;
static uint16_t last_time_stamp1 = 0;
static uint16_t last_time_stamp2 = 0;
static uint16_t last_time_stamp3 = 0;
@ -150,13 +192,15 @@ extern "C" {
or_report(kbd_parser3.report);
or_report(kbd_parser4.report);
changed = true;
matrix_is_mod = true;
dprintf("state: %02X %02X", local_keyboard_report.mods, local_keyboard_report.reserved);
for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
dprintf(" %02X", local_keyboard_report.keys[i]);
}
dprint("\r\n");
} else {
matrix_is_mod = false;
}
uint16_t timer;
@ -179,12 +223,16 @@ extern "C" {
}
}
matrix_scan_kb();
return changed;
return matrix_is_mod;
}
bool matrix_is_on(uint8_t row, uint8_t col) {
uint8_t code = CODE(row, col);
if (!IS_ANY(code)) {
return false;
}
if (IS_MODIFIER_KEYCODE(code)) {
if (local_keyboard_report.mods & ROW_BITS(code)) {
return true;
@ -232,4 +280,59 @@ extern "C" {
led_set_user(usb_led);
led_update_kb((led_t){.raw = usb_led});
}
#if defined(ARDUINO) && ARDUINO > 101
static bool init_done = false;
void keyboard_post_init_kb(void) {
init_done = true;
keyboard_post_init_user();
}
void notify_usb_device_state_change_kb(enum usb_device_state usb_device_state) {
if (usb_device_state == USB_DEVICE_STATE_SUSPEND) {
if (!init_done) return;
clear_keyboard();
usb_host.suspend();
# ifdef UHS2_POWER_SAVING
// power down when remote wake is not enabled
if (!USB_Device_RemoteWakeupEnabled) {
dprintf("[p]");
usb_host.powerDown();
}
# endif
}
notify_usb_device_state_change_user(usb_device_state);
}
void suspend_power_down_kb(void) {
if (USB_Device_RemoteWakeupEnabled) {
if (usb_host.checkRemoteWakeup()) {
USB_Device_SendRemoteWakeup();
}
}
suspend_power_down_user();
}
void suspend_wakeup_init_kb(void) {
if (!init_done) return;
# ifdef UHS2_POWER_SAVING
// power down when remote wake is not enabled
if (!USB_Device_RemoteWakeupEnabled) {
usb_host.powerUp();
// USB state cannot be retained through power down/up cycle
// device should be enumerated and initialize from the beginning
usb_host.ReleaseAllDevices();
usb_host.setUsbTaskState(USB_STATE_DETACHED);
}
# endif
usb_host.resume();
}
#endif
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,23 @@
/*
Copyright 2022 Jeff Shufelt <jshuf@puppyfish.com> @jshuf
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
// We have 1 2-bit layout option and 13 1-bit layout options
// in the VIA usb_usb.json, so we need 15 bits (2 bytes)
// of layout option storage.
#define VIA_EEPROM_LAYOUT_OPTIONS_SIZE 2
#define DYNAMIC_KEYMAP_LAYER_COUNT 3

View File

@ -0,0 +1,66 @@
/*
Copyright 2022 Jeff Shufelt <jshuf@puppyfish.com> @jshuf
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include QMK_KEYBOARD_H
const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
/* 0: plain Qwerty without layer switching
* ,---------------. ,---------------. ,---------------.
* |F13|F14|F15|F16| |F17|F18|F19|F20| |F21|F22|F23|F24|
* ,---. |---------------| |---------------| |---------------| ,-----------. ,---------------. ,-------.
* |Esc| |F1 |F2 |F3 |F4 | |F5 |F6 |F7 |F8 | |F9 |F10|F11|F12| |PrS|ScL|Pau| |VDn|VUp|Mut|Pwr| | Help |
* `---' `---------------' `---------------' `---------------' `-----------' `---------------' `-------'
* ,-----------------------------------------------------------. ,-----------. ,---------------. ,-------.
* | `| 1| 2| 3| 4| 5| 6| 7| 8| 9| 0| -| =|JPY|Bsp| |Ins|Hom|PgU| |NmL| /| *| -| |Stp|Agn|
* |-----------------------------------------------------------| |-----------| |---------------| |-------|
* |Tab | Q| W| E| R| T| Y| U| I| O| P| [| ]| \ | |Del|End|PgD| | 7| 8| 9| +| |Mnu|Und|
* |-----------------------------------------------------------| `-----------' |---------------| |-------|
* |CapsL | A| S| D| F| G| H| J| K| L| ;| :| #|Retn| | 4| 5| 6|KP,| |Sel|Cpy|
* |-----------------------------------------------------------| ,---. |---------------| |-------|
* |Shft| <| Z| X| C| V| B| N| M| ,| ,| /| RO|Shift | |Up | | 1| 2| 3|KP=| |Exe|Pst|
* |-----------------------------------------------------------| ,-----------. |---------------| |-------|
* |Ctl|Gui|Alt|MHEN|HNJ| Space |H/E|HENK|KANA|Alt|Gui|App|Ctl| |Lef|Dow|Rig| | 0 | .|Ent| |Fnd|Cut|
* `-----------------------------------------------------------' `-----------' `---------------' `-------'
*/
[0] = LAYOUT_all(
KC_F13, KC_F14, KC_F15, KC_F16, KC_F17, KC_F18, KC_F19, KC_F20, KC_F21, KC_F22, KC_F23, KC_F24,
KC_ESC, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_PSCR, KC_SCRL, KC_PAUS, KC_VOLD, KC_VOLU, KC_MUTE, KC_PWR, KC_HELP,
KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_INT3, KC_BSPC, KC_INS, KC_HOME, KC_PGUP, KC_NUM, KC_PSLS, KC_PAST, KC_PMNS, KC_STOP, KC_AGIN,
KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, KC_DEL, KC_END, KC_PGDN, KC_P7, KC_P8, KC_P9, KC_PPLS, KC_MENU, KC_UNDO,
KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_NUHS, KC_ENT, KC_P4, KC_P5, KC_P6, KC_PCMM, KC_SLCT, KC_COPY,
KC_LSFT, KC_NUBS, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_INT1, KC_RSFT, KC_UP, KC_P1, KC_P2, KC_P3, KC_PEQL, KC_EXEC, KC_PSTE,
KC_LCTL, KC_LGUI, KC_LALT, KC_INT5, KC_LNG2, KC_SPC, KC_LNG1, KC_INT4, KC_INT2, KC_RALT, KC_RGUI, MO(1), KC_RCTL, KC_LEFT, KC_DOWN, KC_RGHT, KC_P0, KC_PDOT, KC_PENT, KC_FIND, KC_CUT
),
[1] = LAYOUT_all(
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, KC_UP, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, KC_LEFT, KC_DOWN, KC_RGHT, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, MO(2), _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
),
[2] = LAYOUT_all(
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
QK_BOOT, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, EE_CLR, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
DB_TOGG, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
)
};

View File

@ -0,0 +1,2 @@
VIA_ENABLE = yes
LTO_ENABLE = yes

View File

@ -3,3 +3,7 @@ CUSTOM_MATRIX = yes
SRC += custom_matrix.cpp
DEFAULT_FOLDER = converter/usb_usb/hasu
MAGIC_ENABLE = no
SPACE_CADET_ENABLE = no
GRAVE_ESC_ENABLE = no