STM32 Traffic Control
Loading...
Searching...
No Matches
lights.h File Reference

Public API for Traffic Light control and GPIO management. More...

#include <stdint.h>
#include <stdbool.h>

Go to the source code of this file.

Classes

struct  TrafficLight
 Traffic light configuration and runtime state. More...

Macros

#define PIN_LIGHT1_RED   10
 GPIO pin assignments for traffic light lEDs.
#define PIN_LIGHT1_GREEN   4
#define PIN_LIGHT2_RED   5
#define PIN_LIGHT2_GREEN   3
#define PIN_LIGHT3_RED   2
#define PIN_LIGHT3_GREEN   1
#define PIN_LIGHT4_RED   14
#define PIN_LIGHT4_GREEN   13
#define NUM_LIGHTS   4
 Total number of traffic light in the system.

Enumerations

enum  LightState { RED , YELLOW , GREEN , OFF }
 Enumeration of possible traffic light states. More...

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]
 Global array of traffic light instances.

Detailed Description

Public API for Traffic Light control and GPIO management.

Macro Definition Documentation

◆ NUM_LIGHTS

#define NUM_LIGHTS   4

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

#define PIN_LIGHT2_RED   5

◆ PIN_LIGHT3_GREEN

#define PIN_LIGHT3_GREEN   1

◆ PIN_LIGHT3_RED

#define PIN_LIGHT3_RED   2

◆ PIN_LIGHT4_GREEN

#define PIN_LIGHT4_GREEN   13

◆ PIN_LIGHT4_RED

#define PIN_LIGHT4_RED   14

Enumeration Type Documentation

◆ LightState

enum LightState

Enumeration of possible traffic light states.

Enumerator
RED 

Stop

YELLOW 

Prepare to stop

GREEN 

Go

OFF 

Light turned off

Function Documentation

◆ 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
lightNum1Index of the first traffic light in the pair
lightNum2Index 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
lightNum1Index of the first traffic light in the pair
lightNum2Index 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
lightNum1Index of the first traffic light in the pair
lightNum2Index of the second traffic light in the pair
Returns

◆ lights_update()

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.

Parameters
lightPointer to a TrafficLight structure containing the current state and GPIO pin mappings

◆ map_lights()

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.

Variable Documentation

◆ Light

TrafficLight Light[NUM_LIGHTS]
extern

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)