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

View File

@@ -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 (keyId<LAYOUT_KEY_COUNT) {
if (keyMasks[keyId]!=0 && keyMasks[keyId]!=modifierState){
//Mask out key presses after releasing modifier keys
return (uhk_key_t) { .raw = 0 };
if (keyId < LAYOUT_KEY_COUNT) {
if (keyMasks[keyId]!=0 && keyMasks[keyId]!=modifierState) {
// Mask out key presses after releasing modifier keys
return (uhk_key_t){.raw=0};
}
uhk_key_t k = layout[keyId][modifierState];
keyMasks[keyId] = modifierState;
if (k.raw==0) {
if (k.raw == 0) {
k = layout[keyId][0];
}
return k;
} else {
return (uhk_key_t) { .raw = 0 };
return (uhk_key_t){.raw=0};
}
}
static void clearKeymasks(const uint8_t *leftKeyStates, const uint8_t *rightKeyStates){
int i;
for (i=0; i<KEY_STATE_COUNT; ++i){
for (i=0; i<KEY_STATE_COUNT; i++){
if (rightKeyStates[i]==0){
keyMasks[i] = 0;
}
if (leftKeyStates[i]==0){
if (leftKeyStates[i]==0) {
keyMasks[LAYOUT_LEFT_OFFSET+i] = 0;
}
}
@@ -43,47 +42,51 @@ static uint8_t LEDVal = 0;
static int8_t LEDstep = 10;
bool pressKey(uhk_key_t key, int scancodeIdx, usb_keyboard_report_t *report) {
if (key.type != UHK_KEY_SIMPLE)
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);
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<KEY_STATE_COUNT; keyId++) {
if (scancodeIdx>=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<KEY_STATE_COUNT; keyId++) {
if (scancodeIdx>=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++;
}
}
}

View File

@@ -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}

View File

@@ -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);
}
}
}

View File

@@ -7,11 +7,11 @@ void main() {
InitPeripherials();
InitClock();
LedDriver_InitAllLeds(0);
usbKeyboadTask();
UsbKeyboadTask();
InitUsb();
while (1){
usbKeyboadTask();
UsbKeyboadTask();
asm("wfi");
}
}

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});
TEST_LED_ON();
}

View File

@@ -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,

View File

@@ -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