Mynput: Game controller for Color Quest.
Dependencies: Adafruit_NeoPixel MMA8451Q PinDetect_KL25Z USBDevice mbed
Fork of idd_fa15_hw3_lauren_bill_tomas by
main.cpp@1:1f45953fccf9, 2015-09-16 (annotated)
- Committer:
- bkim54
- Date:
- Wed Sep 16 02:32:55 2015 +0000
- Revision:
- 1:1f45953fccf9
- Parent:
- 0:d307eb0be182
- Child:
- 2:bab0105de714
added on_off button and flex sensor analog input readings;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bkim54 | 0:d307eb0be182 | 1 | #include "mbed.h" |
bkim54 | 0:d307eb0be182 | 2 | #include "MMA8451Q.h" |
bkim54 | 0:d307eb0be182 | 3 | #include "Timer.h" |
bkim54 | 1:1f45953fccf9 | 4 | #include "USBKeyboard.h" |
bkim54 | 1:1f45953fccf9 | 5 | #include "PinDetect.h" |
bkim54 | 0:d307eb0be182 | 6 | |
bkim54 | 0:d307eb0be182 | 7 | // define I2C Pins and address for KL25Z. Taken from default sample code. |
bkim54 | 0:d307eb0be182 | 8 | PinName const SDA = PTE25; |
bkim54 | 0:d307eb0be182 | 9 | PinName const SCL = PTE24; |
bkim54 | 0:d307eb0be182 | 10 | Timer timer; |
bkim54 | 0:d307eb0be182 | 11 | int timer_begin; |
bkim54 | 0:d307eb0be182 | 12 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
bkim54 | 0:d307eb0be182 | 13 | |
bkim54 | 1:1f45953fccf9 | 14 | #define LEAN_LEFT_THRESH -0.4 |
bkim54 | 1:1f45953fccf9 | 15 | #define LEAN_RIGHT_THRESH 0.4 |
bkim54 | 1:1f45953fccf9 | 16 | #define LEAN_BACK_THRESH -0.5 |
bkim54 | 1:1f45953fccf9 | 17 | #define JUMP_THRESH 0.4 |
bkim54 | 1:1f45953fccf9 | 18 | #define FLEX_THRESH 0.4 |
bkim54 | 1:1f45953fccf9 | 19 | const char LEFT = 'A'; |
bkim54 | 1:1f45953fccf9 | 20 | const char RIGHT = 'D'; |
bkim54 | 1:1f45953fccf9 | 21 | const char LASER = 'J'; |
bkim54 | 1:1f45953fccf9 | 22 | const char SHIELD = 'K'; |
bkim54 | 1:1f45953fccf9 | 23 | const char SLOW = 'L'; |
bkim54 | 1:1f45953fccf9 | 24 | const char ENTER = '\n'; |
bkim54 | 1:1f45953fccf9 | 25 | const char SPACE = ' '; |
bkim54 | 0:d307eb0be182 | 26 | |
bkim54 | 0:d307eb0be182 | 27 | //serial connection to PC via USB |
bkim54 | 0:d307eb0be182 | 28 | Serial pc(USBTX, USBRX); |
bkim54 | 0:d307eb0be182 | 29 | |
bkim54 | 1:1f45953fccf9 | 30 | USBKeyboard keyboard; |
bkim54 | 1:1f45953fccf9 | 31 | AnalogIn leftArm(A0); |
bkim54 | 1:1f45953fccf9 | 32 | AnalogIn rightArm(A1); |
bkim54 | 1:1f45953fccf9 | 33 | AnalogIn wrist(A2); |
bkim54 | 1:1f45953fccf9 | 34 | PinDetect on_off(D15); |
bkim54 | 1:1f45953fccf9 | 35 | bool on = false; |
bkim54 | 1:1f45953fccf9 | 36 | |
bkim54 | 1:1f45953fccf9 | 37 | |
bkim54 | 1:1f45953fccf9 | 38 | void on_off_pressed() { |
bkim54 | 1:1f45953fccf9 | 39 | on = !on; |
bkim54 | 1:1f45953fccf9 | 40 | } |
bkim54 | 0:d307eb0be182 | 41 | int main(void) |
bkim54 | 0:d307eb0be182 | 42 | { |
bkim54 | 0:d307eb0be182 | 43 | //configure on-board I2C accelerometer on KL25Z |
bkim54 | 0:d307eb0be182 | 44 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
bkim54 | 0:d307eb0be182 | 45 | //map read acceleration to PWM output on red status LED |
bkim54 | 0:d307eb0be182 | 46 | PwmOut rled(LED_RED); |
bkim54 | 1:1f45953fccf9 | 47 | float x,y,z; |
bkim54 | 1:1f45953fccf9 | 48 | |
bkim54 | 1:1f45953fccf9 | 49 | on_off.attach_asserted(&on_off_pressed); |
bkim54 | 1:1f45953fccf9 | 50 | on_off.setAssertValue(0); //pins are PullUp, so there activelow buttons. |
bkim54 | 1:1f45953fccf9 | 51 | on_off.setSampleFrequency(); // Defaults to 20ms. |
bkim54 | 1:1f45953fccf9 | 52 | |
bkim54 | 0:d307eb0be182 | 53 | timer.start(); |
bkim54 | 0:d307eb0be182 | 54 | timer_begin = timer.read_ms(); |
bkim54 | 0:d307eb0be182 | 55 | while (true) { |
bkim54 | 1:1f45953fccf9 | 56 | if (on) { |
bkim54 | 1:1f45953fccf9 | 57 | x = acc.getAccX(); |
bkim54 | 1:1f45953fccf9 | 58 | y = acc.getAccY(); |
bkim54 | 1:1f45953fccf9 | 59 | z = acc.getAccZ(); |
bkim54 | 1:1f45953fccf9 | 60 | |
bkim54 | 1:1f45953fccf9 | 61 | if ( y > LEAN_RIGHT_THRESH ) { |
bkim54 | 1:1f45953fccf9 | 62 | pc.printf("Lean right\n"); |
bkim54 | 1:1f45953fccf9 | 63 | keyboard.putc(RIGHT); |
bkim54 | 1:1f45953fccf9 | 64 | } else if (y < LEAN_LEFT_THRESH) { |
bkim54 | 1:1f45953fccf9 | 65 | pc.printf("Lean left\n"); |
bkim54 | 1:1f45953fccf9 | 66 | keyboard.putc(LEFT); |
bkim54 | 1:1f45953fccf9 | 67 | } |
bkim54 | 1:1f45953fccf9 | 68 | |
bkim54 | 1:1f45953fccf9 | 69 | if ( z < LEAN_BACK_THRESH) { |
bkim54 | 1:1f45953fccf9 | 70 | pc.printf("Leaning back\n"); |
bkim54 | 1:1f45953fccf9 | 71 | keyboard.putc(SLOW); |
bkim54 | 1:1f45953fccf9 | 72 | } |
bkim54 | 1:1f45953fccf9 | 73 | if( x < JUMP_THRESH && timer.read_ms() - timer_begin > 300 ) { |
bkim54 | 1:1f45953fccf9 | 74 | timer_begin = timer.read_ms(); |
bkim54 | 1:1f45953fccf9 | 75 | pc.printf("Jump\n"); |
bkim54 | 1:1f45953fccf9 | 76 | keyboard.putc(ENTER); |
bkim54 | 1:1f45953fccf9 | 77 | keyboard.putc(SPACE); |
bkim54 | 1:1f45953fccf9 | 78 | } |
bkim54 | 1:1f45953fccf9 | 79 | |
bkim54 | 1:1f45953fccf9 | 80 | if(wrist.read() < FLEX_THRESH) { |
bkim54 | 1:1f45953fccf9 | 81 | pc.printf("Shoot\n"); |
bkim54 | 1:1f45953fccf9 | 82 | keyboard.putc(LASER); |
bkim54 | 1:1f45953fccf9 | 83 | } |
bkim54 | 1:1f45953fccf9 | 84 | |
bkim54 | 1:1f45953fccf9 | 85 | if(leftArm.read() < FLEX_THRESH && rightArm.read()) { |
bkim54 | 1:1f45953fccf9 | 86 | pc.printf("Shield\n"); |
bkim54 | 1:1f45953fccf9 | 87 | keyboard.putc(SHIELD); |
bkim54 | 1:1f45953fccf9 | 88 | } |
bkim54 | 0:d307eb0be182 | 89 | } |
bkim54 | 0:d307eb0be182 | 90 | } |
bkim54 | 0:d307eb0be182 | 91 | } |