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 "Led.h";
#include "Taster.h"; #include "Taster.h";
#include "Timer.h"
#include <util/delay.h>; #include <util/delay.h>;
/* enums */ /* enums */
@ -19,12 +20,8 @@ enum{
/* Programmieren eine Ampelsteuerung auf einer Straßenkreuzung */ /* Programmieren eine Ampelsteuerung auf einer Straßenkreuzung */
/* handle the delays locally for easy changing */ /* Stoplight and the corresponding LEDs
//int delayShort = 5000; | GREEN | YELLOW | RED
//int delayLong = 30000;
/* Stoplight and the corresponding Leds
| GREEN | YELLOW | RED
---------------------------------- ----------------------------------
SL 1 | Led1 | Led4 | Led6 SL 1 | Led1 | Led4 | Led6
SL 2 | Led2 | Led5 | Led7 SL 2 | Led2 | Led5 | Led7
@ -41,24 +38,14 @@ uint16_t g_startMS;
int delayShort = 5000; int delayShort = 5000;
int delayLong = 30000; int delayLong = 30000;
void stopLightInit(){
g_startMS=Timer_getTick();
}
/* Task 3 (2021-11-11) */ /* Task 3 (2021-11-11) */
void void
runStopLightTask1() runStopLightTask1()
{ {
switch(StoplightState){ switch(StoplightState){
case INIT: case INIT:
Led1_On(); // Set stoplight 1 to green setS1ToGreen();
Led2_Off(); setS2ToRed();
Led3_Off();
Led4_Off();
Led5_Off();
Led6_Off();
Led7_On(); // Set stoplight 2 to red
Led8_Off();
if(Timer_getTick() - g_startMS >= delayLong) { if(Timer_getTick() - g_startMS >= delayLong) {
g_startMS=Timer_getTick(); g_startMS=Timer_getTick();
@ -67,8 +54,7 @@ runStopLightTask1()
break; break;
case S1_YELLOW: case S1_YELLOW:
Led1_Off(); setS1ToYellow();
Led4_On();
if(Timer_getTick() - g_startMS >= delayShort) { if(Timer_getTick() - g_startMS >= delayShort) {
g_startMS=Timer_getTick(); g_startMS=Timer_getTick();
@ -77,8 +63,7 @@ runStopLightTask1()
break; break;
case S1_RED: case S1_RED:
Led4_Off(); setS1ToRed();
Led6_On();
if(Timer_getTick() - g_startMS >= delayShort) { if(Timer_getTick() - g_startMS >= delayShort) {
g_startMS=Timer_getTick(); g_startMS=Timer_getTick();
@ -87,8 +72,7 @@ runStopLightTask1()
break; break;
case S2_RED_YELLOW: case S2_RED_YELLOW:
Led5_On(); setS2ToRedYellow();
Led7_On();
if(Timer_getTick() - g_startMS >= delayShort) { if(Timer_getTick() - g_startMS >= delayShort) {
g_startMS=Timer_getTick(); g_startMS=Timer_getTick();
@ -97,9 +81,7 @@ runStopLightTask1()
break; break;
case S2_GREEN: case S2_GREEN:
Led5_Off(); setS2ToGreen();
Led7_Off();
Led2_On();
if(Timer_getTick() - g_startMS >= delayLong) { if(Timer_getTick() - g_startMS >= delayLong) {
g_startMS=Timer_getTick(); g_startMS=Timer_getTick();
@ -108,8 +90,8 @@ runStopLightTask1()
break; break;
case S2_YELLOW: case S2_YELLOW:
Led2_Off(); setS2ToYellow();
Led5_On();
if(Timer_getTick() - g_startMS >= delayShort) { if(Timer_getTick() - g_startMS >= delayShort) {
g_startMS=Timer_getTick(); g_startMS=Timer_getTick();
StoplightState = S2_RED; StoplightState = S2_RED;
@ -117,8 +99,8 @@ runStopLightTask1()
break; break;
case S2_RED: case S2_RED:
Led5_Off(); setS2ToRed();
Led7_On();
if(Timer_getTick() - g_startMS >= delayShort) { if(Timer_getTick() - g_startMS >= delayShort) {
g_startMS=Timer_getTick(); g_startMS=Timer_getTick();
StoplightState = S1_RED_YELLOW; StoplightState = S1_RED_YELLOW;
@ -126,8 +108,7 @@ runStopLightTask1()
break; break;
case S1_RED_YELLOW: case S1_RED_YELLOW:
Led4_On(); setS2ToRedYellow();
Led6_On();
if(Timer_getTick() - g_startMS >= delayShort) { if(Timer_getTick() - g_startMS >= delayShort) {
g_startMS=Timer_getTick(); g_startMS=Timer_getTick();
StoplightState = S1_GREEN; StoplightState = S1_GREEN;
@ -135,9 +116,7 @@ runStopLightTask1()
break; break;
case S1_GREEN: case S1_GREEN:
Led4_Off(); setS1ToGreen();
Led6_Off();
Led1_On();
if(Timer_getTick() - g_startMS >= delayLong) { if(Timer_getTick() - g_startMS >= delayLong) {
g_startMS=Timer_getTick(); g_startMS=Timer_getTick();
StoplightState = S1_YELLOW; StoplightState = S1_YELLOW;
@ -151,88 +130,125 @@ runStopLightTask2(void)
{ {
switch(StoplightState){ switch(StoplightState){
case INIT: case INIT:
Led1_On(); // Set stoplight 1 to green setS1ToGreen();
Led2_Off(); setS2ToRed();
Led3_Off();
Led4_Off();
Led5_Off();
Led6_Off();
Led7_On(); // Set stoplight 2 to red
Led8_Off();
if(Taster1_get()) { if(Taster1_get()) {
StoplightState = S1_YELLOW; g_startMS=Timer_getTick();
} StoplightState = S1_YELLOW;
break; }
break;
case S1_YELLOW: case S1_YELLOW:
Led1_Off(); setS1ToYellow();
Led4_On();
if(Timer_getTick() - g_startMS >= delayShort) { if(Timer_getTick() - g_startMS >= delayShort) {
g_startMS=Timer_getTick(); g_startMS=Timer_getTick();
StoplightState = S1_RED; StoplightState = S1_RED;
} }
break; break;
case S1_RED: case S1_RED:
Led4_Off(); setS1ToRed();
Led6_On();
if(Timer_getTick() - g_startMS >= delayShort) { if(Timer_getTick() - g_startMS >= delayShort) {
g_startMS=Timer_getTick(); g_startMS=Timer_getTick();
StoplightState = S2_GREEN; StoplightState = S2_GREEN;
} }
break; break;
case S2_GREEN: case S2_GREEN:
Led5_Off(); setS2ToGreen();
Led7_Off();
Led2_On();
if(Timer_getTick() - g_startMS >= delayLong) { if(Timer_getTick() - g_startMS >= delayLong) {
g_startMS=Timer_getTick(); g_startMS=Timer_getTick();
StoplightState = S2_YELLOW; StoplightState = S2_RED;
} }
break; 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: case S2_RED:
Led5_Off(); setS2ToRed();
Led7_On(); if(Timer_getTick() - g_startMS >= delayShort) {
if(Timer_getTick() - g_startMS >= delayShort) { g_startMS=Timer_getTick();
g_startMS=Timer_getTick(); StoplightState = S1_RED_YELLOW;
StoplightState = S1_RED_YELLOW; }
} break;
break;
case S1_RED_YELLOW: case S1_RED_YELLOW:
Led4_On(); setS1ToRedYellow();
Led6_On(); if(Timer_getTick() - g_startMS >= delayShort) {
if(Timer_getTick() - g_startMS >= delayShort) { g_startMS=Timer_getTick();
g_startMS=Timer_getTick(); StoplightState = S1_GREEN;
StoplightState = S1_GREEN; }
} break;
break;
case S1_GREEN: case S1_GREEN:
Led4_Off(); setS1ToGreen();
Led6_Off();
Led1_On();
if( (Timer_getTick() - g_startMS >= delayLong) && Taster1_get()) { // Wait 30 seconds before allowing the button to be pressed again
g_startMS=Timer_getTick(); if( (Timer_getTick() - g_startMS >= delayLong) && Taster1_get()) {
StoplightState = S1_YELLOW; g_startMS=Timer_getTick();
} StoplightState = S1_YELLOW;
break; }
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 runStopLightTask1(void);
void runStopLightTask2(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_ */ #endif /* STOPLIGHT_H_ */