Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE PinDetect SDFileSystem mbed
Fork of mythermostat by
main.cpp
00001 #include "mbed.h" 00002 #include "SDFileSystem.h" 00003 #include "uLCD_4DGL.h" 00004 #include "PinDetect.h" 00005 #include "Speaker.h" 00006 #include <cstdlib> 00007 #include <ctime> 00008 00009 uLCD_4DGL uLCD(p28, p27, p29); // serial tx, serial rx, reset pin; 00010 SDFileSystem sd(p5, p6, p7, p8, "sd"); 00011 AnalogIn noise(p20); // For Random Number Generation 00012 Timer timer; // For measuring Elapsed time 00013 DigitalIn pbLeft(p16); 00014 DigitalIn pbRight(p17); 00015 DigitalIn pbExit(p18); 00016 00017 int ObjLeftNum; 00018 int ObjRightNum; 00019 int CorrectNum = 0; 00020 int TrialNum = 0; 00021 00022 int DrawWorld(); 00023 int ChooseSide(); 00024 void Print(int Result, int CompareMe, int netTime); 00025 void ExitLemur(); 00026 00027 int main() 00028 { 00029 uLCD.display_GETOUT(PORTRAIT); // Portrait screen 00030 uLCD.background_color(RED); // Background color Red 00031 uLCD.printf("Welcome Lemur!"); // Welcome message 00032 wait(2.0); // pause system for 2.0 00033 uLCD.cls(); // clear screen 00034 00035 // Seed of random number with the noise from pin20 00036 uint32_t seed = 0; 00037 for(int i = 0; i<32; ++i) { 00038 seed ^= noise.read_u16(); 00039 if(seed & 1<31) { 00040 seed <<=1; 00041 seed |= 1; 00042 } else { 00043 seed <<= 1; 00044 } 00045 } 00046 srand(seed); 00047 00048 // Calling the Game functions and it's implementation 00049 int Result, CompareMe, startT, endT; // Local vars 00050 while(CompareMe != 3) { // Check untill user presses the exit pushbutton 00051 Result = DrawWorld(); // Drawing two rectangles 00052 timer.start(); // Starting timer to keep track how much time taken 00053 startT = timer.read_ms(); 00054 CompareMe = ChooseSide(); // Lemur decide 00055 endT = timer.read_ms(); 00056 timer.stop(); // End of timer 00057 if (CompareMe != 3) { 00058 ++TrialNum; // Update trail numbers 00059 Print(Result, CompareMe, endT-startT); // Print result 00060 } 00061 } 00062 ExitLemur(); // Execute after user pressed exit button 00063 } 00064 00065 int DrawWorld() 00066 { 00067 // Drawing two rectangles 00068 uLCD.rectangle(2, 4, 62, 124, WHITE); 00069 uLCD.rectangle(64, 4, 124, 124, WHITE); 00070 00071 // Defining co-ordinate map vectors 00072 int LeftX[18] = {12, 32, 52, 12, 32, 52, 12, 32, 52, 12, 32, 52, 12, 32, 52, 12, 32, 52}; 00073 int RightX[18] = {74, 94, 114, 74, 94, 114, 74, 94, 114, 74, 94, 114, 74, 94, 114, 74, 94, 114}; 00074 int Y[18] = {14, 14, 14, 34, 34, 34, 54, 54, 54, 74, 74, 74, 94, 94, 94, 114, 114, 114}; 00075 00076 // Left bin objects 00077 ObjLeftNum = rand()%15 + 1; 00078 bool valid; 00079 while(true) { 00080 ObjRightNum = rand()%15 + 1; 00081 if(!(ObjRightNum == ObjLeftNum)) { 00082 break; 00083 } 00084 } 00085 int color[6] = {BLACK,LGREY,DGREY, GREEN, BLUE,WHITE}; // six different color 00086 int LNum, RNum, Shape, Radius, ColorIdx; 00087 bool Flag[18] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};; 00088 00089 // draw left bin objects 00090 for (int i = 0; i < ObjLeftNum; i++) { 00091 do { 00092 valid = false; 00093 LNum = rand()%18; 00094 if (Flag[LNum]) { 00095 valid = true; 00096 } 00097 } while(valid); 00098 Flag[LNum] = true; 00099 ColorIdx = rand()%6; 00100 Shape = rand()%3; 00101 Radius = rand()%9 + 1; 00102 if (Shape == 0) { 00103 uLCD.filled_circle(LeftX[LNum],Y[LNum], Radius, color[ColorIdx]); 00104 } else if (Shape == 1) { 00105 uLCD.triangle(LeftX[LNum], Y[LNum] + Radius, LeftX[LNum]-Radius, Y[LNum]-Radius, LeftX[LNum]+Radius,Y[LNum]-Radius, color[ColorIdx]); 00106 } else { 00107 uLCD.filled_rectangle(LeftX[LNum]-Radius, Y[LNum] + Radius, LeftX[LNum]+Radius, Y[LNum]-Radius, color[ColorIdx]); 00108 } 00109 } 00110 00111 // draw right bin objects 00112 for (int i = 0; i < 18; i++) { 00113 Flag[i] = false; 00114 } 00115 for (int i = 0; i < ObjRightNum; i++) { 00116 while(true) { 00117 RNum = rand()%18; 00118 if(!Flag[RNum]) { 00119 break; 00120 } 00121 } 00122 Flag[RNum] = true; 00123 Radius = rand()%9 + 1; 00124 ColorIdx = rand()%6; 00125 Shape = rand()%3; 00126 if (Shape == 0) { 00127 uLCD.filled_circle(RightX[RNum],Y[RNum], Radius, color[ColorIdx]); 00128 } else if (Shape == 1) { 00129 uLCD.triangle(RightX[RNum], Y[RNum] + Radius,RightX[RNum]-Radius, Y[RNum]-Radius, RightX[RNum]+Radius,Y[RNum]-Radius, color[ColorIdx]); 00130 } else { 00131 uLCD.filled_rectangle(RightX[RNum]-Radius, Y[RNum] + Radius, RightX[RNum]+Radius, Y[RNum]-Radius, color[ColorIdx]); 00132 } 00133 } 00134 // Comparing number of objects in left and right 00135 if (ObjLeftNum < ObjRightNum) { 00136 return 1; 00137 } else { 00138 return 2; 00139 } 00140 } 00141 00142 // This return what button is pressed and GETOUT to be performed 00143 int ChooseSide() 00144 { 00145 pbLeft.mode(PullUp); 00146 pbRight.mode(PullUp); 00147 pbExit.mode(PullUp); 00148 00149 // Implementation of left, right and exit buttons 00150 bool GETOUT = false; 00151 int Choose = 0; 00152 while(!GETOUT) { 00153 if(!pbLeft) { 00154 GETOUT = true; 00155 Choose = 1; // Choosing left button 00156 } 00157 if(!pbRight) { 00158 GETOUT = true; 00159 Choose = 2; // Choosing right button 00160 } 00161 if(!pbExit) { 00162 GETOUT = true; 00163 Choose = 3; // Choosing Exit button 00164 } 00165 } 00166 return Choose; 00167 } 00168 00169 // To Print out the Results such as: if Lemur is correct or not, 00170 // Num of objs in each bin, time taken, number of trials, accuracy 00171 void Print(int Result, int CompareMe, int netTime) 00172 { 00173 uLCD.cls(); 00174 // Compare the Lemur choise 00175 if (Result == CompareMe) { // if Correct 00176 ++CorrectNum; // Increase CorrectNumber by 1 00177 uLCD.printf("Correct Lemur!!\n\n"); 00178 } else { // If incorrect 00179 uLCD.printf("Incorrect Lemur!!\n\n"); 00180 } 00181 double Accuracy = (double)CorrectNum/TrialNum; // Calculate Accuracy 00182 uLCD.printf("ObjInLeft: %i\nObjInRight: %i\nDelay(ms): %d\n", ObjLeftNum, ObjRightNum,netTime); 00183 uLCD.printf("TrialNum: %i\nAccuracy: %.2f\n\n", TrialNum, Accuracy); 00184 00185 mkdir("/sd/mydir/LemurResults.txt", 0777); 00186 FILE *fp = fopen("/sd/mydir/LemurResults.txt", "a"); 00187 if(fp == NULL) { 00188 uLCD.printf("Error Open \n"); 00189 } 00190 if (Result == CompareMe) { 00191 fprintf(fp, "Correct Lemur!!\n"); 00192 } else { 00193 fprintf(fp, "Incorrect Lemur!!\n"); 00194 } 00195 fprintf(fp,"LObj: %i RObj: %i Trial#: %i Accur: %#.2f Delay(ms): %i\n\n\n",ObjLeftNum, ObjRightNum, TrialNum, (double)CorrectNum/(TrialNum),netTime); 00196 fclose(fp); 00197 00198 wait(1.0); 00199 uLCD.cls(); 00200 } 00201 00202 // Exiting the game with message 00203 void ExitLemur() 00204 { 00205 uLCD.cls(); 00206 uLCD.text_width(1.8); 00207 uLCD.text_height(2); 00208 uLCD.color(WHITE); 00209 uLCD.printf("\nBYE LEMUR\n\nTake a break!\n"); 00210 }
Generated on Fri Jul 15 2022 19:36:49 by
1.7.2
