Dependencies:   BSP_DISCO_F746NG

Revision:
3:1d1ced98a985
Parent:
2:f3bee2fcc2a0
Child:
4:875a2d0a996d
--- a/main.cpp	Wed Jan 08 14:41:01 2020 +0000
+++ b/main.cpp	Thu Jan 09 13:39:12 2020 +0000
@@ -2,142 +2,115 @@
     Author: Christian Andresen
     Website: xcha11.skp-dp.sde.dk
     Date: 10-01-2020
-    Brief: Basic arduino project involving sensors (Button, Touch), digital output (LED, Buzzer), and onboard LCD Screen.
+    Brief: STM32 project
+    Project includes:
+        Button/Touch sensor input, connected to LED lights and grove buzzer.
+        Infinite counter of 0-9999. Resets automatically.
+        Blue screen of death
+        Multithreading
+        Debug statements located within vital processes, for easy checking over console.
 
 */
 #include "mbed.h" // mbed-os 5
 #include "stm32746g_discovery_lcd.h" // Last updated 27/11/2019
 
-// D2-D4: DigitalIN, D5-D8: DigitalOut.
-DigitalIn button(D2);
-DigitalIn touch(D3);
-//InterruptIn button(D2);
-//InterruptIn touch(D3);
+// D2-D4: Input, D5-D8: Output.
+InterruptIn button(D2);
+InterruptIn touch(D3);
 DigitalOut led(D5);
 DigitalOut buzzer(D6);
 
 // Multithreading
-Thread firstThread;
-Thread secondThread;
+Thread CounterThread;
+Thread InputThread;
 
-// Prototype for the blue screen of death.
+// Prototypes
 void bluescreen();
-// Visible ints, used to register button presses and touch-sensor activation.
+void input();
+void buttonpress();
+void touchpress();
+
+// Button click counter, button-press bool, and determinator.
 int button_count;
+bool buttonclick = false;
+
+// Touch count, touch-sensor bool, and determinator
 int touch_count;
-// Integer used for the 9999 counter.
+bool touchclick = false;
+
+// Integers used for the 9999 counter.
 int countup;
-// restarts is an invisible int, gaining 1 in value eachtime countup reaches 9999. Used for the bluescreen() event.
 int restarts;
 
 // 9999 counter program. Added above main for multithread ability.
 void ninecounter()
