![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
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@6:d5420bcf9ebe, 2015-09-21 (annotated)
- Committer:
- tomasero
- Date:
- Mon Sep 21 03:26:53 2015 +0000
- Revision:
- 6:d5420bcf9ebe
- Parent:
- 5:f547509e5583
Improved responsiveness
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 | 3:888c255f87a7 | 6 | #include "PololuLedStrip.h" |
bkim54 | 0:d307eb0be182 | 7 | |
bkim54 | 0:d307eb0be182 | 8 | // define I2C Pins and address for KL25Z. Taken from default sample code. |
bkim54 | 0:d307eb0be182 | 9 | PinName const SDA = PTE25; |
bkim54 | 0:d307eb0be182 | 10 | PinName const SCL = PTE24; |
bkim54 | 0:d307eb0be182 | 11 | Timer timer; |
bkim54 | 0:d307eb0be182 | 12 | int timer_begin; |
bkim54 | 3:888c255f87a7 | 13 | |
bkim54 | 4:0527cd963c14 | 14 | #define WRIST_LED_COUNT 16 |
bkim54 | 4:0527cd963c14 | 15 | #define CHEST_LED_COUNT 24 |
bkim54 | 0:d307eb0be182 | 16 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
tomasero | 6:d5420bcf9ebe | 17 | #define COUNTER_MOD 100 |
tomasero | 6:d5420bcf9ebe | 18 | #define X_MOVEMENT_REPEATS 5 |
bkim54 | 0:d307eb0be182 | 19 | |
bkim54 | 1:1f45953fccf9 | 20 | #define LEAN_LEFT_THRESH -0.4 |
bkim54 | 1:1f45953fccf9 | 21 | #define LEAN_RIGHT_THRESH 0.4 |
bkim54 | 4:0527cd963c14 | 22 | #define LEAN_BACK_THRESH 0.5 |
bkim54 | 1:1f45953fccf9 | 23 | #define JUMP_THRESH 0.4 |
bkim54 | 4:0527cd963c14 | 24 | #define LASER_THRESH 0.4 |
bkim54 | 4:0527cd963c14 | 25 | #define SHIELD_THRESH 0.41 |
bkim54 | 1:1f45953fccf9 | 26 | const char LEFT = 'A'; |
bkim54 | 1:1f45953fccf9 | 27 | const char RIGHT = 'D'; |
bkim54 | 1:1f45953fccf9 | 28 | const char LASER = 'J'; |
bkim54 | 1:1f45953fccf9 | 29 | const char SHIELD = 'K'; |
bkim54 | 1:1f45953fccf9 | 30 | const char SLOW = 'L'; |
bkim54 | 1:1f45953fccf9 | 31 | const char ENTER = '\n'; |
bkim54 | 1:1f45953fccf9 | 32 | const char SPACE = ' '; |
bkim54 | 0:d307eb0be182 | 33 | |
bkim54 | 0:d307eb0be182 | 34 | //serial connection to PC via USB |
bkim54 | 0:d307eb0be182 | 35 | Serial pc(USBTX, USBRX); |
bkim54 | 0:d307eb0be182 | 36 | |
bkim54 | 3:888c255f87a7 | 37 | PololuLedStrip wristLedStrip(D6); |
bkim54 | 3:888c255f87a7 | 38 | PololuLedStrip chestLedStrip(D7); |
bkim54 | 3:888c255f87a7 | 39 | |
bkim54 | 4:0527cd963c14 | 40 | //rgb_color colors[WRIST_LED_COUNT]; |
bkim54 | 3:888c255f87a7 | 41 | |
bkim54 | 1:1f45953fccf9 | 42 | USBKeyboard keyboard; |
bkim54 | 2:bab0105de714 | 43 | AnalogIn rightArm(A0); |
bkim54 | 1:1f45953fccf9 | 44 | AnalogIn wrist(A2); |
bkim54 | 1:1f45953fccf9 | 45 | PinDetect on_off(D15); |
bkim54 | 1:1f45953fccf9 | 46 | bool on = false; |
tomasero | 5:f547509e5583 | 47 | int counter = 0; |
bkim54 | 1:1f45953fccf9 | 48 | |
bkim54 | 1:1f45953fccf9 | 49 | |
bkim54 | 1:1f45953fccf9 | 50 | void on_off_pressed() { |
bkim54 | 1:1f45953fccf9 | 51 | on = !on; |
bkim54 | 1:1f45953fccf9 | 52 | } |
bkim54 | 3:888c255f87a7 | 53 | |
bkim54 | 3:888c255f87a7 | 54 | rgb_color getColor( char color, int intensity ) { |
bkim54 | 3:888c255f87a7 | 55 | rgb_color pixelColor; |
bkim54 | 3:888c255f87a7 | 56 | switch (color) { |
bkim54 | 3:888c255f87a7 | 57 | case 'r': |
bkim54 | 3:888c255f87a7 | 58 | pixelColor = (rgb_color){ intensity, 0, 0 }; |
bkim54 | 3:888c255f87a7 | 59 | break; |
bkim54 | 3:888c255f87a7 | 60 | case 'g': |
bkim54 | 3:888c255f87a7 | 61 | pixelColor = (rgb_color){ 0, intensity, 0 }; |
bkim54 | 3:888c255f87a7 | 62 | break; |
bkim54 | 3:888c255f87a7 | 63 | case 'b': |
bkim54 | 3:888c255f87a7 | 64 | pixelColor = (rgb_color){ 0, 0, intensity }; |
bkim54 | 3:888c255f87a7 | 65 | break; |
bkim54 | 3:888c255f87a7 | 66 | case 'w': |
bkim54 | 3:888c255f87a7 | 67 | pixelColor = (rgb_color){ intensity, intensity, intensity}; |
bkim54 | 3:888c255f87a7 | 68 | break; |
bkim54 | 3:888c255f87a7 | 69 | default: |
bkim54 | 3:888c255f87a7 | 70 | pixelColor = (rgb_color){ 0, 0 , intensity }; |
bkim54 | 3:888c255f87a7 | 71 | break; |
bkim54 | 3:888c255f87a7 | 72 | } |
bkim54 | 3:888c255f87a7 | 73 | return pixelColor; |
bkim54 | 3:888c255f87a7 | 74 | } |
bkim54 | 4:0527cd963c14 | 75 | /* |
bkim54 | 3:888c255f87a7 | 76 | void cleanRing() { |
bkim54 | 3:888c255f87a7 | 77 | for(uint32_t i = 0; i < LED_COUNT; i++) { |
bkim54 | 3:888c255f87a7 | 78 | colors[i] = (rgb_color){ 0, 0, 0 }; |
bkim54 | 3:888c255f87a7 | 79 | } |
bkim54 | 3:888c255f87a7 | 80 | wristLedStrip.write(colors, LED_COUNT); |
bkim54 | 3:888c255f87a7 | 81 | } |
bkim54 | 4:0527cd963c14 | 82 | */ |
bkim54 | 3:888c255f87a7 | 83 | |
bkim54 | 4:0527cd963c14 | 84 | void pulse(char color, PololuLedStrip led, int led_count) { |
tomasero | 5:f547509e5583 | 85 | if (counter != 0) { return; }; |
bkim54 | 4:0527cd963c14 | 86 | rgb_color colors[led_count]; |
bkim54 | 3:888c255f87a7 | 87 | // Update the colors array. |
tomasero | 5:f547509e5583 | 88 | for( int j = 0; j < 255; j+=10) { |
bkim54 | 4:0527cd963c14 | 89 | for( uint32_t i = 0; i < led_count; i++ ) { |
bkim54 | 3:888c255f87a7 | 90 | colors[i] = getColor(color, j); |
bkim54 | 3:888c255f87a7 | 91 | } |
bkim54 | 4:0527cd963c14 | 92 | led.write(colors, led_count); |
bkim54 | 3:888c255f87a7 | 93 | } |
bkim54 | 3:888c255f87a7 | 94 | //wait_ms(100); |
bkim54 | 3:888c255f87a7 | 95 | } |
bkim54 | 3:888c255f87a7 | 96 | |
bkim54 | 0:d307eb0be182 | 97 | int main(void) |
bkim54 | 0:d307eb0be182 | 98 | { |
bkim54 | 0:d307eb0be182 | 99 | //configure on-board I2C accelerometer on KL25Z |
bkim54 | 0:d307eb0be182 | 100 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
bkim54 | 0:d307eb0be182 | 101 | //map read acceleration to PWM output on red status LED |
bkim54 | 0:d307eb0be182 | 102 | PwmOut rled(LED_RED); |
bkim54 | 1:1f45953fccf9 | 103 | float x,y,z; |
bkim54 | 1:1f45953fccf9 | 104 | |
bkim54 | 1:1f45953fccf9 | 105 | on_off.attach_asserted(&on_off_pressed); |
bkim54 | 1:1f45953fccf9 | 106 | on_off.setAssertValue(0); //pins are PullUp, so there activelow buttons. |
bkim54 | 1:1f45953fccf9 | 107 | on_off.setSampleFrequency(); // Defaults to 20ms. |
bkim54 | 1:1f45953fccf9 | 108 | |
bkim54 | 0:d307eb0be182 | 109 | timer.start(); |
bkim54 | 0:d307eb0be182 | 110 | timer_begin = timer.read_ms(); |
bkim54 | 2:bab0105de714 | 111 | pc.printf("helllo\n"); |
bkim54 | 3:888c255f87a7 | 112 | //pulse(); |
bkim54 | 0:d307eb0be182 | 113 | while (true) { |
bkim54 | 1:1f45953fccf9 | 114 | if (on) { |
bkim54 | 1:1f45953fccf9 | 115 | x = acc.getAccX(); |
bkim54 | 1:1f45953fccf9 | 116 | y = acc.getAccY(); |
bkim54 | 1:1f45953fccf9 | 117 | z = acc.getAccZ(); |
bkim54 | 1:1f45953fccf9 | 118 | |
bkim54 | 4:0527cd963c14 | 119 | if ( x > LEAN_RIGHT_THRESH ) { |
bkim54 | 1:1f45953fccf9 | 120 | pc.printf("Lean right\n"); |
tomasero | 5:f547509e5583 | 121 | for (int i = 0; i < X_MOVEMENT_REPEATS; i++) { |
tomasero | 5:f547509e5583 | 122 | keyboard.putc(RIGHT); |
tomasero | 5:f547509e5583 | 123 | } |
bkim54 | 4:0527cd963c14 | 124 | } else if (x < LEAN_LEFT_THRESH) { |
bkim54 | 1:1f45953fccf9 | 125 | pc.printf("Lean left\n"); |
tomasero | 5:f547509e5583 | 126 | for (int i = 0; i < X_MOVEMENT_REPEATS; i++) { |
tomasero | 5:f547509e5583 | 127 | keyboard.putc(LEFT); |
tomasero | 5:f547509e5583 | 128 | } |
bkim54 | 1:1f45953fccf9 | 129 | } |
bkim54 | 4:0527cd963c14 | 130 | if( y < JUMP_THRESH && timer.read_ms() - timer_begin > 300 ) { |
bkim54 | 1:1f45953fccf9 | 131 | timer_begin = timer.read_ms(); |
bkim54 | 1:1f45953fccf9 | 132 | pc.printf("Jump\n"); |
bkim54 | 1:1f45953fccf9 | 133 | keyboard.putc(ENTER); |
bkim54 | 1:1f45953fccf9 | 134 | keyboard.putc(SPACE); |
bkim54 | 1:1f45953fccf9 | 135 | } |
bkim54 | 4:0527cd963c14 | 136 | if(wrist.read() < LASER_THRESH) { |
bkim54 | 2:bab0105de714 | 137 | pc.printf("Shoot\n"); |
bkim54 | 1:1f45953fccf9 | 138 | keyboard.putc(LASER); |
bkim54 | 4:0527cd963c14 | 139 | pulse('r', wristLedStrip, WRIST_LED_COUNT); |
bkim54 | 4:0527cd963c14 | 140 | pulse('r', chestLedStrip, CHEST_LED_COUNT); |
bkim54 | 3:888c255f87a7 | 141 | } |
bkim54 | 4:0527cd963c14 | 142 | else if(rightArm.read() < SHIELD_THRESH) { |
bkim54 | 1:1f45953fccf9 | 143 | pc.printf("Shield\n"); |
bkim54 | 1:1f45953fccf9 | 144 | keyboard.putc(SHIELD); |
bkim54 | 4:0527cd963c14 | 145 | pulse('g', wristLedStrip, WRIST_LED_COUNT); |
bkim54 | 4:0527cd963c14 | 146 | pulse('g', chestLedStrip, CHEST_LED_COUNT); |
bkim54 | 3:888c255f87a7 | 147 | } |
bkim54 | 4:0527cd963c14 | 148 | else if ( z > LEAN_BACK_THRESH) { |
bkim54 | 3:888c255f87a7 | 149 | pc.printf("Leaning back\n"); |
bkim54 | 3:888c255f87a7 | 150 | keyboard.putc(SLOW); |
bkim54 | 4:0527cd963c14 | 151 | pulse('b', wristLedStrip, WRIST_LED_COUNT); |
bkim54 | 4:0527cd963c14 | 152 | pulse('b', chestLedStrip, CHEST_LED_COUNT); |
bkim54 | 3:888c255f87a7 | 153 | } else{ |
bkim54 | 4:0527cd963c14 | 154 | pulse('w', wristLedStrip, WRIST_LED_COUNT); |
bkim54 | 4:0527cd963c14 | 155 | pulse('w', chestLedStrip, CHEST_LED_COUNT); |
bkim54 | 1:1f45953fccf9 | 156 | } |
tomasero | 5:f547509e5583 | 157 | counter = (counter + 1)%COUNTER_MOD; |
bkim54 | 0:d307eb0be182 | 158 | } |
bkim54 | 0:d307eb0be182 | 159 | } |
bkim54 | 0:d307eb0be182 | 160 | } |