Lab 2 for ECE 2036 (work in progress)

Dependencies:   mbed 4DGL-uLCD-SE SDFileSystem PinDetect

Committer:
jmcmath
Date:
Mon Feb 11 04:31:13 2019 +0000
Revision:
0:f10415100c39
work in progress

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jmcmath 0:f10415100c39 1 #include "mbed.h"
jmcmath 0:f10415100c39 2 #include "uLCD_4DGL.h"
jmcmath 0:f10415100c39 3 #include "AnalogIn.h"
jmcmath 0:f10415100c39 4
jmcmath 0:f10415100c39 5 #include "Speaker.h"
jmcmath 0:f10415100c39 6 #include "PinDetect.h"
jmcmath 0:f10415100c39 7
jmcmath 0:f10415100c39 8 #include "SDFileSystem.h"
jmcmath 0:f10415100c39 9 #include <string>
jmcmath 0:f10415100c39 10
jmcmath 0:f10415100c39 11 #include "MMA8452.h"
jmcmath 0:f10415100c39 12
jmcmath 0:f10415100c39 13 uLCD_4DGL uLCD(p9, p10, p11); // used in all parts
jmcmath 0:f10415100c39 14
jmcmath 0:f10415100c39 15 Speaker mySpeaker(p21); // used in parts 4 and 6
jmcmath 0:f10415100c39 16
jmcmath 0:f10415100c39 17 SDFileSystem sd(p5, p6, p7, p8, "sd"); // used in part 5
jmcmath 0:f10415100c39 18
jmcmath 0:f10415100c39 19 Serial pc(USBTX,USBRX); // used in part 6
jmcmath 0:f10415100c39 20
jmcmath 0:f10415100c39 21 DigitalOut myled1(LED1);
jmcmath 0:f10415100c39 22 DigitalOut myled2(LED2);
jmcmath 0:f10415100c39 23 DigitalOut myled3(LED3);
jmcmath 0:f10415100c39 24 DigitalOut myled4(LED4);
jmcmath 0:f10415100c39 25
jmcmath 0:f10415100c39 26 PinDetect pb1(p16);
jmcmath 0:f10415100c39 27 PinDetect pb2(p17);
jmcmath 0:f10415100c39 28 PinDetect pb3(p18);
jmcmath 0:f10415100c39 29 PinDetect selector(p14);
jmcmath 0:f10415100c39 30
jmcmath 0:f10415100c39 31 int currentPart = 1; // global placeholder
jmcmath 0:f10415100c39 32
jmcmath 0:f10415100c39 33 void part1() // What's up mbed!
jmcmath 0:f10415100c39 34 {
jmcmath 0:f10415100c39 35 // begin transition code
jmcmath 0:f10415100c39 36 uLCD.cls();
jmcmath 0:f10415100c39 37 uLCD.printf("\nLab 2 - part 1:\n What's up mbed!\n");
jmcmath 0:f10415100c39 38 wait(2);
jmcmath 0:f10415100c39 39 uLCD.cls();
jmcmath 0:f10415100c39 40 // end transition code
jmcmath 0:f10415100c39 41
jmcmath 0:f10415100c39 42 uLCD.printf("\nWhat's up mbed!\n");
jmcmath 0:f10415100c39 43 }
jmcmath 0:f10415100c39 44
jmcmath 0:f10415100c39 45 void part2() // Bouncing Ball
jmcmath 0:f10415100c39 46 {
jmcmath 0:f10415100c39 47 // begin transition code
jmcmath 0:f10415100c39 48 uLCD.cls();
jmcmath 0:f10415100c39 49 uLCD.printf("\nLab 2 - part 2:\n Bouncing Balls\n");
jmcmath 0:f10415100c39 50 wait(2);
jmcmath 0:f10415100c39 51 uLCD.cls();
jmcmath 0:f10415100c39 52 // end transition code
jmcmath 0:f10415100c39 53
jmcmath 0:f10415100c39 54
jmcmath 0:f10415100c39 55 uLCD.display_control(PORTRAIT);
jmcmath 0:f10415100c39 56 uLCD.cls();
jmcmath 0:f10415100c39 57 uLCD.printf("Bouncing Ball");
jmcmath 0:f10415100c39 58 uLCD.baudrate(BAUD_3000000); //jack up baud rate to max for fast display
jmcmath 0:f10415100c39 59 wait(1.0);
jmcmath 0:f10415100c39 60
jmcmath 0:f10415100c39 61 //Set up initial conditions
jmcmath 0:f10415100c39 62 float fx=50.0,fy=21.0,vx=0.4,vy=0.3;
jmcmath 0:f10415100c39 63 int x=50,y=21,radius=4;
jmcmath 0:f10415100c39 64 uLCD.background_color(BLACK);
jmcmath 0:f10415100c39 65 uLCD.cls();
jmcmath 0:f10415100c39 66
jmcmath 0:f10415100c39 67 //draw borders
jmcmath 0:f10415100c39 68 uLCD.line(0, 0, 127, 0, WHITE);
jmcmath 0:f10415100c39 69 uLCD.line(127, 0, 127, 127, WHITE);
jmcmath 0:f10415100c39 70 uLCD.line(127, 127, 0, 127, WHITE);
jmcmath 0:f10415100c39 71 uLCD.line(0, 127, 0, 0, WHITE);
jmcmath 0:f10415100c39 72
jmcmath 0:f10415100c39 73 for (int i=0; i<1500; i++)
jmcmath 0:f10415100c39 74 {
jmcmath 0:f10415100c39 75 //draw ball
jmcmath 0:f10415100c39 76 uLCD.circle(x, y, radius, RED);
jmcmath 0:f10415100c39 77 //bounce off edge walls and slow down a bit
jmcmath 0:f10415100c39 78 if ((x<=radius+1) || (x>=126-radius)) vx = -.95*vx;
jmcmath 0:f10415100c39 79 if ((y<=radius+1) || (y>=126-radius)) vy = -.95*vy;
jmcmath 0:f10415100c39 80 //erase old ball location
jmcmath 0:f10415100c39 81 uLCD.filled_circle(x, y, radius, BLACK);
jmcmath 0:f10415100c39 82 //calculate new ball position
jmcmath 0:f10415100c39 83 fx=fx+vx;
jmcmath 0:f10415100c39 84 fy=fy+vy;
jmcmath 0:f10415100c39 85 x=(int)fx;
jmcmath 0:f10415100c39 86 y=(int)fy;
jmcmath 0:f10415100c39 87 } //end for loop
jmcmath 0:f10415100c39 88 }
jmcmath 0:f10415100c39 89
jmcmath 0:f10415100c39 90 void part3() // Digital Thermometer
jmcmath 0:f10415100c39 91 {
jmcmath 0:f10415100c39 92 // begin transition code
jmcmath 0:f10415100c39 93 uLCD.cls();
jmcmath 0:f10415100c39 94 uLCD.printf("\nLab 2 - part 3:\n Digital\n Thermometer\n");
jmcmath 0:f10415100c39 95 wait(2);
jmcmath 0:f10415100c39 96 uLCD.cls();
jmcmath 0:f10415100c39 97 // end transition code
jmcmath 0:f10415100c39 98
jmcmath 0:f10415100c39 99
jmcmath 0:f10415100c39 100 class TMP36
jmcmath 0:f10415100c39 101 {
jmcmath 0:f10415100c39 102 public:
jmcmath 0:f10415100c39 103 TMP36(PinName pin);
jmcmath 0:f10415100c39 104 TMP36();
jmcmath 0:f10415100c39 105 float read();
jmcmath 0:f10415100c39 106 private:
jmcmath 0:f10415100c39 107 AnalogIn _pin;
jmcmath 0:f10415100c39 108 };
jmcmath 0:f10415100c39 109
jmcmath 0:f10415100c39 110 TMP36::TMP36(PinName pin) : _pin(pin) {} //This is an initializer list … pass pin to AnalogIn constructor
jmcmath 0:f10415100c39 111 float TMP36::read()
jmcmath 0:f10415100c39 112 {
jmcmath 0:f10415100c39 113 //convert sensor reading to C
jmcmath 0:f10415100c39 114 return ((_pin.read() * 3.3) - 0.500) * 100.0;
jmcmath 0:f10415100c39 115 // _pin.read() returns normalized voltage value as float
jmcmath 0:f10415100c39 116 }
jmcmath 0:f10415100c39 117
jmcmath 0:f10415100c39 118 //instantiate new class to set p15 to analog input
jmcmath 0:f10415100c39 119 //to read and convert TMP36 sensor's voltage output
jmcmath 0:f10415100c39 120 TMP36 myTMP36(p15);
jmcmath 0:f10415100c39 121
jmcmath 0:f10415100c39 122 float tempC, tempF;
jmcmath 0:f10415100c39 123 while(1)
jmcmath 0:f10415100c39 124 {
jmcmath 0:f10415100c39 125 tempC = myTMP36.read();
jmcmath 0:f10415100c39 126 // convert to F
jmcmath 0:f10415100c39 127 tempF = (9.0 * tempC) / 5.0 + 32.0;
jmcmath 0:f10415100c39 128 // print current temp
jmcmath 0:f10415100c39 129 uLCD.cls();
jmcmath 0:f10415100c39 130 uLCD.printf("\n%5.2f C\n%5.2f F\n\n", tempC, tempF);
jmcmath 0:f10415100c39 131 wait(0.5);
jmcmath 0:f10415100c39 132 }
jmcmath 0:f10415100c39 133 }
jmcmath 0:f10415100c39 134
jmcmath 0:f10415100c39 135 void part4() // Jazzy Tunes
jmcmath 0:f10415100c39 136 {
jmcmath 0:f10415100c39 137 // begin transition code
jmcmath 0:f10415100c39 138 uLCD.cls();
jmcmath 0:f10415100c39 139 uLCD.printf("\nLab 2 - part 4:\n Jazzy Tunes\n");
jmcmath 0:f10415100c39 140 wait(2);
jmcmath 0:f10415100c39 141 uLCD.cls();
jmcmath 0:f10415100c39 142 // end transition code
jmcmath 0:f10415100c39 143
jmcmath 0:f10415100c39 144
jmcmath 0:f10415100c39 145 void pb1_hit_callback (void)
jmcmath 0:f10415100c39 146 {
jmcmath 0:f10415100c39 147 myled1 = !myled1;
jmcmath 0:f10415100c39 148 mySpeaker.PlayNote(200.0,0.25,0.1);
jmcmath 0:f10415100c39 149 }
jmcmath 0:f10415100c39 150 //------------------------------------------------------------------------------
jmcmath 0:f10415100c39 151 // Callback routine is interrupt activated by a debounced pb2 hit
jmcmath 0:f10415100c39 152 // That is … this code runs with interrupt is generated by second button press
jmcmath 0:f10415100c39 153 void pb2_hit_callback (void)
jmcmath 0:f10415100c39 154 {
jmcmath 0:f10415100c39 155 myled2 = !myled2;
jmcmath 0:f10415100c39 156 mySpeaker.PlayNote(400.0,0.25,0.1);
jmcmath 0:f10415100c39 157 }
jmcmath 0:f10415100c39 158 //------------------------------------------------------------------------------
jmcmath 0:f10415100c39 159 // Callback routine is interrupt activated by a debounced pb3 hit
jmcmath 0:f10415100c39 160 // That is … this code runs with interrupt is generated by third button press
jmcmath 0:f10415100c39 161 void pb3_hit_callback (void)
jmcmath 0:f10415100c39 162 {
jmcmath 0:f10415100c39 163 myled3 = !myled3;
jmcmath 0:f10415100c39 164 mySpeaker.PlayNote(800.0,0.25,0.1);
jmcmath 0:f10415100c39 165 }
jmcmath 0:f10415100c39 166 //------------------------------------------------------------------------------
jmcmath 0:f10415100c39 167
jmcmath 0:f10415100c39 168 //setup push buttons
jmcmath 0:f10415100c39 169 pb1.mode(PullUp);
jmcmath 0:f10415100c39 170 pb2.mode(PullUp);
jmcmath 0:f10415100c39 171 pb3.mode(PullUp);
jmcmath 0:f10415100c39 172 // Delay for initial pullup to take effect
jmcmath 0:f10415100c39 173 wait(.01);
jmcmath 0:f10415100c39 174 // Setup Interrupt callback functions for a pb hit
jmcmath 0:f10415100c39 175 pb1.attach_deasserted(&pb1_hit_callback);
jmcmath 0:f10415100c39 176 pb2.attach_deasserted(&pb2_hit_callback);
jmcmath 0:f10415100c39 177 pb3.attach_deasserted(&pb3_hit_callback);
jmcmath 0:f10415100c39 178 // Start sampling pb inputs using interrupts
jmcmath 0:f10415100c39 179 pb1.setSampleFrequency();
jmcmath 0:f10415100c39 180 pb2.setSampleFrequency();
jmcmath 0:f10415100c39 181 pb3.setSampleFrequency();
jmcmath 0:f10415100c39 182 // pushbuttons now setup and running
jmcmath 0:f10415100c39 183 while(1)
jmcmath 0:f10415100c39 184 {
jmcmath 0:f10415100c39 185 myled4 = !myled4;
jmcmath 0:f10415100c39 186 wait(0.5);
jmcmath 0:f10415100c39 187 }
jmcmath 0:f10415100c39 188 }
jmcmath 0:f10415100c39 189
jmcmath 0:f10415100c39 190 void part5_1() // Hello Micro SD Card - Writing
jmcmath 0:f10415100c39 191 {
jmcmath 0:f10415100c39 192 // begin transition code
jmcmath 0:f10415100c39 193 uLCD.cls();
jmcmath 0:f10415100c39 194 uLCD.printf("\nLab 2 - part 5.1:\n Hello Micro SD\n Card - Writing\n");
jmcmath 0:f10415100c39 195 wait(2);
jmcmath 0:f10415100c39 196 uLCD.cls();
jmcmath 0:f10415100c39 197 // end transition code
jmcmath 0:f10415100c39 198
jmcmath 0:f10415100c39 199
jmcmath 0:f10415100c39 200 uLCD.printf("Hello Micro SD\nCard!\n");
jmcmath 0:f10415100c39 201 mkdir("/sd/mydir", 0777);
jmcmath 0:f10415100c39 202 FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
jmcmath 0:f10415100c39 203 if(fp == NULL)
jmcmath 0:f10415100c39 204 {
jmcmath 0:f10415100c39 205 uLCD.printf("Error on open \n");
jmcmath 0:f10415100c39 206 }
jmcmath 0:f10415100c39 207 fprintf(fp, "I love ECE2036");
jmcmath 0:f10415100c39 208 fclose(fp);
jmcmath 0:f10415100c39 209 }
jmcmath 0:f10415100c39 210
jmcmath 0:f10415100c39 211 void part5_2() // Hello Micro SD Card - Reading
jmcmath 0:f10415100c39 212 {
jmcmath 0:f10415100c39 213 // begin transition code
jmcmath 0:f10415100c39 214 uLCD.cls();
jmcmath 0:f10415100c39 215 uLCD.printf("\nLab 2 - part 5.2:\n Hello Micro SD\n Card - Reading\n");
jmcmath 0:f10415100c39 216 wait(2);
jmcmath 0:f10415100c39 217 uLCD.cls();
jmcmath 0:f10415100c39 218 // end transition code
jmcmath 0:f10415100c39 219
jmcmath 0:f10415100c39 220
jmcmath 0:f10415100c39 221 string inputString;
jmcmath 0:f10415100c39 222 uLCD.printf("Content of sdtest.txt is: \n");
jmcmath 0:f10415100c39 223 FILE *fp = fopen("/sd/mydir/sdtest.txt", "r");
jmcmath 0:f10415100c39 224 if(fp == NULL)
jmcmath 0:f10415100c39 225 {
jmcmath 0:f10415100c39 226 uLCD.printf("Error on open \n");
jmcmath 0:f10415100c39 227 }
jmcmath 0:f10415100c39 228 else
jmcmath 0:f10415100c39 229 {
jmcmath 0:f10415100c39 230 while (fscanf(fp,"%s", inputString)!= EOF) //reads in a string delineated by white space
jmcmath 0:f10415100c39 231 {
jmcmath 0:f10415100c39 232 uLCD.printf("%s ", inputString.c_str());
jmcmath 0:f10415100c39 233 }
jmcmath 0:f10415100c39 234 }
jmcmath 0:f10415100c39 235 fclose(fp);
jmcmath 0:f10415100c39 236 }
jmcmath 0:f10415100c39 237
jmcmath 0:f10415100c39 238 void part6() // Triple Axis Accelerometer
jmcmath 0:f10415100c39 239 {
jmcmath 0:f10415100c39 240 // begin transition code
jmcmath 0:f10415100c39 241 uLCD.cls();
jmcmath 0:f10415100c39 242 uLCD.printf("\nLab 2 - part 6:\n Triple Axis\n Accelerometer\n");
jmcmath 0:f10415100c39 243 wait(2);
jmcmath 0:f10415100c39 244 uLCD.cls();
jmcmath 0:f10415100c39 245 // end transition code
jmcmath 0:f10415100c39 246
jmcmath 0:f10415100c39 247
jmcmath 0:f10415100c39 248 // you can play around with the parameters to see the response
jmcmath 0:f10415100c39 249 int radius = 10;
jmcmath 0:f10415100c39 250 int offsetx = 63;
jmcmath 0:f10415100c39 251 int offsety = 63;
jmcmath 0:f10415100c39 252 double factor = 50;
jmcmath 0:f10415100c39 253 double music_factor = 200;
jmcmath 0:f10415100c39 254 bool MusicOn = false;
jmcmath 0:f10415100c39 255
jmcmath 0:f10415100c39 256 //set the push buttons that control sounds
jmcmath 0:f10415100c39 257 pb1.mode(PullUp);
jmcmath 0:f10415100c39 258 pb2.mode(PullUp);
jmcmath 0:f10415100c39 259 //I will not use interupts like in jazzy tunes
jmcmath 0:f10415100c39 260
jmcmath 0:f10415100c39 261
jmcmath 0:f10415100c39 262 double x = 0, y = 0, z = 0;
jmcmath 0:f10415100c39 263
jmcmath 0:f10415100c39 264 MMA8452 acc(p28, p27, 40000); //instantiate an acc object!
jmcmath 0:f10415100c39 265
jmcmath 0:f10415100c39 266 //set parameters -- use these and don't change
jmcmath 0:f10415100c39 267 acc.setBitDepth(MMA8452::BIT_DEPTH_12);
jmcmath 0:f10415100c39 268 acc.setDynamicRange(MMA8452::DYNAMIC_RANGE_4G);
jmcmath 0:f10415100c39 269 acc.setDataRate(MMA8452::RATE_100);
jmcmath 0:f10415100c39 270
jmcmath 0:f10415100c39 271
jmcmath 0:f10415100c39 272 while(1)
jmcmath 0:f10415100c39 273 {
jmcmath 0:f10415100c39 274 uLCD.circle(-1*y*factor+offsety, -1*x*factor+offsetx, radius, BLACK);
jmcmath 0:f10415100c39 275
jmcmath 0:f10415100c39 276 if(!acc.isXYZReady())
jmcmath 0:f10415100c39 277 {
jmcmath 0:f10415100c39 278 wait(0.01);
jmcmath 0:f10415100c39 279 }
jmcmath 0:f10415100c39 280 else
jmcmath 0:f10415100c39 281 {
jmcmath 0:f10415100c39 282 acc.readXYZGravity(&x,&y,&z); //notice this is passed by reference use pointers
jmcmath 0:f10415100c39 283
jmcmath 0:f10415100c39 284 uLCD.circle(-1*y*factor+offsety, -1*x*factor+offsetx, radius, WHITE);
jmcmath 0:f10415100c39 285
jmcmath 0:f10415100c39 286 if (MusicOn) mySpeaker.PlayNote(440.0+x*music_factor,0.25+0.2*y,0.05);
jmcmath 0:f10415100c39 287
jmcmath 0:f10415100c39 288 if (pb1 == true) MusicOn = true;
jmcmath 0:f10415100c39 289
jmcmath 0:f10415100c39 290 if (pb2 == false) MusicOn = false;
jmcmath 0:f10415100c39 291
jmcmath 0:f10415100c39 292 // You can uncomment this line to see the values coming off the MMA8452
jmcmath 0:f10415100c39 293 uLCD.cls();
jmcmath 0:f10415100c39 294 uLCD.printf("\n(%.2f,%.2f,%.2f) \n", x,y,z);
jmcmath 0:f10415100c39 295
jmcmath 0:f10415100c39 296 } //end else
jmcmath 0:f10415100c39 297
jmcmath 0:f10415100c39 298 } //end infinite while loop
jmcmath 0:f10415100c39 299 }
jmcmath 0:f10415100c39 300
jmcmath 0:f10415100c39 301 void main_callback()
jmcmath 0:f10415100c39 302 {
jmcmath 0:f10415100c39 303 switch (currentPart)
jmcmath 0:f10415100c39 304 {
jmcmath 0:f10415100c39 305 case 1: part1();
jmcmath 0:f10415100c39 306 currentPart++;
jmcmath 0:f10415100c39 307 break;
jmcmath 0:f10415100c39 308 case 2: part2();
jmcmath 0:f10415100c39 309 currentPart++;
jmcmath 0:f10415100c39 310 break;
jmcmath 0:f10415100c39 311 case 3: part3();
jmcmath 0:f10415100c39 312 currentPart++;
jmcmath 0:f10415100c39 313 break;
jmcmath 0:f10415100c39 314 case 4: part4();
jmcmath 0:f10415100c39 315 currentPart++;
jmcmath 0:f10415100c39 316 break;
jmcmath 0:f10415100c39 317 case 5: part5_1();
jmcmath 0:f10415100c39 318 part5_2();
jmcmath 0:f10415100c39 319 currentPart++;
jmcmath 0:f10415100c39 320 break;
jmcmath 0:f10415100c39 321 case 6: part6();
jmcmath 0:f10415100c39 322 currentPart = 1;
jmcmath 0:f10415100c39 323 break;
jmcmath 0:f10415100c39 324
jmcmath 0:f10415100c39 325 }
jmcmath 0:f10415100c39 326 }
jmcmath 0:f10415100c39 327
jmcmath 0:f10415100c39 328 int main() {
jmcmath 0:f10415100c39 329 selector.mode(PullUp);
jmcmath 0:f10415100c39 330 wait(.01);
jmcmath 0:f10415100c39 331 selector.attach_deasserted(&main_callback);
jmcmath 0:f10415100c39 332 selector.setSampleFrequency();
jmcmath 0:f10415100c39 333 while(1)
jmcmath 0:f10415100c39 334 {
jmcmath 0:f10415100c39 335 continue;
jmcmath 0:f10415100c39 336 }
jmcmath 0:f10415100c39 337 }