![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
HW2 entry for Peter Gao. One handed keyboard using push buttons and a laser-cut box.
Dependencies: DebounceIn USBDevice mbed
main.cpp@0:98ac2b055ab9, 2014-09-22 (annotated)
- Committer:
- pgao
- Date:
- Mon Sep 22 05:43:36 2014 +0000
- Revision:
- 0:98ac2b055ab9
First commit!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pgao | 0:98ac2b055ab9 | 1 | #include "mbed.h" |
pgao | 0:98ac2b055ab9 | 2 | #include "DebounceIn.h" |
pgao | 0:98ac2b055ab9 | 3 | #include "USBKeyboard.h" |
pgao | 0:98ac2b055ab9 | 4 | |
pgao | 0:98ac2b055ab9 | 5 | USBKeyboard keyboard; |
pgao | 0:98ac2b055ab9 | 6 | Serial pc(USBTX, USBRX); |
pgao | 0:98ac2b055ab9 | 7 | DebounceIn buttons[] = {D15, D14, D13, D12, D11, D10, D9, D8, D7, D6}; |
pgao | 0:98ac2b055ab9 | 8 | DigitalOut capslock_light(LED1); |
pgao | 0:98ac2b055ab9 | 9 | int num_buttons = 10; |
pgao | 0:98ac2b055ab9 | 10 | int is_capital = 0; |
pgao | 0:98ac2b055ab9 | 11 | float key_delay = 0.2; |
pgao | 0:98ac2b055ab9 | 12 | |
pgao | 0:98ac2b055ab9 | 13 | int main() { |
pgao | 0:98ac2b055ab9 | 14 | for (int i = 0; i < num_buttons; i++) { |
pgao | 0:98ac2b055ab9 | 15 | buttons[i].mode(PullUp); |
pgao | 0:98ac2b055ab9 | 16 | pc.printf("button %d is in pullup\n", i); |
pgao | 0:98ac2b055ab9 | 17 | } |
pgao | 0:98ac2b055ab9 | 18 | capslock_light = 1; |
pgao | 0:98ac2b055ab9 | 19 | is_capital = 0; |
pgao | 0:98ac2b055ab9 | 20 | pc.printf("starting loop"); |
pgao | 0:98ac2b055ab9 | 21 | while(1) { |
pgao | 0:98ac2b055ab9 | 22 | for (int i = 2; i < num_buttons; i++) { |
pgao | 0:98ac2b055ab9 | 23 | if (!buttons[0] && !buttons[1]) { |
pgao | 0:98ac2b055ab9 | 24 | if (!buttons[4]) { |
pgao | 0:98ac2b055ab9 | 25 | // space |
pgao | 0:98ac2b055ab9 | 26 | keyboard.keyCode(' '); |
pgao | 0:98ac2b055ab9 | 27 | wait(key_delay); |
pgao | 0:98ac2b055ab9 | 28 | continue; |
pgao | 0:98ac2b055ab9 | 29 | } |
pgao | 0:98ac2b055ab9 | 30 | else if (!buttons[5]) { |
pgao | 0:98ac2b055ab9 | 31 | // backspace |
pgao | 0:98ac2b055ab9 | 32 | char backspace = 8; |
pgao | 0:98ac2b055ab9 | 33 | keyboard.keyCode(backspace); |
pgao | 0:98ac2b055ab9 | 34 | wait(key_delay); |
pgao | 0:98ac2b055ab9 | 35 | continue; |
pgao | 0:98ac2b055ab9 | 36 | } |
pgao | 0:98ac2b055ab9 | 37 | else if (!buttons[6]) { |
pgao | 0:98ac2b055ab9 | 38 | // capslock |
pgao | 0:98ac2b055ab9 | 39 | capslock_light = !capslock_light; |
pgao | 0:98ac2b055ab9 | 40 | keyboard.keyCode(KEY_CAPS_LOCK); |
pgao | 0:98ac2b055ab9 | 41 | wait(key_delay); |
pgao | 0:98ac2b055ab9 | 42 | continue; |
pgao | 0:98ac2b055ab9 | 43 | } |
pgao | 0:98ac2b055ab9 | 44 | } |
pgao | 0:98ac2b055ab9 | 45 | |
pgao | 0:98ac2b055ab9 | 46 | char base_char = 'a'; |
pgao | 0:98ac2b055ab9 | 47 | if (!buttons[0] && !buttons[1]) { |
pgao | 0:98ac2b055ab9 | 48 | base_char = 'y'; |
pgao | 0:98ac2b055ab9 | 49 | } |
pgao | 0:98ac2b055ab9 | 50 | else if (!buttons[0]) { |
pgao | 0:98ac2b055ab9 | 51 | base_char = 'i'; |
pgao | 0:98ac2b055ab9 | 52 | } |
pgao | 0:98ac2b055ab9 | 53 | else if (!buttons[1]) { |
pgao | 0:98ac2b055ab9 | 54 | base_char = 'q'; |
pgao | 0:98ac2b055ab9 | 55 | } |
pgao | 0:98ac2b055ab9 | 56 | |
pgao | 0:98ac2b055ab9 | 57 | if (!buttons[i]) { |
pgao | 0:98ac2b055ab9 | 58 | keyboard.keyCode(base_char + i - 2); |
pgao | 0:98ac2b055ab9 | 59 | wait(key_delay); |
pgao | 0:98ac2b055ab9 | 60 | } |
pgao | 0:98ac2b055ab9 | 61 | } |
pgao | 0:98ac2b055ab9 | 62 | } |
pgao | 0:98ac2b055ab9 | 63 | } |