From a88261240326be9c51c8dbdea5962f4e5706395b Mon Sep 17 00:00:00 2001
From: k2-alfadhala <khalid2.al-fadhala@live.uwe.ac.uk>
Date: Sat, 22 Jul 2023 22:57:19 +0000
Subject: [PATCH] added updates main.c

---
 Lab3/Src/main.c | 117 ++++++++++++++++++++++++++++++------------------
 1 file changed, 74 insertions(+), 43 deletions(-)

diff --git a/Lab3/Src/main.c b/Lab3/Src/main.c
index f7cd805..00ba774 100644
--- a/Lab3/Src/main.c
+++ b/Lab3/Src/main.c
@@ -12,13 +12,20 @@
 
 #include "main.h"
 
-//declaration for the GPIO pins for the LED
+// Declaration for the GPIO pins for the LED
 
 #define LED2_PIN                         GPIO_PIN_14
 #define LED2_GPIO_PORT                   GPIOB
 #define LED2_GPIO_CLK_ENABLE()           __HAL_RCC_GPIOB_CLK_ENABLE()
 #define LED2_GPIO_CLK_DISABLE()          __HAL_RCC_GPIOB_CLK_DISABLE()
 
+// Declaration for the GPIO pins for the wakeup Button
+
+#define BLUE_BUTTON_PIN                   GPIO_PIN_13
+#define BLUE_BUTTON_GPIO_PORT             GPIOC
+#define BLUE_BUTTON_GPIO_CLK_ENABLE()     __HAL_RCC_GPIOC_CLK_ENABLE()
+#define BLUE_BUTTON_GPIO_CLK_DISABLE()    __HAL_RCC_GPIOC_CLK_DISABLE()
+
 /* Private function prototypes -----------------------------------------------*/
 static void SystemClock_Config(void);
 void LED2_Init(void);
@@ -27,38 +34,56 @@ void LED2_Off(void);
 void LED2_DeInit(void);
 void LED2_Toggle(void);
 
+void Blue_PB_Init(void);
+void Blue_PB_DeInit(void);
+uint32_t Blue_PB_GetState(void);
+
 
 int main(void)
 {
-/* STM32L4xx HAL library initialization:
-       - Configure the Flash prefetch, Flash preread and Buffer caches
-       - Systick timer is configured by default as source of time base, but user 
-             can eventually implement his proper time base source (a general purpose 
-             timer for example or other time source), keeping in mind that Time base 
-             duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
-             handled in milliseconds basis.
-       - Low Level Initialization
-     */
+  /* STM32L4xx HAL library initialization:
+     - Configure the Flash prefetch, Flash preread and Buffer caches
+     - Systick timer is configured by default as source of time base, but user 
+       can eventually implement his proper time base source (a general purpose 
+       timer for example or other time source), keeping in mind that Time base 
+       duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
+       handled in milliseconds basis.
+     - Low Level Initialization
+  */
   HAL_Init();
 
   /* Configure the System clock to have a frequency of 80 MHz */
   SystemClock_Config();
 
   /* Configure the User LED */
-
   LED2_Init();
-  /* turn the LED on */
-  LED2_On();
-
-    /* loop for ever */
-    while (1)
-      {
-	LED2_On();
-	HAL_Delay(1000);  //delay for 1000 milliseconds - namely 1 second
-	LED2_Off();
-	HAL_Delay(1000);  //delay for 1000 milliseconds - namely 1 second
-      }
 
+  /* Configure the blue button */
+  Blue_PB_Init();
+
+  /* Turn the LED off initially */
+  LED2_Off();
+
+  /* Loop forever */
+  while (1)
+  {
+    uint32_t but_state = Blue_PB_GetState();
+    if (but_state == 1)
+    {
+      LED2_On();
+      HAL_Delay(200);
+      LED2_Off();
+      HAL_Delay(200);
+      LED2_On();
+      HAL_Delay(200);
+      LED2_Off();
+      HAL_Delay(200);
+    }
+    else
+    {
+      LED2_Off();
+    }
+  }
 }
 
 static void SystemClock_Config(void)
@@ -98,13 +123,8 @@ static void SystemClock_Config(void)
   }
 }
 
-/*
-Inititalise the LED2 GPIO port
-*/
-
 void LED2_Init(void)
 {
-
    GPIO_InitTypeDef  gpio_init_structure;
   
   LED2_GPIO_CLK_ENABLE();
@@ -117,12 +137,7 @@ void LED2_Init(void)
   HAL_GPIO_Init(LED2_GPIO_PORT, &gpio_init_structure);
 }
 
-/*
-
-deinit the GPIO for LED2
-
-*/
-void LED2_DeInit()
+void LED2_DeInit(void)
 {
   GPIO_InitTypeDef  gpio_init_structure;
   
@@ -134,21 +149,11 @@ void LED2_DeInit()
   HAL_GPIO_DeInit(LED2_GPIO_PORT, gpio_init_structure.Pin);
 }
 
-
-/*
-
-Turn LED2 on
-
-*/
 void LED2_On(void)
 {
   HAL_GPIO_WritePin(LED2_GPIO_PORT, LED2_PIN, GPIO_PIN_SET);
 }
 
-/* 
-turn LED2 off
-*/
-
 void LED2_Off(void)
 {
   HAL_GPIO_WritePin(LED2_GPIO_PORT, LED2_PIN, GPIO_PIN_RESET);
@@ -159,3 +164,29 @@ void LED2_Toggle(void)
   HAL_GPIO_TogglePin(LED2_GPIO_PORT, LED2_PIN);
 }
 
+void Blue_PB_Init(void)
+{
+  GPIO_InitTypeDef gpio_init_structure;
+
+  /* Enable the BUTTON clock */
+  BLUE_BUTTON_GPIO_CLK_ENABLE();
+
+  /* Configure Button pin as input */
+  gpio_init_structure.Pin = BLUE_BUTTON_PIN;
+  gpio_init_structure.Mode = GPIO_MODE_INPUT;
+  gpio_init_structure.Pull = GPIO_PULLUP;
+  gpio_init_structure.Speed = GPIO_SPEED_FREQ_HIGH;
+  HAL_GPIO_Init(BLUE_BUTTON_GPIO_PORT, &gpio_init_structure);
+}
+
+void Blue_PB_DeInit(void)
+{
+  GPIO_InitTypeDef gpio_init_structure;
+  gpio_init_structure.Pin = BLUE_BUTTON_PIN;
+  HAL_GPIO_DeInit(BLUE_BUTTON_GPIO_PORT, gpio_init_structure.Pin);
+}
+
+uint32_t Blue_PB_GetState(void)
+{
+  return HAL_GPIO_ReadPin(BLUE_BUTTON_GPIO_PORT, BLUE_BUTTON_PIN);
+}
-- 
GitLab