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

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.

Detailed Description

Traffic light control and GPIO management.

This module implements the logic and GPIO control for a multi-direction traffic system. It:

  • Manages traffic light states
  • Handles state transitions
  • Updates LED outputs using atomic GPIO operations

The module operates on a global array of TrafficLight structures, where each element represents one traffic light at the intersection.

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

Array of Traffic light structures.

Global array of traffic light instances.

Note
The array size is defined by NUM_LIGHTS (4 in this case)