pain
This commit is contained in:
@@ -15,25 +15,31 @@
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
// Feeling sneaky.
|
||||
extern "C" {
|
||||
/* USER CODE END Header */
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
|
||||
extern "C" {
|
||||
|
||||
#include "main.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
|
||||
void SystemClock_Config(void);
|
||||
static void MX_GPIO_Init(void);
|
||||
|
||||
}
|
||||
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
|
||||
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
#include <stdbool.h>
|
||||
|
||||
enum class TrafficState { GREEN, YELLOW, RED };
|
||||
void SetTrafficLights(TrafficState s);
|
||||
|
||||
TrafficState currentState = TrafficState::GREEN;
|
||||
TrafficState currentState = TrafficState::RED;
|
||||
uint32_t stateStartTime = 0; //HAL_GetTick() at the start of this state
|
||||
bool buttonPressedThisCycle = false;
|
||||
bool pedestrianNextCycle = false;
|
||||
@@ -51,11 +57,11 @@ const uint32_t DURATION_RED_PED = 7000;
|
||||
* @retval int
|
||||
*/
|
||||
|
||||
|
||||
void SetTrafficLights(TrafficState s)
|
||||
{
|
||||
// reset all
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Green_Pin|Yellow_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GPIOD, Red_Pin, GPIO_PIN_SET);
|
||||
|
||||
switch (s)
|
||||
@@ -72,6 +78,7 @@ void SetTrafficLights(TrafficState s)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
@@ -84,10 +91,10 @@ int main(void)
|
||||
/* Configure the system clock */
|
||||
SystemClock_Config();
|
||||
MX_GPIO_Init();
|
||||
|
||||
|
||||
stateStartTime = HAL_GetTick();
|
||||
SetTrafficLights(currentState);
|
||||
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* USER CODE END WHILE */
|
||||
@@ -97,103 +104,86 @@ int main(void)
|
||||
switch(currentState)
|
||||
{
|
||||
case TrafficState::GREEN:
|
||||
// Record if the button was pressed during this green cycle
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
/*
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
else
|
||||
{
|
||||
pedestrianNextCycle = false;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
*/
|
||||
|
||||
else if (elapsed >= DURATION_GREEN)
|
||||
// else
|
||||
// {
|
||||
// pedestrianNextCycle = false;
|
||||
// SetTrafficLights(currentState);
|
||||
// }
|
||||
if (elapsed >= DURATION_GREEN)
|
||||
{
|
||||
currentState = TrafficState::YELLOW;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
break;
|
||||
case TrafficState::YELLOW:
|
||||
if (buttonPressedThisCycle || pedestrianNextCycle)
|
||||
pedestrianThisCycle = true;
|
||||
|
||||
case TrafficState::YELLOW:
|
||||
// Record if button pressed during yellow
|
||||
if (buttonPressedThisCycle || pedestrianNextCycle)
|
||||
{
|
||||
pedestrianThisCycle = true;
|
||||
}
|
||||
if (elapsed >= DURATION_YELLOW)
|
||||
{
|
||||
|
||||
currentState = TrafficState::RED;
|
||||
stateStartTime = now;
|
||||
buttonPressedThisCycle = false; //TODO add pressed pedestrian button in yellow
|
||||
SetTrafficLights(currentState);
|
||||
pedestrianNextCycle = false;
|
||||
// If Ped Button was pressed during GREEN or YELLOW, we need to enable WHITE this cycle
|
||||
}
|
||||
break;
|
||||
|
||||
case TrafficState::RED:
|
||||
/*
|
||||
if (pedestrianNextCycle)
|
||||
if (buttonPressedThisCycle)
|
||||
{
|
||||
pedestrianNextCycle = true;
|
||||
}
|
||||
if (pedestrianThisCycle)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_SET); // turn on Pedestrian LED
|
||||
if (elapsed >= DURATION_RED_PED)
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
pedestrianThisCycle = false;
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
buttonPressedThisCycle = false; // reset button flag on transition
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_GPIO_WritePin(GPIOD,White_Pin,GPIO_PIN_RESET);
|
||||
if(elapsed >= DURATION_RED_PED)
|
||||
if(elapsed >= DURATION_RED_NOPED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
if (buttonPressedThisCycle)
|
||||
pedestrianNextCycle = true;
|
||||
|
||||
if (pedestrianThisCycle) {
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
pedestrianThisCycle = false;
|
||||
pedestrianNextCycle = false;
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
buttonPressedThisCycle = false;
|
||||
} else {
|
||||
HAL_GPIO_WritePin(White_GPIO_Port, White_Pin, GPIO_PIN_RESET);
|
||||
if (elapsed >= DURATION_RED_NOPED)
|
||||
{
|
||||
currentState = TrafficState::GREEN;
|
||||
stateStartTime = now;
|
||||
SetTrafficLights(currentState);
|
||||
buttonPressedThisCycle = false;
|
||||
buttonPressedThisCycle = false; // reset button flag on transition
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}// switch
|
||||
/* USER CODE BEGIN 3 */
|
||||
}
|
||||
}// while
|
||||
/* USER CODE END 3 */
|
||||
}
|
||||
}// main
|
||||
|
||||
/**
|
||||
* @brief System Clock Configuration
|
||||
* @retval None
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
void SystemClock_Config(void)
|
||||
{
|
||||
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
||||
@@ -278,28 +268,27 @@ static void MX_GPIO_Init(void)
|
||||
}
|
||||
|
||||
/* USER CODE BEGIN 4 */
|
||||
//extern "C" void EXTI15_10_IRQHandler()
|
||||
//{
|
||||
// HAL_GPIO_EXTI_IRQHandler(PedButton_Pin);
|
||||
//}
|
||||
|
||||
/*
|
||||
extern "C" void EXTI15_10_IRQHandler()
|
||||
{
|
||||
HAL_GPIO_EXTI_IRQHandler(PedButton_Pin);
|
||||
}
|
||||
*/
|
||||
|
||||
extern "C" void HAL_GPIO_EXTI_CallBack(uint16_t GPIO_Pin)
|
||||
extern "C" void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
|
||||
{
|
||||
static uint32_t lastInterruptTime = 0;
|
||||
uint32_t now = HAL_GetTick();
|
||||
|
||||
// software debounce
|
||||
if (now - lastInterruptTime < 100)
|
||||
/* software debounce */
|
||||
if(now - lastInterruptTime < 100)
|
||||
{
|
||||
return;
|
||||
|
||||
}
|
||||
lastInterruptTime = now;
|
||||
|
||||
if (GPIO_Pin == PedButton_Pin)
|
||||
{
|
||||
buttonPressedThisCycle = true;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/* USER CODE END 4 */
|
||||
@@ -335,3 +324,4 @@ void assert_failed(uint8_t *file, uint32_t line)
|
||||
}
|
||||
#endif /* USE_FULL_ASSERT */
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user