Aaron S / Mbed 2 deprecated Lab4

Dependencies:   4DGL-uLCD-SE PinDetect SDFileSystem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Timer.h"
00003 #include "uLCD_4DGL.h"
00004 #include "Speaker.h"
00005 #include "PinDetect.h"
00006 #include <iostream>
00007 #include <sstream>
00008 #include <string>
00009 #include "SDFileSystem.h"
00010 using namespace std;
00011 uLCD_4DGL uLCD(p28, p27, p29); // serial tx, serial rx, reset pin;
00012 Timer t;
00013 SDFileSystem sd(p5, p6, p7, p8, "sd");
00014 DigitalIn rightButton(p16);
00015 DigitalIn leftButton(p17);
00016 DigitalIn reset(p18);
00017 Speaker mySpeaker(p21);
00018 unsigned int m_z=800,m_w=12345; //random number generator shamelessly stolen from https://developer.mbed.org/questions/2886/Why-is-the-rand-function-not-the-least-b/
00019 unsigned int rnd()
00020 {
00021     m_z = 36969 * (m_z & 65535) + (m_z >>16);
00022     m_w = 18000 * (m_w & 65535) + (m_w >>16);
00023     return ((m_z <<16) + m_w);
00024 }
00025 int generateSquare(int box[], int side) //makes a square shape
00026 {
00027     int loc = abs(((int)rnd()))%18;
00028     while(box[loc]==1) {
00029         loc = abs((int)rnd())%18;
00030     }
00031     int col = loc%3;
00032     int row = (loc-col)/3;
00033     int y1 = (row*20);
00034     int x1 = (col*20);
00035     if(side==1) {
00036         x1 = x1 + 68;
00037     }
00038     int x2 = x1 + 19;
00039     int y2 = y1 + 19;
00040     int options[7] = {0xFFFFFF, 0xFF0000, 0x00FF00, 0x0000FF, 0xFF00FF, 0xFFFF00, 0x00FFFF};
00041     int color = options[abs((int)rnd())%7];
00042 
00043     uLCD.filled_rectangle(x1, y1, x2, y2, color);
00044     return loc;
00045 }
00046 int generateTriangle(int box[], int side) //makes a triangle shape
00047 {
00048     int loc = abs(((int)rnd()))%18;
00049     while(box[loc]==1) {
00050         loc = abs((int)rnd())%18;
00051     }
00052     int col = loc%3;
00053     int row = (loc-col)/3;
00054     int y1 = (row*20)+1;
00055     int x1 = (col*20);
00056     if(side==1) {
00057         x1 = x1 + 68;
00058     }
00059     int x2 = x1 + 19;
00060     int y2 = y1;
00061     int y3 = y1 + 19;
00062     int x3 = x1 + 10;
00063     int options[7] = {0xFFFFFF, 0xFF0000, 0x00FF00, 0x0000FF, 0xFF00FF, 0xFFFF00, 0x00FFFF};
00064     int color = options[abs((int)rnd())%7];
00065 
00066     uLCD.triangle(x1, y1, x2, y2, x3, y3, color);
00067     return loc;
00068 }
00069 int generateCircle(int box[], int side) //makes a circle shape
00070 {
00071     int loc = abs(((int)rnd()))%18;
00072     while(box[loc]==1) {
00073         loc = abs((int)rnd())%18;
00074     }
00075     int col = loc%3;
00076     int row = (loc-col)/3;
00077     int y = (row*20)+10;
00078     int x = (col*20)+10;
00079     if(side==1) {
00080         x = x + 68;
00081     }
00082     int options[7] = {0xFFFFFF, 0xFF0000, 0x00FF00, 0x0000FF, 0xFF00FF, 0xFFFF00, 0x00FFFF};
00083     int color = options[abs((int)rnd())%7];
00084     int size = (abs((int)rnd())%4)+5;
00085     uLCD.filled_circle(x, y, size, color);
00086     return loc;
00087 }
00088 string logData(int nT, int nC, int time, bool correct, FILE *fp) //logs data to file AND prints to screen
00089 {
00090     std::ostringstream s;
00091     s << "Trial Number: " << nT << endl;
00092     if(correct) {
00093         s << "Lemur was correct" << endl;
00094     } else {
00095         s << "Lemur was incorrect\n";
00096 
00097     }
00098     s << "Fraction correct so far: " << nC << "/" << nT << endl;
00099     s << "Time elapsed during last trial, in ms: " << time << endl;
00100     fprintf(fp, s.str().c_str());
00101     return s.str();
00102 }
00103 int main()
00104 {
00105     //startup screen
00106     uLCD.display_control(PORTRAIT);
00107     uLCD.cls();
00108     uLCD.printf("Hello Friendly    Lemur!");
00109     //super mario
00110     mySpeaker.PlayNote(660,.1,0.1);
00111     mySpeaker.PlayNote(0,.15,0.1);
00112     mySpeaker.PlayNote(660,.1,0.1);
00113     mySpeaker.PlayNote(0,.3,0.1);
00114     mySpeaker.PlayNote(660,.1,0.1);
00115     mySpeaker.PlayNote(0,.3,0.1);
00116     mySpeaker.PlayNote(510,.1,0.1);
00117     mySpeaker.PlayNote(0,.1,0.1);
00118     mySpeaker.PlayNote(660,.1,0.1);
00119     mySpeaker.PlayNote(0,.3,0.1);
00120     mySpeaker.PlayNote(770,.1,0.1);
00121     mySpeaker.PlayNote(0,.55,0.1);
00122     mySpeaker.PlayNote(380,.1,0.1);
00123     mySpeaker.PlayNote(0,.575,0.1);
00124     mySpeaker.PlayNote(510,.1,0.1);
00125     mySpeaker.PlayNote(0,.45,0.1);
00126     mySpeaker.PlayNote(380,.1,0.1);
00127     mySpeaker.PlayNote(0,.4,0.1);
00128     mySpeaker.PlayNote(320,.1,0.1);
00129     mySpeaker.PlayNote(0,.5,0.1);
00130     mySpeaker.PlayNote(440,.1,0.1);
00131     mySpeaker.PlayNote(0,.3,0.1);
00132     mySpeaker.PlayNote(480,.08,0.1);
00133     mySpeaker.PlayNote(0,.33,0.1);
00134     mySpeaker.PlayNote(450,.1,0.1);
00135     mySpeaker.PlayNote(0,.15,0.1);
00136     mySpeaker.PlayNote(430,.1,0.1);
00137     mySpeaker.PlayNote(0,.3,0.1);
00138     mySpeaker.PlayNote(380,.1,0.1);
00139     mySpeaker.PlayNote(0,.2,0.1);
00140     mySpeaker.PlayNote(660,.08,0.1);
00141     mySpeaker.PlayNote(0,.2,0.1);
00142     mySpeaker.PlayNote(760,.05,0.1);
00143     mySpeaker.PlayNote(0,.15,0.1);
00144     mySpeaker.PlayNote(860,.1,0.1);
00145     mySpeaker.PlayNote(0,.3,0.1);
00146     mySpeaker.PlayNote(700,.08,0.1);
00147     mySpeaker.PlayNote(0,.15,0.1);
00148     mySpeaker.PlayNote(760,.05,0.1);
00149     mySpeaker.PlayNote(0,.35,0.1);
00150     mySpeaker.PlayNote(660,.08,0.1);
00151     mySpeaker.PlayNote(0,.3,0.1);
00152     mySpeaker.PlayNote(520,.08,0.1);
00153     mySpeaker.PlayNote(0,.15,0.1);
00154     mySpeaker.PlayNote(580,.08,0.1);
00155     mySpeaker.PlayNote(0,.15,0.1);
00156     mySpeaker.PlayNote(480,.08,0.1);
00157 
00158     mkdir("/sd/mydir", 0777);
00159     FILE *fp = fopen("/sd/mydir/LemurResults.txt", "w");
00160     leftButton.mode(PullUp);
00161     rightButton.mode(PullUp);
00162     reset.mode(PullUp);             //some random variables that will be needed later (escaping from loops, keeping track of score, etc
00163     bool runTests = true;
00164     bool getoutoutside = false;
00165     bool getoutinside= false;
00166     bool isCorrect = false;
00167     int numCorrect = 0;
00168     int numTrials = 0;
00169     uLCD.baudrate(BAUD_3000000); //jack up baud rate to max for fast display
00170     wait(2);
00171 //Set up initial conditions
00172     uLCD.background_color(BLACK);
00173     uLCD.cls();
00174 //draw borders
00175     while(runTests) {
00176         numTrials++;
00177         uLCD.rectangle(0, 0, 59, 120, WHITE);
00178         uLCD.rectangle(68, 0, 127, 120, WHITE);         //basic setup completed every time
00179         int boxL[18] = {};
00180         int boxR[18] = {};
00181 //15 is max number of circles
00182         int leftNum=(abs((int)rnd())%14)+1;
00183         int rightNum=(abs((int)rnd())%14)+1;
00184         while((leftNum==rightNum)||(leftNum==rightNum-1)||(leftNum==rightNum+1)||(leftNum==rightNum-2)||(leftNum==rightNum+2)) {
00185             rightNum=(abs((int)rnd())%14)+1;
00186         }
00187         bool answer = false; //false=left side less
00188         if(leftNum>rightNum) {
00189             answer = 1;   //true= right side less
00190         }
00191         for(int i = 0; i<leftNum; i++) {            //these loops place the already determined number of random shapes
00192             int choose = abs((int)rnd())%3;
00193             int newSpot = -1;
00194             if(choose==1)
00195                 newSpot = generateSquare(boxL, 0);
00196             else if(choose==2)
00197                 newSpot = generateTriangle(boxL, 0);
00198             else
00199                 newSpot = generateCircle(boxL, 0);
00200             boxL[newSpot] = 1;
00201         }
00202         for(int i = 0; i<rightNum; i++) {
00203             int choose = abs((int)rnd())%3;
00204             int newSpot = -1;
00205             if(choose==1)
00206                 newSpot = generateSquare(boxR, 1);
00207             else if(choose==2)
00208                 newSpot = generateTriangle(boxR, 1);
00209             else
00210                 newSpot = generateCircle(boxR, 1);
00211             boxR[newSpot] = 1;
00212         }
00213         while(!getoutinside) {          //lemur chooses which has fewer
00214             t.start();
00215             if(!leftButton) {
00216                 getoutinside=true;
00217                 if(!answer) {
00218                     numCorrect++;
00219                     isCorrect = true;
00220                 }
00221                 t.stop();
00222 
00223             }
00224             if(!rightButton) {
00225                 getoutinside=true;
00226                 if(answer) {
00227                     numCorrect++;
00228                     isCorrect = true;
00229                 }
00230                 t.stop();
00231             }
00232         }
00233         int time = t.read_ms();     //collect results, send it to logData function
00234         t.reset();
00235         getoutinside=false;
00236         wait(.25);
00237         uLCD.cls();
00238         string str = logData(numTrials, numCorrect, time, isCorrect, fp);
00239         uLCD.printf(str.c_str());
00240         while(!getoutoutside) {         //check if lemur is supposed to continue (third button)
00241             if(!reset) {
00242                 getoutoutside=true;
00243                 runTests = false;
00244             }
00245             if((!leftButton)||(!rightButton)) {
00246                 getoutoutside=true;
00247             }
00248         }
00249         isCorrect = false;          //reset variables
00250         getoutoutside = false;
00251         uLCD.cls();
00252     }
00253     uLCD.printf("Goodbye, thanks for testing!");    //black screen, close file
00254     wait(4);
00255     uLCD.cls();
00256     fclose(fp);
00257 }//end main