commit 8b4a42456b0b897ec1ce3fa14f670d2ac13f706e
parent f34bf57bc5dae6fd294d535e737789e748ae18af
Author: mntmn <lukas@mntmn.com>
Date: Thu, 28 Nov 2019 22:33:32 +0100
WIP factor out scancodes and scanning function
Diffstat:
1 file changed, 117 insertions(+), 248 deletions(-)
diff --git a/reform-keyboard2-fw/Keyboard.c b/reform-keyboard2-fw/Keyboard.c
@@ -37,6 +37,7 @@
#include <avr/io.h>
#include "serial.h"
#include "ssd1306.h"
+#include "scancodes.h"
/** Buffer to hold the previously generated Keyboard HID report, for comparison purposes inside the HID class driver. */
static uint8_t PrevKeyboardHIDReportBuffer[sizeof(USB_KeyboardReport_Data_t)];
@@ -67,6 +68,120 @@ USB_ClassInfo_HID_Device_t Keyboard_HID_Interface =
#define set_input(portdir,pin) portdir &= ~(1<<pin)
#define set_output(portdir,pin) portdir |= (1<<pin)
+const uint8_t matrix[15*6] = {
+ KEY_ESCAPE, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_F11, KEY_F12, HID_KEYBOARD_SC_EXSEL, HID_KEYBOARD_SC_EXSEL,
+
+ KEY_GRAVE_ACCENT_AND_TILDE, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_0, KEY_MINUS_AND_UNDERSCORE, KEY_EQUAL_AND_PLUS, KEY_BACKSPACE, 0,
+
+ KEY_TAB, KEY_Q, KEY_W, KEY_E, KEY_R, KEY_T, KEY_Y, KEY_U, KEY_I, KEY_O, KEY_P, KEY_OPENING_BRACKET_AND_OPENING_BRACE, KEY_CLOSING_BRACKET_AND_CLOSING_BRACE, KEY_BACKSLASH_AND_PIPE, 0,
+
+ HID_KEYBOARD_SC_LEFT_CONTROL, KEY_F13, KEY_A, KEY_S, KEY_D, KEY_F, KEY_G, KEY_H, KEY_J, KEY_K, KEY_L, KEY_SEMICOLON_AND_COLON, KEY_APOSTROPHE_AND_QUOTE, KEY_ENTER, 0,
+
+ HID_KEYBOARD_SC_LEFT_SHIFT, KEY_DELETE, KEY_Z, KEY_X, KEY_C, KEY_V, KEY_B, KEY_N, KEY_M, HID_KEYBOARD_SC_COMMA_AND_LESS_THAN_SIGN, HID_KEYBOARD_SC_DOT_AND_GREATER_THAN_SIGN, KEY_SLASH_AND_QUESTION_MARK, HID_KEYBOARD_SC_UP_ARROW, HID_KEYBOARD_SC_RIGHT_SHIFT, 0,
+
+ HID_KEYBOARD_SC_EXSEL, HID_KEYBOARD_SC_LEFT_GUI, HID_KEYBOARD_SC_LEFT_CONTROL, KEY_SPACE, HID_KEYBOARD_SC_LEFT_ALT, HID_KEYBOARD_SC_RIGHT_ALT, KEY_SPACE, HID_KEYBOARD_SC_PAGE_UP, HID_KEYBOARD_SC_PAGE_DOWN, HID_KEYBOARD_SC_LEFT_ARROW, HID_KEYBOARD_SC_DOWN_ARROW, HID_KEYBOARD_SC_RIGHT_ARROW, 0,0,0
+};
+
+// f8 = sleep
+// 49 = mute
+// 84 = scroll lock
+
+void remote_turn_on_som(void) {
+ ser_write('1');
+ ser_write('p');
+ ser_write('\r');
+}
+
+void remote_turn_off_som(void) {
+ ser_write('0');
+ ser_write('p');
+ ser_write('\r');
+}
+
+
+char metaPressed = 0;
+uint8_t lastKeyCodes = 0;
+
+void process_keyboard(char usb_report_mode, USB_KeyboardReport_Data_t* KeyboardReport) {
+ uint8_t metaPressedNow = 0;
+ uint8_t keyPressedNow = 0;
+ // how many keys are pressed this round
+ uint8_t usedKeyCodes = 0;
+
+ // pull ROWs low one after the other
+ for (int y=0; y<6; y++) {
+
+ switch (y) {
+ case 0: output_low(PORTB, 6); break;
+ case 1: output_low(PORTB, 5); break;
+ case 2: output_low(PORTB, 4); break;
+ case 3: output_low(PORTD, 7); break;
+ case 4: output_low(PORTD, 6); break;
+ case 5: output_low(PORTD, 4); break;
+ }
+
+ // check input COLs
+ for (int x=0; x<14; x++) {
+ uint16_t keycode = matrix[y*15+x];
+ uint8_t pressed = 0;
+
+ // column pins are all over the place
+ switch (x) {
+ case 0: pressed = !(PIND&(1<<5)); break;
+ case 1: pressed = !(PIND&(1<<3)); break;
+ case 2: pressed = !(PIND&(1<<2)); break;
+ case 3: pressed = !(PINC&(1<<7)); break;
+ case 4: pressed = !(PINB&(1<<3)); break;
+ case 5: pressed = !(PINB&(1<<2)); break;
+ case 6: pressed = !(PINB&(1<<1)); break;
+ case 7: pressed = !(PINB&(1<<0)); break;
+ case 8: pressed = !(PINF&(1<<0)); break;
+ case 9: pressed = !(PINF&(1<<1)); break;
+ case 10: pressed = !(PINF&(1<<4)); break;
+ case 11: pressed = !(PINF&(1<<5)); break;
+ case 12: pressed = !(PINF&(1<<6)); break;
+ case 13: pressed = !(PINC&(1<<6)); break;
+ }
+
+ if (pressed) {
+ if (keycode == HID_KEYBOARD_SC_EXSEL) {
+ metaPressedNow = 1;
+
+ if (keycode == KEY_0) {
+ remote_turn_off_som();
+ }
+ else if (keycode == KEY_1) {
+ remote_turn_on_som();
+ }
+
+ } else {
+ if (usb_report_mode && KeyboardReport) {
+ KeyboardReport->KeyCode[usedKeyCodes++] = keycode;
+ keyPressedNow = keycode;
+ }
+ }
+ }
+ }
+
+ switch (y) {
+ case 0: output_high(PORTB, 6); break;
+ case 1: output_high(PORTB, 5); break;
+ case 2: output_high(PORTB, 4); break;
+ case 3: output_high(PORTD, 7); break;
+ case 4: output_high(PORTD, 6); break;
+ case 5: output_high(PORTD, 4); break;
+ }
+ }
+
+ metaPressed = metaPressedNow;
+
+ if (lastKeyCodes!=usedKeyCodes) {
+ gfx_poke(0,2,usedKeyCodes+'0');
+ iota_gfx_flush();
+ lastKeyCodes = usedKeyCodes;
+ }
+}
+
int main(void)
{
SetupHardware();
@@ -74,6 +189,7 @@ int main(void)
for (;;)
{
+ process_keyboard(0, NULL);
HID_Device_USBTask(&Keyboard_HID_Interface);
USB_USBTask();
}
@@ -155,164 +271,6 @@ void EVENT_USB_Device_StartOfFrame(void)
HID_Device_MillisecondElapsed(&Keyboard_HID_Interface);
}
-#define KEY_A 0x04
-#define KEY_B 0x05
-#define KEY_C 0x06
-#define KEY_D 0x07
-#define KEY_E 0x08
-#define KEY_F 0x09
-#define KEY_G 0x0A
-#define KEY_H 0x0B
-#define KEY_I 0x0C
-#define KEY_J 0x0D
-#define KEY_K 0x0E
-#define KEY_L 0x0F
-#define KEY_M 0x10
-#define KEY_N 0x11
-#define KEY_O 0x12
-#define KEY_P 0x13
-#define KEY_Q 0x14
-#define KEY_R 0x15
-#define KEY_S 0x16
-#define KEY_T 0x17
-#define KEY_U 0x18
-#define KEY_V 0x19
-#define KEY_W 0x1A
-#define KEY_X 0x1B
-#define KEY_Y 0x1C
-#define KEY_Z 0x1D
-#define KEY_1 0x1E
-#define KEY_2 0x1F
-#define KEY_3 0x20
-#define KEY_4 0x21
-#define KEY_5 0x22
-#define KEY_6 0x23
-#define KEY_7 0x24
-#define KEY_8 0x25
-#define KEY_9 0x26
-#define KEY_0 0x27
-#define KEY_ENTER 0x28
-#define KEY_ESCAPE 0x29
-#define KEY_BACKSPACE 0x2A
-#define KEY_TAB 0x2B
-#define KEY_SPACE 0x2C
-#define KEY_MINUS_AND_UNDERSCORE 0x2D
-#define KEY_EQUAL_AND_PLUS 0x2E
-#define KEY_OPENING_BRACKET_AND_OPENING_BRACE 0x2F
-#define KEY_CLOSING_BRACKET_AND_CLOSING_BRACE 0x30
-#define KEY_BACKSLASH_AND_PIPE 0x31
-#define KEY_NON_US_HASHMARK_AND_TILDE 0x32
-#define KEY_SEMICOLON_AND_COLON 0x33
-#define KEY_APOSTROPHE_AND_QUOTE 0x34
-#define KEY_GRAVE_ACCENT_AND_TILDE 0x35
-#define KEY_COMMA_AND_LESS_THAN_SIGN 0x36
-#define KEY_DOT_AND_GREATER_THAN_SIGN 0x37
-#define KEY_SLASH_AND_QUESTION_MARK 0x38
-#define KEY_CAPS_LOCK 0x39
-#define KEY_F1 0x3A
-#define KEY_F2 0x3B
-#define KEY_F3 0x3C
-#define KEY_F4 0x3D
-#define KEY_F5 0x3E
-#define KEY_F6 0x3F
-#define KEY_F7 0x40
-#define KEY_F8 0x41
-#define KEY_F9 0x42
-#define KEY_F10 0x43
-#define KEY_F11 0x44
-#define KEY_F12 0x45
-#define KEY_PRINT_SCREEN 0x46
-#define KEY_SCROLL_LOCK 0x47
-#define KEY_PAUSE 0x48
-#define KEY_INSERT 0x49
-#define KEY_HOME 0x4A
-#define KEY_PAGE_UP 0x4B
-#define KEY_DELETE 0x4C
-#define KEY_END 0x4D
-#define KEY_PAGE_DOWN 0x4E
-#define KEY_RIGHT_ARROW 0x4F
-#define KEY_LEFT_ARROW 0x50
-#define KEY_DOWN_ARROW 0x51
-#define KEY_UP_ARROW 0x52
-#define KEY_NUM_LOCK 0x53
-#define KEY_KEYPAD_SLASH 0x54
-#define KEY_KEYPAD_ASTERISK 0x55
-#define KEY_KEYPAD_MINUS 0x56
-#define KEY_KEYPAD_PLUS 0x57
-#define KEY_KEYPAD_ENTER 0x58
-#define KEY_KEYPAD_1_AND_END 0x59
-#define KEY_KEYPAD_2_AND_DOWN_ARROW 0x5A
-#define KEY_KEYPAD_3_AND_PAGE_DOWN 0x5B
-#define KEY_KEYPAD_4_AND_LEFT_ARROW 0x5C
-#define KEY_KEYPAD_5 0x5D
-#define KEY_KEYPAD_6_AND_RIGHT_ARROW 0x5E
-#define KEY_KEYPAD_7_AND_HOME 0x5F
-#define KEY_KEYPAD_8_AND_UP_ARROW 0x60
-#define KEY_KEYPAD_9_AND_PAGE_UP 0x61
-#define KEY_KEYPAD_0_AND_INSERT 0x62
-#define KEY_KEYPAD_DOT_AND_DELETE 0x63
-#define KEY_NON_US_BACKSLASH_AND_PIPE 0x64
-#define KEY_APPLICATION 0x65
-#define KEY_POWER 0x66
-#define KEY_KEYPAD_EQUAL_SIGN 0x67
-#define KEY_F13 0x68
-#define KEY_F14 0x69
-#define KEY_F15 0x6A
-#define KEY_F16 0x6B
-#define KEY_F17 0x6C
-#define KEY_F18 0x6D
-#define KEY_F19 0x6E
-#define KEY_F20 0x6F
-#define KEY_F21 0x70
-#define KEY_F22 0x71
-#define KEY_F23 0x72
-#define KEY_F24 0x73
-
-#define KEY_MUTE 0x7F
-#define KEY_VOLUME_UP 0x80
-#define KEY_VOLUME_DOWN 0x81
-
-
-#ifdef NEO2
-
-const uint8_t matrix[15*6] = {
- KEY_ESCAPE, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_F11, KEY_F12, HID_KEYBOARD_SC_PAGE_UP, HID_KEYBOARD_SC_PAGE_DOWN,
- KEY_GRAVE_ACCENT_AND_TILDE, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_0, KEY_MINUS_AND_UNDERSCORE, KEY_EQUAL_AND_PLUS, KEY_BACKSPACE, 0,
- KEY_TAB, KEY_Q, KEY_W, KEY_E, KEY_R, KEY_T, KEY_Y, KEY_U, KEY_I, KEY_O, KEY_P, KEY_OPENING_BRACKET_AND_OPENING_BRACE, KEY_CLOSING_BRACKET_AND_CLOSING_BRACE, KEY_ENTER, 0,
- HID_KEYBOARD_SC_CAPS_LOCK, KEY_A, KEY_S, KEY_D, KEY_F, KEY_G, KEY_H, KEY_J, KEY_K, KEY_L, KEY_SEMICOLON_AND_COLON, KEY_APOSTROPHE_AND_QUOTE, HID_KEYBOARD_SC_CAPS_LOCK,0,0,
- HID_KEYBOARD_SC_LEFT_SHIFT, KEY_Z, KEY_X, KEY_C, KEY_V, KEY_B, KEY_N, KEY_M, HID_KEYBOARD_SC_COMMA_AND_LESS_THAN_SIGN, HID_KEYBOARD_SC_DOT_AND_GREATER_THAN_SIGN, KEY_SLASH_AND_QUESTION_MARK, HID_KEYBOARD_SC_UP_ARROW, HID_KEYBOARD_SC_RIGHT_SHIFT, 0,0,
- HID_KEYBOARD_SC_LEFT_CONTROL, HID_KEYBOARD_SC_RIGHT_ALT, HID_KEYBOARD_SC_LEFT_ALT, KEY_SPACE, HID_KEYBOARD_SC_RIGHT_ALT, HID_KEYBOARD_SC_LEFT_ARROW, HID_KEYBOARD_SC_DOWN_ARROW, HID_KEYBOARD_SC_RIGHT_ARROW, KEY_SPACE,KEY_SPACE,0,0,0,0,0
-};
-
-#else
-const uint8_t matrix[15*6] = {
- KEY_ESCAPE, KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_F11, KEY_F12, HID_KEYBOARD_SC_EXSEL, HID_KEYBOARD_SC_EXSEL,
-
- KEY_GRAVE_ACCENT_AND_TILDE, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_0, KEY_MINUS_AND_UNDERSCORE, KEY_EQUAL_AND_PLUS, KEY_BACKSPACE, 0,
-
- KEY_TAB, KEY_Q, KEY_W, KEY_E, KEY_R, KEY_T, KEY_Y, KEY_U, KEY_I, KEY_O, KEY_P, KEY_OPENING_BRACKET_AND_OPENING_BRACE, KEY_CLOSING_BRACKET_AND_CLOSING_BRACE, KEY_BACKSLASH_AND_PIPE, 0,
-
- HID_KEYBOARD_SC_LEFT_CONTROL, KEY_F13, KEY_A, KEY_S, KEY_D, KEY_F, KEY_G, KEY_H, KEY_J, KEY_K, KEY_L, KEY_SEMICOLON_AND_COLON, KEY_APOSTROPHE_AND_QUOTE, KEY_ENTER, 0,
-
- HID_KEYBOARD_SC_LEFT_SHIFT, KEY_DELETE, KEY_Z, KEY_X, KEY_C, KEY_V, KEY_B, KEY_N, KEY_M, HID_KEYBOARD_SC_COMMA_AND_LESS_THAN_SIGN, HID_KEYBOARD_SC_DOT_AND_GREATER_THAN_SIGN, KEY_SLASH_AND_QUESTION_MARK, HID_KEYBOARD_SC_UP_ARROW, HID_KEYBOARD_SC_RIGHT_SHIFT, 0,
-
- HID_KEYBOARD_SC_EXSEL, HID_KEYBOARD_SC_LEFT_GUI, HID_KEYBOARD_SC_LEFT_CONTROL, KEY_SPACE, HID_KEYBOARD_SC_LEFT_ALT, HID_KEYBOARD_SC_RIGHT_ALT, KEY_SPACE, HID_KEYBOARD_SC_PAGE_UP, HID_KEYBOARD_SC_PAGE_DOWN, HID_KEYBOARD_SC_LEFT_ARROW, HID_KEYBOARD_SC_DOWN_ARROW, HID_KEYBOARD_SC_RIGHT_ARROW, 0,0,0
-};
-
-// f8 = sleep
-// 49 = mute
-// 84 = scroll lock
-
-/*const uint8_t matrix2[15*6] = {
- 0x84, KEY_F14, KEY_F15, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8, KEY_F9, 0x49, HID_KEYBOARD_SC_VOLUME_DOWN, HID_KEYBOARD_SC_VOLUME_UP, 0x62, 0xf8,
- KEY_GRAVE_ACCENT_AND_TILDE, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_0, KEY_MINUS_AND_UNDERSCORE, KEY_EQUAL_AND_PLUS, KEY_BACKSPACE, 0,
- KEY_TAB, KEY_Q, KEY_W, KEY_E, KEY_R, KEY_T, KEY_Y, KEY_U, KEY_I, KEY_O, KEY_P, KEY_OPENING_BRACKET_AND_OPENING_BRACE, KEY_CLOSING_BRACKET_AND_CLOSING_BRACE, KEY_ENTER, 0,
- HID_KEYBOARD_SC_RIGHT_CONTROL, KEY_A, KEY_S, KEY_D, KEY_F, KEY_G, KEY_H, KEY_J, KEY_K, KEY_L, KEY_SEMICOLON_AND_COLON, KEY_APOSTROPHE_AND_QUOTE, KEY_BACKSLASH_AND_PIPE,0,0,
- HID_KEYBOARD_SC_LEFT_SHIFT, KEY_Z, KEY_X, KEY_C, KEY_V, KEY_B, KEY_N, KEY_M, HID_KEYBOARD_SC_COMMA_AND_LESS_THAN_SIGN, HID_KEYBOARD_SC_DOT_AND_GREATER_THAN_SIGN, HID_KEYBOARD_SC_PAGE_UP, KEY_SLASH_AND_QUESTION_MARK, HID_KEYBOARD_SC_RIGHT_SHIFT, 0,0,
- HID_KEYBOARD_SC_EXSEL, HID_KEYBOARD_SC_LEFT_GUI, HID_KEYBOARD_SC_LEFT_ALT, KEY_SPACE, HID_KEYBOARD_SC_HOME, HID_KEYBOARD_SC_PAGE_DOWN, HID_KEYBOARD_SC_END, HID_KEYBOARD_SC_RIGHT_ALT, KEY_SPACE,KEY_SPACE,0,0,0,0,0
- };*/
-#endif
-
/** HID class driver callback function for the creation of HID reports to the host.
*
* \param[in] HIDInterfaceInfo Pointer to the HID class interface configuration structure being referenced
@@ -324,21 +282,6 @@ const uint8_t matrix[15*6] = {
* \return Boolean \c true to force the sending of the report, \c false to let the library determine if it needs to be sent
*/
-void remote_turn_on_som(void) {
- ser_write('1');
- ser_write('p');
- ser_write('\r');
-}
-
-void remote_turn_off_som(void) {
- ser_write('0');
- ser_write('p');
- ser_write('\r');
-}
-
-char metaPressed = 0;
-uint8_t lastKeyCodes = 0;
-
bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDInterfaceInfo,
uint8_t* const ReportID,
const uint8_t ReportType,
@@ -346,82 +289,8 @@ bool CALLBACK_HID_Device_CreateHIDReport(USB_ClassInfo_HID_Device_t* const HIDIn
uint16_t* const ReportSize)
{
USB_KeyboardReport_Data_t* KeyboardReport = (USB_KeyboardReport_Data_t*)ReportData;
- uint8_t metaPressedNow = 0;
- uint8_t keyPressedNow = 0;
- // how many keys are pressed this round
- uint8_t usedKeyCodes = 0;
-
- // pull ROWs low one after the other
- for (int y=0; y<6; y++) {
-
- switch (y) {
- case 0: output_low(PORTB, 6); break;
- case 1: output_low(PORTB, 5); break;
- case 2: output_low(PORTB, 4); break;
- case 3: output_low(PORTD, 7); break;
- case 4: output_low(PORTD, 6); break;
- case 5: output_low(PORTD, 4); break;
- }
- // check input COLs
- for (int x=0; x<14; x++) {
- uint16_t keycode = matrix[y*15+x];
- uint8_t pressed = 0;
-
- // column pins are all over the place
- switch (x) {
- case 0: pressed = !(PIND&(1<<5)); break;
- case 1: pressed = !(PIND&(1<<3)); break;
- case 2: pressed = !(PIND&(1<<2)); break;
- case 3: pressed = !(PINC&(1<<7)); break;
- case 4: pressed = !(PINB&(1<<3)); break;
- case 5: pressed = !(PINB&(1<<2)); break;
- case 6: pressed = !(PINB&(1<<1)); break;
- case 7: pressed = !(PINB&(1<<0)); break;
- case 8: pressed = !(PINF&(1<<0)); break;
- case 9: pressed = !(PINF&(1<<1)); break;
- case 10: pressed = !(PINF&(1<<4)); break;
- case 11: pressed = !(PINF&(1<<5)); break;
- case 12: pressed = !(PINF&(1<<6)); break;
- case 13: pressed = !(PINC&(1<<6)); break;
- }
-
- if (pressed) {
- if (keycode == HID_KEYBOARD_SC_EXSEL) {
- metaPressedNow = 1;
-
- if (keycode == KEY_0) {
- remote_turn_off_som();
- }
- else if (keycode == KEY_1) {
- remote_turn_on_som();
- }
-
- } else {
- KeyboardReport->KeyCode[usedKeyCodes++] = keycode;
- keyPressedNow = keycode;
- }
- }
- }
-
- switch (y) {
- case 0: output_high(PORTB, 6); break;
- case 1: output_high(PORTB, 5); break;
- case 2: output_high(PORTB, 4); break;
- case 3: output_high(PORTD, 7); break;
- case 4: output_high(PORTD, 6); break;
- case 5: output_high(PORTD, 4); break;
- }
- }
-
- metaPressed = metaPressedNow;
-
- if (lastKeyCodes!=usedKeyCodes) {
- gfx_poke(0,2,usedKeyCodes+'0');
- iota_gfx_flush();
- ser_write(usedKeyCodes+'0');
- lastKeyCodes = usedKeyCodes;
- }
+ process_keyboard(1, KeyboardReport);
*ReportSize = sizeof(USB_KeyboardReport_Data_t);
return false;