Mutli-tap using accelerometer data.
Dependencies: DebounceIn MMA8451Q USBDevice mbed
main.cpp@1:0014f5540da0, 2015-09-14 (annotated)
- Committer:
- jkolb
- Date:
- Mon Sep 14 17:30:16 2015 +0000
- Revision:
- 1:0014f5540da0
- Parent:
- 0:053edc46badd
Final version of accelerometer-based multi-tap text input.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jkolb | 0:053edc46badd | 1 | #include "mbed.h" |
jkolb | 0:053edc46badd | 2 | #include "DebounceIn.h" |
jkolb | 0:053edc46badd | 3 | #include "MMA8451Q.h" |
jkolb | 0:053edc46badd | 4 | #include "USBKeyboard.h" |
jkolb | 0:053edc46badd | 5 | |
jkolb | 0:053edc46badd | 6 | USBKeyboard keyboard; |
jkolb | 0:053edc46badd | 7 | char leftTable[] = {'Q', 'A', 'D', 'G', 'J', 'M', 'P', 'T', 'W', '\b'}; |
jkolb | 0:053edc46badd | 8 | char centerTable[] = {'Z', 'B', 'E', 'H', 'K', 'N', 'R', 'U', 'X', ' '}; |
jkolb | 0:053edc46badd | 9 | char rightTable[] = {'Q', 'C', 'F', 'I', 'L', 'O', 'S', 'V', 'Y', '\b'}; |
jkolb | 0:053edc46badd | 10 | |
jkolb | 0:053edc46badd | 11 | DebounceIn buttons[] = { |
jkolb | 0:053edc46badd | 12 | DebounceIn(D2), |
jkolb | 0:053edc46badd | 13 | DebounceIn(D3), |
jkolb | 0:053edc46badd | 14 | DebounceIn(D4), |
jkolb | 0:053edc46badd | 15 | DebounceIn(D5), |
jkolb | 0:053edc46badd | 16 | DebounceIn(D6), |
jkolb | 0:053edc46badd | 17 | DebounceIn(D7), |
jkolb | 0:053edc46badd | 18 | DebounceIn(D8), |
jkolb | 0:053edc46badd | 19 | DebounceIn(D9), |
jkolb | 0:053edc46badd | 20 | DebounceIn(D10), |
jkolb | 0:053edc46badd | 21 | DebounceIn(D11) |
jkolb | 0:053edc46badd | 22 | }; |
jkolb | 0:053edc46badd | 23 | DigitalOut red(LED_RED); |
jkolb | 0:053edc46badd | 24 | DigitalOut blue(LED_BLUE); |
jkolb | 0:053edc46badd | 25 | DigitalOut green(LED_GREEN); |
jkolb | 0:053edc46badd | 26 | |
jkolb | 0:053edc46badd | 27 | MMA8451Q acc(PTE25, PTE24, (0x1d << 1)); |
jkolb | 0:053edc46badd | 28 | |
jkolb | 0:053edc46badd | 29 | int main() { |
jkolb | 0:053edc46badd | 30 | red = 1; |
jkolb | 0:053edc46badd | 31 | blue = 1; |
jkolb | 0:053edc46badd | 32 | green = 1; |
jkolb | 0:053edc46badd | 33 | for (int i = 0; i < 10; i++) { |
jkolb | 0:053edc46badd | 34 | buttons[i].mode(PullUp); |
jkolb | 0:053edc46badd | 35 | } |
jkolb | 0:053edc46badd | 36 | |
jkolb | 0:053edc46badd | 37 | while(1) { |
jkolb | 0:053edc46badd | 38 | for (int i = 0; i < 10; i++) { |
jkolb | 0:053edc46badd | 39 | if (buttons[i]) { |
jkolb | 0:053edc46badd | 40 | int yAccel = acc.getAccY(); |
jkolb | 0:053edc46badd | 41 | if (yAccel < -2000) { |
jkolb | 0:053edc46badd | 42 | red = 0; |
jkolb | 0:053edc46badd | 43 | keyboard.keyCode(leftTable[i]); |
jkolb | 0:053edc46badd | 44 | } else if (yAccel > 2000) { |
jkolb | 0:053edc46badd | 45 | green = 0; |
jkolb | 0:053edc46badd | 46 | keyboard.keyCode(rightTable[i]); |
jkolb | 0:053edc46badd | 47 | } else { |
jkolb | 0:053edc46badd | 48 | blue = 0; |
jkolb | 0:053edc46badd | 49 | keyboard.keyCode(centerTable[i]); |
jkolb | 0:053edc46badd | 50 | } |
jkolb | 0:053edc46badd | 51 | } |
jkolb | 0:053edc46badd | 52 | } |
jkolb | 0:053edc46badd | 53 | wait(0.2); |
jkolb | 0:053edc46badd | 54 | red = 1; |
jkolb | 0:053edc46badd | 55 | blue = 1; |
jkolb | 0:053edc46badd | 56 | green = 1; |
jkolb | 0:053edc46badd | 57 | } |
jkolb | 0:053edc46badd | 58 | } |