New version - adding of lights/controls to temperature monitoring

Dependencies:   mbed N5110v02 TMP102 JoystickIoT

Revision:
3:80c1eba78f9b
Parent:
2:e0091b5311f1
Child:
4:0090145b3286
diff -r e0091b5311f1 -r 80c1eba78f9b main.cpp
--- a/main.cpp	Fri Dec 17 08:34:45 2021 +0000
+++ b/main.cpp	Tue Dec 21 17:12:38 2021 +0000
@@ -1,6 +1,5 @@
 /* * Print String
 * @ File main.cpp
-* Prints a string of characters to the screen buffer, string is cut off after the 83rd  pixel.
 * @param x - the column number (0 to 83)
 * @param y - the row number (0-5) - the display is split into 6 banks - each bank can be considered a row
 * @author - David Leaming - 25574043
@@ -23,6 +22,8 @@
 //        VCC,SCE,RST,D/C,MOSI,SCLK,LED 
 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);                                      // Create Screen Object - K64F - pwr from 3V3, GND Pin also needs connecting
 
+//                  y     x     button
+Joystick joystick(PTB10,PTB11,PTC16);                                           // Define Joystick Object 
 
 Serial pc(USBTX,USBRX);                                                         // UART connection for PC
 
@@ -30,55 +31,103 @@
 DigitalOut g_led(LED_GREEN);                                                    // K64F on-board LEDs 
 DigitalOut b_led(LED_BLUE);                                                     // K64F on-board LEDs 
 
+DigitalOut LED01 (PTA1);                                                        // PCB Surface Mounted LED's - LED1
+DigitalOut LED02 (PTA2);                                                        // PCB Surface Mounted LED's - LED2
+DigitalOut LED03 (PTC2);                                                        // PCB Surface Mounted LED's - LED3
+DigitalOut LED04 (PTC3);                                                        // PCB Surface Mounted LED's - LED4
+DigitalOut LED05 (PTC4);                                                        // PCB Surface Mounted LED's - LED5
+DigitalOut LED06 (PTD3);                                                        // PCB Surface Mounted LED's - LED6
+
 InterruptIn sw2(SW2);                                                           // K64F on-board switches
 InterruptIn sw3(SW3);                                                           // K64F on-board switches
+InterruptIn ButtonA (PTB9);                                                     // PCB Button - A
+InterruptIn ButtonB (PTD0);                                                     // PCB Button - B
+
+volatile int g_ButtonA_flag=0;                                                  // flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global
+volatile int g_ButtonB_flag=0;                                                  // flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global
+volatile int g_sw2_flag=0;                                                      // flag - must be volatile as changes within ISR - g_ prefix makes it easier to distinguish it as global
 
 void error();                                                                   // error function hangs flashing an LED
 void init_serial();                                                             // setup serial port
 void init_K64F();                                                               // set-up the on-board LEDs and switches
+void init_PCB();                                                                // set-up the PCB LEDs and buttons
+void ButtonA_isr();
+void ButtonB_isr();
+void sw2_isr();
 
 int main()
 {
     init_K64F();                                                                // initialise the board
     init_serial();                                                              // initialise the serial port
-    
+    init_PCB();                                                                 // initialise the PCB
+        
     tmp102.init();                                                              // call the sensor init method using dot syntax
     lcd.init();                                                                 // initialise display
+    joystick.init();                                                            // initialise joystick
+    
+    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
+    ButtonA.rise(&ButtonA_isr);                                                 // External push button, pin set to 0V by pull down command, means a rising edge is looked for
+    ButtonB.rise(&ButtonB_isr);                                                 // External push button, pin set to 0V by pull down command, means a rising edge is looked for
     
     lcd.setContrast(0.5);                                                       // change set contrast in range 0.0 to 1.0
     
+    char buffer[14];                                                            // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
+    
+        lcd.clear();                                                            // clear buffer at start of every loop
+        lcd.printString("Smart Cold",0,0);                                      // can directly print strings at specified co-ordinates (must be less than 84 pixels to fit on display)
+        lcd.printString("Storage",0,1);                                         // Just a welcome message before auto moving to main menu
+        lcd.printString("Monitoring",0,2);
+        lcd.printString("",0,2);                                                // Blank Line
+        lcd.printString("V1.0 - 2021",0,4);
+        lcd.printString("David Leaming",0,5);
+        lcd.refresh();                                                          // need to refresh display after setting pixels or writing strings 
+        wait(5.0);                                                              // leave welcome screen on for designated amount of time
+        lcd.clear();                                                            // clear buffer at start of every loop
+        lcd.refresh();                                                          // need to refresh display after setting pixels or writing strings 
+        lcd.printString("Use Joystick",0,0);                                    // Instruction for use of menu
+        lcd.printString("To Navigate",0,1);
+        lcd.printString("",0,2);                                                // Blank Line
+        lcd.printString("A = Select",0,3);  
+        lcd.printString("B = Back",0,4);   
+        lcd.refresh();                                                          // need to refresh display after setting pixels or writing strings
+        wait(5.0);  
+        
+        MainMenu:                                                               // Initial Main Menu Creation to allow mode to be selected
+            
+              
+        
     while (1) {
         
         lcd.normalMode();                                                       // normal colour mode
         lcd.setBrightness(0.5);                                                 // put LED backlight on 50%
         lcd.clear();                                                            // clear buffer at start of every loop
-        
-        char buffer[14];                                                        // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
-
-        
-        int temperature = 27;
-        int length = sprintf(buffer,"T = %2d C",temperature);                   // print formatted data to buffer - it is important the format specifier ensures the length will fit in the buffer
-        if (length <= 14)                                                       // if string will fit on display (assuming printing at x=0)
-        lcd.printString(buffer,0,1);                                            // display on screen
-
-        float pressure = 1012.3;                                                // same idea with floats
-        length = sprintf(buffer,"P = %.2f mb",pressure);
-        if (length <= 14)
-        lcd.printString(buffer,0,2);
-
-        lcd.refresh();                                                          // need to refresh display after setting pixels or writing strings
-        wait(2.0);     
-        
-        lcd.clear();                                                            // clear buffer at start of every loop
         lcd.refresh();                                                          // need to refresh display after setting pixels or writing strings 
-        wait(0.5);
         
         float T = tmp102.get_temperature();                                     // read temperature and print to lcd
-        length = sprintf(buffer,"T = %.2f C",T);
-        if (length <= 14)
-        lcd.printString(buffer,0,2);
+        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
+        if (length <= 14)                                                       // if string will fit on display (assuming printing at x=0)
+        lcd.printString(buffer,0,2);                                            // display on screen
         lcd.refresh();                                                          // need to refresh display after setting pixels or writing strings 
         wait(2.0);
+                    
+            if (g_ButtonA_flag) {                                               // check if flag i.e. interrupt has occured
+            g_ButtonA_flag = 0;                                                 // if it has, clear the flag
+
+            printf("Button A Press \n");                                        // send message over serial port - can observe in CoolTerm etc.
+            LED01=0;
+            wait(5.0);
+            LED01=1;     
+      }
+      
+            if (g_ButtonB_flag) {                                               // check if flag i.e. interrupt has occured
+            g_ButtonB_flag = 0;                                                 // if it has, clear the flag
+
+            printf("Button B Press \n");                                        // send message over serial port - can observe in CoolTerm etc.
+            LED02=0;
+            wait(5.0);
+            LED02=1;
+                
+      }
         
     }
 
@@ -98,3 +147,32 @@
     sw3.mode(PullNone);                                                         // resistors that are enabled by default using InterruptIn
 
 }
+
+void init_PCB ()
+{
+    LED01 = 1;                                                                  // PCB surface mounted LED's are active low - write a 1 to turn them off initiallly
+    LED02 = 1;                                     
+    LED03 = 1;
+    LED04 = 1;
+    LED05 = 1;
+    LED06 = 1;
+    
+    ButtonA.mode(PullDown);                                                     // Set pin to Pull Down to OV, meaning that a rising edge is looked for when button is pressed
+    ButtonB.mode(PullDown);                                                     // Set pin to Pull Down to OV, meaning that a rising edge is looked for when button is pressed
+    
+}
+
+void ButtonA_isr()                                                              // ButtonA event-triggered interrupt
+{
+    g_ButtonA_flag = 1;                                                         // set flag in ISR
+}
+
+void ButtonB_isr()                                                              // ButtonB event-triggered interrupt
+{
+    g_ButtonB_flag = 1;                                                         // set flag in ISR
+}
+
+void sw2_isr()                                                                  // SW2 event-triggered interrupt
+{
+    g_sw2_flag = 1;                                                             // set flag in ISR
+}
\ No newline at end of file