ELEC2645 Year 2 Project Arnau Segura Doncel 200867010

Dependencies:   N5110 SRF02 TMP102 mbed

Files at this revision

API Documentation at this revision

Comitter:
ollie140
Date:
Thu May 05 09:41:14 2016 +0000
Commit message:
Final code for the level 2 year project

Changed in this revision

N5110.lib Show annotated file Show diff for this revision Revisions of this file
SRF02.lib Show annotated file Show diff for this revision Revisions of this file
TMP102.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 4a66fd16c223 N5110.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/N5110.lib	Thu May 05 09:41:14 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/eencae/code/N5110/#ba8addc061ea
diff -r 000000000000 -r 4a66fd16c223 SRF02.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SRF02.lib	Thu May 05 09:41:14 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/eencae/code/SRF02/#8e6587d88773
diff -r 000000000000 -r 4a66fd16c223 TMP102.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TMP102.lib	Thu May 05 09:41:14 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/eencae/code/TMP102/#1b601445b336
diff -r 000000000000 -r 4a66fd16c223 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu May 05 09:41:14 2016 +0000
@@ -0,0 +1,717 @@
+/**
+@file main.cpp
+@brief program implementation
+
+*/
+#include "main.h"
+
+int main()
+{
+    introScreen();
+    while (1) {
+        if (g_timer2_flag) {
+            g_timer2_flag = 0;
+            if (current_screen == menu_state) { //Main menu
+                mainMenu();
+            } else if (current_screen == modes_state) { //modes menu
+                modesMenu();
+            } else if (current_screen == peripherals_state) { // peripherals menu
+                peripheralsMenu();
+            } else if (current_screen == brightness_state) { // brightness menu
+                brightnessMenu();
+            } else if (current_screen == sound_state) { // sound menu
+                soundMenu();
+            } else if (current_screen == ledMenu_state) { // LED menu
+                LEDMenu();
+            } else if (current_screen == nightMenu_state) { // night menu
+                nightMenu();
+            } else if (current_screen == parkingMode_state) { // parking mode setup
+                printBars();
+            } else if (current_screen == numericMode_state) { // numeric mode setup
+                numericMode();
+            }
+        }
+        sleep();
+    }
+}
+
+
+float getDistance()
+{
+
+    emptyVariable = 0; // initialise the empty variable
+
+    for (int i = 0 ; i<10; i++) { // intitialises the measurement for loop
+
+        realDistance = srf.getDistanceCm(); // get a distance measurement for every i value
+        emptyVariable = emptyVariable + realDistance ; // add all these measurements up
+    }
+
+    distance = emptyVariable/10.0; // divide all the measurements by 10 to take the average
+    return distance; // return the distance
+
+}
+
+void drawCar()
+{
+    lcd.drawRect(1,8,56,32,1);  // main car rectangle
+    lcd.drawRect(15,6,9,1,1);   // right front wheel
+    lcd.drawRect(40,6,9,1,1);   // right back wheel
+    lcd.drawRect(15,41,9,1,1);  // left front wheel
+    lcd.drawRect(40,41,9,1,1);  // left back wheel
+    lcd.drawRect(20,14,9,20,2); // front window
+    lcd.drawRect(42,16,6,16,2);  // back window
+
+}
+
+void reallyCloseDistance()
+{
+    lcd.drawRect(60,8,3,32,1);  // really close bar
+    lcd.drawRect(66,8,3,32,1);  // close bar
+    lcd.drawRect(72,8,3,32,1);  // medium bar
+    lcd.drawRect(78,8,3,32,1);  // far bar
+}
+
+void closeDistance()
+{
+    lcd.drawRect(66,8,3,32,1);  // close bar
+    lcd.drawRect(72,8,3,32,1);  // medium bar
+    lcd.drawRect(78,8,3,32,1);  // far bar
+}
+
+void mediumDistance()
+{
+    lcd.drawRect(72,8,3,32,1);  // medium bar
+    lcd.drawRect(78,8,3,32,1);  // far bar
+}
+
+void farDistance()
+{
+    lcd.drawRect(78,8,3,32,1);  // far bar
+}
+
+void printBars()
+{
+    potTicker.detach(); // detach the potentiometer ticker 
+
+    sensorTicker.attach(&timer_isr,0.7); // attach a ticker that will be called every 0.7 seconds
+
+    while (current_screen == parkingMode_state) { // while the parking mode menu is true 
+
+        dist = getDistance(); // get the distance in cm
+
+        T = tmp102.get_temperature(); // get the temperature in C
+
+        temp = floor(T); // floor the temperature to get an integer value
+
+        LED = 0; // Set the LED to off
+
+        if (g_timer_flag) { // if the timer event is called
+
+            g_timer_flag = 0; // reset flag back to 0
+
+            lcd.clear();// clear the screen
+
+            length2 = sprintf(buffer2,"%dC",temp); // assign a value to the string lenght
+
+            if (length2 <= 14) { // if string will fit on display
+                lcd.printString(buffer2,66,0); // print the desired string
+            }
+
+            if (dist > 140) { // if distance is more than 140 cm
+                drawCar(); // draw the car shape
+                buzzerOFF();
+            } else if (dist <= 140 && dist > 100) { // if distance is between 140 cm and 100 cm
+                drawCar(); // draw the car shape
+                farDistance(); // draw 1 bar
+                buzzerOFF();
+            } else if (dist <= 100 && dist > 70) { // if distance is between 100 cm and 70 cm
+                drawCar(); // draw the car shape
+                mediumDistance(); // draw 2 bars
+                buzzerOFF();
+            } else if (dist <= 70 && dist > 40) { // if distance is between 70 cm and 40 cm
+                drawCar(); // draw the car shape
+                closeDistance(); // draw 3 bars
+                buzzerOFF();
+            } else { // if distance is less than 40 cm
+                drawCar(); // draw the car shape
+                reallyCloseDistance(); // draw 4 bars
+                
+                if (buzzer_state == buzzerON_state) {
+                    buzzerON();
+                } else {
+                    buzzerOFF();
+                }
+                
+                if (led_state == LEDON_state) { // if the state of the LED menu is on 
+                    LED = 1; // turn on the LED
+                } else {
+                    LED = 0; // keep the LED off
+                }
+            }
+
+            if (g_botButton_flag) { // if the button is pressed
+                g_botButton_flag = 0; // reset the flag
+
+                LED = 0; // turn off the LED
+                
+                buzzerOFF(); // sets the duty cycle to 0
+                
+                sensorTicker.detach(); // detach the sensor ticker to save computing powr and battery
+
+                current_screen = menu_state; // go back to the main menu
+
+                potTicker.attach(&timer2_isr,0.2); // attach the potentiometer ticker
+
+            }
+            lcd.refresh(); // refresh the screen
+        }
+        sleep(); // send the MCU back to sleep to save power
+    }
+
+}
+
+void numericMode()
+{
+    potTicker.detach(); // detanch the ticker potentiometer
+
+    sensorTicker.attach(&timer_isr,0.7); // attach the sensor ticker to be called every 0.7 seconds
+
+    while (current_screen == numericMode_state) {
+
+        dist = getDistance(); // get the distance in cm
+
+        T = tmp102.get_temperature(); // get the temperature in C
+
+        if (g_timer_flag) { // if time event is triggered
+
+            lcd.clear(); // clear the LCD
+
+            length1 = sprintf(buffer,"D = %.2f cm",dist); // assign a lenght integer for the distance string
+
+            length2 = sprintf(buffer2,"T = %.2f C",T); // assign a lenght integer for the temperature string
+
+            if (length1 <= 14) { // if string will fit on display
+                lcd.printString(buffer,0,2);
+            }
+            if (length2 <= 14) { // if string will fit on display
+                lcd.printString(buffer2,0,4);
+            }
+
+            if (g_botButton_flag) { // if the event trigger is activated by pressing the bottom button
+                g_botButton_flag = 0; // reset the flag 
+                
+                sensorTicker.detach(); // detach the sensor ticker
+
+                current_screen = menu_state; // go back to the main menu
+
+                potTicker.attach(&timer2_isr,0.2); // attach the potentiometer ticker
+
+            }
+
+            lcd.refresh(); // refresh the LCD
+        }
+        sleep(); // save power by sending the MCU to sleep
+    }
+}
+
+
+void introScreen()
+{
+
+    init_K64F(); // intialise all of the K64F's on board peripherals
+    init_serial();
+    potTicker.attach(&timer2_isr,0.2); // attach a ticker for the potentiometer
+    tmp102.init(); // initialise the temperature sensor
+    lcd.init(); // initialise the LCD display
+    lcd.setBrightness(1.0);
+    lcd.printString("PARKING",21,0); 
+    lcd.printString("SENSOR",25,2);
+    lcd.printString("BY",36,3);
+    lcd.printString("ARNAU SEGURA",6,4); 
+    led_state = LEDON_state; // start with the warning LED on the ON state
+    buzzer.period(1/1000.0);
+    buzzer_state = buzzerON_state;
+    wait(3); // wait 3 seconds
+    lcd.clear(); // clear the LCD
+    lcd.refresh(); // refresh the LCD
+
+}
+
+void mainMenu()
+{
+
+    lcd.clear(); // clear the LCD
+    lcd.printString("MENU",28,0); 
+    lcd.printString("MODES",25,2); 
+    lcd.printString("PERIPHERALS",9,4);
+    
+    g_botButton_flag = 0; // set the flag to 0 on the main menu so that if the bottom button is accidentally pressed the bug where the user would be forced to stay in main menu is solved
+
+    if (pot < 0.5f) { // if the potentiometer is normalised to less than 0.5
+        MenuLine2(); // invert pixels in bank 3
+        selected_option = modes_state;
+    } else {
+        MenuLine4(); // invert pixels in bank 5
+        selected_option = peripherals_state;
+    }
+    goToSelectedOption(); // if the top button is pressed go to the selected option
+    lcd.refresh();
+}
+
+void peripheralsMenu()
+{
+    lcd.clear();
+    lcd.printString("PERIPHERALS",9,0);
+    lcd.printString("BRIGHTNESS",12,2);
+    lcd.printString("SOUND",25,3);
+    lcd.printString("LEDs",28,4);
+    lcd.printString("NIGHT LCD",15,5);
+    if (pot <= 0.25f) {
+        MenuLine2(); // invert pixels in bank 3
+        selected_option = brightness_state;
+    } else if (pot <= 0.5f) {
+        MenuLine3(); // invert pixels in bank 4
+        selected_option = sound_state;
+    } else if (pot <= 0.75f) {
+        MenuLine4(); // invert pixels in bank 5
+        selected_option = ledMenu_state;
+    } else {
+        MenuLine5(); // invert pixels in bank 6
+        selected_option = nightMenu_state;
+    }
+    goToMainMenu(); // if bottom button is pressed go back to main menu
+    goToSelectedOption(); // if the top button is pressed go to the selected option
+    lcd.refresh();
+}
+
+void modesMenu()
+{
+    lcd.clear();
+    lcd.printString("MODES",25,0);
+    lcd.printString("PARKING MODE",6,2);
+    lcd.printString("NUMERIC MODE",6,4);
+
+    if (pot <= 0.5f) {
+        MenuLine2(); // invert pixels in bank 3
+        selected_option = parkingMode_state;
+    } else {
+        MenuLine4(); // invert pixels in bank 5
+        selected_option = numericMode_state;
+    }
+    goToMainMenu(); // go back to main menu
+    goToSelectedOption(); // go to selected option
+    lcd.refresh();
+}
+
+void soundMenu()
+{
+    lcd.clear();
+    lcd.printString("SOUND MENU",12,0);
+    lcd.printString("SOUND ON",18,3);
+    lcd.printString("SOUND OFF",15,5);
+
+    if (pot <= 0.5f) {
+        MenuLine3Short(); // partially inverts bank 4 of the LCD
+        if (g_topButton_flag) {
+            g_topButton_flag = 0;
+            
+            buzzer_state = buzzerON_state;
+            circle_position_SOUND = 27; // set y position for the circle to be drawed
+        }
+    } else {
+        MenuLine5Short(); // partially inverts bank 6 of the LCD
+        if (g_topButton_flag) {
+            g_topButton_flag = 0;
+            
+            buzzer_state = buzzerOFF_state;
+            circle_position_SOUND = 43; // set y position for the circle to be drawed
+        }
+    }
+    
+    lcd.drawCircle(4,circle_position_SOUND,2,1); // draw a circle next to the selected option for the respective y position
+    lcd.drawCircle(80,circle_position_SOUND,2,1); // draw a circle next to the selected option for the respective y position
+    goToMainMenu(); // go back to main menu
+    lcd.refresh();
+}
+
+void buzzerON()
+{
+    buzzer.write(0.5); // set the buzzers duty cycle to 50%
+}
+
+void buzzerOFF()
+{
+    buzzer.write(0.0); // set the buzzers duty cycle to 0%
+}
+
+void LEDMenu()
+{
+
+    lcd.clear();
+    lcd.printString("LED MENU",18,0);
+    lcd.printString("LED ON",24,3);
+    lcd.printString("LED OFF",21,5);
+
+    if (pot <= 0.5f) {
+        MenuLine3Short(); // partially inverts bank 4 of the LCD
+        if (g_topButton_flag) {
+            g_topButton_flag = 0;
+
+            circle_position_LED = 27; // set y position for the circle to be drawn
+            led_state = LEDON_state; // set the empty led state variable to the LED On state
+        }
+
+    } else {
+        MenuLine5Short(); // partially inverts bank 6 of the LCD
+        if (g_topButton_flag) {
+            g_topButton_flag = 0;
+
+            circle_position_LED = 43; // set y position for the circle to be drawn
+            led_state = LEDOFF_state; // set the empty led state variable to the LED Off state
+        }
+    }
+    
+    lcd.drawCircle(4,circle_position_LED,2,1); // draw a circle next to the selected option for the respective y position
+    lcd.drawCircle(80,circle_position_LED,2,1); // draw a circle next to the selected option for the respective y position
+    goToMainMenu(); // go to main menu
+    lcd.refresh();
+
+}
+
+void brightnessMenu()
+{
+    lcd.clear();
+    lcd.printString("BRIGHTNESS",12,0);
+    lcd.printString("0%",36,1);
+    lcd.printString("25%",33,2);
+    lcd.printString("50%",33,3);
+    lcd.printString("75%",33,4);
+    lcd.printString("100%",30,5);
+
+    if (pot <= 0.2f) {
+        MenuLine1Short(); // partially inverts bank 2 of the LCD
+        if (g_topButton_flag) {
+            g_topButton_flag = 0;
+            circle_position_brightness = 11; // set y position for circle
+            screenLED.write(0.0); // set the brighness to 0%
+        }
+    } else if (pot <= 0.4f) {
+        MenuLine2Short(); // partially inverts bank 3 of the LCD
+        if (g_topButton_flag) {
+            g_topButton_flag = 0;
+            circle_position_brightness = 19; // set y position for circle
+            screenLED.write(0.25); // set the brighness to 25%
+        }
+    } else if (pot <= 0.6f) {
+        MenuLine3Short(); // partially inverts bank 4 of the LCD
+        if (g_topButton_flag) {
+            g_topButton_flag = 0;
+            circle_position_brightness = 27; // set y position for circle
+            screenLED.write(0.5); // set the brighness to 50%
+        }
+    } else if (pot <= 0.8f) {
+        MenuLine4Short(); // partially inverts bank 5 of the LCD
+        if (g_topButton_flag) {
+            g_topButton_flag = 0;
+            circle_position_brightness = 35; // set y position for circle
+            screenLED.write(0.75); // set the brighness to 75%
+        }
+    } else {
+        MenuLine5Short(); // partially inverts bank 6 of the LCD
+        if (g_topButton_flag) {
+            g_topButton_flag = 0;
+            circle_position_brightness = 43; // set y position for circle
+            screenLED.write(1.0); // set the brighness to 100%
+        }
+    }
+    
+    lcd.drawCircle(4,circle_position_brightness,2,1); // draw a circle next to the selected option for the respective y position
+    lcd.drawCircle(80,circle_position_brightness,2,1); // draw a circle next to the selected option for the respective y position
+    goToMainMenu(); // go back to main menu
+    lcd.refresh();
+}
+
+void nightMenu()
+{
+    lcd.clear();
+    lcd.printString("NIGHT MODE",12,0);
+    lcd.printString("DAY",33,3);
+    lcd.printString("NIGHT",27,5);
+    
+    if (pot <= 0.5f) {
+        MenuLine3Short();
+        if (g_topButton_flag) {
+            g_topButton_flag = 0;
+
+            circle_position_NIGHT = 27; // set y position for circle
+            lcd.normalMode(); // set the sreen to normal mode
+        }
+
+    } else {
+        MenuLine5Short();
+        if (g_topButton_flag) {
+            g_topButton_flag = 0;
+
+            circle_position_NIGHT = 43; // set y position for circle
+            lcd.inverseMode(); // set the screen to inverse mode
+        }
+    }
+    lcd.drawCircle(4,circle_position_NIGHT,2,1); // draw a circle next to the selected option for the respective y position
+    lcd.drawCircle(80,circle_position_NIGHT,2,1); // draw a circle next to the selected option for the respective y position
+    goToMainMenu(); // go back to main menu
+    lcd.refresh();
+}
+
+void goToMainMenu()
+{
+    if (g_botButton_flag) { // if the bottom button is pressed
+        g_botButton_flag = 0; // sets the flag to 0
+
+        current_screen = menu_state; // the current screen will go to the main menu (state 0)
+    }
+}
+
+void goToSelectedOption()
+{
+    if (g_topButton_flag) {
+        g_topButton_flag = 0;
+
+        current_screen = selected_option; // the current screen will go to the selected option
+
+    }
+}
+
+void MenuLine1()
+{
+    for (int i = 0; i < WIDTH; i++) { //scans all the pixels in the second bank and inverts them
+        for (int j = 8; j <= 14; j++) {
+            if (lcd.getPixel(i,j)) {
+
+                lcd.clearPixel(i,j);
+
+            } else {
+
+                lcd.setPixel(i,j);
+            }
+        }
+    }
+}
+
+void MenuLine1Short()
+{
+    for (int i = 15; i < 70; i++) { // scans most of the pixels in the second bank and inverts them
+        for (int j = 8; j <= 14; j++) {
+            if (lcd.getPixel(i,j)) {
+
+                lcd.clearPixel(i,j);
+
+            } else {
+
+                lcd.setPixel(i,j);
+            }
+        }
+    }
+}
+void MenuLine2()
+{
+    for (int i = 0; i < WIDTH; i++) { //scans all the pixels in the third bank and inverts them
+        for (int j = 16; j <= 22; j++) {
+            if (lcd.getPixel(i,j)) {
+
+                lcd.clearPixel(i,j);
+
+            } else {
+
+                lcd.setPixel(i,j);
+            }
+        }
+    }
+}
+
+void MenuLine2Short()
+{
+    for (int i = 15; i < 70; i++) { // scans most of the pixels in the third bank and inverts them
+        for (int j = 16; j <= 22; j++) {
+            if (lcd.getPixel(i,j)) {
+
+                lcd.clearPixel(i,j);
+
+            } else {
+
+                lcd.setPixel(i,j);
+            }
+        }
+    }
+}
+
+void MenuLine3()
+{
+    for (int i = 0; i < WIDTH; i++) { //scans all the pixels in the fourth bank and inverts them
+        for (int j = 24; j <= 30; j++) {
+            if (lcd.getPixel(i,j)) {
+
+                lcd.clearPixel(i,j);
+
+            } else {
+
+                lcd.setPixel(i,j);
+            }
+        }
+    }
+}
+
+void MenuLine3Short()
+{
+    for (int i = 15; i < 70; i++) { // scans most of the pixels in the fourth bank and inverts them
+        for (int j = 24; j <= 30; j++) {
+            if (lcd.getPixel(i,j)) {
+
+                lcd.clearPixel(i,j);
+
+            } else {
+
+                lcd.setPixel(i,j);
+            }
+        }
+    }
+}
+
+void MenuLine4()
+{
+    for (int i = 0; i < WIDTH; i++) { //scans all the pixels in the fifth bank and inverts them
+        for (int j = 32; j <= 38; j++) {
+            if (lcd.getPixel(i,j)) {
+
+                lcd.clearPixel(i,j);
+
+            } else {
+
+                lcd.setPixel(i,j);
+            }
+        }
+    }
+}
+
+void MenuLine4Short()
+{
+    for (int i = 15; i < 70; i++) { // scans most of the pixels in the fifth bank and inverts them
+        for (int j = 32; j <= 38; j++) {
+            if (lcd.getPixel(i,j)) {
+
+                lcd.clearPixel(i,j);
+
+            } else {
+
+                lcd.setPixel(i,j);
+            }
+        }
+    }
+}
+
+void MenuLine5()
+{
+    for (int i = 0; i < WIDTH; i++) { //scans all the pixels in the sixth bank and inverts them
+        for (int j = 40; j <= 46; j++) {
+            if (lcd.getPixel(i,j)) {
+
+                lcd.clearPixel(i,j);
+
+            } else {
+
+                lcd.setPixel(i,j);
+            }
+        }
+    }
+}
+
+void MenuLine5Short()
+{
+    for (int i = 15; i < 70; i++) { // scans most of the pixels in the sixth bank and inverts them
+        for (int j = 40; j <= 46; j++) {
+            if (lcd.getPixel(i,j)) {
+
+                lcd.clearPixel(i,j);
+
+            } else {
+
+                lcd.setPixel(i,j);
+            }
+        }
+    }
+}
+
+void init_serial()
+{
+    // set to highest baud - ensure terminal software matches
+    pc.baud(115200);
+}
+
+void init_K64F()
+{
+    // on-board LEDs are active-low, so set pin high to turn them off.
+    r_led = 1;
+    g_led = 1;
+    b_led = 1;
+
+    // since the on-board switches have external pull-ups, we should disable the internal pull-down
+    // resistors that are enabled by default using InterruptIn
+    sw2.mode(PullNone);
+    sw3.mode(PullNone);
+
+    topButton.mode(PullDown); // set the top button to pull-down mode
+    botButton.mode(PullDown); // set the bottom button to pull-down mode
+    topButton.rise(&topButton_isr); // recognise the press of the button as a rise up (from mbed pin to 3V3)
+    botButton.rise(&botButton_isr); // recognise the press of the button as a rise up (from mbed pin to 3V3)
+
+}
+
+// time-triggered interrupt used for the sensorTicker
+void timer_isr()
+{
+    g_timer_flag = 1;   // set flag in ISR
+}
+
+//time triggered interrupt used for the buttonsDebounce ticker
+void timer1_isr()
+{
+    g_timer1_flag = 0;   // set flag in ISR
+
+    buttonsDebounce.detach(); // detach the buttonsDebounce ticker
+}
+
+//time triggered interrupt used for the potTicker 
+void timer2_isr()
+{
+    g_timer2_flag = 1;   // set flag in ISR
+}
+
+//event triggered interrupt
+void botButton_isr()
+{
+    if (!g_timer1_flag) { // when the button is pressed if the buttonsDebounce flag is on 0
+
+        g_botButton_flag = 1; // set the flag to 1
+
+        g_timer1_flag = 1; // set the flag to 1
+
+        buttonsDebounce.attach(&timer1_isr,0.2); // attach a short delay of 0.2 that will prevent the button from providing multiple inputs to the MCU
+
+    }
+
+}
+
+//event triggered interrupt
+void topButton_isr()
+{
+    if (!g_timer1_flag) { // when the button is pressed if the buttonsDebounce flag is on 0
+
+        g_topButton_flag = 1; // set the flag to 1
+
+        g_timer1_flag = 1; // set the flag to 1
+
+        buttonsDebounce.attach(&timer1_isr,0.2); // attach a short delay of 0.2 that will prevent the button from providing multiple inputs to the MCU
+
+    }
+
+}
\ No newline at end of file
diff -r 000000000000 -r 4a66fd16c223 main.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h	Thu May 05 09:41:14 2016 +0000
@@ -0,0 +1,419 @@
+/**
+@file   main.h
+@brief  Header file containing functions prototypes, defines and global variables.
+@brief  Parking Sensor Project (Year 2)
+@brief  ELEC 2645
+@author Arnau Segura Doncel
+@date   March 2016
+*/
+#ifndef MAIN_H
+#define MAIN_H
+
+#include "mbed.h"
+#include "N5110.h"
+#include "SRF02.h"
+#include "TMP102.h"
+
+//Inputs
+
+/**
+@namespace SRF02
+@brief Ultra sonic distance sensor I2C mode SDA and SCL clock lines (no pull-up resitors required)
+*/
+SRF02 srf(I2C_SDA,I2C_SCL);
+
+/**
+@namespace TMP102
+@brief Temperature sensor I2C mode SDA and SCL clock lines (no pull-up resitors required)
+*/
+TMP102 tmp102(I2C_SDA,I2C_SCL);
+
+/**
+@namespace Upper_Button
+@brief Interrupt button (top button)
+*/
+InterruptIn topButton(PTB19);
+
+/**
+@namespace Lower_Button
+@brief Interrupt button (bottom button)
+*/
+InterruptIn botButton(PTB18);
+
+/**
+@namespace sw2
+@brief sw2 on the K64F
+*/
+InterruptIn sw2(SW2);
+
+/**
+@namespace sw3
+@brief sw3 on the K64F
+*/
+InterruptIn sw3(SW3);
+
+/**
+@namespace Potentiometer
+@brief An analogue input
+*/
+AnalogIn pot(PTB10);
+
+//Outputs
+
+/**
+@namespace LCD
+@brief LCD screen outputs
+*/
+N5110 lcd(PTE26,PTA0,PTC4,PTD0,PTD2,PTD1,PTC3);
+
+/**
+@namespace Screens_LED
+@brief onboard LED on the nokia N5110 screen
+*/
+PwmOut screenLED(PTC3);
+
+/**
+@namespace Buzzer
+@brief PWM controlled buzzer that serves as sound alert
+*/
+PwmOut buzzer(PTA2);
+
+/**
+@namespace Warning_LED
+@brief LED that will warn the user
+*/
+DigitalOut LED(PTC2);
+
+/**
+@namespace red_led
+@brief red LED on the K64F
+*/
+DigitalOut r_led(LED_RED);
+
+/**
+@namespace green_led
+@brief green LED on the K64F
+*/
+DigitalOut g_led(LED_GREEN);
+
+/**
+@namespace blue_led
+@brief blue LED on the K64F
+*/
+DigitalOut b_led(LED_BLUE);
+
+/**
+@namespace serial
+@brief output serial for USB communication (debugging)
+*/
+Serial pc(USBTX,USBRX);
+
+//Rest of objects
+/**
+@namespace sensorTicker
+@brief Sensor ticker that will periodically call for distance and temperature measurements
+*/
+Ticker sensorTicker;
+
+/**
+@namespace buttonsDebounce
+@brief Button Debounce ticker that will periodically call everytime a button has been pressed and will prevent multiple inputs from being recognised
+*/
+Ticker buttonsDebounce;
+
+/**
+@namespace potTicker
+@brief Potetiometer ticker that will periodically call to check the position of the potentiometer
+*/
+Ticker potTicker;
+
+//functions
+/**
+method that will get the average of 10 distance measurements
+@returns Average of 10 distance measurements
+*/
+float getDistance();
+
+/**
+Method that draws the car on the screen
+*/
+void drawCar();
+
+/**
+Method that lets the user know the car is really close to an object
+*/
+void reallyCloseDistance();
+
+/**
+Method that lets the user know the car is close to an object
+*/
+void closeDistance();
+
+/**
+Method that lets the user know the car is almost close to an object
+*/
+void mediuDistance();
+
+/**
+Method that lets the user know the car far from an object
+*/
+void farDistance();
+
+/**
+Method that prints the bars to show the user the obstacles distance
+*/
+void printBars();
+
+/**
+Method that prints the distance and the temperature on the screen
+*/
+void numericMode();
+
+/**
+Initialises serial communication
+*/
+void init_serial();
+
+/**
+Initialises all the K64F's on board peripherals
+*/
+void init_K64F();
+
+/**
+Sets the buzzers duty cycle to 0
+*/
+void buzzerOFF();
+
+/**
+Sets the buzzers duty cycle to 50%
+*/
+void buzzerON();
+
+/** @name Interrupt Service Routine Functions
+* Sets the functions of the ISR
+*/
+///@{
+/**
+Time triggered interrupt method
+*/
+void timer_isr();
+
+/**
+Time triggered interrupt method
+*/
+void timer1_isr();
+
+/**
+Time triggered interrupt method
+*/
+void timer2_isr();
+
+/**
+Event triggered interrupt method
+*/
+void botButton_isr();
+
+/**
+Event triggered interrupt method
+*/
+void topButton_isr();
+///@}
+
+/** @name Menu related functions
+* All the functions that set up the menus of the project
+*/
+///@{
+/**
+Scans the second bank of 6 and inverts all the pixels
+*/
+void MenuLine1();
+
+/**
+Scans the second bank of 6 and inverts some of the pixels
+*/
+void MenuLine1Short();
+
+/**
+Scans the third bank of 6 and inverts all the pixels
+*/
+void MenuLine2();
+
+/**
+Scans the third bank of 6 and inverts some of the pixels
+*/
+void MenuLine2Short();
+
+/**
+Scans the fourth bank of 6 and inverts all the pixels
+*/
+void MenuLine3();
+
+/**
+Scans the fourth bank of 6 and inverts some of the pixels
+*/
+void MenuLine3Short();
+
+/**
+Scans the fifth bank of 6 and inverts all the pixels
+*/
+void MenuLine4();
+
+/**
+Scans the fifth bank of 6 and inverts some of the pixels
+*/
+void MenuLine4Short();
+
+/**
+Scans the sixth bank of 6 and inverts all the pixels
+*/
+void MenuLine5();
+
+/**
+Scans the sixth bank of 6 and inverts some of the pixels
+*/
+void MenuLine5Short();
+
+/**
+Sets up the intro screen.
+Displays the intro message
+*/
+void introScreen();
+
+/**
+Sets up the main menu.
+Normalises potentiometer values to the highlighted bank of text
+in order to navigate the menus more easily
+*/
+void mainMenu();
+
+/**
+Simple function that will send the user back to the main menu whenever the bottom button is pressed
+*/
+void goToMainMenu();
+
+/**
+Simple function that will send the user to a certain screen depending on what state it is
+*/
+void goToSelectedOption();
+
+/**
+Sets up the peripherals menu. The peripherals are the LED and the buzzer
+*/
+void peripheralsMenu();
+
+/**
+Sets up the modes menu. The user will be able to use the parking sensor 
+or a numerical value sensor that will indicate both temperature and distance.
+*/
+void modesMenu();
+
+/**
+Sets up the sound menu. Either turn sound off or on
+*/
+void soundMenu();
+
+/**
+Sets up the LED menu. The led will either turn on at
+critical distances from an object or it won't
+*/
+void LEDMenu();
+
+/**
+Sets up the brightness menu. The backlight led will go from a range of
+0% to 100% brightness.
+*/
+void brightnessMenu();
+
+/**
+Sets up the day/night menu. The lcd will change from normal mode to inverse mode
+when the user desires so
+*/
+void nightMenu();
+///@}
+
+//Variables
+float distance; /*!< @brief Float that stores the average distance measurement from the getDistance function*/
+
+float dist; /*!< @brief Float that stores the average distance measurement to be used in the modes functions*/
+
+float T; /*!< @brief Float that stores the temperature measurement to be used in the modes functions*/
+
+int emptyVariable; /*!< @brief Integer that serves as an empty variable to store the distance average measurements*/
+
+int realDistance; /*!< @brief Integer that stores the distance measurements in cm from the distance sensor*/
+
+int temp; /*!< @brief Integer that stores the floored temperature measurement to be used in the parking mode function [printBars()]*/
+
+int current_screen; /*!< @brief Integer that serves as an empty variable to store the current screen state*/
+
+int selected_option; /*!< @brief Integer that serves as an empty variable to store the selected option once an interrupt has occurred*/
+
+int led_state; /*!< @brief Integer that serves as an empty variable to store the LED state (either on or off)*/
+
+int buzzer_state; /*!< @brief Integer that serves as an empty variable to store the buzzer state (either on or off)*/
+
+int circle_position_SOUND = 27; /*!< @brief Integer that stores the initial y value for the circles in the sound menu*/
+
+int circle_position_LED = 27; /*!< @brief Integer that stores the initial y value for the circles in the LED menu*/
+
+int circle_position_brightness = 43; /*!< @brief Integer that stores the initial y value for the circles in the brightness menu*/
+
+int circle_position_NIGHT = 27; /*!< @brief Integer that stores the initial y value for the circles in the night menu*/
+
+int length1; /*!< @brief Integer that stores the length of the distance string to be printed in the numeric mode*/
+
+int length2; /*!< @brief Integer that stores the length of the temperature string to be printed in the numeric mode*/
+
+/** @name Constant integers
+* Constant integers used as unchangeable states for menu navigation and states of LED and buzzer
+*/
+///@{
+
+const int menu_state = 0; /*!< @brief Constant integer that signals the menu state*/
+
+const int peripherals_state = 1; /*!< @brief Constant integer that signals the peripherals state*/
+
+const int brightness_state = 2; /*!< @brief Constant integer that signals the brightness state*/
+
+const int sound_state = 3; /*!< @brief Constant integer that signals the sound menu state*/
+
+const int ledMenu_state = 4; /*!< @brief Constant integer that signals the LED menu state*/
+
+const int nightMenu_state = 5; /*!< @brief Constant integer that signals the night menu state*/
+
+const int modes_state = 6; /*!< @brief Constant integer that signals the modes state state*/
+
+const int parkingMode_state = 7; /*!< @brief Constant integer that signals the parking mode state*/
+
+const int numericMode_state = 8; /*!< @brief Constant integer that signals the numeric mode state*/
+
+const int LEDON_state = 9; /*!< @brief Constant integer that signals the LED is ON state*/
+
+const int LEDOFF_state = 10; /*!< @brief Constant integer that signals the LED is OFF state*/
+
+const int buzzerON_state = 11; /*!< @brief Constant integer that signals the buzzer is ON state*/
+
+const int buzzerOFF_state = 12; /*!< @brief Constant integer that signals the buzzer is OFF state*/
+///@}
+
+char buffer[14]; /*!< @brief Char that stores an array of characters to print the distance in the numeric mode*/
+
+char buffer2[14]; /*!< @brief Char that stores an array of characters to print the temperature in the numeric mode and parking mode*/
+
+//Flags
+/** @name Interrupt Service Routine Flags
+* Set flags in ISR
+*/
+///@{
+volatile int g_timer_flag = 0; /*!< @brief Volatile integer that serves as a time triggered ISR for the sensor ticker*/
+
+volatile int g_timer1_flag = 0; /*!< @brief Volatile integer that serves as a time triggered ISR for the button debouncer ticker*/
+
+volatile int g_timer2_flag = 0; /*!< @brief Volatile integer that serves as a time triggered ISR for the potentiometer reading ticker*/
+
+volatile int g_topButton_flag = 0; /*!< @brief Volatile integer that serves as an event triggered ISR for when the top button is pressed*/
+
+volatile int g_botButton_flag = 0; /*!< @brief Volatile integer that serves as an event triggered ISR for when the bottom button is pressed*/
+///@}
+
+#endif
diff -r 000000000000 -r 4a66fd16c223 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu May 05 09:41:14 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/99a22ba036c9
\ No newline at end of file