#ifndef KEYMAPPER_H_ #define KEYMAPPER_H_ #include // Necessary libraries to include #include #define Btn_0 0b0000 // Creates all the buttons. #define Btn_1 0b0100 #define Btn_2 0b1000 #define Btn_3 0b1100 #define Btn_4 0b0001 #define Btn_5 0b0101 #define Btn_6 0b1001 #define Btn_7 0b1101 #define Btn_8 0b0010 #define Btn_9 0b0110 #define Btn_A 0b1010 #define Btn_B 0b1110 #define Btn_C 0b0011 #define Btn_D 0b0111 #define Btn_E 0b1011 #define Btn_F 0b1111 void Keymapper_init(){ DDRA &= 0b11100001; // Sets in/out for port A DDRD |= 0b00000010; // Sets in/out for port D } int getForBtn(int btn, int andor){ // Converts port D byte to appropriate don't care for buttons int first3bits = 0b111 * andor; // andor: 0 = or, 1 = and int lastbit = 0b1 * andor; int result = (first3bits<<5) | (btn<<1) | lastbit; return result; } int getButton(){ // Function for waiting until button is pressed. // Runs until a button is pressed, at which point it returns the key pressed as hex. while(1){ if(PIND & (1<<2)){ PIND = PIND | 0b00000010; if(PINA == (PINA | getForBtn(Btn_0, 0)) && PINA == (PINA & getForBtn(Btn_0, 1))){ //Button 0 return 0; } if(PINA == (PINA | getForBtn(Btn_1, 0)) && PINA == (PINA & getForBtn(Btn_1, 1))){ //Button 1 return 1; } if(PINA == (PINA | getForBtn(Btn_2, 0)) && PINA == (PINA & getForBtn(Btn_2, 1))){ //Button 2 return 2; } if(PINA == (PINA | getForBtn(Btn_3, 0)) && PINA == (PINA & getForBtn(Btn_3, 1))){ //Button 3 return 3; } if(PINA == (PINA | getForBtn(Btn_4, 0)) && PINA == (PINA & getForBtn(Btn_4, 1))){ //Button 4 return 4; } if(PINA == (PINA | getForBtn(Btn_5, 0)) && PINA == (PINA & getForBtn(Btn_5, 1))){ //Button 5 return 5; } if(PINA == (PINA | getForBtn(Btn_6, 0)) && PINA == (PINA & getForBtn(Btn_6, 1))){ //Button 6 return 6; } if(PINA == (PINA | getForBtn(Btn_7, 0)) && PINA == (PINA & getForBtn(Btn_7, 1))){ //Button 7 return 7; } if(PINA == (PINA | getForBtn(Btn_8, 0)) && PINA == (PINA & getForBtn(Btn_8, 1))){ //Button 8 return 8; } if(PINA == (PINA | getForBtn(Btn_9, 0)) && PINA == (PINA & getForBtn(Btn_9, 1))){ //Button 9 return 9; } if(PINA == (PINA | getForBtn(Btn_A, 0)) && PINA == (PINA & getForBtn(Btn_A, 1))){ //Button A return 10; } if(PINA == (PINA | getForBtn(Btn_B, 0)) && PINA == (PINA & getForBtn(Btn_B, 1))){ //Button B return 11; } if(PINA == (PINA | getForBtn(Btn_C, 0)) && PINA == (PINA & getForBtn(Btn_C, 1))){ //Button C return 12; } if(PINA == (PINA | getForBtn(Btn_D, 0)) && PINA == (PINA & getForBtn(Btn_D, 1))){ //Button D return 13; } if(PINA == (PINA | getForBtn(Btn_E, 0)) && PINA == (PINA & getForBtn(Btn_E, 1))){ //Button E return 14; } if(PINA == (PINA | getForBtn(Btn_F, 0)) && PINA == (PINA & getForBtn(Btn_F, 1))){ //Button F return 15; } } } } #endif /* KEYMAPPER_H_ */