Jakobi Blackburn / Mbed 2 deprecated UltrasonicDistanceSensor-el13jb

Dependencies:   N5110WN PowerControl SRF02 mbed

Revision:
7:962b5f044a9b
Parent:
6:a5cd7eb72264
Child:
8:ffcde84b8cf3
--- a/main.cpp	Wed Apr 08 23:02:14 2015 +0000
+++ b/main.cpp	Sun Apr 12 22:08:16 2015 +0000
@@ -7,55 +7,53 @@
 
 int main()
 {
-    pc.baud(9600);  //sets baud rate
-    timer.attach(&timerExpired,0.2);
-    timer2.attach(&timer2Expired,1);
-    timer3.attach(&timer3Expired,2);
-    LogTog.rise(&logToggle);
-    UnitTog.rise(&unitToggle);
-    VisTog.rise(&visToggle);
-    pc.attach(&serialISR); // attach serial ISR
-    set_time(1427846400); // initialise time to 1st April 2015 00:00:00
-
-    BLED.period(0.02);  //sets the frequency of the Backlight at 50Hz
-    BLED=BLEDLevel.read();  //sets the brightness
-    lcd.init(); //to initilize the display
-
-    checkerBoard();
-    wait(0.2);
-    lcd.clear();
+    PHY_PowerDown();
+    semihost_powerdown();
+    pc.baud(9600);  ///sets baud rate
+    timer.attach(&timerExpired,0.2); /// Sets the speed of the timer and attaches a function to run
+    timer2.attach(&timer2Expired,1); /// Sets the speed of the timer and attaches a function to run
+    timer3.attach(&timer3Expired,2); /// Sets the speed of the timer and attaches a function to run
+    LogTog.rise(&logToggle); /// attaches a function to the rise of the LogTog button
+    UnitTog.rise(&unitToggle); /// attaches a function to the rise of the UnitTog button
+    VisTog.rise(&visToggle); /// attaches a function to the rise of the VisTog button
+    pc.attach(&serialISR); /// attach serial ISR
+    set_time(1427846400); /// initialise time to 1st April 2015 00:00:00
 
-    introTune(); // plays the intro tune
-    lcd.printString("Parking",20,0);
-    lcd.printString("Sensor",23,1);
-    lcd.printString("by",35,2);
-    lcd.printString("Jakobi",24,3);
+    BLED.period(0.02);  ///sets the frequency of the Backlight at 50Hz
+    BLED=BLEDLevel.read();  ///sets the brightness
+    lcd.init(); ///to initilize the display
+    introTune(); /// plays the intro tune
+    lcd.printString("---+--+--+---",3,0);
+    lcd.printString("Distance",18,1);
+    lcd.printString("Sensor",25,2);
+    lcd.printString("By Jakobi",16,3);
     lcd.printString("Blackburn",16,4);
+    lcd.printString("---+--+--+---",3,5);
     wait(2);
-    lcd.clear();
+    lcd.clear(); /// clears the opening message
     while(1) {
-        if (setTimeFlag) { // if updated time has been sent
-            setTimeFlag = 0; // clear flag
-            setTime(); // update time
+        if (setTimeFlag) { /// if updated time has been sent
+            setTimeFlag = 0; /// clear flag
+            setTime(); /// update time
         }
-        if(distance>=100) {
-            if(timer3Flag) {
-                pc.printf("Scan Speed = 0.5 \n");
-                theMain();
+        if(distance>=100) { /// uses the 2 second timer if the distance is above 100
+            if(timer3Flag) { /// 2 second timer's flag
+                pc.printf("Scan Speed = 0.5 \n"); /// prints the scan speed across the serial port
+                theMain(); /// Contains the main code
 
             }
-        } else if(distance>=50) {
-            if(timer2Flag) {
-                pc.printf("Scan Speed = 1 \n");
-                theMain();
+        } else if(distance>=50) { /// uses the 1 second timer if the distance is above 50 and less than 100
+            if(timer2Flag) {/// 1 second timer's flag
+                pc.printf("Scan Speed = 1 \n"); /// prints the scan speed across the serial port
+                theMain(); /// Contains the main code
             }
-        } else if(distance<50) {
-            if(timerFlag) {
-                pc.printf("Scan Speed = 5 \n");
-                theMain();
+        } else if(distance<50) { /// uses the 0.2 second timer if the distance is less than 50
+            if(timerFlag) {/// 0.2 second timer's flag
+                pc.printf("Scan Speed = 5 \n"); /// prints the scan speed across the serial port
+                theMain(); /// Contains the main code
             }
         } else {
-            error(1);
+            error(1); //displays an error message across the mBed leds
         }
 
         Sleep();
@@ -78,19 +76,19 @@
 
 void theMain()
 {
-    BLED=BLEDLevel.read();  //sets the brightness
-    timerFlag =0; //resets flags
+    BLED=BLEDLevel.read();  ///sets the brightness
+    timerFlag =0; ///resets flags
     timer2Flag=0;
     timer3Flag=0;
-    time_t seconds = time(NULL); // get current time
-    // format time into a string (time and date)
+    time_t seconds = time(NULL); /// get current time
+    /// format time into a string (time and date)
     strftime(buffer, 30 , "%d/%m/%y %R", localtime(&seconds));
-    float dist = getDistance()*unitX; //reads the distance and sets it to a float value.
-    //prints the value to the serial port.
+    float dist = getDistance()*unitX; ///reads the distance and sets it to a float value.
+    ///prints the value to the serial port.
     if(state==1) {
-        pc.printf("%s , Distance = %.2f %s \n",buffer,dist,units);   //prints the value to the serial port.
+        pc.printf("%s , Distance = %.2f %s \n",buffer,dist,units);   ///prints the value to the serial port.
     } else if(state !=1) {
-        pc.printf("%s , Distance = %.0f %s \n",buffer,dist,units);   //prints the value to the serial port.
+        pc.printf("%s , Distance = %.0f %s \n",buffer,dist,units);   ///prints the value to the serial port.
     }
     logging(buffer,distance);
     if(visual==0) {
@@ -150,16 +148,16 @@
 
 void unitToggle()
 {
-    state = fsm[state].nextState[UnitTog]; // read input and update current state
-    unitX = fsm[state].unitMultiple; // set output depending on current state
-    units= fsm[state].Unit; //
+    state = fsm[state].nextState[UnitTog]; /// read input and update current state
+    unitX = fsm[state].unitMultiple; /// set output depending on current state
+    units= fsm[state].Unit; ///
     wait(0.2);
     pc.printf("unit multiple = %.2f \n",unitX);
     pc.printf("Units = %s \n",units);
 }
 
 
-void error(int E) //up to 14. IF 15 shown it is a ack bit error for the SRF02
+void error(int E) /// up to 14. IF 15 shown it is a ack bit error for the SRF02
 {
     while(1) {
         leds=0;
@@ -185,12 +183,12 @@
 void logging(char* data,float data1)
 {
     if(logButtonFlag) {
-        FILE *fp = fopen("/local/log.csv", "a"); // open 'log.csv' for appending
-        // if the file doesn't exist it is created, if it exists, data is appended to the end
-        fprintf(fp," %s , %.2f\n ",data,data1); // print string to file
-        fclose(fp); // close file
+        FILE *fp = fopen("/local/log.csv", "a"); /// open 'log.csv' for appending
+        /// if the file doesn't exist it is created, if it exists, data is appended to the end
+        fprintf(fp," %s , %.2f\n ",data,data1); /// print string to file
+        fclose(fp); /// close file
         pc.printf("Data Logged\n");
-        LLED=1; //keeps the led on if it is logging
+        LLED=1; /// keeps the led on if it is logging
     } else {
         pc.printf("WARNING : Unlogged Data\n");
         LLED=0;
@@ -199,19 +197,19 @@
 
 void setTime()
 {
-// print time for debugging
+/// print time for debugging
     pc.printf("set time - %s",rxString);
-// atoi() converts a string to an integer
+/// atoi() converts a string to an integer
     int time = atoi(rxString);
-// update the time
+/// update the time
     set_time(time);
     pc.printf("set time - %d",time);
 }
 void serialISR()
 {
-// when a serial interrupt occurs, read rx string into buffer
+/// when a serial interrupt occurs, read rx string into buffer
     pc.gets(rxString,16);
-// set flag
+/// set flag
     setTimeFlag = 1;
 }
 
@@ -232,7 +230,7 @@
     Buzzer.PlayNote(784.0,0.084,1.0);
 }
 
-void checkerBoard() // creates a checked pattern on the lcd
+void checkerBoard() /// creates a checked pattern on the lcd
 {
     for(int i=0; i<84; i+=2) {
         for(int j=0; j<48; j+=2) {
@@ -242,23 +240,23 @@
     lcd.refresh();
 }
 
-void LCDVis0()    //the max range is 249 (according to the data sheet) and there are 84 pixels wide on the LCD so this creates the amount of cm for each pixel
+void LCDVis0()    /// the max range is 249 (according to the data sheet) and there are 84 pixels wide on the LCD so this creates the amount of cm for each pixel
 {
     lcd.clear();
     if(logButtonFlag) {
         lcd.printString("-L-",0,5);
     }
     
-    int I = distance*0.337;  //and then multiplies the current distance by that value as (84/249) =0.337...
+    int I = distance*0.337;  /// and then multiplies the current distance by that value as (84/249) =0.337...
     I=I+3;
     if(I>=79){I=79;}
     pc.printf("bar width - %d pixels \n",I);
     lcd.drawRect(0,16,83,16,0);
     lcd.drawRect(2,18,I,12,1);
     if(state==1) {
-        sprintf(Dbuffer, "%.2f", distance*unitX);   //prints the value to the serial port.
+        sprintf(Dbuffer, "%.2f", distance*unitX);   /// prints the value to the serial port.
     } else if(state !=1) {
-        sprintf(Dbuffer, "%.0f", distance*unitX);   //prints the value to the serial port.
+        sprintf(Dbuffer, "%.0f", distance*unitX);   /// prints the value to the serial port.
     }
     lcd.printString (buffer,0,0);
     lcd.printString (Dbuffer,26,5);
@@ -266,14 +264,14 @@
     lcd.refresh();
 }
 
-void LCDVis1()  //the max range is 249 (according to the data sheet) and there are 48 pixels high on the LCD so this creates the amount of cm for each pixel
+void LCDVis1()  /// the max range is 249 (according to the data sheet) and there are 48 pixels high on the LCD so this creates the amount of cm for each pixel
 {
     lcd.clear();
     if(logButtonFlag) {
         lcd.printString("-L-",0,5);
     }
     
-    int J = distance*0.14;  //and then multiplies the current distance by that value as (36/249) = 0.19..(as top 12 pixels are for time)
+    int J = distance*0.14;  /// and then multiplies the current distance by that value as (36/249) = 0.19..(as top 12 pixels are for time)
     if(J>=36) {
         J=36;
     }
@@ -281,9 +279,9 @@
     lcd.drawRect(24,9,34,38,0);
     lcd.drawRect(26,47-J,30,J-2,1);
     if(state==1) {
-        sprintf(Dbuffer, "%.2f", distance*unitX);   //prints the value to the serial port.
+        sprintf(Dbuffer, "%.2f", distance*unitX);   /// prints the value to the serial port.
     } else if(state !=1) {
-        sprintf(Dbuffer, "%.0f", distance*unitX);   //prints the value to the serial port.
+        sprintf(Dbuffer, "%.0f", distance*unitX);   /// prints the value to the serial port.
     }
     lcd.printString (buffer,0,0);
     lcd.printString (Dbuffer,60,3);
@@ -299,16 +297,16 @@
     }
     
     car();
-    int I = distance*0.277;  //and then multiplies the current distance by that value as (69/249) =0.277...
+    int I = distance*0.277;  /// and then multiplies the current distance by that value as (69/249) =0.277...
     if(I>=84) {
         I=84;
     }
     pc.printf("bar width - %d pixels \n",I);
     lcd.drawRect(I+15,9,69,29,1);
     if(state==1) {
-        sprintf(Dbuffer, "%.2f", distance*unitX);   //prints the value to the serial port.
+        sprintf(Dbuffer, "%.2f", distance*unitX);   /// prints the value to the serial port.
     } else if(state !=1) {
-        sprintf(Dbuffer, "%.0f", distance*unitX);   //prints the value to the serial port.
+        sprintf(Dbuffer, "%.0f", distance*unitX);   /// prints the value to the serial port.
     }
     lcd.printString (buffer,0,0);
     lcd.printString (Dbuffer,26,5);
@@ -420,119 +418,30 @@
 
 void car()
 {
-    lcd.setPixel(4,19);
-    lcd.setPixel(5,19);
-    lcd.setPixel(6,19);
-    lcd.setPixel(10,19);
-    lcd.setPixel(11,19);
-    lcd.setPixel(12,19);
-    lcd.setPixel(4,20);
-    lcd.setPixel(5,20);
-    lcd.setPixel(6,20);
-    lcd.setPixel(10,20);
-    lcd.setPixel(11,20);
-    lcd.setPixel(12,20);
-    lcd.setPixel(1,21);
-    lcd.setPixel(2,21);
-    lcd.setPixel(3,21);
-    lcd.setPixel(4,21);
-    lcd.setPixel(5,21);
-    lcd.setPixel(6,21);
-    lcd.setPixel(7,21);
-    lcd.setPixel(8,21);
-    lcd.setPixel(9,21);
-    lcd.setPixel(10,21);
-    lcd.setPixel(11,21);
-    lcd.setPixel(12,21);
-    lcd.setPixel(13,21);
-    lcd.setPixel(0,22);
-    lcd.setPixel(1,22);
-    lcd.setPixel(2,22);
-    lcd.setPixel(3,22);
-    lcd.setPixel(4,22);
-    lcd.setPixel(6,22);
-    lcd.setPixel(7,22);
-    lcd.setPixel(8,22);
-    lcd.setPixel(9,22);
-    lcd.setPixel(10,22);
-    lcd.setPixel(12,22);
-    lcd.setPixel(13,22);
-    lcd.setPixel(0,23);
-    lcd.setPixel(1,23);
-    lcd.setPixel(2,23);
-    lcd.setPixel(3,23);
-    lcd.setPixel(6,23);
-    lcd.setPixel(7,23);
-    lcd.setPixel(8,23);
-    lcd.setPixel(9,23);
-    lcd.setPixel(10,23);
-    lcd.setPixel(12,23);
-    lcd.setPixel(13,23);
-    lcd.setPixel(0,24);
-    lcd.setPixel(1,24);
-    lcd.setPixel(2,24);
-    lcd.setPixel(3,24);
-    lcd.setPixel(6,24);
-    lcd.setPixel(7,24);
-    lcd.setPixel(8,24);
-    lcd.setPixel(9,24);
-    lcd.setPixel(10,24);
-    lcd.setPixel(12,24);
-    lcd.setPixel(13,24);
-    lcd.setPixel(0,25);
-    lcd.setPixel(1,25);
-    lcd.setPixel(2,25);
-    lcd.setPixel(3,25);
-    lcd.setPixel(6,25);
-    lcd.setPixel(7,25);
-    lcd.setPixel(8,25);
-    lcd.setPixel(9,25);
-    lcd.setPixel(10,25);
-    lcd.setPixel(12,25);
-    lcd.setPixel(13,25);
-    lcd.setPixel(0,26);
-    lcd.setPixel(1,26);
-    lcd.setPixel(2,26);
-    lcd.setPixel(3,26);
-    lcd.setPixel(4,26);
-    lcd.setPixel(6,26);
-    lcd.setPixel(7,26);
-    lcd.setPixel(8,26);
-    lcd.setPixel(9,26);
-    lcd.setPixel(10,26);
-    lcd.setPixel(12,26);
-    lcd.setPixel(13,26);
-    lcd.setPixel(1,27);
-    lcd.setPixel(2,27);
-    lcd.setPixel(3,27);
-    lcd.setPixel(4,27);
-    lcd.setPixel(5,27);
-    lcd.setPixel(6,27);
-    lcd.setPixel(7,27);
-    lcd.setPixel(8,27);
-    lcd.setPixel(9,27);
-    lcd.setPixel(10,27);
-    lcd.setPixel(11,27);
-    lcd.setPixel(12,27);
-    lcd.setPixel(13,27);
-    lcd.setPixel(4,28);
-    lcd.setPixel(5,28);
-    lcd.setPixel(6,28);
-    lcd.setPixel(10,28);
-    lcd.setPixel(11,28);
-    lcd.setPixel(12,28);
-    lcd.setPixel(4,29);
-    lcd.setPixel(5,29);
-    lcd.setPixel(6,29);
-    lcd.setPixel(10,29);
-    lcd.setPixel(11,29);
-    lcd.setPixel(12,29);
+    lcd.setPixel(4,19);lcd.setPixel(5,19);lcd.setPixel(6,19);lcd.setPixel(10,19);lcd.setPixel(11,19);lcd.setPixel(12,19);
+    lcd.setPixel(4,20);lcd.setPixel(5,20);lcd.setPixel(6,20);lcd.setPixel(10,20);lcd.setPixel(11,20);lcd.setPixel(12,20);
+    lcd.setPixel(1,21);lcd.setPixel(2,21);lcd.setPixel(3,21);lcd.setPixel(4,21);lcd.setPixel(5,21);lcd.setPixel(6,21);
+    lcd.setPixel(7,21);lcd.setPixel(8,21);lcd.setPixel(9,21);lcd.setPixel(10,21);lcd.setPixel(11,21);lcd.setPixel(12,21);
+    lcd.setPixel(13,21);lcd.setPixel(0,22);lcd.setPixel(1,22);lcd.setPixel(2,22);lcd.setPixel(3,22);lcd.setPixel(4,22);
+    lcd.setPixel(6,22);lcd.setPixel(7,22);lcd.setPixel(8,22);lcd.setPixel(9,22);lcd.setPixel(10,22);lcd.setPixel(12,22);
+    lcd.setPixel(13,22);lcd.setPixel(0,23);lcd.setPixel(1,23);lcd.setPixel(2,23);lcd.setPixel(3,23);lcd.setPixel(6,23);
+    lcd.setPixel(7,23);lcd.setPixel(8,23);lcd.setPixel(9,23);lcd.setPixel(10,23);lcd.setPixel(12,23);lcd.setPixel(13,23);
+    lcd.setPixel(0,24);lcd.setPixel(1,24);lcd.setPixel(2,24);lcd.setPixel(3,24);lcd.setPixel(6,24);lcd.setPixel(7,24);
+    lcd.setPixel(8,24);lcd.setPixel(9,24);lcd.setPixel(10,24);lcd.setPixel(12,24);lcd.setPixel(13,24);lcd.setPixel(0,25);
+    lcd.setPixel(1,25);lcd.setPixel(2,25);lcd.setPixel(3,25);lcd.setPixel(6,25);lcd.setPixel(7,25);lcd.setPixel(8,25);
+    lcd.setPixel(9,25);lcd.setPixel(10,25);lcd.setPixel(12,25);lcd.setPixel(13,25);lcd.setPixel(0,26);lcd.setPixel(1,26);
+    lcd.setPixel(2,26);lcd.setPixel(3,26);lcd.setPixel(4,26);lcd.setPixel(6,26);lcd.setPixel(7,26);lcd.setPixel(8,26);
+    lcd.setPixel(9,26);lcd.setPixel(10,26);lcd.setPixel(12,26);lcd.setPixel(13,26);lcd.setPixel(1,27);lcd.setPixel(2,27);
+    lcd.setPixel(3,27);lcd.setPixel(4,27);lcd.setPixel(5,27);lcd.setPixel(6,27);lcd.setPixel(7,27);lcd.setPixel(8,27);
+    lcd.setPixel(9,27);lcd.setPixel(10,27);lcd.setPixel(11,27);lcd.setPixel(12,27);lcd.setPixel(13,27);lcd.setPixel(4,28);
+    lcd.setPixel(5,28);lcd.setPixel(6,28);lcd.setPixel(10,28);lcd.setPixel(11,28);lcd.setPixel(12,28);lcd.setPixel(4,29);
+    lcd.setPixel(5,29);lcd.setPixel(6,29);lcd.setPixel(10,29);lcd.setPixel(11,29);lcd.setPixel(12,29);
 }
 
 void visToggle()
 {
-    Vstate = Vfsm[Vstate].nextState[VisTog]; // read input and update current state
-    visual = Vfsm[Vstate].visual; // set output depending on current state
+    Vstate = Vfsm[Vstate].nextState[VisTog]; /// read input and update current state
+    visual = Vfsm[Vstate].visual; /// set output depending on current state
     wait(0.2);
     if(visual==0) {
         pc.printf("----Left to Right----\n\r");
@@ -546,4 +455,9 @@
     if(visual==3) {
         pc.printf("----Numbers----\n\r");
     }
+}
+
+int semihost_powerdown(){
+    uint32_t arg;
+    return __semihost(USR_POWERDOWN, &arg);
 }
\ No newline at end of file