(DA) Internet of Things and Smart Electronics- ELE3006M2122 / Mbed 2 deprecated Final_Project_V15_DLeaming_25574043

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* 
00002 * Prints a string of characters to the screen buffer, string is cut off after the 83rd  pixel.
00003 * @param x - the column number (0 to 83)
00004 * @param y - the row number (0-5) - the display is split into 6 banks - each bank can be considered a row
00005 *
00006 * @ File main.cpp
00007 * @ Author - David Leaming - 25574043
00008 * @ Date - January 2022
00009 *
00010 * Acknowledgements 
00011 * Craig A. Evans, University of Leeds, TMP102 Library, Feb 2016
00012 * Dr Edmond Nurellari, University of Lincoln, Joystick, N5110 Libraries & SD Card Libraries
00013 * Paul Staron, Piezo Buzzer utility, April 2019
00014 */ 
00015 
00016 #include "mbed.h"                                                               // include the library header, ensure the library has been imported into the project
00017 #include "TMP102.h"
00018 #include "N5110.h"
00019 #include "Joystick.h"
00020 #include "Bitmap.h"
00021 #include "SDFileSystem.h"
00022 
00023 TMP102 tmp102(I2C_SDA,I2C_SCL);                                                 // Create TMP102 object  
00024 
00025 //        VCC,SCE,RST,D/C,MOSI,SCLK,LED 
00026 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);                                      // Create Screen Object - K64F - pwr from 3V3, GND Pin also needs connecting
00027 
00028 //                  y     x     button
00029 Joystick joystick(PTB10,PTB11,PTC16);                                           // Define Joystick Object 
00030 
00031 SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd");                                  // MOSI, MISO, SCK, CS - Connections to SD card holder on K64F (SPI interface)
00032 
00033 Serial pc(USBTX,USBRX);                                                         // UART connection for PC
00034 
00035 struct State {                                                                  // Struct for state
00036     int output;                                                                 // output value
00037     float time;                                                                 // time in state
00038     int nextState[9];                                                           // array of next states
00039 };
00040 
00041 State fsm[11] = {
00042     {15,0.5,{0,1,0,0,0,10,0,0,0}},                                              // State 0 - 15 Degrees
00043     {16,0.5,{1,2,1,1,1,0,1,1,1}},                                               // State 1 - 16 Degrees
00044     {17,0.5,{2,3,2,2,2,1,2,2,2}},                                               // State 2 - 17 Degrees
00045     {18,0.5,{3,4,3,3,3,2,3,3,3}},                                               // State 3 - 18 Degrees
00046     {19,0.5,{4,5,4,4,4,3,4,4,4}},                                               // State 4 - 19 Degrees
00047     {20,0.5,{5,6,5,5,5,4,5,5,5}},                                               // State 5 - 20 Degrees
00048     {21,0.5,{6,7,6,6,6,5,6,6,6}},                                               // State 6 - 21 Degrees
00049     {22,0.5,{7,8,7,7,7,6,7,7,7}},                                               // State 7 - 22 Degrees
00050     {23,0.5,{8,9,8,8,8,7,8,8,8}},                                               // State 8 - 23 Degrees
00051     {24,0.5,{9,10,9,9,9,8,9,9,9}},                                              // State 9 - 24 Degrees
00052     {25,0.5,{10,1,10,10,10,9,10,10,10}}                                         // State 10 - 25 Degrees   
00053 };
00054 
00055 Ticker ticker_menu;                                                             // Create Menu ticker object
00056 
00057 DigitalOut r_led(LED_RED);                                                      // K64F on-board LEDs 
00058 DigitalOut g_led(LED_GREEN);                                                    // K64F on-board LEDs 
00059 DigitalOut b_led(LED_BLUE);                                                     // K64F on-board LEDs 
00060 
00061 PwmOut LED01 (PTA1);                                                            // PCB Surface Mounted LED's - LED1
00062 PwmOut LED02 (PTA2);                                                            // PCB Surface Mounted LED's - LED2
00063 PwmOut LED03 (PTC2);                                                            // PCB Surface Mounted LED's - LED3
00064 PwmOut LED04 (PTC3);                                                            // PCB Surface Mounted LED's - LED4
00065 PwmOut LED05 (PTC4);                                                            // PCB Surface Mounted LED's - LED5
00066 PwmOut LED06 (PTD3);                                                            // PCB Surface Mounted LED's - LED6
00067 
00068 PwmOut Buzzer (PTC10);                                                          // PCB Surface Mounted Piezo Buzzer
00069 
00070 InterruptIn sw2(SW2);                                                           // K64F on-board switches
00071 InterruptIn sw3(SW3);                                                           // K64F on-board switches
00072 InterruptIn ButtonA (PTB9);                                                     // PCB Button - A
00073 InterruptIn ButtonB (PTD0);                                                     // PCB Button - B
00074 InterruptIn ButtonBack (PTB10);                                                 // PCB Button - Back
00075 
00076 volatile int g_ButtonA_flag = 0;                                                // Flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global
00077 volatile int g_ButtonB_flag = 0;                                                // Flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global
00078 volatile int g_ButtonBack_flag = 0;                                             // Flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global
00079 volatile int g_sw2_flag = 0;                                                    // Flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global
00080 volatile int g_menu_timer_flag = 0;                                             // Flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global
00081 volatile int option = 0;                                                        // Menu option selection based on joystick direction
00082 volatile int g_state = 0;                                                       // 
00083 volatile int g_StartTemp = 0;                                                   // 
00084 
00085 void error();                                                                   // error function hangs flashing an LED
00086 void init_serial();                                                             // setup serial port
00087 void init_K64F();                                                               // set-up the on-board LEDs and switches
00088 void init_PCB();                                                                // set-up the PCB LEDs and buttons
00089 void ButtonA_isr();                                                             // 
00090 void ButtonB_isr();                                                             // 
00091 void ButtonBack_isr();                                                          // 
00092 void sw2_isr();                                                                 // 
00093 void menu_timer_isr();                                                          // 
00094 void OnStartup();                                                               // 
00095 void Run();                                                                     // 
00096 void StartTemp();                                                               //
00097 void delete_file(char filename[]);                                              //
00098 void WriteToFile();                                                             // Function to attempt to write temperature to file
00099 
00100 int main()
00101 {
00102     init_K64F();                                                                // initialise the board
00103     init_serial();                                                              // initialise the serial port
00104     init_PCB();                                                                 // initialise the PCB
00105         
00106     tmp102.init();                                                              // call the sensor init method using dot syntax
00107     lcd.init();                                                                 // initialise display
00108     joystick.init();                                                            // initialise joystick
00109     
00110     ticker_menu.attach(&menu_timer_isr,0.2);                                    // Attach ticker for the Joystick
00111     
00112     sw2.fall(&sw2_isr);                                                         // SW2 has a pull-up resistor, so the pin will be at 3.3 V by default and fall to 0 V when pressed. We therefore need to look for a falling edge on the pin to fire the interrupt
00113     ButtonA.rise(&ButtonA_isr);                                                 // External push button, pin set to 0V by pull down command, means a rising edge is looked for
00114     ButtonB.rise(&ButtonB_isr);                                                 // External push button, pin set to 0V by pull down command, means a rising edge is looked for
00115     
00116     lcd.setContrast(0.5);                                                       // change set contrast in range 0.0 to 1.0
00117     
00118     OnStartup();                                                                // Call intro screen   
00119     Run();                                                                      // Call main-menu and functions
00120 }      
00121 
00122 void init_serial() {
00123     pc.baud(115200);                                                            // set to highest baud - ensure terminal software matches
00124 }
00125 
00126 void init_K64F() 
00127 {
00128     r_led = 1;                                                                  // on-board LEDs are active-low, so set pin high to turn them off.
00129     g_led = 1;                                                                  // on-board LEDs are active-low, so set pin high to turn them off.
00130     b_led = 1;                                                                  // on-board LEDs are active-low, so set pin high to turn them off.
00131     
00132     sw2.mode(PullNone);                                                         // since the on-board switches have external pull-ups, we should disable the internal pull-down
00133     sw3.mode(PullNone);                                                         // resistors that are enabled by default using InterruptIn
00134 }
00135 
00136 void init_PCB ()
00137 {
00138     LED01 = 1;                                                                  // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly
00139     LED02 = 1;                                                                  // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly
00140     LED03 = 1;                                                                  // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly
00141     LED04 = 1;                                                                  // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly    
00142     LED05 = 1;                                                                  // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly
00143     LED06 = 1;                                                                  // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly    
00144     
00145     Buzzer = 0;                                                                 // Ensure Piezo Buzzer is off
00146     
00147     ButtonA.mode(PullDown);                                                     // Set pin to Pull Down to OV, meaning that a rising edge is looked for when button is pressed
00148     ButtonB.mode(PullDown);                                                     // Set pin to Pull Down to OV, meaning that a rising edge is looked for when button is pressed
00149 }
00150 
00151 void ButtonA_isr()                                                              // ButtonA event-triggered interrupt
00152 {
00153     g_ButtonA_flag = 1;                                                         // set flag in ISR
00154 }
00155 
00156 void ButtonB_isr()                                                              // ButtonB event-triggered interrupt
00157 {
00158     g_ButtonB_flag = 1;                                                         // set flag in ISR
00159 }
00160 
00161 void ButtonBack_isr()                                                           // ButtonB event-triggered interrupt
00162 {
00163     g_ButtonBack_flag = 1;                                                      // set flag in ISR
00164 }
00165 
00166 void sw2_isr()                                                                  // SW2 event-triggered interrupt
00167 {
00168     g_sw2_flag = 1;                                                             // set flag in ISR
00169 }
00170 
00171 void menu_timer_isr()
00172 {
00173     g_menu_timer_flag = 1;                                                      // set flag in ISR
00174 }
00175 
00176 void OnStartup()                                                                // Run some start up display 
00177 {
00178     Buzzer.period(1.0/659.0);                                                   // Welcome sounds from Piezo
00179     Buzzer = 0.5;                                                               //                   
00180     wait(0.5);                                                                  // 
00181     Buzzer.period(1.0/494.0);                                                   // 
00182     Buzzer = 0.5;                                                               //    
00183     wait(0.5);                                                                  // 
00184     Buzzer.period(1.0/554.0);                                                   // 
00185     Buzzer = 0.5;                                                               // 
00186     wait(0.5);                                                                  // 
00187     Buzzer = 0;                                                                 // Turn off welcome sounds 
00188     lcd.clear();                                                                // Clear buffer at start of every loop
00189     lcd.printString("--------------",0,0);                                      // Can directly print strings at specified co-ordinates (must be less than 84 pixels to fit on display)
00190     lcd.printString("  Smart Cold",0,1);                                        // Just a welcome message before auto moving to main menu
00191     lcd.printString("   Storage",0,2);                                          //
00192     lcd.printString("  Monitoring",0,3);                                        // 
00193     lcd.printString("V15 - Jan 2022",0,4);                                      //
00194     lcd.printString("--------------",0,5);                                      //
00195     lcd.refresh();                                                              // Need to refresh display after setting pixels or writing strings 
00196     wait(5.0);                                                                  // Leave welcome screen on for designated amount of time
00197     lcd.clear();                                                                // Clear buffer at start of every loop
00198     lcd.refresh();                                                              // Need to refresh display after setting pixels or writing strings 
00199     lcd.printString("--------------",0,0);                                      //
00200     lcd.printString(" Use Joystick",0,1);                                       // Instruction for use of menu
00201     lcd.printString(" To Navigate",0,2);                                        //
00202     lcd.printString("",0,3);                                                    // Blank Line
00203     lcd.printString("  A = Select",0,4);                                        //
00204     lcd.printString("--------------",0,5);                                      //
00205     lcd.refresh();                                                              // Need to refresh display after setting pixels or writing strings
00206     wait(5.0);                                                                  //
00207     init_PCB();                                                                 // Ran again to ensure all LED's etc are turned off
00208     printf("Transition to Temp Selection %i\n",StartTemp);                      // Observe on serial port - ensure transition to correct screen
00209 }
00210 
00211 enum EMenuState                                                                 // An enum controlling the current state of the display.
00212 {
00213     MENUSTATE_StartTemp,                                                        // Defining each menu state to be called upon later
00214     MENUSTATE_Main,                                                             // Defining each menu state to be called upon later
00215     MENUSTATE_Monitor,                                                          // Defining each menu state to be called upon later
00216     MENUSTATE_OneOff,                                                           // Defining each menu state to be called upon later
00217     MENUSTATE_Results,                                                          // Defining each menu state to be called upon later
00218     MENUSTATE_About,                                                            // Defining each menu state to be called upon later
00219     MENUSTATE_Author,                                                           // Defining each menu state to be called upon later
00220     
00221     MENUSTATTE_Num,                                                             // This is a special enum value that just gives is a way to get the number of elements in the enum.
00222 };
00223 
00224 void Run()
00225 {
00226     int MenuState = MENUSTATE_StartTemp;                                                              // 
00227     int SelectedItem = 0;                                                       // 
00228     int NumMenuItems = 1;                                                       // 
00229     
00230     char buffer[14];                                                            // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
00231     
00232     while(1){
00233         if (g_menu_timer_flag){                                                 //        
00234             g_menu_timer_flag = 0;                                              // 
00235             
00236             bool bAButtonWasPressed = g_ButtonA_flag;                           // Get the value of the input flags and reset them
00237             bool bBButtonWasPressed = g_ButtonB_flag;                           // Get the value of the input flags and reset them
00238             g_ButtonA_flag = 0;                                                 // 
00239             g_ButtonB_flag = 0;                                                 // 
00240             
00241             lcd.clear();                                                        // clear buffer at start of every loop
00242                         
00243             int NewMenuState = MENUSTATTE_Num;                                  // The new menu we want to transition to, if any.
00244         
00245             switch(MenuState)                                                   // Update and Draw whichever menu we're on.
00246             {      
00247                 case MENUSTATE_StartTemp:                                       //
00248                 {                    
00249                     NumMenuItems = 1;                                           // Dont for get to set this when changing number of items in the menu. We need this to wrap the selection around properly etc.
00250                     if(SelectedItem >= NumMenuItems)                            // 
00251                     {
00252                         SelectedItem = 0;                                       // Something has gone wrong, reset selected item.
00253                     }
00254                     Direction d = joystick.get_direction();                               
00255                     
00256                     StartTemp();
00257                     
00258                     float g_StartTemp = fsm[g_state].output;                    // read temperature and print to lcd
00259                     pc.printf("T = %f C\n",g_StartTemp);                        // Print to serial - allows testing without device attached
00260                     printf ("Joystick Direction Points = %i\n",d);              //
00261                     printf ("State selected = %i\n", g_state);                  //
00262                     int length = sprintf(buffer," T = %.2f C",g_StartTemp);     // print formatted data to buffer - it is important the format specifier ensures the length will fit in the buffer
00263                     if (length <= 14){                                          // if string will fit on display (assuming printing at x=0)
00264                         lcd.printString("- Set Target -",0,0);                  // 
00265                         lcd.printString("---- Temp ----",0,1);                  // 
00266                         lcd.printString(buffer,0,3);                            // display on screen
00267                         lcd.printString("'A' to Select",0,5);                   //                      
00268                         lcd.refresh();      
00269                     }                                                           // need to refresh display after setting pixels or writing strings 
00270  
00271                     if(bAButtonWasPressed)                                      // If A was pressed then we transition to the selected screen.
00272                     {
00273                         if(SelectedItem == 0)                                   // If 0 line is selected, move to detailed menu 
00274                         {                                                       // Actually 0 line + 1, see circle draw and selection below
00275                             NewMenuState = MENUSTATE_Main;                      // 
00276                         }
00277                     }
00278                 }
00279                 break;                                                          //         
00280                 case MENUSTATE_Main:                                            //
00281                 {
00282                     NumMenuItems = 5;                                           // Dont for get to set this when changing number of items in the menu. We need this to wrap the selection around properly etc.
00283                     if(SelectedItem >= NumMenuItems)                            // 
00284                     {
00285                         SelectedItem = 0;                                       // Something has gone wrong, reset selected item.
00286                     }
00287                     lcd.printString("---- MENU ----",0,0);                      // Menu title and selectable options    
00288                     lcd.printString("M1 - Monitor",0,1);                        // Printed to the LCD screen
00289                     lcd.printString("M2 - One-off",0,2);                        // 
00290                     lcd.printString("Results",0,3);                             //
00291                     lcd.printString("About",0,4);                               //
00292                     lcd.printString("Author",0,5);                              // 
00293                     
00294                     if(bAButtonWasPressed)                                      // If A was pressed then we transition to the selected screen.
00295                     {
00296                         if(SelectedItem == 0)                                   // If 0 line is selected, move to detailed menu 
00297                         {                                                       // Actually 0 line + 1, see circle draw and selection below
00298                             NewMenuState = MENUSTATE_Monitor;                   // 
00299                         }
00300                         else if(SelectedItem == 1)                              // If 1 line is selected, move to detailed menu 
00301                         {                                                       // Actually 1 line + 1, see circle draw and selection below
00302                             NewMenuState = MENUSTATE_OneOff;                    // 
00303                         }
00304                         else if(SelectedItem == 2)                              // If 2 line is selected, move to detailed menu 
00305                         {                                                       // Actually 2 line + 1, see circle draw and selection below
00306                             NewMenuState = MENUSTATE_Results;                   // 
00307                         }
00308                         else if(SelectedItem == 3)                              // If 2 line is selected, move to detailed menu 
00309                         {                                                       // Actually 2 line + 1, see circle draw and selection below
00310                             NewMenuState = MENUSTATE_About;                     // 
00311                         }
00312                         else if(SelectedItem == 4)                              // If 3 line is selected, move to detailed menu 
00313                         {                                                       // Actually 3 line + 1, see circle draw and selection below
00314                             NewMenuState = MENUSTATE_Author;                    // 
00315                         }
00316                     }
00317                 }
00318                 break;                                                          // 
00319                 case MENUSTATE_Monitor:                                         // Call constant measurement menu following top menu selection
00320                 {
00321                     NumMenuItems = 1;                                           // Detail the number of items in Menu -  need this to wrap the selection around properly etc.
00322                     
00323                     if(SelectedItem >= NumMenuItems)                            // 
00324                     {
00325                         NewMenuState = MENUSTATE_Main;                          // Something has gone wrong, drop back to the main menu.
00326                     }
00327                     
00328                     float T = tmp102.get_temperature();                         // read temperature and print to lcd
00329                     pc.printf("T = %f K\n",T);                                  // Print to serial - allows testing without device attached
00330                     int length = sprintf(buffer," T = %.2f C",T);               // print formatted data to buffer - it is important the format specifier ensures the length will fit in the buffer
00331                     if (length <= 14)                                           // if string will fit on display (assuming printing at x=0)
00332                     lcd.printString("-- Constant --",0,0);                      // 
00333                     lcd.printString("- Monitoring -",0,1);                      // 
00334                     lcd.printString(buffer,0,3);                                // display on screen
00335                     lcd.printString(" 'A' to Menu",0,5);                        // 
00336                     lcd.refresh();                                              // need to refresh display after setting pixels or writing strings 
00337                     wait(0.5);                                                  // 
00338                     
00339                     if (T >= g_StartTemp + 2) {                                 // High temp alarm condition - in real world would be lot lower!!
00340                         LED01 = !LED01;                                         // Flash LED01 if temperature is over specified - Simulated starting of cold blowers
00341                         LED02 = !LED02;                                         // Flash LED02 if temperature is over specified - Simulated starting of cold blowers
00342                         LED03 = !LED03;                                         // Flash LED03 if temperature is over specified - Simulated starting of cold blowers
00343                         printf("WARNING - High Temp!! \n");                     //
00344                         Buzzer.period(1.0/554.0);                               // Warning Buzzer to extremely high 
00345                         Buzzer = 0.5;                                           // 
00346                         wait(0.5);                                              // 
00347                         Buzzer = 0;                                             //
00348                     } 
00349                     else  {
00350                         LED01 = 1;                                              // LED01 off if temperature is below specified - Simulated stopping of cold blowers
00351                         LED02 = 1;                                              // LED01 off if temperature is below specified - Simulated stopping of cold blowers
00352                         LED03 = 1;                                              // LED01 off if temperature is below specified - Simulated stopping of cold blowers
00353                         Buzzer = 0;                                             // Buzzer off if temperature is below specified - Simulated stopping of cold blowers
00354                     } 
00355                     
00356                     if (T <= g_StartTemp - 2) {                                 // High temp alarm condition - in real world would be lot lower!!
00357                         LED04 = !LED04;                                         // Flash LED01 if temperature is over specified - Simulated starting of heaters
00358                         LED05 = !LED05;                                         // Flash LED02 if temperature is over specified - Simulated starting of heaters
00359                         LED06 = !LED06;                                         // Flash LED03 if temperature is over specified - Simulated starting of heaters
00360                         printf("WARNING - Low Temp!! \n");                      //
00361                         Buzzer.period(1.0/554.0);                               // Warning Buzzer to extremely high 
00362                         Buzzer = 0.5;                                           // 
00363                         wait(0.5);                                              // 
00364                         Buzzer = 0;                                             //
00365                     } 
00366                     else  {
00367                         LED04 = 1;                                              // LED01 off if temperature is below specified - Simulated stopping of heaters
00368                         LED05 = 1;                                              // LED01 off if temperature is below specified - Simulated stopping of heaters
00369                         LED06 = 1;                                              // LED01 off if temperature is below specified - Simulated stopping of heaters
00370                         Buzzer = 0;                                             // Buzzer off if temperature is below specified - Simulated stopping of heaters
00371                     } 
00372                     if(bAButtonWasPressed)                                      // Check if button was pressed
00373                     {
00374                         if(SelectedItem == 0)                                   // 
00375                         {
00376                             NewMenuState = MENUSTATE_Main;                      // Transition back to the main menu
00377                             LED01 = 1;                                          // Turn off LED upon transition back to main menu
00378                             LED02 = 1;                                          // Turn off LED upon transition back to main menu
00379                             LED03 = 1;                                          // Turn off LED upon transition back to main menu
00380                             LED04 = 1;                                          // Turn off LED upon transition back to main menu
00381                             LED05 = 1;                                          // Turn off LED upon transition back to main menu
00382                             LED06 = 1;                                          // Turn off LED upon transition back to main menu
00383                         }
00384                     }
00385                 }
00386                 break;      
00387                 case MENUSTATE_OneOff:                                          // Call a one off measurement menu following top menu selection
00388                 {
00389                     NumMenuItems = 1;                                           // Detail the number of items in Menu -  need this to wrap the selection around properly etc.
00390                     if(SelectedItem >= NumMenuItems)                            // 
00391                     {
00392                         NewMenuState = MENUSTATE_Main;                          // Something has gone wrong, drop back to the main menu.
00393                     }
00394                     
00395                     T = tmp102.get_temperature();                         // read temperature and print to lcd
00396                     pc.printf("T = %f K\n",T);                                  // Print to serial - allows testing without device attached
00397                     int length = sprintf(buffer," T = %.2f C",T);               // print formatted data to buffer - it is important the format specifier ensures the length will fit in the buffer
00398                     if (length <= 14){                                          // if string will fit on display (assuming printing at x=0)
00399                         lcd.printString("-- One-Off --",0,0);                   // 
00400                         lcd.printString("-- Measure --",0,1);                   // 
00401                         lcd.printString(buffer,0,3);                            // display on screen
00402                         lcd.printString(" 'A' to Menu",0,5);                    // 
00403                     }
00404                                         
00405                     if(bAButtonWasPressed)                                      // Check if button was pressed
00406                     {
00407                         if(SelectedItem == 0)                                   // 
00408                         {
00409                             NewMenuState = MENUSTATE_Main;                      // Take us back to top menu
00410                         }
00411                     }                   
00412                     
00413                 }
00414                 break; 
00415                 case MENUSTATE_Results:                                         // Call Author menu following top menu selection
00416                 {
00417 
00418                     NumMenuItems = 1;                                           // Detail the number of items in Menu -  need this to wrap the selection around properly etc.
00419                     if(SelectedItem >= NumMenuItems)                            // 
00420                     {
00421                         NewMenuState = MENUSTATE_Main;                          // Something has gone wrong, drop back to the main menu.
00422                     }
00423                     
00424                     WriteToFile();                                              //
00425                     
00426                     lcd.printString("--- Results ---",0,0);                     // 
00427                     lcd.printString(" ",0,1);                                   // 
00428                     lcd.printString(" ",0,2);                                   // 
00429                     lcd.printString(" ",0,3);                                   // 
00430                     lcd.printString(" ",0,4);                                   // 
00431                     lcd.printString(" 'A' to Menu",0,5);                        // 
00432                                         
00433                     if(bAButtonWasPressed)                                      // Check if button was pressed
00434                     {
00435                         if(SelectedItem == 0)                                   // 
00436                         {
00437                             NewMenuState = MENUSTATE_Main;                      // Take us back to top menu 
00438                         }
00439                     }
00440                 }
00441                 break;     
00442                 case MENUSTATE_About:                                           // Call About menu following top menu selection
00443                 {
00444                     NumMenuItems = 1;                                           // Detail the number of items in Menu -  need this to wrap the selection around properly etc.
00445                     if(SelectedItem >= NumMenuItems)                            // 
00446                     {
00447                         NewMenuState = MENUSTATE_Main;                          // Something has gone wrong, drop back to the main menu.
00448                     }
00449                     
00450                     lcd.printString("--- About ---",0,0);                       // 
00451                     lcd.printString("ELE3006M - IoT",0,1);                      // 
00452                     lcd.printString("    Project",0,2);                         // 
00453                     lcd.printString("Uni of Lincoln",0,3);                      // 
00454                     lcd.printString(" 'A' to Menu",0,5);                        // 
00455                     lcd.refresh();                                              // 
00456                                         
00457                     if(bAButtonWasPressed)                                      // Check if button was pressed
00458                     {
00459                         if(SelectedItem == 0)                                   // 
00460                         {
00461                             NewMenuState = MENUSTATE_Main;                      // Transition back to Main Menu
00462                         }
00463                     }
00464                 }
00465                 break;
00466                 case MENUSTATE_Author:                                          // Call Author menu following top menu selection
00467                 {
00468 
00469                     NumMenuItems = 1;                                           // Detail the number of items in Menu -  need this to wrap the selection around properly etc.
00470                     if(SelectedItem >= NumMenuItems)                            // 
00471                     {
00472                         NewMenuState = MENUSTATE_Main;                          // Something has gone wrong, drop back to the main menu.
00473                     }
00474                     
00475                     lcd.printString("--- Author ---",0,0);                      // 
00476                     lcd.printString("David Leaming ",0,1);                      // 
00477                     lcd.printString("   25574043 ",0,2);                        // 
00478                     lcd.printString("  VolkerRail",0,3);                        // 
00479                     lcd.printString(" 'A' to Menu",0,5);                        // 
00480                                         
00481                     if(bAButtonWasPressed)                                      // Check if button was pressed
00482                     {
00483                         if(SelectedItem == 0)                                   // 
00484                         {
00485                             NewMenuState = MENUSTATE_Main;                      // Take us back to top menu 
00486                         }
00487                     }
00488                 }
00489                 break;
00490                 
00491                 default:
00492                 {
00493                     NewMenuState = MENUSTATE_Main;                              // Something has gone wrong, drop back to the main menu.
00494                 }
00495             };
00496             
00497             if(NewMenuState != MENUSTATTE_Num)                                  // If we have requested a new menu state.
00498             {
00499                 printf("Transitioning to MenuState: %i\n", NewMenuState);       // Observe on serial port - ensure transition to correct screen
00500                 
00501                 MenuState = NewMenuState;                                       // We want to transition the menu to a new state.
00502                 
00503                                                                                 // Do any bookkeeping needed when moving to new state.
00504                 SelectedItem = 0;                                               // Reset the selected item.   
00505                 
00506                 lcd.clear();                                                    // Clear the display for one frame on state transition.
00507             }                                                                   
00508             else
00509             {
00510                 unsigned int SelectionMarkerRadius = 4;                                                         // If we have not selected to move to a new menu.
00511                 unsigned int SelectionMarkerX = WIDTH - (2 * SelectionMarkerRadius);                            // Draw a marker circle at end of line to show which is the currently selected item.
00512                 unsigned int SelectionMarkerY = (HEIGHT / 5) * (SelectedItem + 1);                              // +1 because of the menu title being on first row
00513                 lcd.drawCircle(SelectionMarkerX, SelectionMarkerY, SelectionMarkerRadius, FILL_BLACK);          // Fill the circle black so it can be seen easily                                                   
00514                             
00515                                                                                 // Handle Joystick Input
00516                 Direction d = joystick.get_direction();                                                                 
00517                 printf("Direction = %i\n"); 
00518                 switch (joystick.get_direction())  {                            // Call to check direction joystick is pointing                                     
00519                     case N:        
00520                     {   
00521                         SelectedItem--;                                           
00522                         printf("Selection decremented to %i\n", SelectedItem); 
00523                     }
00524                     break;                                                      //
00525                     case S:   
00526                     {
00527                         SelectedItem++;
00528                         printf("Selection incremented to %i\n", SelectedItem); 
00529                     }
00530                     break;                                                      //
00531                 }
00532                 
00533                 if(SelectedItem < 0)                                            // Wrap the selection around to the start/end of the menu if it goes below 0 or above NumMenuItems.
00534                 {                   
00535                     SelectedItem = NumMenuItems - 1;                            // 
00536                 }
00537                 else if(SelectedItem >= NumMenuItems)                           //
00538                 {
00539                     SelectedItem = 0;                                           // 
00540                 }
00541             }
00542             
00543             lcd.refresh();                                                      //Finally update the display.
00544         }
00545     }       
00546 }  
00547 
00548 void StartTemp()                                                                //
00549 {
00550     Direction d = joystick.get_direction();                                     //
00551              
00552         g_StartTemp = fsm[g_state].output;                                      // set ouput depending on current state
00553         wait(fsm[g_state].time);                                                // wait in that state for desired time           
00554         g_state = fsm[g_state].nextState[d];                                    // read input and update curent state 
00555 }
00556 
00557 void WriteToFile()                                                              // Function to attempt to write temperature to file
00558 {
00559     printf("#### SD Card Example #####\n");
00560     FILE *fp;                                                                   // this is our file pointer
00561     
00562     float T = tmp102.get_temperature();                                         // read temperature and print to lcd
00563     pc.printf("T = %f K\n",T);                                                  // Print to serial - allows testing without device attached
00564     
00565     fp = fopen("OvernightTemps", "w");
00566     int ONight_Temp = T;                                                        // 
00567     
00568     if (fp == NULL) {                                                           // if it can't open the file then print error message
00569     printf("Error! Unable to open file!\n");
00570     } 
00571     else {                                                                      // opened file so can write
00572         printf("Writing to file....");
00573         for(int i = T; i <= 50; i++) {
00574             float T = 1000.0F/i;                                                // dummy variable
00575             fprintf(fp, "%d,%f\n",i,T);                                         // print formatted string to file (CSV)
00576         }
00577         printf("Done.\n");
00578         fclose(fp);                                                             // ensure you close the file after writing
00579     }
00580 }