2021-11-11 18:28:42 +01:00
|
|
|
|
/*
|
|
|
|
|
* StopLight.c
|
|
|
|
|
*
|
|
|
|
|
* Created: 11/11/2021 16:26:22
|
|
|
|
|
* Author: n0x
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "Led.h";
|
|
|
|
|
#include "Taster.h";
|
2021-11-11 18:30:45 +01:00
|
|
|
|
#include "Timer.h"
|
2021-11-18 19:34:41 +01:00
|
|
|
|
#include "StopLight.h"
|
2021-11-11 18:28:42 +01:00
|
|
|
|
|
|
|
|
|
/* enums */
|
|
|
|
|
enum{
|
|
|
|
|
S1_GREEN, S1_YELLOW, S1_RED, S1_RED_YELLOW,
|
|
|
|
|
S2_GREEN, S2_YELLOW, S2_RED, S2_RED_YELLOW,
|
2021-11-11 18:46:37 +01:00
|
|
|
|
INIT, WAIT
|
2021-11-11 18:28:42 +01:00
|
|
|
|
} StoplightState = INIT; /* Emus for the stoplight task (task 3) */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Programmieren eine Ampelsteuerung auf einer Stra<72>enkreuzung */
|
|
|
|
|
|
2021-11-11 18:30:45 +01:00
|
|
|
|
/* Stoplight and the corresponding LEDs
|
|
|
|
|
| GREEN | YELLOW | RED
|
2021-11-11 18:28:42 +01:00
|
|
|
|
----------------------------------
|
|
|
|
|
SL 1 | Led1 | Led4 | Led6
|
|
|
|
|
SL 2 | Led2 | Led5 | Led7
|
2021-11-11 18:46:37 +01:00
|
|
|
|
|
2021-11-11 18:28:42 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
uint16_t g_startMS;
|
|
|
|
|
int delayShort = 5000;
|
|
|
|
|
int delayLong = 30000;
|
|
|
|
|
|
|
|
|
|
/* Task 3 (2021-11-11) */
|
|
|
|
|
void
|
|
|
|
|
runStopLightTask1()
|
|
|
|
|
{
|
|
|
|
|
switch(StoplightState){
|
|
|
|
|
case INIT:
|
2021-11-11 18:30:45 +01:00
|
|
|
|
setS1ToGreen();
|
|
|
|
|
setS2ToRed();
|
2021-11-11 18:28:42 +01:00
|
|
|
|
|
|
|
|
|
if(Timer_getTick() - g_startMS >= delayLong) {
|
|
|
|
|
g_startMS=Timer_getTick();
|
|
|
|
|
StoplightState = S1_YELLOW;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case S1_YELLOW:
|
2021-11-11 18:30:45 +01:00
|
|
|
|
setS1ToYellow();
|
2021-11-11 18:28:42 +01:00
|
|
|
|
|
|
|
|
|
if(Timer_getTick() - g_startMS >= delayShort) {
|
|
|
|
|
g_startMS=Timer_getTick();
|
|
|
|
|
StoplightState = S1_RED;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case S1_RED:
|
2021-11-11 18:30:45 +01:00
|
|
|
|
setS1ToRed();
|
2021-11-11 18:28:42 +01:00
|
|
|
|
|
|
|
|
|
if(Timer_getTick() - g_startMS >= delayShort) {
|
|
|
|
|
g_startMS=Timer_getTick();
|
|
|
|
|
StoplightState = S2_RED_YELLOW;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case S2_RED_YELLOW:
|
2021-11-11 18:30:45 +01:00
|
|
|
|
setS2ToRedYellow();
|
2021-11-11 18:28:42 +01:00
|
|
|
|
|
|
|
|
|
if(Timer_getTick() - g_startMS >= delayShort) {
|
|
|
|
|
g_startMS=Timer_getTick();
|
|
|
|
|
StoplightState = S2_GREEN;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case S2_GREEN:
|
2021-11-11 18:30:45 +01:00
|
|
|
|
setS2ToGreen();
|
2021-11-11 18:28:42 +01:00
|
|
|
|
|
|
|
|
|
if(Timer_getTick() - g_startMS >= delayLong) {
|
|
|
|
|
g_startMS=Timer_getTick();
|
|
|
|
|
StoplightState = S2_YELLOW;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case S2_YELLOW:
|
2021-11-11 18:30:45 +01:00
|
|
|
|
setS2ToYellow();
|
|
|
|
|
|
2021-11-11 18:28:42 +01:00
|
|
|
|
if(Timer_getTick() - g_startMS >= delayShort) {
|
|
|
|
|
g_startMS=Timer_getTick();
|
|
|
|
|
StoplightState = S2_RED;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case S2_RED:
|
2021-11-11 18:30:45 +01:00
|
|
|
|
setS2ToRed();
|
|
|
|
|
|
2021-11-11 18:28:42 +01:00
|
|
|
|
if(Timer_getTick() - g_startMS >= delayShort) {
|
|
|
|
|
g_startMS=Timer_getTick();
|
|
|
|
|
StoplightState = S1_RED_YELLOW;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case S1_RED_YELLOW:
|
2021-11-18 19:34:41 +01:00
|
|
|
|
setS1ToRedYellow();
|
|
|
|
|
|
2021-11-11 18:28:42 +01:00
|
|
|
|
if(Timer_getTick() - g_startMS >= delayShort) {
|
|
|
|
|
g_startMS=Timer_getTick();
|
|
|
|
|
StoplightState = S1_GREEN;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case S1_GREEN:
|
2021-11-11 18:30:45 +01:00
|
|
|
|
setS1ToGreen();
|
2021-11-18 19:34:41 +01:00
|
|
|
|
|
2021-11-11 18:28:42 +01:00
|
|
|
|
if(Timer_getTick() - g_startMS >= delayLong) {
|
|
|
|
|
g_startMS=Timer_getTick();
|
|
|
|
|
StoplightState = S1_YELLOW;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
runStopLightTask2(void)
|
|
|
|
|
{
|
|
|
|
|
switch(StoplightState){
|
|
|
|
|
case INIT:
|
2021-11-11 18:30:45 +01:00
|
|
|
|
setS1ToGreen();
|
|
|
|
|
setS2ToRed();
|
|
|
|
|
|
2021-11-11 18:46:37 +01:00
|
|
|
|
StoplightState = WAIT;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case WAIT:
|
2021-11-11 18:30:45 +01:00
|
|
|
|
if(Taster1_get()) {
|
|
|
|
|
g_startMS=Timer_getTick();
|
|
|
|
|
StoplightState = S1_YELLOW;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2021-11-11 18:28:42 +01:00
|
|
|
|
|
|
|
|
|
case S1_YELLOW:
|
2021-11-11 18:30:45 +01:00
|
|
|
|
setS1ToYellow();
|
|
|
|
|
|
|
|
|
|
if(Timer_getTick() - g_startMS >= delayShort) {
|
|
|
|
|
g_startMS=Timer_getTick();
|
|
|
|
|
StoplightState = S1_RED;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2021-11-11 18:28:42 +01:00
|
|
|
|
|
|
|
|
|
case S1_RED:
|
2021-11-11 18:30:45 +01:00
|
|
|
|
setS1ToRed();
|
2021-11-11 18:28:42 +01:00
|
|
|
|
|
2021-11-11 18:30:45 +01:00
|
|
|
|
if(Timer_getTick() - g_startMS >= delayShort) {
|
|
|
|
|
g_startMS=Timer_getTick();
|
|
|
|
|
StoplightState = S2_GREEN;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2021-11-11 18:28:42 +01:00
|
|
|
|
|
|
|
|
|
case S2_GREEN:
|
2021-11-11 18:30:45 +01:00
|
|
|
|
setS2ToGreen();
|
2021-11-11 18:28:42 +01:00
|
|
|
|
|
2021-11-11 18:30:45 +01:00
|
|
|
|
if(Timer_getTick() - g_startMS >= delayLong) {
|
|
|
|
|
g_startMS=Timer_getTick();
|
|
|
|
|
StoplightState = S2_RED;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2021-11-11 18:28:42 +01:00
|
|
|
|
|
|
|
|
|
case S2_RED:
|
2021-11-11 18:30:45 +01:00
|
|
|
|
setS2ToRed();
|
|
|
|
|
if(Timer_getTick() - g_startMS >= delayShort) {
|
|
|
|
|
g_startMS=Timer_getTick();
|
|
|
|
|
StoplightState = S1_RED_YELLOW;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2021-11-11 18:28:42 +01:00
|
|
|
|
|
|
|
|
|
case S1_RED_YELLOW:
|
2021-11-11 18:30:45 +01:00
|
|
|
|
setS1ToRedYellow();
|
|
|
|
|
if(Timer_getTick() - g_startMS >= delayShort) {
|
|
|
|
|
g_startMS=Timer_getTick();
|
|
|
|
|
StoplightState = S1_GREEN;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2021-11-11 18:28:42 +01:00
|
|
|
|
|
|
|
|
|
case S1_GREEN:
|
2021-11-11 18:30:45 +01:00
|
|
|
|
setS1ToGreen();
|
2021-11-11 18:46:37 +01:00
|
|
|
|
if(Timer_getTick() - g_startMS >= delayLong) {
|
2021-11-11 18:30:45 +01:00
|
|
|
|
g_startMS=Timer_getTick();
|
2021-11-11 18:46:37 +01:00
|
|
|
|
StoplightState = WAIT;
|
2021-11-11 18:30:45 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
2021-11-11 18:28:42 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-11 18:30:45 +01:00
|
|
|
|
|
|
|
|
|
/* 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();
|
|
|
|
|
}
|
2021-11-11 18:28:42 +01:00
|
|
|
|
//*/
|