diff --git a/shared/key_matrix.c b/shared/key_matrix.c index 75c0b67..20d6dfd 100644 --- a/shared/key_matrix.c +++ b/shared/key_matrix.c @@ -1,6 +1,45 @@ #include "fsl_gpio.h" #include "key_matrix.h" +#ifdef OLD_MATRIX + +// This ifdef clause will be completely erased as soon as Santiago's prototype will be out of use. +// Since the creation of his v6.0 prototype the diodes of the key matrix have been reversed in order +// to optimize the key scanning algorithm. + +void KeyMatrix_Init(key_matrix_t *keyMatrix) +{ + for (uint8_t col_i=0; col_icolNum; col_i++) { + key_matrix_pin_t col = keyMatrix->cols[col_i]; + CLOCK_EnableClock(col.clock); + PORT_SetPinConfig(col.port, col.pin, + &(port_pin_config_t){.pullSelect=kPORT_PullDisable, .mux=kPORT_MuxAsGpio}); + GPIO_PinInit(col.gpio, col.pin, &(gpio_pin_config_t){kGPIO_DigitalOutput, 0}); + } + + for (uint8_t row_i=0; row_irowNum; row_i++) { + key_matrix_pin_t row = keyMatrix->rows[row_i]; + CLOCK_EnableClock(row.clock); + PORT_SetPinConfig(row.port, row.pin, + &(port_pin_config_t){.pullSelect=kPORT_PullDown, .mux=kPORT_MuxAsGpio}); + GPIO_PinInit(row.gpio, row.pin, &(gpio_pin_config_t){kGPIO_DigitalInput}); + } +} + +void KeyMatrix_Scan(key_matrix_t *keyMatrix) +{ + for (uint8_t col=0; colcolNum; col++) { + GPIO_WritePinOutput(keyMatrix->cols[col].gpio, keyMatrix->cols[col].pin, 1); + for (uint8_t row=0; rowrowNum; row++) { + keyMatrix->keyStates[row*keyMatrix->colNum + col] = GPIO_ReadPinInput(keyMatrix->rows[row].gpio, keyMatrix->rows[row].pin); + } + GPIO_WritePinOutput(keyMatrix->cols[col].gpio, keyMatrix->cols[col].pin, 0); + for (volatile uint32_t i=0; i<100; i++); // Wait for the new port state to settle. This avoid bogus key state detection. + } +} + +#else + void KeyMatrix_Init(key_matrix_t *keyMatrix) { for (key_matrix_pin_t *row = keyMatrix->rows; row < keyMatrix->rows + keyMatrix->rowNum; row++) { @@ -37,3 +76,5 @@ void KeyMatrix_Scan(key_matrix_t *keyMatrix) for (volatile uint32_t i=0; i<100; i++); // Wait for the new port state to settle. This avoid bogus key state detection. } } + +#endif