IDD Fall 2015 / Mbed 2 deprecated idd_hw3_RAIDEN_X

Dependencies:   Adafruit_NeoPixel MMA8451Q USBDevice mbed

Fork of Raiden by Christian Le

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 "USBKeyboard.h"
00004 #include "USBHID.h"
00005 #include "raiden_const.h"
00006 #include "PololuLedStrip.h"
00007 
00008 // you can't try to use USBKeyboard if it doesn't exist otherwise it will hang
00009 #define DEBUG 0
00010 
00011 #define MMA8451_I2C_ADDRESS (0x1d<<1)
00012 #define THRESHOLD 0.2
00013 
00014 #define LED_COUNT 24
00015 #define DATA_PIN D6
00016 PololuLedStrip LEDStrip(D8);
00017 
00018 uint8_t r = 255;
00019 uint8_t g = 255;
00020 uint8_t b = 255;
00021 
00022 void flash() {
00023     rgb_color white = (rgb_color){254,254,254};
00024     rgb_color all_white[LED_COUNT];
00025     for (int i = 0; i < LED_COUNT; i++) {
00026         all_white[i] = white;
00027     }
00028     LEDStrip.write(all_white, LED_COUNT);
00029 }
00030 void flashOff() {
00031     rgb_color black = (rgb_color){0,0,0};
00032     rgb_color all_black[LED_COUNT];
00033     for (int i = 0; i < LED_COUNT; i++) {
00034         all_black[i] = black;
00035     }
00036     LEDStrip.write(all_black, LED_COUNT);
00037 }
00038 
00039 int main(void) {
00040     Serial mac(USBTX, USBRX);
00041     
00042     MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
00043     PwmOut rled(LED_RED);
00044     PwmOut bled(LED_BLUE);
00045 
00046     AnalogIn flex(A0);
00047     
00048     #if DEBUG == 0
00049         USBKeyboard keyboard;
00050         HID_REPORT report;
00051  
00052     report.data[0] = 1; // this is the USB ID
00053     report.data[1] = 0; // this is a modifier key (ie: ctrl/shift/alt)
00054     report.data[2] = 0; // i haven't figured this one out
00055     report.data[3] = 0; // UP
00056     report.data[4] = 0; // DOWN
00057     report.data[5] = 0; // LEFT
00058     report.data[6] = 0; // RIGHT
00059     report.data[7] = 0; // SHOOT
00060     report.data[8] = 0; // BOMB
00061     report.length = 9;
00062     #endif
00063 
00064     mac.printf("R A I D E N   X", flex.read());
00065     flash();
00066     wait(2);
00067     flashOff();
00068     while (true) {
00069         mac.printf("%f", flex.read());
00070 
00071         float x = acc.getAccX();
00072         float y = acc.getAccY();
00073 
00074         if (x < 0 - THRESHOLD) {
00075             mac.printf("Down\r\n");
00076             #if DEBUG == 0
00077                 report.data[6] = 0;
00078                 report.data[5] = keymap[DOWN_ARROW].usage;
00079             #endif
00080         } else if (x > 0 + THRESHOLD) {
00081             mac.printf("Up\r\n");
00082             #if DEBUG == 0
00083                 report.data[6] = keymap[UP_ARROW].usage;
00084                 report.data[5] = 0;
00085             #endif
00086         } else {
00087             #if DEBUG == 0
00088                 report.data[5] = 0;
00089                 report.data[6] = 0;
00090             #endif
00091         }
00092 
00093         if (y < 0 - THRESHOLD) {
00094             mac.printf("Left\r\n");
00095             #if DEBUG == 0
00096                 report.data[3] = keymap[LEFT_ARROW].usage;
00097                 report.data[4] = 0;
00098             #endif
00099         } else if (y > 0 + THRESHOLD) {
00100             mac.printf("Right\r\n");
00101             #if DEBUG == 0
00102                 report.data[3] = 0;
00103                 report.data[4] = keymap[RIGHT_ARROW].usage;
00104             #endif
00105         } else {
00106             #if DEBUG == 0
00107                 report.data[3] = 0;
00108                 report.data[4] = 0;
00109             #endif
00110         }
00111         
00112         if (flex.read() < 0.3) {
00113             mac.printf("Bomb");
00114             flash();
00115             #if DEBUG == 0
00116                 report.data[8] = keymap['x'].usage;
00117             #endif
00118         } else {
00119             flashOff();
00120             #if DEBUG == 0
00121                 report.data[8] = 0;
00122             #endif
00123         }
00124 
00125         rled = 1.0 - abs(acc.getAccX());
00126         bled = 1.0 - abs(acc.getAccY());
00127         
00128         #if DEBUG == 0
00129             report.data[7] = keymap['z'].usage;
00130             keyboard.send(&report);
00131         #elif DEBUG == 1
00132             wait(0.5);
00133         #endif
00134     }
00135 }