Init
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
#include "tca8418.h"
|
||||
#include "i2c.h"
|
||||
#include "pins.h"
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
KeyboardState_t keyboardState;
|
||||
|
||||
void tca8418_init(void) {
|
||||
i2c_write_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_KP_GPIO1, 0x7F);
|
||||
i2c_write_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_KP_GPIO2, 0xFF);
|
||||
i2c_write_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_KP_GPIO3, 0x00);
|
||||
|
||||
i2c_write_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_CFG,
|
||||
TCA8418_CFG_INT_ENABLE | TCA8418_CFG_OVERFLOW_MODE);
|
||||
|
||||
i2c_write_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_UNLOCK1, 0x00);
|
||||
i2c_write_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_UNLOCK2, 0x00);
|
||||
i2c_write_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_KP_LCK_TIMER, 0x00);
|
||||
|
||||
i2c_write_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_INT_STAT, 0xFF);
|
||||
memset(&keyboardState, 0, sizeof(keyboardState));
|
||||
}
|
||||
|
||||
int tca8418_read(uint8_t *key, uint8_t *pressed) {
|
||||
uint8_t count;
|
||||
|
||||
// read event counter
|
||||
i2c_read_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_KEY_LCK_EC, &count);
|
||||
|
||||
count &= 0x0F;
|
||||
|
||||
// no pending events
|
||||
if (!count)
|
||||
return 0;
|
||||
|
||||
uint8_t ev;
|
||||
|
||||
// reading this register POPS one FIFO event
|
||||
i2c_read_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_KEY_EVENT_A, &ev);
|
||||
|
||||
// bit7:
|
||||
// 0 = press
|
||||
// 1 = release
|
||||
*pressed = ((ev & 0x80) != 0);
|
||||
|
||||
// lower 7 bits = key code
|
||||
*key = ev & 0x7F;
|
||||
|
||||
// sanity check
|
||||
if (*key >= KEYBOARD_KEYS_COUNT)
|
||||
return 0;
|
||||
|
||||
// clear ONLY key event interrupt
|
||||
i2c_write_reg(KEYBOARD_I2C_ADDRESS, TCA8418_REG_INT_STAT,
|
||||
TCA8418_INT_KEY_EVENT);
|
||||
|
||||
// update key state table
|
||||
keyboardState.pressedKeys[*key] = *pressed;
|
||||
|
||||
// update modifier state
|
||||
switch (*key) {
|
||||
|
||||
case KEYBOARD_MOD_FN:
|
||||
keyboardState.fnPressed = *pressed;
|
||||
break;
|
||||
|
||||
case KEYBOARD_MOD_ALT:
|
||||
keyboardState.altPressed = *pressed;
|
||||
break;
|
||||
|
||||
case KEYBOARD_MOD_CTRL:
|
||||
keyboardState.ctrlPressed = *pressed;
|
||||
break;
|
||||
|
||||
case KEYBOARD_MOD_OPT:
|
||||
keyboardState.optPressed = *pressed;
|
||||
break;
|
||||
|
||||
case KEYBOARD_MOD_SHIFT:
|
||||
keyboardState.shiftPressed = *pressed;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
char getKeyboardChar(const uint8_t code) {
|
||||
if (keyboardState.shiftPressed) {
|
||||
return keymap_shift[code];
|
||||
} else {
|
||||
return keymap_normal[code];
|
||||
}
|
||||
}
|
||||
|
||||
const char *getKeyboardKeyName(const uint8_t code) {
|
||||
if (keyboardState.shiftPressed) {
|
||||
return keymap_shift_name[code];
|
||||
} else {
|
||||
return keymap_normal_name[code];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user