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: N5110WN PowerControl SRF02 mbed
main.cpp@8:ffcde84b8cf3, 2015-04-30 (annotated)
- Committer:
- JakBlackburn
- Date:
- Thu Apr 30 17:12:41 2015 +0000
- Revision:
- 8:ffcde84b8cf3
- Parent:
- 7:962b5f044a9b
- Child:
- 9:163159830eaf
updated documentation;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| JakBlackburn | 1:7f151ab172cb | 1 | /** |
| JakBlackburn | 1:7f151ab172cb | 2 | @file main.cpp |
| JakBlackburn | 1:7f151ab172cb | 3 | @brief Program implementation |
| JakBlackburn | 0:73bfbe8729d4 | 4 | |
| JakBlackburn | 1:7f151ab172cb | 5 | */ |
| JakBlackburn | 1:7f151ab172cb | 6 | #include "main.h" |
| JakBlackburn | 0:73bfbe8729d4 | 7 | |
| JakBlackburn | 3:00933efbe463 | 8 | int main() |
| JakBlackburn | 3:00933efbe463 | 9 | { |
| JakBlackburn | 7:962b5f044a9b | 10 | PHY_PowerDown(); |
| JakBlackburn | 8:ffcde84b8cf3 | 11 | //semihost_powerdown(); |
| JakBlackburn | 7:962b5f044a9b | 12 | pc.baud(9600); ///sets baud rate |
| JakBlackburn | 7:962b5f044a9b | 13 | timer.attach(&timerExpired,0.2); /// Sets the speed of the timer and attaches a function to run |
| JakBlackburn | 7:962b5f044a9b | 14 | timer2.attach(&timer2Expired,1); /// Sets the speed of the timer and attaches a function to run |
| JakBlackburn | 7:962b5f044a9b | 15 | timer3.attach(&timer3Expired,2); /// Sets the speed of the timer and attaches a function to run |
| JakBlackburn | 7:962b5f044a9b | 16 | LogTog.rise(&logToggle); /// attaches a function to the rise of the LogTog button |
| JakBlackburn | 7:962b5f044a9b | 17 | UnitTog.rise(&unitToggle); /// attaches a function to the rise of the UnitTog button |
| JakBlackburn | 7:962b5f044a9b | 18 | VisTog.rise(&visToggle); /// attaches a function to the rise of the VisTog button |
| JakBlackburn | 7:962b5f044a9b | 19 | pc.attach(&serialISR); /// attach serial ISR |
| JakBlackburn | 7:962b5f044a9b | 20 | BLED.period(0.02); ///sets the frequency of the Backlight at 50Hz |
| JakBlackburn | 7:962b5f044a9b | 21 | BLED=BLEDLevel.read(); ///sets the brightness |
| JakBlackburn | 8:ffcde84b8cf3 | 22 | lcd.init(); ///initilizes the display |
| JakBlackburn | 7:962b5f044a9b | 23 | introTune(); /// plays the intro tune |
| JakBlackburn | 8:ffcde84b8cf3 | 24 | lcd.printString("---+--+--+---",3,0);/// Displays an introduction message |
| JakBlackburn | 7:962b5f044a9b | 25 | lcd.printString("Distance",18,1); |
| JakBlackburn | 7:962b5f044a9b | 26 | lcd.printString("Sensor",25,2); |
| JakBlackburn | 7:962b5f044a9b | 27 | lcd.printString("By Jakobi",16,3); |
| JakBlackburn | 2:a1eaa4d74b63 | 28 | lcd.printString("Blackburn",16,4); |
| JakBlackburn | 7:962b5f044a9b | 29 | lcd.printString("---+--+--+---",3,5); |
| JakBlackburn | 3:00933efbe463 | 30 | wait(2); |
| JakBlackburn | 7:962b5f044a9b | 31 | lcd.clear(); /// clears the opening message |
| JakBlackburn | 0:73bfbe8729d4 | 32 | while(1) { |
| JakBlackburn | 7:962b5f044a9b | 33 | if (setTimeFlag) { /// if updated time has been sent |
| JakBlackburn | 7:962b5f044a9b | 34 | setTimeFlag = 0; /// clear flag |
| JakBlackburn | 7:962b5f044a9b | 35 | setTime(); /// update time |
| JakBlackburn | 1:7f151ab172cb | 36 | } |
| JakBlackburn | 7:962b5f044a9b | 37 | if(distance>=100) { /// uses the 2 second timer if the distance is above 100 |
| JakBlackburn | 7:962b5f044a9b | 38 | if(timer3Flag) { /// 2 second timer's flag |
| JakBlackburn | 7:962b5f044a9b | 39 | pc.printf("Scan Speed = 0.5 \n"); /// prints the scan speed across the serial port |
| JakBlackburn | 7:962b5f044a9b | 40 | theMain(); /// Contains the main code |
| JakBlackburn | 5:f9b97f057836 | 41 | |
| JakBlackburn | 5:f9b97f057836 | 42 | } |
| JakBlackburn | 7:962b5f044a9b | 43 | } else if(distance>=50) { /// uses the 1 second timer if the distance is above 50 and less than 100 |
| JakBlackburn | 7:962b5f044a9b | 44 | if(timer2Flag) {/// 1 second timer's flag |
| JakBlackburn | 7:962b5f044a9b | 45 | pc.printf("Scan Speed = 1 \n"); /// prints the scan speed across the serial port |
| JakBlackburn | 7:962b5f044a9b | 46 | theMain(); /// Contains the main code |
| JakBlackburn | 5:f9b97f057836 | 47 | } |
| JakBlackburn | 7:962b5f044a9b | 48 | } else if(distance<50) { /// uses the 0.2 second timer if the distance is less than 50 |
| JakBlackburn | 7:962b5f044a9b | 49 | if(timerFlag) {/// 0.2 second timer's flag |
| JakBlackburn | 7:962b5f044a9b | 50 | pc.printf("Scan Speed = 5 \n"); /// prints the scan speed across the serial port |
| JakBlackburn | 7:962b5f044a9b | 51 | theMain(); /// Contains the main code |
| JakBlackburn | 5:f9b97f057836 | 52 | } |
| JakBlackburn | 8:ffcde84b8cf3 | 53 | } else { /// otherwise |
| JakBlackburn | 8:ffcde84b8cf3 | 54 | error(1); /// displays an error message across the mBed leds |
| JakBlackburn | 5:f9b97f057836 | 55 | } |
| JakBlackburn | 5:f9b97f057836 | 56 | |
| JakBlackburn | 8:ffcde84b8cf3 | 57 | Sleep();/// Whilst the code is not doing anything else, it will sleep |
| JakBlackburn | 0:73bfbe8729d4 | 58 | } |
| JakBlackburn | 0:73bfbe8729d4 | 59 | } |
| JakBlackburn | 0:73bfbe8729d4 | 60 | |
| JakBlackburn | 3:00933efbe463 | 61 | void timerExpired() |
| JakBlackburn | 3:00933efbe463 | 62 | { |
| JakBlackburn | 8:ffcde84b8cf3 | 63 | timerFlag=1; /// sets the timer flag high |
| JakBlackburn | 0:73bfbe8729d4 | 64 | } |
| JakBlackburn | 0:73bfbe8729d4 | 65 | |
| JakBlackburn | 5:f9b97f057836 | 66 | void timer2Expired() |
| JakBlackburn | 5:f9b97f057836 | 67 | { |
| JakBlackburn | 8:ffcde84b8cf3 | 68 | timer2Flag=1;/// sets the timer flag high |
| JakBlackburn | 5:f9b97f057836 | 69 | } |
| JakBlackburn | 5:f9b97f057836 | 70 | void timer3Expired() |
| JakBlackburn | 5:f9b97f057836 | 71 | { |
| JakBlackburn | 8:ffcde84b8cf3 | 72 | timer3Flag=1;/// sets the timer flag high |
| JakBlackburn | 5:f9b97f057836 | 73 | } |
| JakBlackburn | 5:f9b97f057836 | 74 | |
| JakBlackburn | 5:f9b97f057836 | 75 | void theMain() |
| JakBlackburn | 5:f9b97f057836 | 76 | { |
| JakBlackburn | 7:962b5f044a9b | 77 | BLED=BLEDLevel.read(); ///sets the brightness |
| JakBlackburn | 8:ffcde84b8cf3 | 78 | timerFlag =0; ///resets the timer flags |
| JakBlackburn | 5:f9b97f057836 | 79 | timer2Flag=0; |
| JakBlackburn | 5:f9b97f057836 | 80 | timer3Flag=0; |
| JakBlackburn | 8:ffcde84b8cf3 | 81 | time_t seconds = time(NULL); /// gets the current time |
| JakBlackburn | 8:ffcde84b8cf3 | 82 | /// formats that time into a string (time and date) |
| JakBlackburn | 5:f9b97f057836 | 83 | strftime(buffer, 30 , "%d/%m/%y %R", localtime(&seconds)); |
| JakBlackburn | 7:962b5f044a9b | 84 | float dist = getDistance()*unitX; ///reads the distance and sets it to a float value. |
| JakBlackburn | 7:962b5f044a9b | 85 | ///prints the value to the serial port. |
| JakBlackburn | 8:ffcde84b8cf3 | 86 | if(state==1) { |
| JakBlackburn | 8:ffcde84b8cf3 | 87 | pc.printf("%s , Distance = %.2f %s \n",buffer,dist,units); |
| JakBlackburn | 5:f9b97f057836 | 88 | } else if(state !=1) { |
| JakBlackburn | 8:ffcde84b8cf3 | 89 | pc.printf("%s , Distance = %.0f %s \n",buffer,dist,units); |
| JakBlackburn | 5:f9b97f057836 | 90 | } |
| JakBlackburn | 8:ffcde84b8cf3 | 91 | logging(buffer,distance); /// runs the logging function |
| JakBlackburn | 8:ffcde84b8cf3 | 92 | if(visual==0) { /// if the visual toggle is set in the first mode, |
| JakBlackburn | 8:ffcde84b8cf3 | 93 | LCDVis0(); /// runs the first Visual function, Changing the Display |
| JakBlackburn | 8:ffcde84b8cf3 | 94 | } else if(visual==1) {/// if the visual toggle is set in the second mode, |
| JakBlackburn | 8:ffcde84b8cf3 | 95 | LCDVis1(); /// runs the second Visual function, Changing the Display |
| JakBlackburn | 8:ffcde84b8cf3 | 96 | } else if(visual==2) {/// if the visual toggle is set in the third mode, |
| JakBlackburn | 8:ffcde84b8cf3 | 97 | LCDVis2(); /// runs the third Visual function, Changing the Display |
| JakBlackburn | 8:ffcde84b8cf3 | 98 | } else if(visual==3) {/// if the visual toggle is set in the forth mode, |
| JakBlackburn | 8:ffcde84b8cf3 | 99 | LCDVis3(); /// runs the forth Visual function, Changing the Display |
| JakBlackburn | 8:ffcde84b8cf3 | 100 | } else { /// if there is no Visual mode selected then |
| JakBlackburn | 8:ffcde84b8cf3 | 101 | error(2); /// there is an error and so the error function runs |
| JakBlackburn | 5:f9b97f057836 | 102 | } |
| JakBlackburn | 5:f9b97f057836 | 103 | } |
| JakBlackburn | 5:f9b97f057836 | 104 | |
| JakBlackburn | 0:73bfbe8729d4 | 105 | |
| JakBlackburn | 3:00933efbe463 | 106 | void warnings() |
| JakBlackburn | 3:00933efbe463 | 107 | { |
| JakBlackburn | 8:ffcde84b8cf3 | 108 | WLED=1;/// Warning LED is turned on |
| JakBlackburn | 8:ffcde84b8cf3 | 109 | volume= BuzVol.read(); /// sets the volume of the buzzer |
| JakBlackburn | 8:ffcde84b8cf3 | 110 | if(distance<20) { /// if the distance is less than 20 cm |
| JakBlackburn | 8:ffcde84b8cf3 | 111 | Buzzer.ContNote(1000.0, volume); /// uses the new member to create a continuous note |
| JakBlackburn | 8:ffcde84b8cf3 | 112 | ///(new member was written and added to speaker.h) |
| JakBlackburn | 6:a5cd7eb72264 | 113 | |
| JakBlackburn | 8:ffcde84b8cf3 | 114 | } else {/// if the distance isnt less than 20 |
| JakBlackburn | 8:ffcde84b8cf3 | 115 | Buzzer.PlayNote(1000.0, 0.1, volume);/// plays a note for 0.1 seconds |
| JakBlackburn | 8:ffcde84b8cf3 | 116 | WLED=0;/// and turns off the warning LED (creating a beep and a flash) |
| JakBlackburn | 5:f9b97f057836 | 117 | } |
| JakBlackburn | 0:73bfbe8729d4 | 118 | } |
| JakBlackburn | 8:ffcde84b8cf3 | 119 | |
| JakBlackburn | 3:00933efbe463 | 120 | float getDistance() |
| JakBlackburn | 3:00933efbe463 | 121 | { |
| JakBlackburn | 8:ffcde84b8cf3 | 122 | int dist0 = sensor.getDistanceCm(); /// gets the distance 10 times |
| JakBlackburn | 0:73bfbe8729d4 | 123 | int dist1 = sensor.getDistanceCm(); |
| JakBlackburn | 0:73bfbe8729d4 | 124 | int dist2 = sensor.getDistanceCm(); |
| JakBlackburn | 0:73bfbe8729d4 | 125 | int dist3 = sensor.getDistanceCm(); |
| JakBlackburn | 0:73bfbe8729d4 | 126 | int dist4 = sensor.getDistanceCm(); |
| JakBlackburn | 0:73bfbe8729d4 | 127 | int dist5 = sensor.getDistanceCm(); |
| JakBlackburn | 8:ffcde84b8cf3 | 128 | int dist6 = sensor.getDistanceCm(); |
| JakBlackburn | 8:ffcde84b8cf3 | 129 | int dist7 = sensor.getDistanceCm(); |
| JakBlackburn | 8:ffcde84b8cf3 | 130 | int dist8 = sensor.getDistanceCm(); |
| JakBlackburn | 8:ffcde84b8cf3 | 131 | int dist9 = sensor.getDistanceCm(); |
| JakBlackburn | 8:ffcde84b8cf3 | 132 | distance=(dist0+dist1+dist2+dist3+dist4+dist5+dist6+dist7+dist8+dist9)/10;/// averages those distances |
| JakBlackburn | 5:f9b97f057836 | 133 | |
| JakBlackburn | 8:ffcde84b8cf3 | 134 | warnings(); /// runs the warnings function |
| JakBlackburn | 8:ffcde84b8cf3 | 135 | return distance; /// returns the average distance |
| JakBlackburn | 0:73bfbe8729d4 | 136 | } |
| JakBlackburn | 0:73bfbe8729d4 | 137 | |
| JakBlackburn | 3:00933efbe463 | 138 | void unitToggle() |
| JakBlackburn | 3:00933efbe463 | 139 | { |
| JakBlackburn | 8:ffcde84b8cf3 | 140 | state = fsm[state].nextState[UnitTog]; /// reads the input and updates the current state |
| JakBlackburn | 8:ffcde84b8cf3 | 141 | unitX = fsm[state].unitMultiple; /// sets the unit multiple depending on the current state |
| JakBlackburn | 8:ffcde84b8cf3 | 142 | units= fsm[state].Unit; /// sets the unit depending on the current state |
| JakBlackburn | 1:7f151ab172cb | 143 | wait(0.2); |
| JakBlackburn | 1:7f151ab172cb | 144 | pc.printf("unit multiple = %.2f \n",unitX); |
| JakBlackburn | 2:a1eaa4d74b63 | 145 | pc.printf("Units = %s \n",units); |
| JakBlackburn | 1:7f151ab172cb | 146 | } |
| JakBlackburn | 1:7f151ab172cb | 147 | |
| JakBlackburn | 5:f9b97f057836 | 148 | |
| JakBlackburn | 8:ffcde84b8cf3 | 149 | void error(int E) /// has up to 14 possible errors. |
| JakBlackburn | 8:ffcde84b8cf3 | 150 | { ///If a 15 is shown it is a ack bit error for the SRF02 |
| JakBlackburn | 8:ffcde84b8cf3 | 151 | while(1) { /// loops flashing the error number on the onboard LED's |
| JakBlackburn | 1:7f151ab172cb | 152 | leds=0; |
| JakBlackburn | 1:7f151ab172cb | 153 | wait(0.2); |
| JakBlackburn | 1:7f151ab172cb | 154 | leds=E; |
| JakBlackburn | 1:7f151ab172cb | 155 | wait(0.2); |
| JakBlackburn | 1:7f151ab172cb | 156 | } |
| JakBlackburn | 0:73bfbe8729d4 | 157 | } |
| JakBlackburn | 0:73bfbe8729d4 | 158 | |
| JakBlackburn | 4:2e9aa626a02c | 159 | void logToggle() |
| JakBlackburn | 1:7f151ab172cb | 160 | { |
| JakBlackburn | 8:ffcde84b8cf3 | 161 | logButtonFlag = !logButtonFlag; ///flips the flag |
| JakBlackburn | 8:ffcde84b8cf3 | 162 | if(logButtonFlag) { /// if flag is high the device is logging so |
| JakBlackburn | 8:ffcde84b8cf3 | 163 | LLED=1; /// Logging LED will be on |
| JakBlackburn | 8:ffcde84b8cf3 | 164 | pc.printf("-------\n Data Logging\n\r");/// prints a message to the serial port |
| JakBlackburn | 8:ffcde84b8cf3 | 165 | } else { /// if flag is low the device isn't logging so |
| JakBlackburn | 8:ffcde84b8cf3 | 166 | LLED=0; /// Logging LED is turned off |
| JakBlackburn | 8:ffcde84b8cf3 | 167 | pc.printf("-------\n Stopped Logging Data\n\r");/// prints a message to the serial port |
| JakBlackburn | 0:73bfbe8729d4 | 168 | } |
| JakBlackburn | 1:7f151ab172cb | 169 | wait(0.2); |
| JakBlackburn | 1:7f151ab172cb | 170 | } |
| JakBlackburn | 1:7f151ab172cb | 171 | |
| JakBlackburn | 1:7f151ab172cb | 172 | void logging(char* data,float data1) |
| JakBlackburn | 1:7f151ab172cb | 173 | { |
| JakBlackburn | 8:ffcde84b8cf3 | 174 | if(logButtonFlag) { //if the logging flag is high |
| JakBlackburn | 8:ffcde84b8cf3 | 175 | FILE *fp = fopen("/local/log.csv", "a"); /// opens 'log.csv' for appending |
| JakBlackburn | 7:962b5f044a9b | 176 | /// if the file doesn't exist it is created, if it exists, data is appended to the end |
| JakBlackburn | 7:962b5f044a9b | 177 | fprintf(fp," %s , %.2f\n ",data,data1); /// print string to file |
| JakBlackburn | 7:962b5f044a9b | 178 | fclose(fp); /// close file |
| JakBlackburn | 8:ffcde84b8cf3 | 179 | pc.printf("Data Logged\n");/// prints a message to the serial port |
| JakBlackburn | 7:962b5f044a9b | 180 | LLED=1; /// keeps the led on if it is logging |
| JakBlackburn | 8:ffcde84b8cf3 | 181 | } else {/// otherwise |
| JakBlackburn | 8:ffcde84b8cf3 | 182 | pc.printf("WARNING : Unlogged Data\n");/// prints a message to the serial port |
| JakBlackburn | 8:ffcde84b8cf3 | 183 | LLED=0; ///sets the logging LED to off |
| JakBlackburn | 0:73bfbe8729d4 | 184 | } |
| JakBlackburn | 1:7f151ab172cb | 185 | } |
| JakBlackburn | 1:7f151ab172cb | 186 | |
| JakBlackburn | 1:7f151ab172cb | 187 | void setTime() |
| JakBlackburn | 1:7f151ab172cb | 188 | { |
| JakBlackburn | 8:ffcde84b8cf3 | 189 | /// prints Unix time for debugging |
| JakBlackburn | 8:ffcde84b8cf3 | 190 | pc.printf("\n \r Unix set time - %s\n",rxString); |
| JakBlackburn | 7:962b5f044a9b | 191 | /// atoi() converts a string to an integer |
| JakBlackburn | 1:7f151ab172cb | 192 | int time = atoi(rxString); |
| JakBlackburn | 7:962b5f044a9b | 193 | /// update the time |
| JakBlackburn | 1:7f151ab172cb | 194 | set_time(time); |
| JakBlackburn | 8:ffcde84b8cf3 | 195 | pc.printf("Set time - %s \n \r ",buffer);///prints the time to the serial port |
| JakBlackburn | 1:7f151ab172cb | 196 | } |
| JakBlackburn | 1:7f151ab172cb | 197 | void serialISR() |
| JakBlackburn | 1:7f151ab172cb | 198 | { |
| JakBlackburn | 7:962b5f044a9b | 199 | /// when a serial interrupt occurs, read rx string into buffer |
| JakBlackburn | 1:7f151ab172cb | 200 | pc.gets(rxString,16); |
| JakBlackburn | 7:962b5f044a9b | 201 | /// set flag |
| JakBlackburn | 1:7f151ab172cb | 202 | setTimeFlag = 1; |
| JakBlackburn | 2:a1eaa4d74b63 | 203 | } |
| JakBlackburn | 2:a1eaa4d74b63 | 204 | |
| JakBlackburn | 2:a1eaa4d74b63 | 205 | void introTune() |
| JakBlackburn | 8:ffcde84b8cf3 | 206 | {/// plays several notes to create a tune |
| JakBlackburn | 2:a1eaa4d74b63 | 207 | Buzzer.PlayNote(1319.0,0.084,1.0); |
| JakBlackburn | 2:a1eaa4d74b63 | 208 | wait(0.01); |
| JakBlackburn | 2:a1eaa4d74b63 | 209 | Buzzer.PlayNote(1319.0,0.084,1.0); |
| JakBlackburn | 2:a1eaa4d74b63 | 210 | wait(0.094); |
| JakBlackburn | 2:a1eaa4d74b63 | 211 | Buzzer.PlayNote(1319.0,0.084,1.0); |
| JakBlackburn | 2:a1eaa4d74b63 | 212 | wait(0.094); |
| JakBlackburn | 2:a1eaa4d74b63 | 213 | Buzzer.PlayNote(1047.0,0.084,1.0); |
| JakBlackburn | 2:a1eaa4d74b63 | 214 | wait(0.01); |
| JakBlackburn | 2:a1eaa4d74b63 | 215 | Buzzer.PlayNote(1319.0,0.084,1.0); |
| JakBlackburn | 2:a1eaa4d74b63 | 216 | wait(0.094); |
| JakBlackburn | 2:a1eaa4d74b63 | 217 | Buzzer.PlayNote(1568.0,0.084,1.0); |
| JakBlackburn | 2:a1eaa4d74b63 | 218 | wait(0.26); |
| JakBlackburn | 2:a1eaa4d74b63 | 219 | Buzzer.PlayNote(784.0,0.084,1.0); |
| JakBlackburn | 2:a1eaa4d74b63 | 220 | } |
| JakBlackburn | 2:a1eaa4d74b63 | 221 | |
| JakBlackburn | 3:00933efbe463 | 222 | |
| JakBlackburn | 8:ffcde84b8cf3 | 223 | void LCDVis0() /// the max range is 249 (according to the data sheet) |
| JakBlackburn | 8:ffcde84b8cf3 | 224 | {///and there are 84 pixels wide on the LCD so this can be used to map the amount of cm for each pixel |
| JakBlackburn | 8:ffcde84b8cf3 | 225 | lcd.clear();/// clears the screen |
| JakBlackburn | 8:ffcde84b8cf3 | 226 | if(logButtonFlag) { /// if the logging flag is high |
| JakBlackburn | 8:ffcde84b8cf3 | 227 | lcd.printString("-L-",0,5); /// prints an L onto the botton left of the screen |
| JakBlackburn | 6:a5cd7eb72264 | 228 | } |
| JakBlackburn | 6:a5cd7eb72264 | 229 | |
| JakBlackburn | 7:962b5f044a9b | 230 | int I = distance*0.337; /// and then multiplies the current distance by that value as (84/249) =0.337... |
| JakBlackburn | 6:a5cd7eb72264 | 231 | I=I+3; |
| JakBlackburn | 6:a5cd7eb72264 | 232 | if(I>=79){I=79;} |
| JakBlackburn | 8:ffcde84b8cf3 | 233 | pc.printf("bar width - %d pixels \n",I);/// prints the width to the serial port |
| JakBlackburn | 8:ffcde84b8cf3 | 234 | lcd.drawRect(0,16,83,16,0); ///draws a frame for the bar |
| JakBlackburn | 8:ffcde84b8cf3 | 235 | lcd.drawRect(2,18,I,12,1); /// draws a rectangle inside the frame at the calculated width |
| JakBlackburn | 8:ffcde84b8cf3 | 236 | if(state==1) { |
| JakBlackburn | 8:ffcde84b8cf3 | 237 | sprintf(Dbuffer, "%.2f", distance*unitX); /// prints the value to the serial port. |
| JakBlackburn | 8:ffcde84b8cf3 | 238 | } else if(state !=1) { |
| JakBlackburn | 8:ffcde84b8cf3 | 239 | sprintf(Dbuffer, "%.0f", distance*unitX); |
| JakBlackburn | 8:ffcde84b8cf3 | 240 | } |
| JakBlackburn | 8:ffcde84b8cf3 | 241 | lcd.printString (buffer,0,0);/// prints the time and date at the top of the screen |
| JakBlackburn | 8:ffcde84b8cf3 | 242 | lcd.printString (Dbuffer,26,5); /// prints the distance |
| JakBlackburn | 8:ffcde84b8cf3 | 243 | lcd.printString (units,50,5); /// and the units |
| JakBlackburn | 8:ffcde84b8cf3 | 244 | lcd.refresh();/// it then refreshes the screen |
| JakBlackburn | 8:ffcde84b8cf3 | 245 | } |
| JakBlackburn | 8:ffcde84b8cf3 | 246 | |
| JakBlackburn | 8:ffcde84b8cf3 | 247 | void LCDVis1() /// the max range is 249 (according to the data sheet) |
| JakBlackburn | 8:ffcde84b8cf3 | 248 | {///and there are 48 pixels high on the LCD so this can be used to map the amount of cm for each pixel |
| JakBlackburn | 8:ffcde84b8cf3 | 249 | lcd.clear(); /// clears the screen |
| JakBlackburn | 8:ffcde84b8cf3 | 250 | if(logButtonFlag) {/// if the logging flag is high |
| JakBlackburn | 8:ffcde84b8cf3 | 251 | lcd.printString("-L-",0,5);/// prints an L onto the botton left of the screen |
| JakBlackburn | 8:ffcde84b8cf3 | 252 | } |
| JakBlackburn | 8:ffcde84b8cf3 | 253 | |
| JakBlackburn | 8:ffcde84b8cf3 | 254 | int J = distance*0.14; /// and then multiplies the current distance by that value as (36/249) = 0.14..(as top 12 pixels are for time) |
| JakBlackburn | 8:ffcde84b8cf3 | 255 | if(J>=36) { |
| JakBlackburn | 8:ffcde84b8cf3 | 256 | J=36; |
| JakBlackburn | 8:ffcde84b8cf3 | 257 | } |
| JakBlackburn | 8:ffcde84b8cf3 | 258 | pc.printf("bar height - %d pixels \n",J);// prints the height to the serial port |
| JakBlackburn | 8:ffcde84b8cf3 | 259 | lcd.drawRect(24,9,34,38,0);///draws a frame for the bar |
| JakBlackburn | 8:ffcde84b8cf3 | 260 | lcd.drawRect(26,47-J,30,J-2,1);/// draws a rectangle inside the frame at the calculated height |
| JakBlackburn | 8:ffcde84b8cf3 | 261 | if(state==1) { |
| JakBlackburn | 8:ffcde84b8cf3 | 262 | sprintf(Dbuffer, "%.2f", distance*unitX); /// prints the value to the serial port. |
| JakBlackburn | 8:ffcde84b8cf3 | 263 | } else if(state !=1) { |
| JakBlackburn | 8:ffcde84b8cf3 | 264 | sprintf(Dbuffer, "%.0f", distance*unitX); |
| JakBlackburn | 8:ffcde84b8cf3 | 265 | } |
| JakBlackburn | 8:ffcde84b8cf3 | 266 | lcd.printString (buffer,0,0);/// prints the time and date at the top of the screen |
| JakBlackburn | 8:ffcde84b8cf3 | 267 | lcd.printString (Dbuffer,60,3);/// prints the distance |
| JakBlackburn | 8:ffcde84b8cf3 | 268 | lcd.printString (units,64,4);/// and the units |
| JakBlackburn | 8:ffcde84b8cf3 | 269 | lcd.refresh(); /// it then refreshes the screen |
| JakBlackburn | 8:ffcde84b8cf3 | 270 | } |
| JakBlackburn | 8:ffcde84b8cf3 | 271 | |
| JakBlackburn | 8:ffcde84b8cf3 | 272 | void LCDVis2() |
| JakBlackburn | 8:ffcde84b8cf3 | 273 | { |
| JakBlackburn | 8:ffcde84b8cf3 | 274 | lcd.clear();/// clears the screen |
| JakBlackburn | 8:ffcde84b8cf3 | 275 | if(logButtonFlag) {/// if the logging flag is high |
| JakBlackburn | 8:ffcde84b8cf3 | 276 | lcd.printString("-L-",0,5);/// prints an L onto the botton left of the screen |
| JakBlackburn | 8:ffcde84b8cf3 | 277 | } |
| JakBlackburn | 8:ffcde84b8cf3 | 278 | car(); /// displays a small car shape |
| JakBlackburn | 8:ffcde84b8cf3 | 279 | int I = distance*0.277; /// and then multiplies the current distance by that value as (69/249) =0.277... |
| JakBlackburn | 8:ffcde84b8cf3 | 280 | if(I>=84) { |
| JakBlackburn | 8:ffcde84b8cf3 | 281 | I=84; |
| JakBlackburn | 8:ffcde84b8cf3 | 282 | } |
| JakBlackburn | 4:2e9aa626a02c | 283 | pc.printf("bar width - %d pixels \n",I); |
| JakBlackburn | 8:ffcde84b8cf3 | 284 | lcd.drawRect(I+15,9,69,29,1); /// draws a rectangle inside the frame at the calculated width |
| JakBlackburn | 6:a5cd7eb72264 | 285 | if(state==1) { |
| JakBlackburn | 7:962b5f044a9b | 286 | sprintf(Dbuffer, "%.2f", distance*unitX); /// prints the value to the serial port. |
| JakBlackburn | 6:a5cd7eb72264 | 287 | } else if(state !=1) { |
| JakBlackburn | 7:962b5f044a9b | 288 | sprintf(Dbuffer, "%.0f", distance*unitX); /// prints the value to the serial port. |
| JakBlackburn | 6:a5cd7eb72264 | 289 | } |
| JakBlackburn | 8:ffcde84b8cf3 | 290 | lcd.printString (buffer,0,0);/// prints the time and date at the top of the screen |
| JakBlackburn | 8:ffcde84b8cf3 | 291 | lcd.printString (Dbuffer,26,5);///prints the distance |
| JakBlackburn | 8:ffcde84b8cf3 | 292 | lcd.printString (units,50,5);///and the units |
| JakBlackburn | 8:ffcde84b8cf3 | 293 | lcd.refresh(); /// it then refreshes the screen |
| JakBlackburn | 4:2e9aa626a02c | 294 | } |
| JakBlackburn | 5:f9b97f057836 | 295 | void LCDVis3() |
| JakBlackburn | 5:f9b97f057836 | 296 | { |
| JakBlackburn | 8:ffcde84b8cf3 | 297 | lcd.clear(); /// clears the screen |
| JakBlackburn | 8:ffcde84b8cf3 | 298 | if(logButtonFlag) {/// if the logging flag is high |
| JakBlackburn | 8:ffcde84b8cf3 | 299 | lcd.printString("-L-",0,5);/// prints an L onto the botton left of the screen |
| JakBlackburn | 6:a5cd7eb72264 | 300 | } |
| JakBlackburn | 6:a5cd7eb72264 | 301 | lcd.printString (buffer,0,0); |
| JakBlackburn | 8:ffcde84b8cf3 | 302 | int Dist=distance; /// assigns each digit of the distance |
| JakBlackburn | 8:ffcde84b8cf3 | 303 | if(state==0) { /// to an integer and then depending on the value of that integer |
| JakBlackburn | 8:ffcde84b8cf3 | 304 | lcd.printString("CM",54,3); /// prints a large number to a position on the screen |
| JakBlackburn | 8:ffcde84b8cf3 | 305 | } /// (large numbers designed and added to the N5110 library) |
| JakBlackburn | 5:f9b97f057836 | 306 | if(state==1) { |
| JakBlackburn | 5:f9b97f057836 | 307 | lcd.decimal(); |
| JakBlackburn | 5:f9b97f057836 | 308 | lcd.printString("M",54,3); |
| JakBlackburn | 4:2e9aa626a02c | 309 | } |
| JakBlackburn | 5:f9b97f057836 | 310 | if(state==2) { |
| JakBlackburn | 5:f9b97f057836 | 311 | lcd.number0(57); |
| JakBlackburn | 4:2e9aa626a02c | 312 | lcd.printString("MM",73,3); |
| JakBlackburn | 4:2e9aa626a02c | 313 | } |
| JakBlackburn | 4:2e9aa626a02c | 314 | int S=Dist %10; |
| JakBlackburn | 4:2e9aa626a02c | 315 | pc.printf("singles - %d \n",S); |
| JakBlackburn | 5:f9b97f057836 | 316 | if(S==0) { |
| JakBlackburn | 5:f9b97f057836 | 317 | lcd.number0(38); |
| JakBlackburn | 5:f9b97f057836 | 318 | } else if(S==1) { |
| JakBlackburn | 5:f9b97f057836 | 319 | lcd.number1(38); |
| JakBlackburn | 5:f9b97f057836 | 320 | } else if(S==2) { |
| JakBlackburn | 5:f9b97f057836 | 321 | lcd.number2(38); |
| JakBlackburn | 5:f9b97f057836 | 322 | } else if(S==3) { |
| JakBlackburn | 5:f9b97f057836 | 323 | lcd.number3(38); |
| JakBlackburn | 5:f9b97f057836 | 324 | } else if(S==4) { |
| JakBlackburn | 5:f9b97f057836 | 325 | lcd.number4(38); |
| JakBlackburn | 5:f9b97f057836 | 326 | } else if(S==5) { |
| JakBlackburn | 5:f9b97f057836 | 327 | lcd.number5(38); |
| JakBlackburn | 5:f9b97f057836 | 328 | } else if(S==6) { |
| JakBlackburn | 5:f9b97f057836 | 329 | lcd.number6(38); |
| JakBlackburn | 5:f9b97f057836 | 330 | } else if(S==7) { |
| JakBlackburn | 5:f9b97f057836 | 331 | lcd.number7(38); |
| JakBlackburn | 5:f9b97f057836 | 332 | } else if(S==8) { |
| JakBlackburn | 5:f9b97f057836 | 333 | lcd.number8(38); |
| JakBlackburn | 5:f9b97f057836 | 334 | } else if(S==9) { |
| JakBlackburn | 5:f9b97f057836 | 335 | lcd.number9(38); |
| JakBlackburn | 5:f9b97f057836 | 336 | } else { |
| JakBlackburn | 5:f9b97f057836 | 337 | error(4); |
| JakBlackburn | 3:00933efbe463 | 338 | } |
| JakBlackburn | 5:f9b97f057836 | 339 | |
| JakBlackburn | 4:2e9aa626a02c | 340 | int T=Dist/10 %10; |
| JakBlackburn | 5:f9b97f057836 | 341 | pc.printf("tens - %d \n",T); |
| JakBlackburn | 5:f9b97f057836 | 342 | if(T==0) { |
| JakBlackburn | 5:f9b97f057836 | 343 | lcd.number0(19); |
| JakBlackburn | 5:f9b97f057836 | 344 | } else if(T==1) { |
| JakBlackburn | 5:f9b97f057836 | 345 | lcd.number1(19); |
| JakBlackburn | 5:f9b97f057836 | 346 | } else if(T==2) { |
| JakBlackburn | 5:f9b97f057836 | 347 | lcd.number2(19); |
| JakBlackburn | 5:f9b97f057836 | 348 | } else if(T==3) { |
| JakBlackburn | 5:f9b97f057836 | 349 | lcd.number3(19); |
| JakBlackburn | 5:f9b97f057836 | 350 | } else if(T==4) { |
| JakBlackburn | 5:f9b97f057836 | 351 | lcd.number4(19); |
| JakBlackburn | 5:f9b97f057836 | 352 | } else if(T==5) { |
| JakBlackburn | 5:f9b97f057836 | 353 | lcd.number5(19); |
| JakBlackburn | 5:f9b97f057836 | 354 | } else if(T==6) { |
| JakBlackburn | 5:f9b97f057836 | 355 | lcd.number6(19); |
| JakBlackburn | 5:f9b97f057836 | 356 | } else if(T==7) { |
| JakBlackburn | 5:f9b97f057836 | 357 | lcd.number7(19); |
| JakBlackburn | 5:f9b97f057836 | 358 | } else if(T==8) { |
| JakBlackburn | 5:f9b97f057836 | 359 | lcd.number8(19); |
| JakBlackburn | 5:f9b97f057836 | 360 | } else if(T==9) { |
| JakBlackburn | 5:f9b97f057836 | 361 | lcd.number9(19); |
| JakBlackburn | 5:f9b97f057836 | 362 | } else { |
| JakBlackburn | 5:f9b97f057836 | 363 | error(4); |
| JakBlackburn | 4:2e9aa626a02c | 364 | } |
| JakBlackburn | 4:2e9aa626a02c | 365 | int H=Dist/100 %10; |
| JakBlackburn | 4:2e9aa626a02c | 366 | pc.printf("Hundreds - %d \n",H); |
| JakBlackburn | 5:f9b97f057836 | 367 | if(H==0) { |
| JakBlackburn | 8:ffcde84b8cf3 | 368 | if(state==1){ lcd.number0(0);} |
| JakBlackburn | 5:f9b97f057836 | 369 | } else if(H==1) { |
| JakBlackburn | 5:f9b97f057836 | 370 | lcd.number1(0); |
| JakBlackburn | 5:f9b97f057836 | 371 | } else if(H==2) { |
| JakBlackburn | 5:f9b97f057836 | 372 | lcd.number2(0); |
| JakBlackburn | 5:f9b97f057836 | 373 | } else if(H==3) { |
| JakBlackburn | 5:f9b97f057836 | 374 | lcd.number3(0); |
| JakBlackburn | 5:f9b97f057836 | 375 | } else if(H==4) { |
| JakBlackburn | 5:f9b97f057836 | 376 | lcd.number4(0); |
| JakBlackburn | 5:f9b97f057836 | 377 | } else if(H==5) { |
| JakBlackburn | 5:f9b97f057836 | 378 | lcd.number5(0); |
| JakBlackburn | 5:f9b97f057836 | 379 | } else if(H==6) { |
| JakBlackburn | 5:f9b97f057836 | 380 | lcd.number6(0); |
| JakBlackburn | 5:f9b97f057836 | 381 | } else if(H==7) { |
| JakBlackburn | 5:f9b97f057836 | 382 | lcd.number7(0); |
| JakBlackburn | 5:f9b97f057836 | 383 | } else if(H==8) { |
| JakBlackburn | 5:f9b97f057836 | 384 | lcd.number8(0); |
| JakBlackburn | 5:f9b97f057836 | 385 | } else if(H==9) { |
| JakBlackburn | 5:f9b97f057836 | 386 | lcd.number9(0); |
| JakBlackburn | 5:f9b97f057836 | 387 | } else { |
| JakBlackburn | 5:f9b97f057836 | 388 | error(4); |
| JakBlackburn | 4:2e9aa626a02c | 389 | } |
| JakBlackburn | 5:f9b97f057836 | 390 | lcd.refresh(); |
| JakBlackburn | 4:2e9aa626a02c | 391 | } |
| JakBlackburn | 4:2e9aa626a02c | 392 | |
| JakBlackburn | 4:2e9aa626a02c | 393 | |
| JakBlackburn | 4:2e9aa626a02c | 394 | |
| JakBlackburn | 4:2e9aa626a02c | 395 | |
| JakBlackburn | 4:2e9aa626a02c | 396 | |
| JakBlackburn | 4:2e9aa626a02c | 397 | |
| JakBlackburn | 5:f9b97f057836 | 398 | void car() |
| JakBlackburn | 8:ffcde84b8cf3 | 399 | { /// sets pixels in the design of a car |
| JakBlackburn | 7:962b5f044a9b | 400 | lcd.setPixel(4,19);lcd.setPixel(5,19);lcd.setPixel(6,19);lcd.setPixel(10,19);lcd.setPixel(11,19);lcd.setPixel(12,19); |
| JakBlackburn | 7:962b5f044a9b | 401 | lcd.setPixel(4,20);lcd.setPixel(5,20);lcd.setPixel(6,20);lcd.setPixel(10,20);lcd.setPixel(11,20);lcd.setPixel(12,20); |
| JakBlackburn | 7:962b5f044a9b | 402 | lcd.setPixel(1,21);lcd.setPixel(2,21);lcd.setPixel(3,21);lcd.setPixel(4,21);lcd.setPixel(5,21);lcd.setPixel(6,21); |
| JakBlackburn | 7:962b5f044a9b | 403 | lcd.setPixel(7,21);lcd.setPixel(8,21);lcd.setPixel(9,21);lcd.setPixel(10,21);lcd.setPixel(11,21);lcd.setPixel(12,21); |
| JakBlackburn | 7:962b5f044a9b | 404 | lcd.setPixel(13,21);lcd.setPixel(0,22);lcd.setPixel(1,22);lcd.setPixel(2,22);lcd.setPixel(3,22);lcd.setPixel(4,22); |
| JakBlackburn | 7:962b5f044a9b | 405 | lcd.setPixel(6,22);lcd.setPixel(7,22);lcd.setPixel(8,22);lcd.setPixel(9,22);lcd.setPixel(10,22);lcd.setPixel(12,22); |
| JakBlackburn | 7:962b5f044a9b | 406 | lcd.setPixel(13,22);lcd.setPixel(0,23);lcd.setPixel(1,23);lcd.setPixel(2,23);lcd.setPixel(3,23);lcd.setPixel(6,23); |
| JakBlackburn | 7:962b5f044a9b | 407 | lcd.setPixel(7,23);lcd.setPixel(8,23);lcd.setPixel(9,23);lcd.setPixel(10,23);lcd.setPixel(12,23);lcd.setPixel(13,23); |
| JakBlackburn | 7:962b5f044a9b | 408 | lcd.setPixel(0,24);lcd.setPixel(1,24);lcd.setPixel(2,24);lcd.setPixel(3,24);lcd.setPixel(6,24);lcd.setPixel(7,24); |
| JakBlackburn | 7:962b5f044a9b | 409 | lcd.setPixel(8,24);lcd.setPixel(9,24);lcd.setPixel(10,24);lcd.setPixel(12,24);lcd.setPixel(13,24);lcd.setPixel(0,25); |
| JakBlackburn | 7:962b5f044a9b | 410 | lcd.setPixel(1,25);lcd.setPixel(2,25);lcd.setPixel(3,25);lcd.setPixel(6,25);lcd.setPixel(7,25);lcd.setPixel(8,25); |
| JakBlackburn | 7:962b5f044a9b | 411 | lcd.setPixel(9,25);lcd.setPixel(10,25);lcd.setPixel(12,25);lcd.setPixel(13,25);lcd.setPixel(0,26);lcd.setPixel(1,26); |
| JakBlackburn | 7:962b5f044a9b | 412 | lcd.setPixel(2,26);lcd.setPixel(3,26);lcd.setPixel(4,26);lcd.setPixel(6,26);lcd.setPixel(7,26);lcd.setPixel(8,26); |
| JakBlackburn | 7:962b5f044a9b | 413 | lcd.setPixel(9,26);lcd.setPixel(10,26);lcd.setPixel(12,26);lcd.setPixel(13,26);lcd.setPixel(1,27);lcd.setPixel(2,27); |
| JakBlackburn | 7:962b5f044a9b | 414 | lcd.setPixel(3,27);lcd.setPixel(4,27);lcd.setPixel(5,27);lcd.setPixel(6,27);lcd.setPixel(7,27);lcd.setPixel(8,27); |
| JakBlackburn | 7:962b5f044a9b | 415 | lcd.setPixel(9,27);lcd.setPixel(10,27);lcd.setPixel(11,27);lcd.setPixel(12,27);lcd.setPixel(13,27);lcd.setPixel(4,28); |
| JakBlackburn | 7:962b5f044a9b | 416 | lcd.setPixel(5,28);lcd.setPixel(6,28);lcd.setPixel(10,28);lcd.setPixel(11,28);lcd.setPixel(12,28);lcd.setPixel(4,29); |
| JakBlackburn | 7:962b5f044a9b | 417 | lcd.setPixel(5,29);lcd.setPixel(6,29);lcd.setPixel(10,29);lcd.setPixel(11,29);lcd.setPixel(12,29); |
| JakBlackburn | 3:00933efbe463 | 418 | } |
| JakBlackburn | 3:00933efbe463 | 419 | |
| JakBlackburn | 3:00933efbe463 | 420 | void visToggle() |
| JakBlackburn | 3:00933efbe463 | 421 | { |
| JakBlackburn | 8:ffcde84b8cf3 | 422 | lcd.init(); |
| JakBlackburn | 7:962b5f044a9b | 423 | Vstate = Vfsm[Vstate].nextState[VisTog]; /// read input and update current state |
| JakBlackburn | 7:962b5f044a9b | 424 | visual = Vfsm[Vstate].visual; /// set output depending on current state |
| JakBlackburn | 3:00933efbe463 | 425 | wait(0.2); |
| JakBlackburn | 5:f9b97f057836 | 426 | if(visual==0) { |
| JakBlackburn | 8:ffcde84b8cf3 | 427 | pc.printf("----Left to Right----\n\r"); /// prints to the serial which mode is selected |
| JakBlackburn | 3:00933efbe463 | 428 | } |
| JakBlackburn | 3:00933efbe463 | 429 | if(visual==1) { |
| JakBlackburn | 3:00933efbe463 | 430 | pc.printf("----Up to Down----\n\r"); |
| JakBlackburn | 3:00933efbe463 | 431 | } |
| JakBlackburn | 3:00933efbe463 | 432 | if(visual==2) { |
| JakBlackburn | 4:2e9aa626a02c | 433 | pc.printf("----Car & Wall----\n\r"); |
| JakBlackburn | 4:2e9aa626a02c | 434 | } |
| JakBlackburn | 4:2e9aa626a02c | 435 | if(visual==3) { |
| JakBlackburn | 4:2e9aa626a02c | 436 | pc.printf("----Numbers----\n\r"); |
| JakBlackburn | 3:00933efbe463 | 437 | } |
| JakBlackburn | 7:962b5f044a9b | 438 | } |
| JakBlackburn | 7:962b5f044a9b | 439 | int semihost_powerdown(){ |
| JakBlackburn | 7:962b5f044a9b | 440 | uint32_t arg; |
| JakBlackburn | 7:962b5f044a9b | 441 | return __semihost(USR_POWERDOWN, &arg); |
| JakBlackburn | 0:73bfbe8729d4 | 442 | } |