-{    
-    HAL_Delay(3000);
+{
+    HAL_Delay(10000);
     while(1) {
         if (countup != 9999) {
             wait(0.001);
             countup++;
 
+        } else if (restarts == 10) {
+            break;
         } else {
-            countup = 9900;
+            countup = 0;
             restarts++;
+            printf("Counter has finished counting. Reset nr. %d. \r\n", restarts);
         }
     }
 }
-/* // USE ONLY IF TOUCH SENSOR AND BUTTON ARE INTERRUPT-IN
-void buttonpress() {
-    led = 1; // Turn LED on
-            buzzer = 1; // Emit sound from buzzer
-            wait(0.25);
-            button_count++; // Register a button has been pressed, so it can be displayed later.
-            led = 0; // Turn LED off
-            buzzer = 0; // Halt sound emmital from buzzer.
-}
 
-void touchpress() {
-    led = 1; // Turn LED on
-            wait(0.25);
-            touch_count++; // Register that the touch sensor has been triggered, so it can be displayed layer.
-            led = 0; // Turn LED off
-} */
-
-void inputcounter()
-{
-    HAL_Delay(3000);
-    while(1) {
-        //button.rise(&buttonpress); USE IF BUTTON IS INTERRUPT IN (BRUG IKKE RISE I WHILE - Flyt 2 linjer op)
-        //touch.rise(&touchpress); USE IF TOUCH SENSOR IS INTERRUPT IN
-
-        // USE IF BUTTON AND TOUCH SENSOR IS DIGITAL IN
-        // Register if the button has been pressed.
-        if(button) {
-            led = 1; // Turn LED on
-            buzzer = 1; // Emit sound from buzzer
-            wait(0.5);
-            button_count++; // Register a button has been pressed, so it can be displayed later.
-            led = 0; // Turn LED off
-            buzzer = 0; // Halt sound emmital from buzzer.
-        }
-        // Register if touchscreen has been touched.
-        else if(touch) {
-            led = 1; // Turn LED on
-            wait(0.69);
-            touch_count++; // Register that the touch sensor has been triggered, so it can be displayed layer.
-            led = 0; // Turn LED off
-        }
-        // If nothing is being toched, prevent anything from happening.
-        else {
-            led = 0;
-            buzzer = 0;
-        }
-    }
-}
 // Main part of the program
 int main()
 {
-    firstThread.start(&ninecounter); // Start multithread main and ninecounter()
-    secondThread.start(&inputcounter);
+    CounterThread.start(&ninecounter); // Start multithread ninecounter & inputcounter
+    InputThread.start(&input);
+
+    button.rise(&buttonpress);
+    touch.rise(&touchpress);
 
-    // Prepare 3 text elements for later print to LCD
-    uint8_t text1[30];
-    uint8_t text2[30];
-    uint8_t text3[30];
-    // Give integers a default value of 0
-    button_count = 0;
-    touch_count = 0;
-    countup = 9900;
-    restarts = 0;
+    uint8_t text[30]; // Prepare text element for later print to LCD
+    button_count = 0; // Amount of times the button has been pressed
+    touch_count = 0; // Amount of times the touch sensor has been activated
+    countup = 0; // Default value for the counter
+    restarts = 0; // Amount of times 9999 counter has been reset
 
     // Boot up LCD screen
     BSP_LCD_Init();
     BSP_LCD_LayerDefaultInit(LTDC_ACTIVE_LAYER, LCD_FB_START_ADDRESS);
     BSP_LCD_SelectLayer(LTDC_ACTIVE_LAYER);
-    
+
     // Clear the screen, add black background and red text
     BSP_LCD_Clear(LCD_COLOR_BLACK);
     BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
     BSP_LCD_SetTextColor(LCD_COLOR_RED);
-    
+
     // Bootup Message
-    BSP_LCD_DisplayStringAt(0, 130, (uint8_t *) "SYSTEM BOOTING", CENTER_MODE);
-    HAL_Delay(2500);
+    printf("Device is booting up \r\n");
+    BSP_LCD_DisplayStringAt(0, 100, (uint8_t *) "HELLDROP STUDIO", CENTER_MODE);
+    BSP_LCD_DisplayStringAt(0, 150, (uint8_t *) "A big hard business", CENTER_MODE);
+    BSP_LCD_DisplayStringAt(0, 175, (uint8_t *) "in a big hard building", CENTER_MODE);
+    HAL_Delay(10000);
     // Change LCD Screen to system
     BSP_LCD_Clear(LCD_COLOR_BLACK);
+    printf("Device has started functioning \r\n");
     // Studio name & department
     BSP_LCD_DisplayStringAt(0, 25, (uint8_t *) "HELLDROP STUDIO SOFTWARE", CENTER_MODE);
     BSP_LCD_DisplayStringAt(0, 250, (uint8_t *) "Code by Christian Andresen", CENTER_MODE);
 
+    // While there are less than 10 restarts of the counter..
     while(restarts < 10) {
-        sprintf((char*)text1, "Button count: %d", button_count);
-        sprintf((char*)text2, "Touch count: %d", touch_count);
-        sprintf((char*)text3, "   %d  (%d) ", countup, restarts);
-
-        BSP_LCD_DisplayStringAt(0, 75, (uint8_t *)&text1, CENTER_MODE);
-        BSP_LCD_DisplayStringAt(0, 125, (uint8_t *)&text2, CENTER_MODE);
-        BSP_LCD_DisplayStringAt(0, 175, (uint8_t *)&text3, CENTER_MODE);
+        // Combine string and integer value.
+        sprintf((char*)text, "Button count: %d", button_count);
+        // Print value to LCD screen
+        BSP_LCD_DisplayStringAt(0, 100, (uint8_t *)&text, CENTER_MODE);
+        sprintf((char*)text, "Touch count: %d", touch_count);
+        BSP_LCD_DisplayStringAt(0, 150, (uint8_t *)&text, CENTER_MODE);
+        sprintf((char*)text, "   %d  (%d) ", countup, restarts);
+        BSP_LCD_DisplayStringAt(0, 200, (uint8_t *)&text, CENTER_MODE);
     }
-    if (restarts >= 10) {
+    if (restarts == 10) { // If there are 10 restarts or more, trigger BSOD
         bluescreen();
     }
 }
@@ -145,6 +118,7 @@
 void bluescreen()
 {
     //BLUE SCREEN OF DEATH
+    printf("FATAL ERROR. SYSTEM REQUIRES MANUAL REBOOT \r\n");
     BSP_LCD_Clear(LCD_COLOR_BLUE);
     BSP_LCD_SetBackColor(LCD_COLOR_BLUE);
     BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
@@ -154,4 +128,44 @@
     BSP_LCD_DisplayStringAt(0, 150, (uint8_t *) "and needs to reboot.", CENTER_MODE);
     BSP_LCD_DisplayStringAt(0, 250, (uint8_t *) "HAL Error", LEFT_MODE);
     BSP_LCD_DisplayStringAt(0, 250, (uint8_t *) "ERROR CODE: 40", RIGHT_MODE);
+}
+
+void input()
+{
+    HAL_Delay(10000);
+    while(1) {
+        if(buttonclick == true) {
+            printf("A button has been pressed. \r\n");
+            button_count++; // Add to button counter
+            led = 1; // Turn on LED
+            buzzer = 1; // Turn on buzzer
+            wait(0.5); // Prevent immediate shutdown
+            // Turn both off
+            led = 0;
+            buzzer = 0;
+            buttonclick = false; // End button click
+        } else if(touchclick == true) {
+            printf("Touch sensor has been activated \r\n");
+            touch_count++; // Add to touch counter on the LCD
+            led = 1;
+            wait(0.5);
+            led = 0;
+            touchclick = false; // End touch click
+        } else if (restarts == 10) {
+            break;
+        } else { // Failsafe if neither are turned off during click.
+            led = 0;
+            buzzer = 0;
+        }
+    }
+}
+
+void buttonpress()
+{
+    buttonclick = true;
+}
+
+void touchpress()
+{
+    touchclick = true;
 }
\ No newline at end of file