Coding style fixes.

This commit is contained in:
László Monda
2016-12-12 00:21:39 +01:00
parent de6218ce0c
commit f66eb06712
7 changed files with 121 additions and 123 deletions
+53 -50
View File
@@ -2,38 +2,37 @@
#include "led_driver.h" #include "led_driver.h"
static uint8_t keyMasks[LAYOUT_KEY_COUNT]; static uint8_t keyMasks[LAYOUT_KEY_COUNT];
static uint8_t modifierState = 0; static uint8_t modifierState = 0;
static inline __attribute__((always_inline)) uhk_key_t getKeycode(KEYBOARD_LAYOUT(layout), uint8_t keyId) static inline __attribute__((always_inline)) uhk_key_t getKeycode(KEYBOARD_LAYOUT(layout), uint8_t keyId)
{ {
if (keyId<LAYOUT_KEY_COUNT) { if (keyId < LAYOUT_KEY_COUNT) {
if (keyMasks[keyId]!=0 && keyMasks[keyId]!=modifierState){ if (keyMasks[keyId]!=0 && keyMasks[keyId]!=modifierState) {
//Mask out key presses after releasing modifier keys // Mask out key presses after releasing modifier keys
return (uhk_key_t) { .raw = 0 }; return (uhk_key_t){.raw=0};
} }
uhk_key_t k = layout[keyId][modifierState]; uhk_key_t k = layout[keyId][modifierState];
keyMasks[keyId] = modifierState; keyMasks[keyId] = modifierState;
if (k.raw==0) { if (k.raw == 0) {
k = layout[keyId][0]; k = layout[keyId][0];
} }
return k; return k;
} else { } else {
return (uhk_key_t) { .raw = 0 }; return (uhk_key_t){.raw=0};
} }
} }
static void clearKeymasks(const uint8_t *leftKeyStates, const uint8_t *rightKeyStates){ static void clearKeymasks(const uint8_t *leftKeyStates, const uint8_t *rightKeyStates){
int i; int i;
for (i=0; i<KEY_STATE_COUNT; ++i){ for (i=0; i<KEY_STATE_COUNT; i++){
if (rightKeyStates[i]==0){ if (rightKeyStates[i]==0){
keyMasks[i] = 0; keyMasks[i] = 0;
} }
if (leftKeyStates[i]==0){ if (leftKeyStates[i]==0) {
keyMasks[LAYOUT_LEFT_OFFSET+i] = 0; keyMasks[LAYOUT_LEFT_OFFSET+i] = 0;
} }
} }
@@ -43,47 +42,51 @@ static uint8_t LEDVal = 0;
static int8_t LEDstep = 10; static int8_t LEDstep = 10;
bool pressKey(uhk_key_t key, int scancodeIdx, usb_keyboard_report_t *report) { bool pressKey(uhk_key_t key, int scancodeIdx, usb_keyboard_report_t *report) {
if (key.type != UHK_KEY_SIMPLE) if (key.type != UHK_KEY_SIMPLE) {
return false; return false;
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; if (!key.simple.key) {
return true; 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) { bool layerOn(uhk_key_t key) {
modifierState |= (1 << (key.layer.target - 1)); modifierState |= (1 << (key.layer.target - 1));
return false; return false;
} }
bool layerOff(uhk_key_t key) { bool layerOff(uhk_key_t key) {
modifierState &= ~(1 << (key.layer.target - 1)); modifierState &= ~(1 << (key.layer.target - 1));
return false; return false;
} }
bool handleKey(uhk_key_t key, int scancodeIdx, usb_keyboard_report_t *report, uint8_t keyState) { bool handleKey(uhk_key_t key, int scancodeIdx, usb_keyboard_report_t *report, uint8_t keyState) {
switch (key.type) { switch (key.type) {
case UHK_KEY_SIMPLE: case UHK_KEY_SIMPLE:
if (keyState) if (keyState) {
return pressKey (key, scancodeIdx, report); return pressKey (key, scancodeIdx, report);
break; }
case UHK_KEY_LAYER: break;
if (keyState) case UHK_KEY_LAYER:
return layerOn (key); if (keyState) {
return layerOff (key); return layerOn(key);
break; }
default: return layerOff(key);
break; break;
} default:
return false; break;
}
return false;
} }
void fillKeyboardReport(usb_keyboard_report_t *report, const uint8_t *leftKeyStates, const uint8_t *rightKeyStates, KEYBOARD_LAYOUT(layout)) { 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); clearKeymasks(leftKeyStates, rightKeyStates);
for (uint8_t keyId=0; keyId<KEY_STATE_COUNT; keyId++) { for (uint8_t keyId=0; keyId<KEY_STATE_COUNT; keyId++) {
if (scancodeIdx>=USB_KEYBOARD_MAX_KEYS) { if (scancodeIdx >= USB_KEYBOARD_MAX_KEYS) {
break; break;
} }
uhk_key_t code=getKeycode(layout, keyId); uhk_key_t code = getKeycode(layout, keyId);
if (handleKey(code, scancodeIdx, report, rightKeyStates[keyId])) { if (handleKey(code, scancodeIdx, report, rightKeyStates[keyId])) {
scancodeIdx++; scancodeIdx++;
} }
} }
for (uint8_t keyId=0; keyId<KEY_STATE_COUNT; keyId++) { for (uint8_t keyId=0; keyId<KEY_STATE_COUNT; keyId++) {
if (scancodeIdx>=USB_KEYBOARD_MAX_KEYS) { if (scancodeIdx >= USB_KEYBOARD_MAX_KEYS) {
break; 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])) { if (handleKey(code, scancodeIdx, report, leftKeyStates[keyId])) {
scancodeIdx++; scancodeIdx++;
} }
} }
} }
+56 -59
View File
@@ -5,20 +5,17 @@
#include "lufa/HIDClassCommon.h" #include "lufa/HIDClassCommon.h"
#include "usb_composite_device.h" #include "usb_composite_device.h"
/** // Keyboard layout is a 2D array of scan codes.
* 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
* 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
* of the active keys of the key_matrix_t structure. In case of left half, an // offset of 35 is added.
* offset of 35 is added. //
* // For each Key ID, there are 4 different possible scan codes:
* For each Key ID, there are 4 different possible scan codes: // - default, when no modifiers are pressed
* - default, when no modifiers are pressed // - mod layer
* - mod layer // - fn layer
* - fn layer // - mod+fn layer
* - mod+fn layer
*
*/
#define KEY_STATE_COUNT (5*7) #define KEY_STATE_COUNT (5*7)
@@ -28,55 +25,55 @@
#define LAYOUT_LEFT_OFFSET KEY_STATE_COUNT #define LAYOUT_LEFT_OFFSET KEY_STATE_COUNT
typedef enum { typedef enum {
UHK_KEY_SIMPLE, UHK_KEY_SIMPLE,
UHK_KEY_MOUSE, UHK_KEY_MOUSE,
UHK_KEY_LAYER, UHK_KEY_LAYER,
UHK_KEY_LAYER_TOGGLE, UHK_KEY_LAYER_TOGGLE,
UHK_KEY_KEYMAP, UHK_KEY_KEYMAP,
UHK_KEY_MACRO, UHK_KEY_MACRO,
UHK_KEY_LPRESSMOD, UHK_KEY_LPRESSMOD,
UHK_KEY_LPRESSLAYER, UHK_KEY_LPRESSLAYER,
} uhk_key_type_t; } uhk_key_type_t;
typedef union { typedef union {
struct { struct {
uint8_t type; uint8_t type;
union { union {
struct { struct {
uint8_t __unused_bits; uint8_t __unused_bits;
uint8_t mods; uint8_t mods;
uint8_t key; uint8_t key;
} __attribute__ ((packed)) simple; } __attribute__ ((packed)) simple;
struct { struct {
uint8_t __unused_bits; uint8_t __unused_bits;
uint8_t scrollActions; // bitfield uint8_t scrollActions; // bitfield
uint8_t moveActions; // bitfield uint8_t moveActions; // bitfield
} __attribute__ ((packed)) mouse; } __attribute__ ((packed)) mouse;
struct { struct {
uint16_t __unused_bits; uint16_t __unused_bits;
uint8_t target; uint8_t target;
} __attribute__ ((packed)) layer; } __attribute__ ((packed)) layer;
struct { struct {
uint16_t __unused_bits; uint16_t __unused_bits;
uint8_t target; uint8_t target;
} __attribute__ ((packed)) keymap; } __attribute__ ((packed)) keymap;
struct { struct {
uint8_t __unused_bits; uint8_t __unused_bits;
uint16_t index; uint16_t index;
} __attribute__ ((packed)) macro; } __attribute__ ((packed)) macro;
struct { struct {
uint8_t longPressMod; // single mod, or bitfield? uint8_t longPressMod; // single mod, or bitfield?
uint8_t mods; // for the alternate action uint8_t mods; // for the alternate action
uint8_t key; uint8_t key;
} __attribute__ ((packed)) longpressMod; } __attribute__ ((packed)) longpressMod;
struct { struct {
uint8_t longPressLayer; uint8_t longPressLayer;
uint8_t mods; uint8_t mods;
uint8_t key; uint8_t key;
} __attribute__ ((packed)) longpressLayer; } __attribute__ ((packed)) longpressLayer;
}; };
} __attribute__ ((packed)); } __attribute__ ((packed));
uint32_t raw; uint32_t raw;
} __attribute__ ((packed)) uhk_key_t; } __attribute__ ((packed)) uhk_key_t;
#define Key_NoKey {.raw = 0} #define Key_NoKey {.raw = 0}
+2 -2
View File
@@ -44,10 +44,10 @@ void LedDriver_SetAllLedsTo(uint8_t val)
LedDriver_WriteRegister(address, i, val); LedDriver_WriteRegister(address, i, val);
} }
for (i=FRAME_REGISTER_LED_CONTROL_FIRST; i<=FRAME_REGISTER_LED_CONTROL_LAST; i++) { 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++) { for (i=FRAME_REGISTER_BLINK_CONTROL_FIRST; i<=FRAME_REGISTER_BLINK_CONTROL_LAST; i++) {
LedDriver_WriteRegister(address, i, 0x00); LedDriver_WriteRegister(address, i, 0x00);
} }
} }
} }
+2 -2
View File
@@ -7,11 +7,11 @@ void main() {
InitPeripherials(); InitPeripherials();
InitClock(); InitClock();
LedDriver_InitAllLeds(0); LedDriver_InitAllLeds(0);
usbKeyboadTask(); UsbKeyboadTask();
InitUsb(); InitUsb();
while (1){ while (1){
usbKeyboadTask(); UsbKeyboadTask();
asm("wfi"); asm("wfi");
} }
} }
-1
View File
@@ -8,4 +8,3 @@ extern void InitTestLed()
GPIO_PinInit(TEST_LED_GPIO, TEST_LED_GPIO_PIN, &(gpio_pin_config_t){kGPIO_DigitalOutput, 1}); GPIO_PinInit(TEST_LED_GPIO, TEST_LED_GPIO_PIN, &(gpio_pin_config_t){kGPIO_DigitalOutput, 1});
TEST_LED_ON(); TEST_LED_ON();
} }
+7 -8
View File
@@ -82,10 +82,10 @@ void readLeftKeys(uint8_t *stateVector){
} }
} }
void usbKeyboadTask(){ void UsbKeyboadTask(){
//Producer task for USB packets. When the USB interrupt is called, // Producer task for USB packets. When the USB interrupt is called,
//the newest packet is sent out immediately, thus not doing long task // the newest packet is sent out immediately, thus not doing long task
//in the interrupt handler. // in the interrupt handler.
int newLayout = 1-activeLayout; int newLayout = 1-activeLayout;
static uint8_t leftKeyStates[KEY_STATE_COUNT]; static uint8_t leftKeyStates[KEY_STATE_COUNT];
@@ -102,13 +102,12 @@ void usbKeyboadTask(){
fillKeyboardReport(&UsbKeyboardReport[newLayout], leftKeyStates, keyMatrix.keyStates, defaultKeyboardLayout); fillKeyboardReport(&UsbKeyboardReport[newLayout], leftKeyStates, keyMatrix.keyStates, defaultKeyboardLayout);
//Change to the new layout in atomic operation (int copy). Even if // 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 // the copy is not atomic itself, only single bit changes. So it can
//never be a problem // never be a problem
activeLayout = newLayout; activeLayout = newLayout;
} }
static usb_status_t UsbKeyboardAction(void) static usb_status_t UsbKeyboardAction(void)
{ {
return USB_DeviceHidSend(UsbCompositeDevice.keyboardHandle, USB_KEYBOARD_ENDPOINT_INDEX, return USB_DeviceHidSend(UsbCompositeDevice.keyboardHandle, USB_KEYBOARD_ENDPOINT_INDEX,
+1 -1
View File
@@ -36,5 +36,5 @@
extern usb_status_t UsbKeyboardSetConfiguration(class_handle_t handle, uint8_t configuration); 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 usb_status_t UsbKeyboardSetInterface(class_handle_t handle, uint8_t interface, uint8_t alternateSetting);
extern void usbKeyboadTask(); extern void UsbKeyboadTask();
#endif #endif