Emmanuel Adolphus / Mbed 2 deprecated Car_Parker

Dependencies:   N5110 PinDetect PowerControl SRF02 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002 @file main.cpp
00003 @brief ELEC2645 Embedded Systems Project
00004 @brief Ultrasonic Distance sensor/Car Parker
00005 @brief Designed to help car owners park thier cars more effectively
00006 it basically uses burst of sound waves to measure the distance of objects
00007 from the device
00008 @brief Revision 1.0
00009 @author Adolphus George Emmanuel
00010 @date May 2015
00011 */
00012 
00013 #ifndef MAIN_H
00014 #define MAIN_H
00015 
00016 #include "mbed.h"
00017 #include "N5110.h"
00018 #include "SRF02.h"
00019 #include "PowerControl/PowerControl.h"
00020 #include "PowerControl/EthernetPowerControl.h"
00021 
00022 /**
00023 @see URL: http://mbed.org/users/AjK/libraries/PinDetect/lkyxpw  for the PinDetect
00024 */
00025 #include "PinDetect.h"
00026 
00027 
00028 #define USR_POWERDOWN  (0x104)
00029 
00030 /**
00031 @namespace Nokia N5110 LCD, LEDs, Temperatuure Sensor, Distnce Sensor, Buzzer, Button
00032 @brief GPIOs for LCD, LEDs, Temperature and Distance sensor, Printing of serial bus,Button debounce, LDR, buzzer, etc.
00033 */
00034 N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
00035 BusOut leds(LED1, LED2, LED3, LED4);  // Creat a bus out for on board LEDs
00036 I2C tmp102(p28,p27);                  // Creat a new I2C instance for temperature sensor and initialise
00037 SRF02 srf(p28, p27);                  // Creat a new I2C intsance for Distance sensor and initialise
00038 Serial serial(USBTX,USBRX);           // for serial output through USB
00039 PinDetect  pin (p15);                 // an interupt button
00040 AnalogIn ain(p16);                    // analogue in for the LDR used to control the sleep mode
00041 AnalogIn ain2(p20);
00042 InterruptIn button(p15);
00043 Timeout timer;
00044 PwmOut buzzer(p21); // buzzer output
00045 //////////////////////////////////////////////
00046 PwmOut led(p26);  // pwm to control LCD backlight
00047 DigitalOut sce(p8);  // for LCD SCe
00048 SPI spi(p11,NC, p13); //create a new SPI intance and initialise
00049 DigitalOut pwr(p7);   // for LCD power control
00050 DigitalOut dc(p10);  // for LCD Dc input
00051 PwmOut led2(p24); // Pwm for LED alert
00052 
00053 Ticker timer2; // creat ticker function
00054 Ticker timer3;
00055 //////////////////////////////////////////////////////
00056 Timeout callUpTime;
00057 
00058 /**
00059 Creates a flag to be used by the button
00060 @returns an integer
00061 */
00062 int buttonFlag= 0;
00063 void buttonPressed()
00064 {
00065     buttonFlag = 1;
00066 }
00067 
00068 /**
00069 Defines the frequency of the pwm and the beat for sound and LED flashing for alerts
00070 @param frequncy -  float for frequency
00071 @param beat - to set the tempo
00072 @see https://developer.mbed.org/media/uploads/phill/mbed_course_notes_-_pulse_width_modulation.pdf
00073 */
00074 float frequency[]= {659,554};  //frequency array for both sound and LED (Taken from the mbed website)
00075 float beat[]= {1,1}; //beat array for both sound and LED
00076 //////////////////////////////////////////////////////
00077 /**
00078 Defines the x,y pixels for the lcd display
00079 */
00080 int next[84][48];
00081 int nx=84;
00082 int ny =48;
00083 int i=0;
00084 int j=0;
00085 
00086 /**
00087 Gets the distance measurement from the sensor
00088 */
00089 int distance = srf.getDistanceCm();
00090 //////////////////////////////////////////////////////
00091 
00092 void error(); // Error laert function for the range finer
00093 void setBrightness(float brightness);  // Set Brightness function
00094 void clearRAM();  // Clear RAM function
00095 void setXYAddress(int x, int y);  // Set XY Address function
00096 void resting();  // Function to get distance and display and to geive alerts and also place the car image according to the distance
00097 void turnOfDisplay();  // To turn of display
00098 void turnOnDisplay();  // To turn on display
00099 void sendCommand(unsigned char command);
00100 void buzzerAlert();  // Buzzer alert function
00101 void initialise();  // Function to initialise LCd and display welcome messag
00102 void initSPI(); // To initialse SPI
00103 void flipped();
00104 void flippedStop();
00105 void setPixel();
00106 void controlBrightness();
00107 /////////////////////////////////////
00108 
00109 void initTMP102();  // To initialise Temperature
00110 void stopper(); // To get and dispaly temprature
00111 float vol; // float voltage for the potentiometer
00112 ////////////////////////////////
00113 
00114 void firstImage(); // To draw the vertical brick wall
00115 ////////////////////////
00116 // Debounce for button pressed to call up the time and Brightness with battery life infromation
00117 void p15Pressed(void);
00118 void p15Released(void);
00119 ///////////////////////////////////////
00120 
00121 void serialISR(); // ISR that is called when serial data is received
00122 void setTime(); // function to set the UNIX time
00123 int setTimeFlag = 0; // flag for ISR
00124 char rxString[16]; // buffer to store received string
00125 char buffer[30];  // creat a charracter
00126 void timerInterrupt();
00127 ///////////////////////////////
00128 
00129 void setter(int, int, int, int, int, int, int, int, int,int, int, int, int,int, int, int); // Intergers to store the position of the car images
00130 void movingCar(int);
00131 void nextGeneration(); // TO clear and set the pixels according to the next set of conditions
00132 void drawLine();  //For horizontal line
00133 ////////////////////////////////////////////////////////////
00134 float brightness; // float for brightness values
00135 void battery();  // to indicate battery life
00136 //#Temperature Declearation
00137 void error();
00138 float getTemperature(); // float to get temperature
00139 void ledAlert(int);
00140 
00141 /**
00142 @brief Defines the temprature sensors Registers and Address
00143 @Tag for Temperature sensor
00144 @address for ADD0 connected
00145 */
00146 #define TMP102_ADD 0x48
00147 #define TMP102_R_ADD 0x91
00148 #define TMP102_W_ADD 0x90
00149 
00150 // Register address for temperature sensor
00151 #define TEMP_REG 0x00
00152 #define CONFIG_REG 0x01
00153 #define THIGH_REG 0x02
00154 #define TLOW_REG 0x03
00155 ////////////////////////////////////////////////////////////////
00156 /**
00157 @brief to power Down the Ethernet
00158 */
00159 int semihost_powerdown()
00160 {
00161     uint32_t arg;
00162     return __semihost(USR_POWERDOWN, &arg);
00163 }
00164 #endif
00165 
00166 int main()
00167 {
00168     // To intialise lcd and dispaly welcome messages.
00169     initialise();
00170     //////////////////////////////////
00171     // For getting time and date and Temperature
00172     initTMP102();
00173     serial.attach(&serialISR); // attach serial ISR
00174     //set_time(1431109980); // initialise time to what what is inputed
00175     ///////////////////////////////////////
00176 
00177     // to define button thats calls up the time and date it works with the button using debounce
00178     pin.mode(PullDown); // To pull down the button to zero
00179     pin.attach_asserted(&p15Pressed);
00180     pin.attach_deasserted(&p15Released);
00181     pin.setSampleFrequency();
00182 
00183     /////////////////////////////////////
00184     // To power down USB and Ethernet
00185     int result = semihost_powerdown();
00186     PHY_PowerDown();
00187 
00188     button.rise(&buttonPressed); // For button
00189 
00190     while(1) {
00191         // resting();
00192 
00193         nextGeneration();
00194         float voltage = 5.0 * ain.read(); // to declear and initialise the LDR
00195         if(voltage <=2.50) { // If there is light
00196             resting();
00197             setBrightness( brightness);
00198 
00199 
00200         } else if(voltage >= 2.55) {  // If the garrage is dark
00201             turnOfDisplay();
00202             led2 = 0;
00203             buzzer = 0;
00204             timer2.detach();
00205 
00206         }
00207         Sleep();
00208     }
00209 }
00210 
00211 /**
00212 @brief This function is used to set the X-Y address in RAM for futire data write to the LCD
00213 */
00214 void setXYAddress(int x, int y)
00215 {
00216 
00217     if (x > 83);
00218     x  = 83;
00219     if (y > 5);
00220     y = 5;
00221     if(x < 0);
00222     x =0;
00223     if (y < 0);
00224     y=0;
00225 
00226 }
00227 
00228 /**
00229 @brief This functions both the setPixel and drawLine function draws the horizontal line which acts as the floor or main road
00230 which the car is moving on
00231 */
00232 void setPixel()
00233 {
00234     for (i = 0; i < WIDTH; i++) {
00235         lcd.setPixel(i, 24);
00236     }
00237     lcd.refresh();
00238 }
00239 
00240 // Function to draw horizontal line
00241 void drawLine()
00242 {
00243     for (i = 0; i < WIDTH; i++) {
00244         lcd.setPixel(i, 25);
00245     }
00246     lcd.refresh();
00247 }
00248 
00249 /**
00250 @brief This turns of the LCD completely
00251 */
00252 void turnOfDisplay()
00253 {
00254     setBrightness(0.0);  // turn backlight off
00255     clearRAM();   // clear RAM to ensure specified current consumption
00256 
00257 }
00258 
00259 /**
00260 @brief By turning the potentiometer, this function allows you to control the LCD brightneess
00261 the potentiometer uses the 3.3V output from the mbed as its input and uses an analouge in to feedback to the mbed
00262 but it must be noted that the signal or voltage the mbed recieves is between 0V to 1V it can not read in anything above this from the potenteiometer
00263 */
00264 void setBrightness(float brightness)
00265 {
00266     float vol = 3.3*ain2;
00267     float voltage = 5.0*ain;
00268     if ((vol >= 0.0) && (vol < 0.3)) {
00269         brightness = 0.0;
00270     }
00271     if ((vol >= 0.3)  && (vol  <0.6)) {
00272         brightness = 0.1;
00273     }
00274     if ((vol >= 0.6) && (vol < 0.9)) {
00275         brightness = 0.2;
00276     }
00277     if ((vol >= 0.9)  && (vol <1.2)) {
00278         brightness = 0.3;
00279     }
00280     if ((vol >= 1.2)  && (vol <1.5)) {
00281         brightness = 0.4;
00282     }
00283 
00284     if ((vol >= 1.5) && (vol < 1.8)) {
00285         brightness = 0.5;
00286     }
00287     if ((vol >= 1.8)  && (vol  <2.1)) {
00288         brightness = 0.6;
00289     }
00290     if ((vol >= 2.1) && (vol < 2.4)) {
00291         brightness = 0.7;
00292     }
00293     if ((vol >= 2.4)&& (vol < 2.7)) {
00294         brightness = 0.8;
00295     }
00296     if ((vol >= 2.7)&& (vol < 3)) {
00297         brightness = 0.9;
00298     }
00299     if ((vol >= 3.0)&& (vol <=3.3)) {
00300         brightness = 1.0;
00301     }
00302     if ((voltage >= 2.55)&& (vol == vol)) {
00303         brightness = 0.0;
00304     }
00305     led.write(brightness);
00306 
00307 }
00308 
00309 /**
00310 @brief This function writes a 0 to the 504 bytes to clear the RAM in other to reduce the current waste
00311 */
00312 void clearRAM()
00313 {
00314     int i;
00315     sce.write(0);
00316     for(i = 0; i <504; i++) {
00317         spi.write(0x00);
00318     }
00319     sce.write(1);
00320 }
00321 
00322 /**
00323 @brief This function "resting" contians codes which gets the distance from the sensor and displays it on the LCD
00324 Also still in this function other functions like the getTempearature are called as they all are display at same time on the screen
00325 And all the alerts both sound and virtual associated with the car parking aspect of the device are implement here
00326 
00327 for PWM out code for buzzer sound,
00328 @see https://developer.mbed.org/media/uploads/phill/mbed_course_notes_-_pulse_width_modulation.pdf
00329 */
00330 void resting()
00331 {
00332     drawLine();
00333     firstImage();
00334     led2 = 0;
00335     buzzer =0;
00336     stopper();
00337 /////////////////////////////////////////
00338 //Get Distance from sensor dispaly and depending on the conditions, get the appropriate alert and information
00339 // This is where the values for the x and y cordinates to draw the car is also inputted and also the alert the triggers the buzzer and lights up the LED
00340     int distance = srf.getDistanceCm(); // get distance in cm and save in distance
00341 
00342     char buffer[14];
00343     int length = sprintf(buffer, "%2d cm",distance); // To define the length of the charracter which must not be more than 14 charracters
00344 
00345     if (length <= 14) {
00346         if (distance > 249) {   // Distance above 249cm which is the max range the sensor can detect
00347             lcd.printString("Out of Range", 4, 1);
00348         }
00349         if ((distance >= 232) && (distance <=249)) {
00350             lcd.printString(buffer, 25,0);
00351             setter(68, 78, 65, 81, 18, 21, 21, 23, 68, 70,76 ,78, 23 ,25, 23, 25); // These are the values that draws the car
00352             lcd.printString("Very safe", 15, 1);
00353         }
00354         if ((distance >= 215)  && (distance  <= 232)) {
00355             lcd.printString(buffer, 25,0);
00356             setter(63, 73, 60, 76, 18, 21, 21, 23, 63, 65, 71 ,73, 23 ,25, 23, 25);
00357             lcd.printString("Very Safe", 15, 1);
00358         }
00359         if((distance >= 198) && (distance <= 215)) {
00360             lcd.printString(buffer, 25,0);
00361             setter(58, 68, 55, 71, 18, 21, 21, 23, 58, 60, 66 ,68, 23 ,25, 23, 25);
00362             lcd.printString("Very Safe", 15, 1);
00363         }
00364 
00365 
00366         if ((distance >= 181)  && (distance <=198)) {
00367             lcd.printString(buffer, 25,0);
00368             setter(53, 63, 50, 66, 18, 21, 21, 23, 53, 55, 61 ,63,23 ,25, 23, 25);
00369             lcd.printString("Very Safe", 15, 1);
00370         }
00371         if((distance >= 164)  && (distance <=181)) {
00372 
00373             lcd.printString(buffer, 25,0);
00374             setter(48, 58, 45, 61, 18, 21, 21, 23, 48, 50, 56 ,58, 23 ,25, 23, 25);
00375             lcd.printString("Safe Distance", 4, 1);
00376 
00377         }
00378 
00379         if ((distance >= 150) && (distance <= 164)) {
00380             lcd.printString(buffer, 25,0);
00381             setter(43, 53, 40, 56, 18, 21, 21, 23, 43, 45, 51 ,53, 23 ,25, 23, 25);
00382             lcd.printString("Safe Distance", 4, 1);
00383 
00384         }
00385         if((distance >= 133)  && (distance  <=150)) {
00386             lcd.printString(buffer, 25,0);
00387             setter(38, 48, 35, 51, 18, 21, 21, 23, 38, 40, 46 ,48,23 ,25, 23, 25);
00388             lcd.printString("Safe Distance", 4, 1);
00389 
00390         }
00391         if((distance >= 116) && (distance <= 133)) {
00392             lcd.printString(buffer, 25,0);
00393             setter(33, 43, 30, 46, 18, 21, 21, 23, 33, 35, 41 ,43, 23 ,25, 23, 25);
00394             lcd.printString("Safe Distance", 4, 1);
00395 
00396         }
00397         if((distance >= 99)  && (distance <=116)) {
00398             lcd.printString(buffer, 25,0);
00399             setter(28, 38, 25, 41, 18, 21, 21, 23, 28, 30, 36 ,38, 23 ,25, 23, 25);
00400             lcd.printString("Safe Distance", 4, 1);
00401 
00402         }
00403         if((distance >= 82)  && (distance <=99)) {
00404             lcd.printString(buffer, 25,0);
00405             setter(23, 33, 20, 36, 18, 21, 21, 23, 23, 25, 31 ,33, 23 ,25, 23, 25);
00406             lcd.printString("Getting Close", 4, 1);
00407             for (int i=0; i<=1; i++) {
00408                 buzzer.period(1/(frequency[i])); // set PWM period
00409                 buzzer=0.9; // set duty cycle
00410                 wait(0.5*beat[i]); // hold for beat period
00411                 led2 = 1;
00412             }
00413         }
00414         if((distance >= 65) && (distance <= 82)) {
00415             lcd.printString(buffer, 25,0);
00416             setter(18, 28, 15, 31, 18, 21, 21, 23, 18, 20, 26 ,28,23 ,25, 23, 25);
00417             lcd.printString("Getting Close", 4, 1);
00418             for (int i=0; i<=1; i++) {
00419                 buzzer.period(1/(frequency[i])); // set PWM period
00420                 buzzer=0.9; // set duty cycle
00421                 wait(0.5*beat[i]); // hold for beat period
00422                 led2 = 1;
00423             }
00424         }
00425         if((distance >= 48)  && (distance<=65)) {
00426             lcd.printString(buffer, 25,0);
00427             setter(13, 23, 10, 26,18, 21, 21, 23, 13, 15, 21 ,23,23 ,25, 23, 25);
00428             lcd.printString("Too Close", 15, 1);
00429             for (int i=0; i<=1; i++) {
00430                 buzzer.period(1/(frequency[i])); // set PWM period
00431                 buzzer=0.9; // set duty cycle
00432                 wait(0.1*beat[i]); // hold for beat period
00433                 led2 = 1;
00434             }
00435         }
00436         if ((distance >= 31)  && (distance <=48)) {
00437             lcd.printString(buffer, 25,0);
00438             setter(8, 18, 5, 21, 18, 21, 21, 23, 8, 10, 16 ,18,23 ,25, 23, 25);
00439             lcd.printString("Too Close", 15, 1);
00440             for (int i=0; i<=1; i++) {
00441                 buzzer.period(1/(frequency[i])); // set PWM period
00442                 buzzer=0.9; // set duty cycle
00443                 wait(0.1*beat[i]); // hold for beat period
00444                 led2 = 1;
00445             }
00446 
00447         }
00448         if (distance <=31) {  // though the sensor can detect up to 13cm but it was set to 31cm so as to always give drivers safe distance from other objects
00449             lcd.printString(buffer, 25,0);
00450             setter(5, 15, 2, 18, 18, 21, 21, 23, 5, 7, 13 ,15, 23 ,25, 23, 25);
00451             lcd.printString("Limit Reached", 4, 1);
00452             for (int i=0; i<=1; i++) {
00453                 buzzer.period(1/(frequency[i])); // set PWM period
00454                 buzzer=0.09; // set duty cycle
00455                 wait(0.01*beat[i]); // hold for beat period
00456                 led2 = 1;
00457 
00458             }
00459         }
00460     }
00461     wait (0.5);  // A little delay though not really needed for the distance but was added to help to be more accurate. Ticker timer would be better off
00462     // but at the time of this project was not really working one of the future aims is to find a way to make it work
00463 
00464     lcd.refresh();
00465 
00466 }
00467 /**
00468 @brief This function is used to draw the vertical brick wall which show the limit the car sensor can car and to alert the driver that he or she is getting close to an object
00469 */
00470 void firstImage()
00471 {
00472     for (j = 0; j < 26; j++) {
00473         lcd.setPixel(0, j);
00474     }
00475     for (j = 0; j < 26; j++) {
00476         lcd.setPixel(1, j);
00477     }
00478     lcd.refresh();
00479 
00480 
00481 }
00482 
00483 /**
00484 @brief This is declears variables which are later used to draw the moving car depending on the values of the x and y cordinates inputted
00485 */
00486 void setter(int xVert, int xHor, int xVert2, int xHor2, int yVert, int yHor, int yVert2, int yHor2, int xVert3, int xHor3, int xVert4, int xHor4, int yVert3, int yHor3, int yVert4, int yHor4)
00487 {
00488     int i,j;
00489 
00490 // Take the given values and draw the car
00491     for (i = xVert; i < xHor; i++) {
00492         for (j = yVert; j < yHor; j++) {
00493 
00494             lcd.setPixel(i, j);
00495         }
00496     }
00497     for (i = xVert2; i < xHor2; i++) {
00498         for (j = yVert2; j < yHor2; j++) {
00499             lcd.setPixel(i, j);
00500 
00501         }
00502     }
00503     for (i = xVert3; i < xHor3; i++) {
00504         for (j = yVert3; j < yHor3; j++) {
00505 
00506             lcd.setPixel(i, j);
00507         }
00508     }
00509     for (i = xVert4; i < xHor4; i++) {
00510         for (j = yVert4; j < yHor4; j++) {
00511             lcd.setPixel(i, j);
00512 
00513         }
00514         lcd.refresh();
00515     }
00516 
00517 
00518 
00519     lcd.refresh();
00520 }
00521 
00522 
00523 
00524 void nextGeneration()  // Function for generting the next set of pixels based on the conditions
00525 {
00526     int nx =84;
00527     int ny =48;
00528     for (int i=0; i<nx; i++) {
00529         for (int j=0; j<ny; j++) {
00530             if (next[i][j]==1) {
00531                 lcd.setPixel(i,j);
00532             } else if(next[i][j]==0) {
00533                 lcd.clearPixel(i,j);
00534             }
00535         }
00536     }
00537     lcd.refresh();
00538 }
00539 
00540 /**
00541 @brief Here the LCD initialisation function is called and the welcome messages and also authors details are
00542 implmemet which runs only once whenever the device is powered on
00543 */
00544 void initialise()
00545 {
00546     lcd.init();  // initialise lcd
00547     initTMP102();
00548     wait(1.0);   // delay
00549     lcd.printString("Welcome",20,3); // welcome screen
00550     wait(2.0);  // dealy
00551     lcd.clear();
00552     lcd.printString("Adolphus G. E.",1,1); // welcome screen
00553     wait(0.5);  // dealy
00554 
00555     lcd.printString("SID: 200183574",1,2); // welcome screen
00556     wait(0.5);  // dealy
00557     lcd.printString("ELEC2645",1,4); // welcome screen
00558     wait(2.0);  // dealy
00559     lcd.clear(); // clear welcome screen
00560     setXYAddress(0,0);  // set lcd address
00561     lcd.refresh();  // refresh
00562 
00563 }
00564 
00565 /**
00566 @brief Functions to get and display the time
00567 */
00568 void tempTime()
00569 {
00570 
00571     time_t seconds = time(NULL);
00572     strftime(buffer,32,"%I:%M %p",localtime(&seconds));
00573     int length = sprintf(buffer,"%s", buffer);
00574     if (length <= 14) {
00575         lcd.printString(buffer,0,0);    // To display time
00576         timerInterrupt(); // To dispaly Date
00577         controlBrightness();
00578         battery();
00579 
00580         if (setTimeFlag) { // if updated time has been sent
00581             setTimeFlag = 0; // clear flag
00582             setTime(); // update time
00583         }
00584 
00585     }
00586 
00587 }
00588 /**
00589 @brief Functions to get and display the date
00590 */
00591 void timerInterrupt()
00592 {
00593     char buffer[30];  // buffer used to store time string
00594     time_t seconds = time(NULL); // get current time
00595 // format time into a string (time and date)
00596     strftime(buffer, 30 , "%D", localtime(&seconds));
00597     int length = sprintf(buffer,"%s", buffer);
00598     if (length <= 14) {
00599         lcd.printString(buffer,0,1);  // dispaly Date
00600     }
00601     lcd.refresh();
00602 
00603 }
00604 
00605 void setTime()
00606 {
00607 // print time for debugging
00608     serial.printf("set_time - %s",rxString);
00609 // atoi() converts a string to an integer
00610     int time = atoi(rxString);
00611 // update the time
00612     set_time(time);
00613 }
00614 
00615 void serialISR()
00616 {
00617 // when a serial interrupt occurs, read rx string into buffer
00618     serial.gets(rxString,16);
00619 // set flag
00620     setTimeFlag = 1;
00621 }
00622 
00623 /**
00624 @brief Displays the temperature and also calls contains the alert messages for the temperature aspect of the device
00625 */
00626 void stopper()
00627 {
00628     float temperature = getTemperature();
00629     char buffer[14];
00630     int length = sprintf(buffer,"Temp: %2.f C", temperature);
00631     if (length <= 14) {
00632         lcd.printString(buffer,13,4);
00633         if (temperature < 10) {
00634 
00635             lcd.printString("Cold Weather", 8, 5);
00636         }
00637         if (temperature >=10 && temperature <=20) {
00638 
00639             lcd.printString("Fairly Warm", 8, 5);
00640         }
00641         if (temperature >20) {
00642 
00643             lcd.printString("Warm Weather", 8, 5);
00644         }
00645     }
00646     lcd.refresh();
00647 
00648 }
00649 /**
00650 @brief hangs in infinite loop flashing error code when something gors wrong for instance is the distance sensor was not detected
00651 */
00652 void error(int code)
00653 {
00654     while(1) {
00655         leds = 0;
00656         wait(0.25);
00657         leds = code;
00658         wait(0.25);
00659     }
00660 }
00661 
00662 /**
00663 @brief Function to initialise the temperature sensor
00664 */
00665 void initTMP102()
00666 {
00667     tmp102.frequency(400000); // set bus speed to 400 kHz
00668     int ack; // used to store acknowledgement bit
00669     char data[2]; // array for data
00670     char reg = CONFIG_REG; // register address
00671 //////// Read current status of configuration register ///////
00672 
00673     ack = tmp102.write(TMP102_W_ADD,&reg,1); // send the slave write address and the configuration register address
00674     if (ack)
00675         error(1); // if we don't receive acknowledgement, flash error message
00676     ack = tmp102.read(TMP102_R_ADD,data,2); // read default 2 bytes from configuration register and store in buffer
00677     if (ack)
00678         error(2); // if we don't receive acknowledgement, flash error message
00679 ///////// Configure the register //////////
00680 // set conversion rate to 1 Hz
00681     data[1] |= (1 << 6); // set bit 6
00682     data[1] &= ~(1 << 7); // clear bit 7
00683 
00684 //////// Send the configured register to the slave ////////////
00685 
00686     ack = tmp102.write(TMP102_W_ADD,&reg,1); // send the slave write address and the configuration register address
00687     if (ack)
00688         error(3); // if we don't receive acknowledgement, flash error message
00689     ack = tmp102.write(TMP102_W_ADD,data,2); // send 2 data bytes to the configuration register
00690     if (ack)
00691         error(4); // if we don't receive acknowledgement, flash error message
00692 
00693 }
00694 
00695 
00696 /**
00697 @brief Function to the temperature  from the Temearature sensor
00698 */
00699 float getTemperature()
00700 
00701 {
00702     int ack; // used to store acknowledgement bit
00703     char data[2]; // array for data
00704     char reg = TEMP_REG; // temperature register address
00705     ack = tmp102.write(TMP102_W_ADD,&reg,1); // send temperature register address
00706     if (ack)
00707         error(5); // if we don't receive acknowledgement, flash error message
00708     ack = tmp102.read(TMP102_R_ADD,data,2); // read 2 bytes from temperature register and store in array
00709     if (ack)
00710         error(6); // if we don't receive acknowledgement, flash error message
00711     int temperature = (data[0] << 4) | (data[1] >> 4);
00712     return temperature*0.0625;
00713 }
00714 
00715 /**
00716 @brief To clear the screen and prepare to show the date and time when the button is pressed
00717 */
00718 void p15Pressed(void)// To clear the screen before calling  up the function
00719 {
00720     lcd.clear();
00721     setXYAddress(0,0);
00722     wait(0.5);
00723     lcd.refresh();
00724 }
00725 
00726 /**
00727 @brief The function is part of the PinDetect library which dose the callback function
00728 */
00729 void p15Released(void) // To show the content of the function (Date and time, Brightness and battey)
00730 {
00731     buzzer =0;
00732     led2 =0;
00733     tempTime(); // This is the name of the function that contains the informations to be dispalyed
00734 
00735     wait(3.0);
00736     lcd.clear();
00737     lcd.refresh();
00738 }
00739 
00740 /**
00741 @biref When the buton is pressed, this function enables the user to view the percentage of the brightness
00742 */
00743 void controlBrightness()
00744 {
00745     float vol = 3.3*ain2;
00746     if ((vol >= 0.0) && (vol < 0.3)) {
00747         lcd.printString("Brightness=0%",0,3);
00748     }
00749     if ((vol >= 0.3)  && (vol  <0.6)) {
00750         lcd.printString("Brightness=10%",0,3);
00751     }
00752     if ((vol >= 0.6) && (vol < 0.9)) {
00753         lcd.printString("Brightness=20%",0,3);
00754     }
00755     if ((vol >= 0.9)  && (vol <1.2)) {
00756         lcd.printString("Brightness=30%",0,3);
00757     }
00758     if ((vol >= 1.2)  && (vol <1.5)) {
00759         lcd.printString("Brightness=40%",0,3);
00760     }
00761 
00762     if ((vol >= 1.5) && (vol < 1.8)) {
00763         lcd.printString("Brightness=50%",0,3);
00764     }
00765     if ((vol >= 1.8)  && (vol  <2.1)) {
00766         lcd.printString("Brightness=60%",0,3);
00767     }
00768     if ((vol >= 2.1) && (vol < 2.4)) {
00769         lcd.printString("Brightness=70%",0,3);
00770     }
00771     if ((vol >= 2.4)&& (vol < 2.7)) {
00772         lcd.printString("Brightness=80%",0,3);
00773     }
00774     if ((vol >= 2.7)&& (vol < 3)) {
00775         lcd.printString("Brightness=90%",0,3);
00776     }
00777     if ((vol >= 3.0)&& (vol <=3.3)) {
00778         lcd.printString("Brightness=100%",0,3);
00779     }
00780 }
00781 
00782 /**
00783 @brief Function to draw the battery symbol, though at the time of this project a way of getting the battery life was not included but will be added in the next revision
00784 */
00785 void battery()
00786 {
00787     lcd.printString("100%", 23, 5);
00788 
00789     for (j = 37; j < 47; j++) {
00790         lcd.setPixel(3, j);
00791     }
00792     for (j = 37; j <47; j++) {
00793         lcd.setPixel(20, j);
00794     }
00795 
00796 
00797     for (i = 3; i < 21; i++) {
00798         lcd.setPixel(i, 37);
00799     }
00800     for (i = 3; i <21; i++) {
00801         lcd.setPixel(i, 46);
00802     }
00803 
00804 
00805     for (j = 40; j < 44; j++) {
00806         lcd.setPixel(21, j);
00807     }
00808     for (j = 40; j <44; j++) {
00809         lcd.setPixel(22, j);
00810     }
00811 
00812     lcd.refresh();
00813 }