Lab 2 for ECE 2036 (work in progress)
Dependencies: mbed 4DGL-uLCD-SE SDFileSystem PinDetect
main.cpp
00001 #include "mbed.h" 00002 #include "uLCD_4DGL.h" 00003 #include "AnalogIn.h" 00004 00005 #include "Speaker.h" 00006 #include "PinDetect.h" 00007 00008 #include "SDFileSystem.h" 00009 #include <string> 00010 00011 #include "MMA8452.h" 00012 00013 uLCD_4DGL uLCD(p9, p10, p11); // used in all parts 00014 00015 Speaker mySpeaker(p21); // used in parts 4 and 6 00016 00017 SDFileSystem sd(p5, p6, p7, p8, "sd"); // used in part 5 00018 00019 Serial pc(USBTX,USBRX); // used in part 6 00020 00021 DigitalOut myled1(LED1); 00022 DigitalOut myled2(LED2); 00023 DigitalOut myled3(LED3); 00024 DigitalOut myled4(LED4); 00025 00026 PinDetect pb1(p16); 00027 PinDetect pb2(p17); 00028 PinDetect pb3(p18); 00029 PinDetect selector(p14); 00030 00031 int currentPart = 1; // global placeholder 00032 00033 void part1() // What's up mbed! 00034 { 00035 // begin transition code 00036 uLCD.cls(); 00037 uLCD.printf("\nLab 2 - part 1:\n What's up mbed!\n"); 00038 wait(2); 00039 uLCD.cls(); 00040 // end transition code 00041 00042 uLCD.printf("\nWhat's up mbed!\n"); 00043 } 00044 00045 void part2() // Bouncing Ball 00046 { 00047 // begin transition code 00048 uLCD.cls(); 00049 uLCD.printf("\nLab 2 - part 2:\n Bouncing Balls\n"); 00050 wait(2); 00051 uLCD.cls(); 00052 // end transition code 00053 00054 00055 uLCD.display_control(PORTRAIT); 00056 uLCD.cls(); 00057 uLCD.printf("Bouncing Ball"); 00058 uLCD.baudrate(BAUD_3000000); //jack up baud rate to max for fast display 00059 wait(1.0); 00060 00061 //Set up initial conditions 00062 float fx=50.0,fy=21.0,vx=0.4,vy=0.3; 00063 int x=50,y=21,radius=4; 00064 uLCD.background_color(BLACK); 00065 uLCD.cls(); 00066 00067 //draw borders 00068 uLCD.line(0, 0, 127, 0, WHITE); 00069 uLCD.line(127, 0, 127, 127, WHITE); 00070 uLCD.line(127, 127, 0, 127, WHITE); 00071 uLCD.line(0, 127, 0, 0, WHITE); 00072 00073 for (int i=0; i<1500; i++) 00074 { 00075 //draw ball 00076 uLCD.circle(x, y, radius, RED); 00077 //bounce off edge walls and slow down a bit 00078 if ((x<=radius+1) || (x>=126-radius)) vx = -.95*vx; 00079 if ((y<=radius+1) || (y>=126-radius)) vy = -.95*vy; 00080 //erase old ball location 00081 uLCD.filled_circle(x, y, radius, BLACK); 00082 //calculate new ball position 00083 fx=fx+vx; 00084 fy=fy+vy; 00085 x=(int)fx; 00086 y=(int)fy; 00087 } //end for loop 00088 } 00089 00090 void part3() // Digital Thermometer 00091 { 00092 // begin transition code 00093 uLCD.cls(); 00094 uLCD.printf("\nLab 2 - part 3:\n Digital\n Thermometer\n"); 00095 wait(2); 00096 uLCD.cls(); 00097 // end transition code 00098 00099 00100 class TMP36 00101 { 00102 public: 00103 TMP36(PinName pin); 00104 TMP36(); 00105 float read(); 00106 private: 00107 AnalogIn _pin; 00108 }; 00109 00110 TMP36::TMP36(PinName pin) : _pin(pin) {} //This is an initializer list … pass pin to AnalogIn constructor 00111 float TMP36::read() 00112 { 00113 //convert sensor reading to C 00114 return ((_pin.read() * 3.3) - 0.500) * 100.0; 00115 // _pin.read() returns normalized voltage value as float 00116 } 00117 00118 //instantiate new class to set p15 to analog input 00119 //to read and convert TMP36 sensor's voltage output 00120 TMP36 myTMP36(p15); 00121 00122 float tempC, tempF; 00123 while(1) 00124 { 00125 tempC = myTMP36.read(); 00126 // convert to F 00127 tempF = (9.0 * tempC) / 5.0 + 32.0; 00128 // print current temp 00129 uLCD.cls(); 00130 uLCD.printf("\n%5.2f C\n%5.2f F\n\n", tempC, tempF); 00131 wait(0.5); 00132 } 00133 } 00134 00135 void part4() // Jazzy Tunes 00136 { 00137 // begin transition code 00138 uLCD.cls(); 00139 uLCD.printf("\nLab 2 - part 4:\n Jazzy Tunes\n"); 00140 wait(2); 00141 uLCD.cls(); 00142 // end transition code 00143 00144 00145 void pb1_hit_callback (void) 00146 { 00147 myled1 = !myled1; 00148 mySpeaker.PlayNote(200.0,0.25,0.1); 00149 } 00150 //------------------------------------------------------------------------------ 00151 // Callback routine is interrupt activated by a debounced pb2 hit 00152 // That is … this code runs with interrupt is generated by second button press 00153 void pb2_hit_callback (void) 00154 { 00155 myled2 = !myled2; 00156 mySpeaker.PlayNote(400.0,0.25,0.1); 00157 } 00158 //------------------------------------------------------------------------------ 00159 // Callback routine is interrupt activated by a debounced pb3 hit 00160 // That is … this code runs with interrupt is generated by third button press 00161 void pb3_hit_callback (void) 00162 { 00163 myled3 = !myled3; 00164 mySpeaker.PlayNote(800.0,0.25,0.1); 00165 } 00166 //------------------------------------------------------------------------------ 00167 00168 //setup push buttons 00169 pb1.mode(PullUp); 00170 pb2.mode(PullUp); 00171 pb3.mode(PullUp); 00172 // Delay for initial pullup to take effect 00173 wait(.01); 00174 // Setup Interrupt callback functions for a pb hit 00175 pb1.attach_deasserted(&pb1_hit_callback); 00176 pb2.attach_deasserted(&pb2_hit_callback); 00177 pb3.attach_deasserted(&pb3_hit_callback); 00178 // Start sampling pb inputs using interrupts 00179 pb1.setSampleFrequency(); 00180 pb2.setSampleFrequency(); 00181 pb3.setSampleFrequency(); 00182 // pushbuttons now setup and running 00183 while(1) 00184 { 00185 myled4 = !myled4; 00186 wait(0.5); 00187 } 00188 } 00189 00190 void part5_1() // Hello Micro SD Card - Writing 00191 { 00192 // begin transition code 00193 uLCD.cls(); 00194 uLCD.printf("\nLab 2 - part 5.1:\n Hello Micro SD\n Card - Writing\n"); 00195 wait(2); 00196 uLCD.cls(); 00197 // end transition code 00198 00199 00200 uLCD.printf("Hello Micro SD\nCard!\n"); 00201 mkdir("/sd/mydir", 0777); 00202 FILE *fp = fopen("/sd/mydir/sdtest.txt", "w"); 00203 if(fp == NULL) 00204 { 00205 uLCD.printf("Error on open \n"); 00206 } 00207 fprintf(fp, "I love ECE2036"); 00208 fclose(fp); 00209 } 00210 00211 void part5_2() // Hello Micro SD Card - Reading 00212 { 00213 // begin transition code 00214 uLCD.cls(); 00215 uLCD.printf("\nLab 2 - part 5.2:\n Hello Micro SD\n Card - Reading\n"); 00216 wait(2); 00217 uLCD.cls(); 00218 // end transition code 00219 00220 00221 string inputString; 00222 uLCD.printf("Content of sdtest.txt is: \n"); 00223 FILE *fp = fopen("/sd/mydir/sdtest.txt", "r"); 00224 if(fp == NULL) 00225 { 00226 uLCD.printf("Error on open \n"); 00227 } 00228 else 00229 { 00230 while (fscanf(fp,"%s", inputString)!= EOF) //reads in a string delineated by white space 00231 { 00232 uLCD.printf("%s ", inputString.c_str()); 00233 } 00234 } 00235 fclose(fp); 00236 } 00237 00238 void part6() // Triple Axis Accelerometer 00239 { 00240 // begin transition code 00241 uLCD.cls(); 00242 uLCD.printf("\nLab 2 - part 6:\n Triple Axis\n Accelerometer\n"); 00243 wait(2); 00244 uLCD.cls(); 00245 // end transition code 00246 00247 00248 // you can play around with the parameters to see the response 00249 int radius = 10; 00250 int offsetx = 63; 00251 int offsety = 63; 00252 double factor = 50; 00253 double music_factor = 200; 00254 bool MusicOn = false; 00255 00256 //set the push buttons that control sounds 00257 pb1.mode(PullUp); 00258 pb2.mode(PullUp); 00259 //I will not use interupts like in jazzy tunes 00260 00261 00262 double x = 0, y = 0, z = 0; 00263 00264 MMA8452 acc(p28, p27, 40000); //instantiate an acc object! 00265 00266 //set parameters -- use these and don't change 00267 acc.setBitDepth(MMA8452::BIT_DEPTH_12); 00268 acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G); 00269 acc.setDataRate(MMA8452::RATE_100); 00270 00271 00272 while(1) 00273 { 00274 uLCD.circle(-1*y*factor+offsety, -1*x*factor+offsetx, radius, BLACK); 00275 00276 if(!acc.isXYZReady()) 00277 { 00278 wait(0.01); 00279 } 00280 else 00281 { 00282 acc.readXYZGravity(&x,&y,&z); //notice this is passed by reference use pointers 00283 00284 uLCD.circle(-1*y*factor+offsety, -1*x*factor+offsetx, radius, WHITE); 00285 00286 if (MusicOn) mySpeaker.PlayNote(440.0+x*music_factor,0.25+0.2*y,0.05); 00287 00288 if (pb1 == true) MusicOn = true; 00289 00290 if (pb2 == false) MusicOn = false; 00291 00292 // You can uncomment this line to see the values coming off the MMA8452 00293 uLCD.cls(); 00294 uLCD.printf("\n(%.2f,%.2f,%.2f) \n", x,y,z); 00295 00296 } //end else 00297 00298 } //end infinite while loop 00299 } 00300 00301 void main_callback() 00302 { 00303 switch (currentPart) 00304 { 00305 case 1: part1(); 00306 currentPart++; 00307 break; 00308 case 2: part2(); 00309 currentPart++; 00310 break; 00311 case 3: part3(); 00312 currentPart++; 00313 break; 00314 case 4: part4(); 00315 currentPart++; 00316 break; 00317 case 5: part5_1(); 00318 part5_2(); 00319 currentPart++; 00320 break; 00321 case 6: part6(); 00322 currentPart = 1; 00323 break; 00324 00325 } 00326 } 00327 00328 int main() { 00329 selector.mode(PullUp); 00330 wait(.01); 00331 selector.attach_deasserted(&main_callback); 00332 selector.setSampleFrequency(); 00333 while(1) 00334 { 00335 continue; 00336 } 00337 }
Generated on Sat Aug 27 2022 03:16:16 by 1.7.2