From e304ce76cb2e66b3009cee4386d9de098e09ce94 Mon Sep 17 00:00:00 2001 From: Omar Magdy Date: Tue, 23 Aug 2022 15:17:50 +0200 Subject: Added a Traffic struct and made the code more generic and easy to maintain --- main.c | 78 +++++++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 54 insertions(+), 24 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index 64cc188..2826b11 100644 --- a/main.c +++ b/main.c @@ -1,26 +1,33 @@ #include "trafficlight.h" -uint8_t cur_color_1 = RED_TF1; -uint8_t cur_color_2 = YELLOW_TF2; -uint32_t period_1 = RED_PERIOD; -uint32_t period_2 = GREEN_PERIOD; +uint32_t period_1 = RED_PERIOD; +uint32_t period_2 = GREEN_PERIOD; +uint32_t period_3 = RED_PERIOD; + +Traffic tf1 = {RED, GPIO_PORTF_BASE, 1, 2, 3}; +Traffic tf2 = {YELLOW, GPIO_PORTA_BASE, 2, 3, 4}; +Traffic tf_ped = {RED, GPIO_PORTB_BASE, 5, 0, 1}; + +void Switch_Handler() { + +} void Timer0_Handler() { TimerIntClear(TIMER0_BASE, TIMER_BOTH); - switch(cur_color_1) { - case RED_TF1: - cur_color_1 = GREEN_TF1; - set_color(cur_color_1); + switch(tf1.cur_color) { + case RED: + tf1.cur_color = GREEN; + set_tf_color(tf1, GREEN); TimerLoadSet(TIMER0_BASE, TIMER_BOTH, GREEN_PERIOD); break; - case YELLOW_TF1: - cur_color_1 = RED_TF1; - set_color(cur_color_1); + case YELLOW: + tf1.cur_color = RED; + set_tf_color(tf1, RED); TimerLoadSet(TIMER0_BASE, TIMER_BOTH, RED_PERIOD); break; - case GREEN_TF1: - cur_color_1 = YELLOW_TF1; - set_color(cur_color_1); + case GREEN: + tf1.cur_color = YELLOW; + set_tf_color(tf1, YELLOW); TimerLoadSet(TIMER0_BASE, TIMER_BOTH, YELLOW_PERIOD); break; } @@ -28,33 +35,56 @@ void Timer0_Handler() { void Timer1_Handler() { TimerIntClear(TIMER1_BASE, TIMER_BOTH); - switch(cur_color_2) { - case RED_TF2: - cur_color_2 = GREEN_TF2; - set_color(cur_color_2); + switch(tf2.cur_color) { + case RED: + tf2.cur_color = GREEN; + set_tf_color(tf2, GREEN); TimerLoadSet(TIMER1_BASE, TIMER_BOTH, GREEN_PERIOD); break; - case YELLOW_TF2: - cur_color_2 = RED_TF2; - set_color(cur_color_2); + case YELLOW: + tf2.cur_color = RED; + set_tf_color(tf2, RED); TimerLoadSet(TIMER1_BASE, TIMER_BOTH, RED_PERIOD); break; - case GREEN_TF2: - cur_color_2 = YELLOW_TF2; - set_color(cur_color_2); + case GREEN: + tf2.cur_color = YELLOW; + set_tf_color(tf2, YELLOW); TimerLoadSet(TIMER1_BASE, TIMER_BOTH, YELLOW_PERIOD); break; } } +void Timer2_Handler() { + TimerIntClear(TIMER2_BASE, TIMER_BOTH); + switch(tf_ped.cur_color) { + case RED: + tf_ped.cur_color = GREEN; + set_tf_color(tf_ped, GREEN); + TimerLoadSet(TIMER2_BASE, TIMER_BOTH, GREEN_PERIOD); + break; + case YELLOW: + tf_ped.cur_color = RED; + set_tf_color(tf_ped, RED); + TimerLoadSet(TIMER2_BASE, TIMER_BOTH, RED_PERIOD); + break; + case GREEN: + tf_ped.cur_color = YELLOW; + set_tf_color(tf_ped, YELLOW); + TimerLoadSet(TIMER2_BASE, TIMER_BOTH, YELLOW_PERIOD); + break; + } +} + int main() { PortInit(GPIO_PORTF_BASE, SYSCTL_PERIPH_GPIOF); // Initialize portf PortInit(GPIO_PORTA_BASE, SYSCTL_PERIPH_GPIOA); // Initialize porta + PortInit(GPIO_PORTB_BASE, SYSCTL_PERIPH_GPIOB); // Initialize portb __asm("CPSID I"); // Disable all interrupts TimerInit(TIMER0_BASE, Timer0_Handler, SYSCTL_PERIPH_TIMER0, period_1); // Intialize Timer0 with period_1 TimerInit(TIMER1_BASE, Timer1_Handler, SYSCTL_PERIPH_TIMER1, period_2); // Intialize Timer1 with period_2 + TimerInit(TIMER2_BASE, Timer2_Handler, SYSCTL_PERIPH_TIMER2, period_3); // Intialize Timer1 with period_2 __asm("CPSIE I"); // Enable all interrupts while(1) { __asm("wfi"); // power saving mode -- cgit v1.2.3