Words
Dependencies: 4DGL-uLCD-SE PinDetect SDFileSystem mbed
main.cpp@0:0072ee664a9b, 2017-03-02 (annotated)
- Committer:
- apaks180
- Date:
- Thu Mar 02 11:35:32 2017 +0000
- Revision:
- 0:0072ee664a9b
- Child:
- 1:3cf69c15ada7
Final version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
apaks180 | 0:0072ee664a9b | 1 | #include "mbed.h" |
apaks180 | 0:0072ee664a9b | 2 | #include "Timer.h" |
apaks180 | 0:0072ee664a9b | 3 | #include "uLCD_4DGL.h" |
apaks180 | 0:0072ee664a9b | 4 | #include "Speaker.h" |
apaks180 | 0:0072ee664a9b | 5 | #include "PinDetect.h" |
apaks180 | 0:0072ee664a9b | 6 | #include <iostream> |
apaks180 | 0:0072ee664a9b | 7 | #include <sstream> |
apaks180 | 0:0072ee664a9b | 8 | #include <string> |
apaks180 | 0:0072ee664a9b | 9 | #include "SDFileSystem.h" |
apaks180 | 0:0072ee664a9b | 10 | using namespace std; |
apaks180 | 0:0072ee664a9b | 11 | uLCD_4DGL uLCD(p28, p27, p29); // serial tx, serial rx, reset pin; |
apaks180 | 0:0072ee664a9b | 12 | Timer t; |
apaks180 | 0:0072ee664a9b | 13 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
apaks180 | 0:0072ee664a9b | 14 | DigitalIn rightButton(p16); |
apaks180 | 0:0072ee664a9b | 15 | DigitalIn leftButton(p17); |
apaks180 | 0:0072ee664a9b | 16 | DigitalIn reset(p18); |
apaks180 | 0:0072ee664a9b | 17 | Speaker mySpeaker(p21); |
apaks180 | 0:0072ee664a9b | 18 | 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/ |
apaks180 | 0:0072ee664a9b | 19 | unsigned int rnd() |
apaks180 | 0:0072ee664a9b | 20 | { |
apaks180 | 0:0072ee664a9b | 21 | m_z = 36969 * (m_z & 65535) + (m_z >>16); |
apaks180 | 0:0072ee664a9b | 22 | m_w = 18000 * (m_w & 65535) + (m_w >>16); |
apaks180 | 0:0072ee664a9b | 23 | return ((m_z <<16) + m_w); |
apaks180 | 0:0072ee664a9b | 24 | } |
apaks180 | 0:0072ee664a9b | 25 | int generateSquare(int box[], int side) |
apaks180 | 0:0072ee664a9b | 26 | { |
apaks180 | 0:0072ee664a9b | 27 | int loc = abs(((int)rnd()))%18; |
apaks180 | 0:0072ee664a9b | 28 | while(box[loc]==1) { |
apaks180 | 0:0072ee664a9b | 29 | loc = abs((int)rnd())%18; |
apaks180 | 0:0072ee664a9b | 30 | } |
apaks180 | 0:0072ee664a9b | 31 | int col = loc%3; |
apaks180 | 0:0072ee664a9b | 32 | int row = (loc-col)/3; |
apaks180 | 0:0072ee664a9b | 33 | int y1 = (row*20); |
apaks180 | 0:0072ee664a9b | 34 | int x1 = (col*20); |
apaks180 | 0:0072ee664a9b | 35 | if(side==1) { |
apaks180 | 0:0072ee664a9b | 36 | x1 = x1 + 68; |
apaks180 | 0:0072ee664a9b | 37 | } |
apaks180 | 0:0072ee664a9b | 38 | int x2 = x1 + 19; |
apaks180 | 0:0072ee664a9b | 39 | int y2 = y1 + 19; |
apaks180 | 0:0072ee664a9b | 40 | int options[7] = {0xFFFFFF, 0xFF0000, 0x00FF00, 0x0000FF, 0xFF00FF, 0xFFFF00, 0x00FFFF}; |
apaks180 | 0:0072ee664a9b | 41 | int color = options[abs((int)rnd())%7]; |
apaks180 | 0:0072ee664a9b | 42 | |
apaks180 | 0:0072ee664a9b | 43 | uLCD.filled_rectangle(x1, y1, x2, y2, color); |
apaks180 | 0:0072ee664a9b | 44 | return loc; |
apaks180 | 0:0072ee664a9b | 45 | } |
apaks180 | 0:0072ee664a9b | 46 | int generateTriangle(int box[], int side) |
apaks180 | 0:0072ee664a9b | 47 | { |
apaks180 | 0:0072ee664a9b | 48 | int loc = abs(((int)rnd()))%18; |
apaks180 | 0:0072ee664a9b | 49 | while(box[loc]==1) { |
apaks180 | 0:0072ee664a9b | 50 | loc = abs((int)rnd())%18; |
apaks180 | 0:0072ee664a9b | 51 | } |
apaks180 | 0:0072ee664a9b | 52 | int col = loc%3; |
apaks180 | 0:0072ee664a9b | 53 | int row = (loc-col)/3; |
apaks180 | 0:0072ee664a9b | 54 | int y1 = (row*20)+1; |
apaks180 | 0:0072ee664a9b | 55 | int x1 = (col*20); |
apaks180 | 0:0072ee664a9b | 56 | if(side==1) { |
apaks180 | 0:0072ee664a9b | 57 | x1 = x1 + 68; |
apaks180 | 0:0072ee664a9b | 58 | } |
apaks180 | 0:0072ee664a9b | 59 | int x2 = x1 + 19; |
apaks180 | 0:0072ee664a9b | 60 | int y2 = y1; |
apaks180 | 0:0072ee664a9b | 61 | int y3 = y1 + 19; |
apaks180 | 0:0072ee664a9b | 62 | int x3 = x1 + 10; |
apaks180 | 0:0072ee664a9b | 63 | int options[7] = {0xFFFFFF, 0xFF0000, 0x00FF00, 0x0000FF, 0xFF00FF, 0xFFFF00, 0x00FFFF}; |
apaks180 | 0:0072ee664a9b | 64 | int color = options[abs((int)rnd())%7]; |
apaks180 | 0:0072ee664a9b | 65 | |
apaks180 | 0:0072ee664a9b | 66 | uLCD.triangle(x1, y1, x2, y2, x3, y3, color); |
apaks180 | 0:0072ee664a9b | 67 | return loc; |
apaks180 | 0:0072ee664a9b | 68 | } |
apaks180 | 0:0072ee664a9b | 69 | int generateCircle(int box[], int side) |
apaks180 | 0:0072ee664a9b | 70 | { |
apaks180 | 0:0072ee664a9b | 71 | int loc = abs(((int)rnd()))%18; |
apaks180 | 0:0072ee664a9b | 72 | while(box[loc]==1) { |
apaks180 | 0:0072ee664a9b | 73 | loc = abs((int)rnd())%18; |
apaks180 | 0:0072ee664a9b | 74 | } |
apaks180 | 0:0072ee664a9b | 75 | int col = loc%3; |
apaks180 | 0:0072ee664a9b | 76 | int row = (loc-col)/3; |
apaks180 | 0:0072ee664a9b | 77 | int y = (row*20)+10; |
apaks180 | 0:0072ee664a9b | 78 | int x = (col*20)+10; |
apaks180 | 0:0072ee664a9b | 79 | if(side==1) { |
apaks180 | 0:0072ee664a9b | 80 | x = x + 68; |
apaks180 | 0:0072ee664a9b | 81 | } |
apaks180 | 0:0072ee664a9b | 82 | int options[7] = {0xFFFFFF, 0xFF0000, 0x00FF00, 0x0000FF, 0xFF00FF, 0xFFFF00, 0x00FFFF}; |
apaks180 | 0:0072ee664a9b | 83 | int color = options[abs((int)rnd())%7]; |
apaks180 | 0:0072ee664a9b | 84 | int size = (abs((int)rnd())%4)+5; |
apaks180 | 0:0072ee664a9b | 85 | uLCD.filled_circle(x, y, size, color); |
apaks180 | 0:0072ee664a9b | 86 | return loc; |
apaks180 | 0:0072ee664a9b | 87 | } |
apaks180 | 0:0072ee664a9b | 88 | string logData(int nT, int nC, int time, bool correct, FILE *fp) |
apaks180 | 0:0072ee664a9b | 89 | { |
apaks180 | 0:0072ee664a9b | 90 | std::ostringstream s; |
apaks180 | 0:0072ee664a9b | 91 | s << "Trial Number: " << nT << endl; |
apaks180 | 0:0072ee664a9b | 92 | if(correct) { |
apaks180 | 0:0072ee664a9b | 93 | s << "Lemur was correct" << endl; |
apaks180 | 0:0072ee664a9b | 94 | } else { |
apaks180 | 0:0072ee664a9b | 95 | s << "Lemur was incorrect\n"; |
apaks180 | 0:0072ee664a9b | 96 | |
apaks180 | 0:0072ee664a9b | 97 | } |
apaks180 | 0:0072ee664a9b | 98 | s << "Fraction correct so far: " << nC << "/" << nT << endl; |
apaks180 | 0:0072ee664a9b | 99 | s << "Time elapsed during last trial, in ms: " << time << endl; |
apaks180 | 0:0072ee664a9b | 100 | fprintf(fp, s.str().c_str()); |
apaks180 | 0:0072ee664a9b | 101 | return s.str(); |
apaks180 | 0:0072ee664a9b | 102 | } |
apaks180 | 0:0072ee664a9b | 103 | int main() |
apaks180 | 0:0072ee664a9b | 104 | { |
apaks180 | 0:0072ee664a9b | 105 | uLCD.display_control(PORTRAIT); |
apaks180 | 0:0072ee664a9b | 106 | uLCD.cls(); |
apaks180 | 0:0072ee664a9b | 107 | uLCD.printf("Hello Friendly Lemur!"); |
apaks180 | 0:0072ee664a9b | 108 | mySpeaker.PlayNote(660,.1,0.1); |
apaks180 | 0:0072ee664a9b | 109 | mySpeaker.PlayNote(0,.15,0.1); |
apaks180 | 0:0072ee664a9b | 110 | mySpeaker.PlayNote(660,.1,0.1); |
apaks180 | 0:0072ee664a9b | 111 | mySpeaker.PlayNote(0,.3,0.1); |
apaks180 | 0:0072ee664a9b | 112 | mySpeaker.PlayNote(660,.1,0.1); |
apaks180 | 0:0072ee664a9b | 113 | mySpeaker.PlayNote(0,.3,0.1); |
apaks180 | 0:0072ee664a9b | 114 | mySpeaker.PlayNote(510,.1,0.1); |
apaks180 | 0:0072ee664a9b | 115 | mySpeaker.PlayNote(0,.1,0.1); |
apaks180 | 0:0072ee664a9b | 116 | mySpeaker.PlayNote(660,.1,0.1); |
apaks180 | 0:0072ee664a9b | 117 | mySpeaker.PlayNote(0,.3,0.1); |
apaks180 | 0:0072ee664a9b | 118 | mySpeaker.PlayNote(770,.1,0.1); |
apaks180 | 0:0072ee664a9b | 119 | mySpeaker.PlayNote(0,.55,0.1); |
apaks180 | 0:0072ee664a9b | 120 | mySpeaker.PlayNote(380,.1,0.1); |
apaks180 | 0:0072ee664a9b | 121 | mySpeaker.PlayNote(0,.575,0.1); |
apaks180 | 0:0072ee664a9b | 122 | mySpeaker.PlayNote(510,.1,0.1); |
apaks180 | 0:0072ee664a9b | 123 | mySpeaker.PlayNote(0,.45,0.1); |
apaks180 | 0:0072ee664a9b | 124 | mySpeaker.PlayNote(380,.1,0.1); |
apaks180 | 0:0072ee664a9b | 125 | mySpeaker.PlayNote(0,.4,0.1); |
apaks180 | 0:0072ee664a9b | 126 | mySpeaker.PlayNote(320,.1,0.1); |
apaks180 | 0:0072ee664a9b | 127 | mySpeaker.PlayNote(0,.5,0.1); |
apaks180 | 0:0072ee664a9b | 128 | mySpeaker.PlayNote(440,.1,0.1); |
apaks180 | 0:0072ee664a9b | 129 | mySpeaker.PlayNote(0,.3,0.1); |
apaks180 | 0:0072ee664a9b | 130 | mySpeaker.PlayNote(480,.08,0.1); |
apaks180 | 0:0072ee664a9b | 131 | mySpeaker.PlayNote(0,.33,0.1); |
apaks180 | 0:0072ee664a9b | 132 | mySpeaker.PlayNote(450,.1,0.1); |
apaks180 | 0:0072ee664a9b | 133 | mySpeaker.PlayNote(0,.15,0.1); |
apaks180 | 0:0072ee664a9b | 134 | mySpeaker.PlayNote(430,.1,0.1); |
apaks180 | 0:0072ee664a9b | 135 | mySpeaker.PlayNote(0,.3,0.1); |
apaks180 | 0:0072ee664a9b | 136 | mySpeaker.PlayNote(380,.1,0.1); |
apaks180 | 0:0072ee664a9b | 137 | mySpeaker.PlayNote(0,.2,0.1); |
apaks180 | 0:0072ee664a9b | 138 | mySpeaker.PlayNote(660,.08,0.1); |
apaks180 | 0:0072ee664a9b | 139 | mySpeaker.PlayNote(0,.2,0.1); |
apaks180 | 0:0072ee664a9b | 140 | mySpeaker.PlayNote(760,.05,0.1); |
apaks180 | 0:0072ee664a9b | 141 | mySpeaker.PlayNote(0,.15,0.1); |
apaks180 | 0:0072ee664a9b | 142 | mySpeaker.PlayNote(860,.1,0.1); |
apaks180 | 0:0072ee664a9b | 143 | mySpeaker.PlayNote(0,.3,0.1); |
apaks180 | 0:0072ee664a9b | 144 | mySpeaker.PlayNote(700,.08,0.1); |
apaks180 | 0:0072ee664a9b | 145 | mySpeaker.PlayNote(0,.15,0.1); |
apaks180 | 0:0072ee664a9b | 146 | mySpeaker.PlayNote(760,.05,0.1); |
apaks180 | 0:0072ee664a9b | 147 | mySpeaker.PlayNote(0,.35,0.1); |
apaks180 | 0:0072ee664a9b | 148 | mySpeaker.PlayNote(660,.08,0.1); |
apaks180 | 0:0072ee664a9b | 149 | mySpeaker.PlayNote(0,.3,0.1); |
apaks180 | 0:0072ee664a9b | 150 | mySpeaker.PlayNote(520,.08,0.1); |
apaks180 | 0:0072ee664a9b | 151 | mySpeaker.PlayNote(0,.15,0.1); |
apaks180 | 0:0072ee664a9b | 152 | mySpeaker.PlayNote(580,.08,0.1); |
apaks180 | 0:0072ee664a9b | 153 | mySpeaker.PlayNote(0,.15,0.1); |
apaks180 | 0:0072ee664a9b | 154 | mySpeaker.PlayNote(480,.08,0.1); |
apaks180 | 0:0072ee664a9b | 155 | |
apaks180 | 0:0072ee664a9b | 156 | mkdir("/sd/mydir", 0777); |
apaks180 | 0:0072ee664a9b | 157 | FILE *fp = fopen("/sd/mydir/LemurResults.txt", "w"); |
apaks180 | 0:0072ee664a9b | 158 | leftButton.mode(PullUp); |
apaks180 | 0:0072ee664a9b | 159 | rightButton.mode(PullUp); |
apaks180 | 0:0072ee664a9b | 160 | reset.mode(PullUp); |
apaks180 | 0:0072ee664a9b | 161 | bool runTests = true; |
apaks180 | 0:0072ee664a9b | 162 | bool getoutoutside = false; |
apaks180 | 0:0072ee664a9b | 163 | bool getoutinside= false; |
apaks180 | 0:0072ee664a9b | 164 | bool isCorrect = false; |
apaks180 | 0:0072ee664a9b | 165 | int numCorrect = 0; |
apaks180 | 0:0072ee664a9b | 166 | int numTrials = 0; |
apaks180 | 0:0072ee664a9b | 167 | uLCD.baudrate(BAUD_3000000); //jack up baud rate to max for fast display |
apaks180 | 0:0072ee664a9b | 168 | wait(2); |
apaks180 | 0:0072ee664a9b | 169 | //Set up initial conditions |
apaks180 | 0:0072ee664a9b | 170 | uLCD.background_color(BLACK); |
apaks180 | 0:0072ee664a9b | 171 | uLCD.cls(); |
apaks180 | 0:0072ee664a9b | 172 | //draw borders |
apaks180 | 0:0072ee664a9b | 173 | while(runTests) { |
apaks180 | 0:0072ee664a9b | 174 | numTrials++; |
apaks180 | 0:0072ee664a9b | 175 | uLCD.rectangle(0, 0, 59, 120, WHITE); |
apaks180 | 0:0072ee664a9b | 176 | uLCD.rectangle(68, 0, 127, 120, WHITE); |
apaks180 | 0:0072ee664a9b | 177 | int boxL[18] = {}; |
apaks180 | 0:0072ee664a9b | 178 | int boxR[18] = {}; |
apaks180 | 0:0072ee664a9b | 179 | //15 is max number of circles |
apaks180 | 0:0072ee664a9b | 180 | int leftNum=(abs((int)rnd())%14)+1; |
apaks180 | 0:0072ee664a9b | 181 | int rightNum=(abs((int)rnd())%14)+1; |
apaks180 | 0:0072ee664a9b | 182 | while((leftNum==rightNum)||(leftNum==rightNum-1)||(leftNum==rightNum+1)||(leftNum==rightNum-2)||(leftNum==rightNum+2)) { |
apaks180 | 0:0072ee664a9b | 183 | rightNum=(abs((int)rnd())%14)+1; |
apaks180 | 0:0072ee664a9b | 184 | } |
apaks180 | 0:0072ee664a9b | 185 | bool answer = false; //false=left side greater |
apaks180 | 0:0072ee664a9b | 186 | if(leftNum<rightNum) { |
apaks180 | 0:0072ee664a9b | 187 | answer = 1; //true= right side greater |
apaks180 | 0:0072ee664a9b | 188 | } |
apaks180 | 0:0072ee664a9b | 189 | for(int i = 0; i<leftNum; i++) { |
apaks180 | 0:0072ee664a9b | 190 | int choose = abs((int)rnd())%3; |
apaks180 | 0:0072ee664a9b | 191 | int newSpot = -1; |
apaks180 | 0:0072ee664a9b | 192 | if(choose==1) |
apaks180 | 0:0072ee664a9b | 193 | newSpot = generateSquare(boxL, 0); |
apaks180 | 0:0072ee664a9b | 194 | else if(choose==2) |
apaks180 | 0:0072ee664a9b | 195 | newSpot = generateTriangle(boxL, 0); |
apaks180 | 0:0072ee664a9b | 196 | else |
apaks180 | 0:0072ee664a9b | 197 | newSpot = generateCircle(boxL, 0); |
apaks180 | 0:0072ee664a9b | 198 | boxL[newSpot] = 1; |
apaks180 | 0:0072ee664a9b | 199 | } |
apaks180 | 0:0072ee664a9b | 200 | for(int i = 0; i<rightNum; i++) { |
apaks180 | 0:0072ee664a9b | 201 | int choose = abs((int)rnd())%3; |
apaks180 | 0:0072ee664a9b | 202 | int newSpot = -1; |
apaks180 | 0:0072ee664a9b | 203 | if(choose==1) |
apaks180 | 0:0072ee664a9b | 204 | newSpot = generateSquare(boxR, 1); |
apaks180 | 0:0072ee664a9b | 205 | else if(choose==2) |
apaks180 | 0:0072ee664a9b | 206 | newSpot = generateTriangle(boxR, 1); |
apaks180 | 0:0072ee664a9b | 207 | else |
apaks180 | 0:0072ee664a9b | 208 | newSpot = generateCircle(boxR, 1); |
apaks180 | 0:0072ee664a9b | 209 | boxR[newSpot] = 1; |
apaks180 | 0:0072ee664a9b | 210 | } |
apaks180 | 0:0072ee664a9b | 211 | while(!getoutinside) { |
apaks180 | 0:0072ee664a9b | 212 | t.start(); |
apaks180 | 0:0072ee664a9b | 213 | if(!leftButton) { |
apaks180 | 0:0072ee664a9b | 214 | getoutinside=true; |
apaks180 | 0:0072ee664a9b | 215 | if(!answer) { |
apaks180 | 0:0072ee664a9b | 216 | numCorrect++; |
apaks180 | 0:0072ee664a9b | 217 | isCorrect = true; |
apaks180 | 0:0072ee664a9b | 218 | } |
apaks180 | 0:0072ee664a9b | 219 | t.stop(); |
apaks180 | 0:0072ee664a9b | 220 | |
apaks180 | 0:0072ee664a9b | 221 | } |
apaks180 | 0:0072ee664a9b | 222 | if(!rightButton) { |
apaks180 | 0:0072ee664a9b | 223 | getoutinside=true; |
apaks180 | 0:0072ee664a9b | 224 | if(answer) { |
apaks180 | 0:0072ee664a9b | 225 | numCorrect++; |
apaks180 | 0:0072ee664a9b | 226 | isCorrect = true; |
apaks180 | 0:0072ee664a9b | 227 | } |
apaks180 | 0:0072ee664a9b | 228 | t.stop(); |
apaks180 | 0:0072ee664a9b | 229 | } |
apaks180 | 0:0072ee664a9b | 230 | } |
apaks180 | 0:0072ee664a9b | 231 | int time = t.read_ms(); |
apaks180 | 0:0072ee664a9b | 232 | t.reset(); |
apaks180 | 0:0072ee664a9b | 233 | getoutinside=false; |
apaks180 | 0:0072ee664a9b | 234 | wait(.25); |
apaks180 | 0:0072ee664a9b | 235 | uLCD.cls(); |
apaks180 | 0:0072ee664a9b | 236 | string str = logData(numTrials, numCorrect, time, isCorrect, fp); |
apaks180 | 0:0072ee664a9b | 237 | uLCD.printf(str.c_str()); |
apaks180 | 0:0072ee664a9b | 238 | while(!getoutoutside) { |
apaks180 | 0:0072ee664a9b | 239 | if(!reset) { |
apaks180 | 0:0072ee664a9b | 240 | getoutoutside=true; |
apaks180 | 0:0072ee664a9b | 241 | runTests = false; |
apaks180 | 0:0072ee664a9b | 242 | } |
apaks180 | 0:0072ee664a9b | 243 | if((!leftButton)||(!rightButton)) { |
apaks180 | 0:0072ee664a9b | 244 | getoutoutside=true; |
apaks180 | 0:0072ee664a9b | 245 | } |
apaks180 | 0:0072ee664a9b | 246 | } |
apaks180 | 0:0072ee664a9b | 247 | isCorrect = false; |
apaks180 | 0:0072ee664a9b | 248 | getoutoutside = false; |
apaks180 | 0:0072ee664a9b | 249 | uLCD.cls(); |
apaks180 | 0:0072ee664a9b | 250 | } |
apaks180 | 0:0072ee664a9b | 251 | uLCD.printf("Goodbye, thanks for testing!"); |
apaks180 | 0:0072ee664a9b | 252 | wait(4); |
apaks180 | 0:0072ee664a9b | 253 | uLCD.cls(); |
apaks180 | 0:0072ee664a9b | 254 | fclose(fp); |
apaks180 | 0:0072ee664a9b | 255 | }//end main |
apaks180 | 0:0072ee664a9b | 256 | |
apaks180 | 0:0072ee664a9b | 257 | |
apaks180 | 0:0072ee664a9b | 258 | |
apaks180 | 0:0072ee664a9b | 259 | |
apaks180 | 0:0072ee664a9b | 260 |