Mynput: Game controller for Color Quest.

Dependencies:   Adafruit_NeoPixel MMA8451Q PinDetect_KL25Z USBDevice mbed

Fork of idd_hw3 by IDD HW3

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MMA8451Q.h"
00003 #include "Timer.h"
00004 #include "USBKeyboard.h"
00005 #include "PinDetect.h"
00006 #include "PololuLedStrip.h"
00007 
00008 // define I2C Pins and address for KL25Z. Taken from default sample code.
00009 PinName const SDA = PTE25;
00010 PinName const SCL = PTE24;
00011 Timer timer;
00012 int timer_begin;
00013 
00014 #define WRIST_LED_COUNT 16
00015 #define CHEST_LED_COUNT 24
00016 #define MMA8451_I2C_ADDRESS (0x1d<<1)
00017 #define COUNTER_MOD 100
00018 #define X_MOVEMENT_REPEATS 5
00019 
00020 #define LEAN_LEFT_THRESH -0.4
00021 #define LEAN_RIGHT_THRESH 0.4
00022 #define LEAN_BACK_THRESH 0.5
00023 #define JUMP_THRESH 0.4
00024 #define LASER_THRESH 0.4
00025 #define SHIELD_THRESH 0.41
00026 const char LEFT =  'A';
00027 const char RIGHT = 'D';
00028 const char LASER = 'J';
00029 const char SHIELD = 'K';
00030 const char SLOW = 'L';
00031 const char ENTER = '\n';
00032 const char SPACE = ' ';
00033 
00034 //serial connection to PC via USB
00035 Serial pc(USBTX, USBRX);
00036 
00037 PololuLedStrip wristLedStrip(D6);
00038 PololuLedStrip chestLedStrip(D7);
00039 
00040 //rgb_color colors[WRIST_LED_COUNT];
00041 
00042 USBKeyboard keyboard;
00043 AnalogIn rightArm(A0);
00044 AnalogIn wrist(A2);
00045 PinDetect on_off(D15);
00046 bool on = false;
00047 int counter = 0;
00048 
00049 
00050 void on_off_pressed() {
00051     on = !on;
00052 }
00053 
00054 rgb_color getColor( char color, int intensity ) {
00055   rgb_color pixelColor;
00056   switch (color) {
00057     case 'r':
00058       pixelColor = (rgb_color){ intensity, 0, 0 };
00059       break;
00060     case 'g':
00061       pixelColor = (rgb_color){ 0, intensity, 0 };
00062       break;
00063     case 'b':
00064       pixelColor = (rgb_color){ 0, 0, intensity };
00065       break;
00066     case 'w':
00067       pixelColor = (rgb_color){ intensity, intensity, intensity};
00068       break;  
00069     default: 
00070         pixelColor = (rgb_color){ 0, 0 , intensity };
00071         break;
00072   }
00073   return pixelColor;
00074 }
00075 /*
00076 void cleanRing() {
00077     for(uint32_t i = 0; i < LED_COUNT; i++) {
00078         colors[i] = (rgb_color){ 0, 0, 0 };
00079     }
00080     wristLedStrip.write(colors, LED_COUNT);
00081 }
00082 */
00083 
00084 void pulse(char color, PololuLedStrip led, int led_count) {
00085     if (counter != 0) { return; };
00086     rgb_color colors[led_count];
00087     // Update the colors array.
00088     for( int j = 0; j < 255; j+=10) {
00089         for( uint32_t i = 0; i < led_count; i++ ) {
00090             colors[i] = getColor(color, j);
00091         }
00092         led.write(colors, led_count);
00093     }
00094     //wait_ms(100);
00095 }
00096 
00097 int main(void)
00098 {
00099     //configure on-board I2C accelerometer on KL25Z
00100     MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); 
00101     //map read acceleration to PWM output on red status LED
00102     PwmOut rled(LED_RED);
00103     float x,y,z;
00104     
00105     on_off.attach_asserted(&on_off_pressed);
00106     on_off.setAssertValue(0); //pins are PullUp, so there activelow buttons.
00107     on_off.setSampleFrequency(); // Defaults to 20ms.
00108     
00109     timer.start();
00110     timer_begin = timer.read_ms();
00111     pc.printf("helllo\n");
00112     //pulse();
00113     while (true) {
00114         if (on) {
00115             x = acc.getAccX();
00116             y = acc.getAccY();
00117             z = acc.getAccZ();
00118             
00119             if ( x > LEAN_RIGHT_THRESH ) {
00120                 pc.printf("Lean right\n");
00121                 for (int i = 0; i < X_MOVEMENT_REPEATS; i++) {
00122                     keyboard.putc(RIGHT);
00123                 }
00124             } else if (x < LEAN_LEFT_THRESH) {
00125                 pc.printf("Lean left\n"); 
00126                 for (int i = 0; i < X_MOVEMENT_REPEATS; i++) {
00127                     keyboard.putc(LEFT);
00128                 }
00129             }
00130             if( y < JUMP_THRESH && timer.read_ms() - timer_begin > 300 ) {                  
00131                 timer_begin = timer.read_ms();
00132                 pc.printf("Jump\n");
00133                 keyboard.putc(ENTER);
00134                 keyboard.putc(SPACE);
00135             }
00136             if(wrist.read() < LASER_THRESH) {
00137                 pc.printf("Shoot\n");
00138                 keyboard.putc(LASER);
00139                 pulse('r', wristLedStrip, WRIST_LED_COUNT);
00140                 pulse('r', chestLedStrip, CHEST_LED_COUNT);
00141             } 
00142             else if(rightArm.read() < SHIELD_THRESH) {
00143                 pc.printf("Shield\n");   
00144                 keyboard.putc(SHIELD); 
00145                 pulse('g', wristLedStrip, WRIST_LED_COUNT);
00146                 pulse('g', chestLedStrip, CHEST_LED_COUNT);
00147             } 
00148             else if ( z > LEAN_BACK_THRESH) {
00149                 pc.printf("Leaning back\n");
00150                 keyboard.putc(SLOW);
00151                 pulse('b', wristLedStrip, WRIST_LED_COUNT);
00152                 pulse('b', chestLedStrip, CHEST_LED_COUNT);
00153             } else{
00154                 pulse('w', wristLedStrip, WRIST_LED_COUNT);
00155                 pulse('w', chestLedStrip, CHEST_LED_COUNT);
00156             }
00157             counter = (counter + 1)%COUNTER_MOD;
00158         }
00159     }
00160 }