Public API for Traffic Light control and GPIO management.
More...
#include <stdint.h>
#include <stdbool.h>
Go to the source code of this file.
|
| 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.
|
Public API for Traffic Light control and GPIO management.
◆ NUM_LIGHTS
Total number of traffic light in the system.
◆ PIN_LIGHT1_GREEN
| #define PIN_LIGHT1_GREEN 4 |
◆ PIN_LIGHT1_RED
| #define PIN_LIGHT1_RED 10 |
GPIO pin assignments for traffic light lEDs.
◆ PIN_LIGHT2_GREEN
| #define PIN_LIGHT2_GREEN 3 |
◆ PIN_LIGHT2_RED
◆ PIN_LIGHT3_GREEN
| #define PIN_LIGHT3_GREEN 1 |
◆ PIN_LIGHT3_RED
◆ PIN_LIGHT4_GREEN
| #define PIN_LIGHT4_GREEN 13 |
◆ PIN_LIGHT4_RED
| #define PIN_LIGHT4_RED 14 |
◆ LightState
Enumeration of possible traffic light states.
| Enumerator |
|---|
| RED | Stop
|
| YELLOW | Prepare to stop
|
| GREEN | Go
|
| OFF | Light turned off
|
◆ lights_init()
| 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.
- Note
- Pins are configured in push-pull output mode with default speed and no internal pull-up or pull-down resistors.
◆ lights_set_green()
| 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().
- Parameters
-
| lightNum1 | Index of the first traffic light in the pair |
| lightNum2 | Index of the second traffic light in the pair |
◆ lights_set_initial_state()
| void lights_set_initial_state |
( |
void | | ) |
|
Set all traffic lights to their initial states.
◆ lights_set_red()
| 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().
- Parameters
-
| lightNum1 | Index of the first traffic light in the pair |
| lightNum2 | Index of the second traffic light in the pair |
- Returns
◆ lights_set_yellow()
| 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().
- Parameters
-
| lightNum1 | Index of the first traffic light in the pair |
| lightNum2 | Index of the second traffic light in the pair |
- Returns
◆ lights_update()
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.
- Parameters
-
| light | Pointer to a TrafficLight structure containing the current state and GPIO pin mappings |
◆ map_lights()
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.
◆ Light
Global array of traffic light instances.
Global array of traffic light instances.
- Note
- The array size is defined by NUM_LIGHTS (4 in this case)