|
STM32 Traffic Control
|
Traffic light control and GPIO management. More...
#include <stdio.h>#include <stdint.h>#include <stdbool.h>#include "stm32f446xx.h"#include "uart.h"#include "lights.h"#include "systick.h"Functions | |
| void | map_lights (void) |
| Initialize and map traffic light configuration. | |
| void | lights_update (const TrafficLight *light) |
| Update traffic light LEDs based on the current light state. | |
| void | lights_set_green (int lightNum1, int lightNum2) |
| Transition a pair of traffic lights to GREEN. | |
| uint32_t | lights_set_yellow (int lightNum1, int lightNum2) |
| Transition a pair of traffic lights from GREEN to YELLOW. | |
| uint32_t | lights_set_red (int lightNum1, int lightNum2) |
| Transition a pair of traffic lights from YELLOW to RED. | |
| void | lights_set_initial_state (void) |
| Set all traffic lights to their initial states. | |
| void | lights_init (void) |
| Initializes GPIO output pins. | |
Variables | |
| TrafficLight | Light [NUM_LIGHTS] |
| Array of Traffic light structures. | |
Traffic light control and GPIO management.
This module implements the logic and GPIO control for a multi-direction traffic system. It:
The module operates on a global array of TrafficLight structures, where each element represents one traffic light at the intersection.
| void lights_init | ( | void | ) |
Initializes GPIO output pins.
This function enables the required GPIO peripheral clocks and configures the GPIO pins connected to the traffic light LEDs as digital outputs.
| void lights_set_green | ( | int | lightNum1, |
| int | lightNum2 ) |
Transition a pair of traffic lights to GREEN.
Transitions the specified pair of traffic lights to GREEN state if currently RED. If GREEN, no state change is performed.
After updating the logical state, the corresponding GPIO outputs are updated via lights_update().
| lightNum1 | Index of the first traffic light in the pair |
| lightNum2 | Index of the second traffic light in the pair |
| void lights_set_initial_state | ( | void | ) |
Set all traffic lights to their initial states.
| uint32_t lights_set_red | ( | int | lightNum1, |
| int | lightNum2 ) |
Transition a pair of traffic lights from YELLOW to RED.
If light in the specified pair is currently YELLOW, they are transitioned to RED state.
After updating the logical state, the corresponding GPIO outputs are updated via lights_update().
| lightNum1 | Index of the first traffic light in the pair |
| lightNum2 | Index of the second traffic light in the pair |
| uint32_t lights_set_yellow | ( | int | lightNum1, |
| int | lightNum2 ) |
Transition a pair of traffic lights from GREEN to YELLOW.
If light in the specified pair is currently GREEN, they are transitioned to YELLOW state. If RED, no state change is performed.
After updating the logical state, the corresponding GPIO outputs are updated via lights_update().
| lightNum1 | Index of the first traffic light in the pair |
| lightNum2 | Index of the second traffic light in the pair |
| void lights_update | ( | const TrafficLight * | light | ) |
Update traffic light LEDs based on the current light state.
Sets or resets the RED and GREEN GPIO outputs for a single traffic light using the GPIOB BSRR register. The LED behavior is determined by the state field of the provided TrafficLight structure.
| light | Pointer to a TrafficLight structure containing the current state and GPIO pin mappings |
| void map_lights | ( | void | ) |
Initialize and map traffic light configuration.
Populates the global 'Light' array with initial traffic light states, car counts, and GPIO pin mappings. Each traffic light is assigned its corresponding RED and GREEN GPIO pins and an initial state based on expected traffic flow.
High-traffic directions are initialized to GREEM, while low-traffic directions start at RED.
| TrafficLight Light[NUM_LIGHTS] |
Array of Traffic light structures.
Global array of traffic light instances.