diff --git a/ion/include/ion/keyboard.h b/ion/include/ion/keyboard.h index 22f82a0ee..2eda21cfc 100644 --- a/ion/include/ion/keyboard.h +++ b/ion/include/ion/keyboard.h @@ -44,14 +44,13 @@ typedef enum { ION_KEY_G_2, ION_KEY_G_3, ION_KEY_G_4, - ION_KEY_G_5, - - ION_KEY_LAST + ION_KEY_G_5 } ion_key_t; #define ION_NUMBER_OF_KEYS 35 -bool ion_key_state(ion_key_t key); +// FIXME: Which state is "true"? +bool ion_key_down(ion_key_t key); // This is our keymap char ion_getchar(); diff --git a/ion/platform/device/keyboard.c b/ion/platform/device/keyboard.c index 325216c7f..ef183402b 100644 --- a/ion/platform/device/keyboard.c +++ b/ion/platform/device/keyboard.c @@ -50,22 +50,27 @@ static inline uint8_t column_for_key(ion_key_t key) { return key%5; } -bool ion_key_state(ion_key_t key) { +bool ion_key_down(ion_key_t key) { /* Drive the corresponding row low, and let all the others float. - * Note: In open-drain mode, a 0 in the register drives low, and a 1 let the + * Note: In open-drain mode, a 0 in the register drives low, and a 1 lets the * pin in Hi-Z (floating). */ - GPIO_ODR(GPIOA) = ~(1 << (column_for_key(key)+ROW_PIN_START)); + uint32_t output_mask = (((uint32_t)1 << (ROW_PIN_END-ROW_PIN_START+1)) - 1) << ROW_PIN_START; + uint32_t previous_odr = GPIO_ODR(GPIOA); + uint32_t new_odr = ~((uint32_t)1 << (row_for_key(key)+ROW_PIN_START)) & output_mask; - // Wait a little... - for (int i=0;i<10000;i++) { + if (new_odr != previous_odr) { + GPIO_ODR(GPIOA) = new_odr; + // We changed the outputs, give the hardware some time to react to this change + // FIXME: Real delay! + for (int i=0;i<1000; i++) { + } } // Read the input of the proper column - uint32_t input = (GPIO_IDR(GPIOA) & (1 << (row_for_key(key)+COLUMN_PIN_START))); + uint32_t input = (GPIO_IDR(GPIOA) & (1 << (column_for_key(key)+COLUMN_PIN_START))); - // The key is pressed if the input is brought low by the output. In other - // words, we want to return "true" (1) if the input is low (0). - // Return the logical opposite of what we've just read + /* The key is down if the input is brought low by the output. In other words, + * we want to return "true" (1) if the input is low (0). */ return (input == 0); } diff --git a/ion/src/keyboard.c b/ion/src/keyboard.c index c60dbfeb4..ff6b9848a 100644 --- a/ion/src/keyboard.c +++ b/ion/src/keyboard.c @@ -2,67 +2,45 @@ #include char charForKey[ION_NUMBER_OF_KEYS] = { - 'A', - 'B', - 'C', - 'D', - 'E', - 'F', - 'G', - 'H', - 'I', - 'J', - 'K', - 'L', - 'M', - 'N', - 'O', - 'P', - 'Q', - 'R', - 'S', - 'T', - 'U', - 'V', - 'W', - 'X', - 'Y', - 'Z', - '0', - '1', - '2', - '3', - '4', - '5', - '6', - '7', - '8' + 'A', 'B', 'C', 'D', 'E', + 'F', 'G', 'H', 'I', 'J', + 'K', 'L', 'M', 'N', 'O', + 'P', 'Q', 'R', 'S', 'T', + 'U', 'V', 'W', 'X', 'Y', + 'Z', '0', '1', '2', '3', + '4', '5', '6', '7', '8' }; char ion_getchar() { + // Let's start by saving which keys we've seen up + bool key_seen_up[ION_NUMBER_OF_KEYS]; + for (ion_key_t k=0; k