Felix HARERIMANA / Mbed 2 deprecated DISCO_F413ZH-trafficproject-SOUND

Dependencies:   mbed

Revision:
7:667534ecd34e
Parent:
6:14626bbcb236
Child:
8:3dd7a4231f6e
--- a/main.cpp	Sun Nov 01 13:34:58 2020 +0000
+++ b/main.cpp	Thu Nov 05 14:14:43 2020 +0000
@@ -1,31 +1,97 @@
-#include "mbed.h"
-DigitalOut trafficYellowlight(p7);
-DigitalOut trafficRedlight(p9);
-DigitalIn pedestrainButton(p8);
-int main() {
-    trafficYellowlight=1;
-    trafficRedlight=0;
-    while(1) 
+#include "mbed.h" // Include the library of Mbed
+#include "C12832.h"  // Include the library of the specific LCD
+
+DigitalOut yellowled(p5, 1); // Initializing the yellow light ON (same as yellowled = 1)
+DigitalOut redled(p7, 0); // Initializing the Red light to OFF (same as redled = 0)
+InterruptIn pedestrianbutton(p6);
+
+C12832 lcd(SPI_MOSI, SPI_SCK, SPI_MISO, p8, p11); // create an instance of LCD
+
+int state = 0;  // State variable indicating that YELLOW LIGHT IS ON
+
+char pedestrianAdvert[] = "2 COFFEES FOR THE PRICE OF 1. ONLY AT CAMELIA";
+char carDriverAdvert[] = "RECEIVE 5%% REDUCTION ON YOUR FULL TANK ....";
+
+#define YELLOWBLINKINGDURATION 10
+#define REDLIGHTONDURATION 15
+
+
+/**
+ * Blinking a yellow LED.
+ *
+ * @param duration length of time to blink .
+ */
+static void yellowblinking(int duration){
+    int counter=0; 
+    while(counter < duration)
     {
-        if(pedestrainButton&trafficYellowlight){
-            int counter=0;
-            printf("counter is now%d\n",counter);
-            // blink for 5 seconds
-            while(counter<10){
-                counter+=1;
-                trafficYellowlight=!trafficYellowlight;
-                wait_ms(5000);//delay of 5 second
-            }
-            trafficYellowlight=0;//switch off
-             trafficRedlight=1; // set trafficReslight on
-             }
-             if(!trafficYellowlight){
-                 trafficYellowlight=!pedestrainButton; // yellow and red lights are switched opposetely
-                 trafficRedlight=!trafficYellowlight; // yellow and red lights are switched opposetely
-             }
-        
-             wait_ms(5000); // wait for 5 second
-               }
+        counter+=1;
+        yellowled = !yellowled;
+        wait(1);
+    }
+    yellowled = 0;
+}
+
+/**
+ * Crossing pedestrian.
+ *
+ * @param duration length of time for pedestrian to cross.
+ */
+static void pedestriancrossing(int duration){
+    redled = 1;
+    int counter=0; 
+    while(counter < duration)
+    {
+        counter+=1;
+        wait(1);
+    }
+    redled = 0;
+    yellowled = 1;
+}
+
+/**
+ * Display content on LCD.
+ *
+ * @param advert The advertising text to be displayed.
+ */
+static void display(char *advert) {
+    lcd.cls();  // Clear LCD
+    lcd.locate(10, 5); // get cursor to position x=3px and y=5px
+    lcd.printf(advert); // Write text into LCD buffer
+    lcd.copy_to_lcd();
 }
 
 
+/**
+ * Callback (Interrupt Service Routine) when a bouton is clicked.
+ *
+ */
+void pedestrian_isr() {
+     wait(1);
+     if (state == 0) // YELLOW IS ON AND RED IS OFF
+     {
+        state = 1; // YELLOW IS BLINKING AND RED IS OFF
+        display(pedestrianAdvert);
+        yellowblinking(YELLOWBLINKINGDURATION);
+        wait(1);
+        state = 2;  // YELLOW IS OFF AND RED IS ON
+        display(carDriverAdvert);
+        pedestriancrossing(REDLIGHTONDURATION);
+        state = 0;
+        lcd.cls();
+     }
+     if(state == 2) // RED 
+     {
+         // extend the duration of RED by 5 seconds
+     }
+}
+
+int main() {
+    pedestrianbutton.rise(&pedestrian_isr); // Registering an ISR for button click
+    while (1) { // DO FOREVER 
+       wait(1);
+    }
+}
+
+
+