Bob Attababy / Mbed 2 deprecated Lab3_1

Dependencies:   TextLCD mbed ADXL345 VS1053b

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <mpr121.h>
00003 #include <VS1053.h>
00004 #include <ADXL345.h>
00005 #include <ctime>
00006 #include <stdlib.h>
00007 #include <math.h>
00008 #include "TextLCD.h"
00009 
00010 DigitalOut myled1(LED1);
00011 DigitalOut myled2(LED2);
00012 DigitalOut myled3(LED3);
00013 DigitalOut myled4(LED4);
00014 PwmOut tone(p21);
00015 AnalogIn   slider(p20);
00016 ADXL345 accelerometer(p5, p6, p7, p8);
00017 Serial pc(USBTX, USBRX);
00018 I2C i2c(p28, p27); //For touch sensor
00019 Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); //touch sensor class
00020 TextLCD lcd(p12, p13, p14, p15, p16, p17);
00021 
00022 // Tickers for game loop functions
00023 Timeout ticker1;
00024 Timeout ticker2;
00025 
00026 InterruptIn TouchPad(p26);
00027 InterruptIn AccTap(p11);
00028 DigitalOut myled5(p29);
00029 DigitalOut myled6(p30);
00030 volatile int Test = 0x0000;
00031 volatile int check = 0;
00032 volatile int i;
00033 volatile int c = 0;
00034 volatile int command = 1;
00035 volatile int ButtonsPressed = 0x0000;
00036 volatile float slid = 0.0;
00037 volatile float Prevslid = 0.0;
00038 volatile float time1 = 2000000;
00039 volatile float time2 = 1500000;
00040 volatile int commandtemp = 0;
00041 volatile int commandarray[20];
00042 volatile int fail = 0;
00043 
00044 void CheckInput(void);
00045 
00046 void AccTapISR() {
00047     if (command == 1) {
00048         myled4 = !myled4;
00049         command = 0;
00050     } else {
00051         fail = 1;
00052     }
00053 }
00054 
00055 void TouchPadISR() {
00056     myled5 = !myled5;
00057     if (command == 2) {
00058         //ButtonsPressed |= mpr121.read(0x00);
00059         mpr121.read(0x00);
00060         //ButtonsPressed |= mpr121.read(0x01)<<8;
00061         ++ButtonsPressed;
00062         if ((ButtonsPressed >= 6) && (ButtonsPressed < 12)) { //0x0F9F) {
00063             myled4 = !myled4;
00064             ButtonsPressed = 0;
00065             command = 0;
00066             fail = 0;
00067         } else if (ButtonsPressed > 12) {
00068             fail = 21;
00069         }
00070 
00071 
00072     } /*else {
00073         fail = 23;
00074         ButtonsPressed = 0;
00075     }*/
00076 }
00077 
00078 
00079 ///Ticker1 function to generate random command number from 1-3
00080 void Command() {
00081     myled6 = !myled6;
00082     // command = 1;
00083     check = 1;
00084     command = commandarray[c];
00085     c++;
00086     if ( c == 20) c = 0;
00087     lcd.cls();
00088     if (command == 1) {
00089         //lcd.printf("Bop-It!\n");
00090         myled1 = 1;
00091         myled2 = 0;
00092         myled3 = 0;
00093     } else if (command == 2) {
00094         //lcd.printf("Square-It!\n");
00095         myled1 = 0;
00096         myled2 = 1;
00097         myled3 = 0;
00098     } else if (command == 3) {
00099         //lcd.printf("Slide-It!\n");
00100         myled1 = 0;
00101         myled2 = 0;
00102         myled3 = 1;
00103     }
00104     tone = .5;
00105     ticker2.attach_us(&CheckInput,time2 );
00106 
00107     time2 = time2 - 50000;
00108 }
00109 
00110 void CheckInput() {
00111     ticker1.attach_us(&Command,time1);
00112     time1 = time1 - 50000;
00113     if (command == 3) {
00114         Prevslid = slid;
00115         slid = slider;
00116         if (abs(slid - Prevslid) > .5) {
00117             myled4 = !myled4;
00118             command = 0;
00119         }
00120         if (command) {
00121             fail = 3;
00122         }
00123     }
00124     if (command && (command != 3))
00125         fail  = 4;
00126     accelerometer.getInterruptSource();
00127 }
00128 
00129 
00130 int main() {
00131     srand( time(NULL));
00132     printf("Start up    \n");
00133     lcd.printf("Play Bop-It!\n");
00134     for (i=0; i< 20; i++) {
00135         commandtemp = rand() % 10;
00136         // printf("Before: %i \n \f", commandtemp);
00137         if (commandtemp == 0) {
00138             commandtemp = 8;
00139         }
00140         if (commandtemp == 9) {
00141             commandtemp = 8;
00142         }
00143         commandtemp = (commandtemp/3) + 1;
00144         // printf("After: %i     \n", commandtemp);
00145         commandarray[i] = commandtemp;
00146     }
00147 
00148     tone.period_ms(1);
00149 
00150     accelerometer.setPowerControl(0x00);
00151     accelerometer.setDataFormatControl(0x0A);
00152     accelerometer.setDataRate(ADXL345_3200HZ);
00153     accelerometer.setInterruptEnableControl(0x01 << 6);
00154     accelerometer.setInterruptMappingControl(0x00);
00155     accelerometer.setTapThreshold(0x8);
00156     accelerometer.setTapDuration(6000);
00157     accelerometer.setTapAxisControl(0x04);
00158     accelerometer.setPowerControl(0x08);
00159 
00160     mpr121.write(0x77, 0x00);
00161     mpr121.write(0x5E, 0x0C);
00162 
00163     AccTap.rise(&AccTapISR);
00164     accelerometer.getInterruptSource();
00165     TouchPad.fall(&TouchPadISR);
00166     TouchPad.mode(PullUp);
00167     ticker1.attach_us( &Command, time1);
00168     slid = slider;
00169     int count = 0;
00170     while (1) {
00171         //printf("%X\n",ButtonsPressed);
00172 
00173         //wait(.26);
00174 
00175         __disable_irq();
00176         if (check) {
00177             if (command == 1) {
00178                 lcd.cls();
00179                 lcd.printf("Bop-It!\n");
00180                 myled1 = 1;
00181                 myled2 = 0;
00182                 myled3 = 0;
00183                 tone = 0;
00184             } else if (command == 2) {
00185                 lcd.cls();
00186                 lcd.printf("Touch Three!\n");
00187                 myled1 = 0;
00188                 myled2 = 1;
00189                 myled3 = 0;
00190                 tone = 0;
00191             } else if (command == 3) {
00192                 lcd.cls();
00193                 lcd.printf("Slide-It!\n");
00194                 myled1 = 0;
00195                 myled2 = 0;
00196                 myled3 = 1;
00197                 tone = 0;
00198             }
00199             check = 0;
00200         }
00201         if (fail) {
00202             ticker1.detach();
00203             ticker2.detach();
00204             lcd.cls();
00205             lcd.printf("Failed on: %d\n", fail);
00206             tone.period_ms(2.3);
00207             tone = .5;
00208             wait(.5);
00209             tone = 0;
00210             while (1);
00211         }
00212         __enable_irq();
00213     }
00214 
00215 }