From f66eb067128356c731d0453077f44501fc9f8128 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Mon, 12 Dec 2016 00:21:39 +0100 Subject: [PATCH] Coding style fixes. --- right/src/keyboard_layout.c | 103 +++++++++++++------------- right/src/keyboard_layout.h | 115 ++++++++++++++--------------- right/src/led_driver.c | 4 +- right/src/main.c | 4 +- right/src/test_led.c | 1 - right/src/usb_interface_keyboard.c | 15 ++-- right/src/usb_interface_keyboard.h | 2 +- 7 files changed, 121 insertions(+), 123 deletions(-) diff --git a/right/src/keyboard_layout.c b/right/src/keyboard_layout.c index 112d822..fde3565 100644 --- a/right/src/keyboard_layout.c +++ b/right/src/keyboard_layout.c @@ -2,38 +2,37 @@ #include "led_driver.h" static uint8_t keyMasks[LAYOUT_KEY_COUNT]; - static uint8_t modifierState = 0; static inline __attribute__((always_inline)) uhk_key_t getKeycode(KEYBOARD_LAYOUT(layout), uint8_t keyId) { - if (keyIdmodifiers |= (1 << i); + if (key.type != UHK_KEY_SIMPLE) { + return false; } - } - report->scancodes[scancodeIdx] = key.simple.key; - return true; + + if (!key.simple.key) { + return false; + } + + for (uint8_t i = 0; i < 8; i++) { + if (key.simple.mods & (1 << i) || key.simple.key == HID_KEYBOARD_SC_LEFT_CONTROL + i) { + report->modifiers |= (1 << i); + } + } + + report->scancodes[scancodeIdx] = key.simple.key; + return true; } bool layerOn(uhk_key_t key) { - modifierState |= (1 << (key.layer.target - 1)); - return false; + modifierState |= (1 << (key.layer.target - 1)); + return false; } bool layerOff(uhk_key_t key) { - modifierState &= ~(1 << (key.layer.target - 1)); - return false; + modifierState &= ~(1 << (key.layer.target - 1)); + return false; } bool handleKey(uhk_key_t key, int scancodeIdx, usb_keyboard_report_t *report, uint8_t keyState) { - switch (key.type) { - case UHK_KEY_SIMPLE: - if (keyState) - return pressKey (key, scancodeIdx, report); - break; - case UHK_KEY_LAYER: - if (keyState) - return layerOn (key); - return layerOff (key); - break; - default: - break; - } - return false; + switch (key.type) { + case UHK_KEY_SIMPLE: + if (keyState) { + return pressKey (key, scancodeIdx, report); + } + break; + case UHK_KEY_LAYER: + if (keyState) { + return layerOn(key); + } + return layerOff(key); + break; + default: + break; + } + return false; } void fillKeyboardReport(usb_keyboard_report_t *report, const uint8_t *leftKeyStates, const uint8_t *rightKeyStates, KEYBOARD_LAYOUT(layout)) { @@ -92,26 +95,26 @@ void fillKeyboardReport(usb_keyboard_report_t *report, const uint8_t *leftKeySta clearKeymasks(leftKeyStates, rightKeyStates); for (uint8_t keyId=0; keyId=USB_KEYBOARD_MAX_KEYS) { + if (scancodeIdx >= USB_KEYBOARD_MAX_KEYS) { break; } - uhk_key_t code=getKeycode(layout, keyId); + uhk_key_t code = getKeycode(layout, keyId); - if (handleKey(code, scancodeIdx, report, rightKeyStates[keyId])) { - scancodeIdx++; - } + if (handleKey(code, scancodeIdx, report, rightKeyStates[keyId])) { + scancodeIdx++; + } } for (uint8_t keyId=0; keyId=USB_KEYBOARD_MAX_KEYS) { + if (scancodeIdx >= USB_KEYBOARD_MAX_KEYS) { break; } - uhk_key_t code=getKeycode(layout, LAYOUT_LEFT_OFFSET+keyId); + uhk_key_t code = getKeycode(layout, LAYOUT_LEFT_OFFSET+keyId); - if (handleKey(code, scancodeIdx, report, leftKeyStates[keyId])) { - scancodeIdx++; - } + if (handleKey(code, scancodeIdx, report, leftKeyStates[keyId])) { + scancodeIdx++; + } } } diff --git a/right/src/keyboard_layout.h b/right/src/keyboard_layout.h index bfbf46b..662f070 100644 --- a/right/src/keyboard_layout.h +++ b/right/src/keyboard_layout.h @@ -5,20 +5,17 @@ #include "lufa/HIDClassCommon.h" #include "usb_composite_device.h" -/** - * Keyboard layout is a 2D array of scan codes. - * - * First dimension is the Key ID of a given key. Key IDs are the indices of the - * of the active keys of the key_matrix_t structure. In case of left half, an - * offset of 35 is added. - * - * For each Key ID, there are 4 different possible scan codes: - * - default, when no modifiers are pressed - * - mod layer - * - fn layer - * - mod+fn layer - * - */ +// Keyboard layout is a 2D array of scan codes. +// +// First dimension is the Key ID of a given key. Key IDs are the indices of the +// of the active keys of the key_matrix_t structure. In case of left half, an +// offset of 35 is added. +// +// For each Key ID, there are 4 different possible scan codes: +// - default, when no modifiers are pressed +// - mod layer +// - fn layer +// - mod+fn layer #define KEY_STATE_COUNT (5*7) @@ -28,55 +25,55 @@ #define LAYOUT_LEFT_OFFSET KEY_STATE_COUNT typedef enum { - UHK_KEY_SIMPLE, - UHK_KEY_MOUSE, - UHK_KEY_LAYER, - UHK_KEY_LAYER_TOGGLE, - UHK_KEY_KEYMAP, - UHK_KEY_MACRO, - UHK_KEY_LPRESSMOD, - UHK_KEY_LPRESSLAYER, + UHK_KEY_SIMPLE, + UHK_KEY_MOUSE, + UHK_KEY_LAYER, + UHK_KEY_LAYER_TOGGLE, + UHK_KEY_KEYMAP, + UHK_KEY_MACRO, + UHK_KEY_LPRESSMOD, + UHK_KEY_LPRESSLAYER, } uhk_key_type_t; typedef union { - struct { + struct { uint8_t type; - union { - struct { - uint8_t __unused_bits; - uint8_t mods; - uint8_t key; - } __attribute__ ((packed)) simple; - struct { - uint8_t __unused_bits; - uint8_t scrollActions; // bitfield - uint8_t moveActions; // bitfield - } __attribute__ ((packed)) mouse; - struct { - uint16_t __unused_bits; - uint8_t target; - } __attribute__ ((packed)) layer; - struct { - uint16_t __unused_bits; - uint8_t target; - } __attribute__ ((packed)) keymap; - struct { - uint8_t __unused_bits; - uint16_t index; - } __attribute__ ((packed)) macro; - struct { - uint8_t longPressMod; // single mod, or bitfield? - uint8_t mods; // for the alternate action - uint8_t key; - } __attribute__ ((packed)) longpressMod; - struct { - uint8_t longPressLayer; - uint8_t mods; - uint8_t key; - } __attribute__ ((packed)) longpressLayer; - }; - } __attribute__ ((packed)); - uint32_t raw; + union { + struct { + uint8_t __unused_bits; + uint8_t mods; + uint8_t key; + } __attribute__ ((packed)) simple; + struct { + uint8_t __unused_bits; + uint8_t scrollActions; // bitfield + uint8_t moveActions; // bitfield + } __attribute__ ((packed)) mouse; + struct { + uint16_t __unused_bits; + uint8_t target; + } __attribute__ ((packed)) layer; + struct { + uint16_t __unused_bits; + uint8_t target; + } __attribute__ ((packed)) keymap; + struct { + uint8_t __unused_bits; + uint16_t index; + } __attribute__ ((packed)) macro; + struct { + uint8_t longPressMod; // single mod, or bitfield? + uint8_t mods; // for the alternate action + uint8_t key; + } __attribute__ ((packed)) longpressMod; + struct { + uint8_t longPressLayer; + uint8_t mods; + uint8_t key; + } __attribute__ ((packed)) longpressLayer; + }; + } __attribute__ ((packed)); + uint32_t raw; } __attribute__ ((packed)) uhk_key_t; #define Key_NoKey {.raw = 0} diff --git a/right/src/led_driver.c b/right/src/led_driver.c index 8a24cfa..a91c708 100644 --- a/right/src/led_driver.c +++ b/right/src/led_driver.c @@ -44,10 +44,10 @@ void LedDriver_SetAllLedsTo(uint8_t val) LedDriver_WriteRegister(address, i, val); } for (i=FRAME_REGISTER_LED_CONTROL_FIRST; i<=FRAME_REGISTER_LED_CONTROL_LAST; i++) { - LedDriver_WriteRegister(address, i, 0xff); + LedDriver_WriteRegister(address, i, 0xff); } for (i=FRAME_REGISTER_BLINK_CONTROL_FIRST; i<=FRAME_REGISTER_BLINK_CONTROL_LAST; i++) { - LedDriver_WriteRegister(address, i, 0x00); + LedDriver_WriteRegister(address, i, 0x00); } } } diff --git a/right/src/main.c b/right/src/main.c index 4c70a51..2ecde77 100644 --- a/right/src/main.c +++ b/right/src/main.c @@ -7,11 +7,11 @@ void main() { InitPeripherials(); InitClock(); LedDriver_InitAllLeds(0); - usbKeyboadTask(); + UsbKeyboadTask(); InitUsb(); while (1){ - usbKeyboadTask(); + UsbKeyboadTask(); asm("wfi"); } } diff --git a/right/src/test_led.c b/right/src/test_led.c index e382140..d85f09d 100644 --- a/right/src/test_led.c +++ b/right/src/test_led.c @@ -8,4 +8,3 @@ extern void InitTestLed() GPIO_PinInit(TEST_LED_GPIO, TEST_LED_GPIO_PIN, &(gpio_pin_config_t){kGPIO_DigitalOutput, 1}); TEST_LED_ON(); } - diff --git a/right/src/usb_interface_keyboard.c b/right/src/usb_interface_keyboard.c index 49fe2eb..96bd006 100644 --- a/right/src/usb_interface_keyboard.c +++ b/right/src/usb_interface_keyboard.c @@ -82,10 +82,10 @@ void readLeftKeys(uint8_t *stateVector){ } } -void usbKeyboadTask(){ - //Producer task for USB packets. When the USB interrupt is called, - //the newest packet is sent out immediately, thus not doing long task - //in the interrupt handler. +void UsbKeyboadTask(){ + // Producer task for USB packets. When the USB interrupt is called, + // the newest packet is sent out immediately, thus not doing long task + // in the interrupt handler. int newLayout = 1-activeLayout; static uint8_t leftKeyStates[KEY_STATE_COUNT]; @@ -102,13 +102,12 @@ void usbKeyboadTask(){ fillKeyboardReport(&UsbKeyboardReport[newLayout], leftKeyStates, keyMatrix.keyStates, defaultKeyboardLayout); - //Change to the new layout in atomic operation (int copy). Even if - //the copy is not atomic itself, only single bit changes. So it can - //never be a problem + // Change to the new layout in atomic operation (int copy). Even if + // the copy is not atomic itself, only single bit changes. So it can + // never be a problem activeLayout = newLayout; } - static usb_status_t UsbKeyboardAction(void) { return USB_DeviceHidSend(UsbCompositeDevice.keyboardHandle, USB_KEYBOARD_ENDPOINT_INDEX, diff --git a/right/src/usb_interface_keyboard.h b/right/src/usb_interface_keyboard.h index c3fc84a..ca2c920 100644 --- a/right/src/usb_interface_keyboard.h +++ b/right/src/usb_interface_keyboard.h @@ -36,5 +36,5 @@ extern usb_status_t UsbKeyboardSetConfiguration(class_handle_t handle, uint8_t configuration); extern usb_status_t UsbKeyboardSetInterface(class_handle_t handle, uint8_t interface, uint8_t alternateSetting); - extern void usbKeyboadTask(); + extern void UsbKeyboadTask(); #endif