Fixed issues with StopLight.c and cleand up the code

This commit is contained in:
_N0x 2021-11-11 18:30:45 +01:00
parent bd8f4ea63a
commit 1720365a53
3 changed files with 129 additions and 104 deletions

View File

@ -7,6 +7,7 @@
#include "Led.h";
#include "Taster.h";
#include "Timer.h"
#include <util/delay.h>;
/* enums */
@ -19,11 +20,7 @@ enum{
/* Programmieren eine Ampelsteuerung auf einer Straßenkreuzung */
/* handle the delays locally for easy changing */
//int delayShort = 5000;
//int delayLong = 30000;
/* Stoplight and the corresponding Leds
/* Stoplight and the corresponding LEDs
| GREEN | YELLOW | RED
----------------------------------
SL 1 | Led1 | Led4 | Led6
@ -41,24 +38,14 @@ uint16_t g_startMS;
int delayShort = 5000;
int delayLong = 30000;
void stopLightInit(){
g_startMS=Timer_getTick();
}
/* Task 3 (2021-11-11) */
void
runStopLightTask1()
{
switch(StoplightState){
case INIT:
Led1_On(); // Set stoplight 1 to green
Led2_Off();
Led3_Off();
Led4_Off();
Led5_Off();
Led6_Off();
Led7_On(); // Set stoplight 2 to red
Led8_Off();
setS1ToGreen();
setS2ToRed();
if(Timer_getTick() - g_startMS >= delayLong) {
g_startMS=Timer_getTick();
@ -67,8 +54,7 @@ runStopLightTask1()
break;
case S1_YELLOW:
Led1_Off();
Led4_On();
setS1ToYellow();
if(Timer_getTick() - g_startMS >= delayShort) {
g_startMS=Timer_getTick();
@ -77,8 +63,7 @@ runStopLightTask1()
break;
case S1_RED:
Led4_Off();
Led6_On();
setS1ToRed();
if(Timer_getTick() - g_startMS >= delayShort) {
g_startMS=Timer_getTick();
@ -87,8 +72,7 @@ runStopLightTask1()
break;
case S2_RED_YELLOW:
Led5_On();
Led7_On();
setS2ToRedYellow();
if(Timer_getTick() - g_startMS >= delayShort) {
g_startMS=Timer_getTick();
@ -97,9 +81,7 @@ runStopLightTask1()
break;
case S2_GREEN:
Led5_Off();
Led7_Off();
Led2_On();
setS2ToGreen();
if(Timer_getTick() - g_startMS >= delayLong) {
g_startMS=Timer_getTick();
@ -108,8 +90,8 @@ runStopLightTask1()
break;
case S2_YELLOW:
Led2_Off();
Led5_On();
setS2ToYellow();
if(Timer_getTick() - g_startMS >= delayShort) {
g_startMS=Timer_getTick();
StoplightState = S2_RED;
@ -117,8 +99,8 @@ runStopLightTask1()
break;
case S2_RED:
Led5_Off();
Led7_On();
setS2ToRed();
if(Timer_getTick() - g_startMS >= delayShort) {
g_startMS=Timer_getTick();
StoplightState = S1_RED_YELLOW;
@ -126,8 +108,7 @@ runStopLightTask1()
break;
case S1_RED_YELLOW:
Led4_On();
Led6_On();
setS2ToRedYellow();
if(Timer_getTick() - g_startMS >= delayShort) {
g_startMS=Timer_getTick();
StoplightState = S1_GREEN;
@ -135,9 +116,7 @@ runStopLightTask1()
break;
case S1_GREEN:
Led4_Off();
Led6_Off();
Led1_On();
setS1ToGreen();
if(Timer_getTick() - g_startMS >= delayLong) {
g_startMS=Timer_getTick();
StoplightState = S1_YELLOW;
@ -151,23 +130,17 @@ runStopLightTask2(void)
{
switch(StoplightState){
case INIT:
Led1_On(); // Set stoplight 1 to green
Led2_Off();
Led3_Off();
Led4_Off();
Led5_Off();
Led6_Off();
Led7_On(); // Set stoplight 2 to red
Led8_Off();
setS1ToGreen();
setS2ToRed();
if(Taster1_get()) {
g_startMS=Timer_getTick();
StoplightState = S1_YELLOW;
}
break;
case S1_YELLOW:
Led1_Off();
Led4_On();
setS1ToYellow();
if(Timer_getTick() - g_startMS >= delayShort) {
g_startMS=Timer_getTick();
@ -176,8 +149,7 @@ runStopLightTask2(void)
break;
case S1_RED:
Led4_Off();
Led6_On();
setS1ToRed();
if(Timer_getTick() - g_startMS >= delayShort) {
g_startMS=Timer_getTick();
@ -186,28 +158,16 @@ runStopLightTask2(void)
break;
case S2_GREEN:
Led5_Off();
Led7_Off();
Led2_On();
setS2ToGreen();
if(Timer_getTick() - g_startMS >= delayLong) {
g_startMS=Timer_getTick();
StoplightState = S2_YELLOW;
}
break;
case S2_YELLOW:
Led2_Off();
Led5_On();
if(Timer_getTick() - g_startMS >= delayShort) {
g_startMS=Timer_getTick();
StoplightState = S2_RED;
}
break;
case S2_RED:
Led5_Off();
Led7_On();
setS2ToRed();
if(Timer_getTick() - g_startMS >= delayShort) {
g_startMS=Timer_getTick();
StoplightState = S1_RED_YELLOW;
@ -215,8 +175,7 @@ runStopLightTask2(void)
break;
case S1_RED_YELLOW:
Led4_On();
Led6_On();
setS1ToRedYellow();
if(Timer_getTick() - g_startMS >= delayShort) {
g_startMS=Timer_getTick();
StoplightState = S1_GREEN;
@ -224,10 +183,9 @@ runStopLightTask2(void)
break;
case S1_GREEN:
Led4_Off();
Led6_Off();
Led1_On();
setS1ToGreen();
// Wait 30 seconds before allowing the button to be pressed again
if( (Timer_getTick() - g_startMS >= delayLong) && Taster1_get()) {
g_startMS=Timer_getTick();
StoplightState = S1_YELLOW;
@ -235,4 +193,62 @@ runStopLightTask2(void)
break;
}
}
/* LED Helper methods to keep the code a little cleaner */
void setS1ToGreen(void)
{
Led1_On();
Led4_Off();
Led6_Off();
}
void setS1ToYellow(void)
{
Led1_Off();
Led4_On();
Led6_Off();
}
void setS1ToRedYellow(void)
{
Led1_Off();
Led4_On();
Led6_On();
}
void setS1ToRed(void)
{
Led1_Off();
Led4_Off();
Led6_On();
}
void setS2ToGreen(void)
{
Led2_On();
Led5_Off();
Led7_Off();
}
void setS2ToYellow(void)
{
Led2_Off();
Led5_On();
Led7_Off();
}
void setS2ToRedYellow(void)
{
Led2_Off();
Led5_On();
Led7_On();
}
void setS2ToRed(void)
{
Led2_Off();
Led5_Off();
Led7_On();
}
//*/

View File

@ -11,4 +11,13 @@
void runStopLightTask1(void);
void runStopLightTask2(void);
void setS1ToGreen(void);
void setS1ToYellow(void);
void setS1ToRedYellow(void);
void setS1ToRed(void);
void setS2ToGreen(void);
void setS2ToYellow(void);
void setS2ToRedYellow(void);
void setS2ToRed(void);
#endif /* STOPLIGHT_H_ */