cpct_keyID getKey() {
// Traverse the keystatus vector from end to start, to help// the compiler better optimize this code
u8 i = 10, *keys = cpct_keyboardStatusBuffer + 9;
u16 keypressed;
// Wait for a keypress
do { cpct_scanKeyboard(); } while ( ! cpct_isAnyKeyPressed() );
// Analyse which key has been pressed and return its keycode
do {
// We analyse groups of keys byte by byte (8 by 8, 1 bit per key)
// If no single key is pressed in this group, all bits will be on, and the byte will be 0xFF
// Then, XORing the byte with 0xFF will result in 0 unless some key from this group is pressed
keypressed = *keys ^ 0xFF;
if (keypressed)
return (keypressed << 8) + (i - 1); // Convert to cpct_keyID format: 8 first bits = key mask, 8 next bits, keyboard row (0-9)
--keys;
} while(--i);
// No key was pressed
return 0;
}
No hay comentarios:
Publicar un comentario