diff --git a/EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj b/EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj
index dd4e4ae..a3c4b3c 100644
--- a/EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj
+++ b/EmbeddedSystemsTHM/EmbeddedSystemsTHM.cproj
@@ -160,6 +160,12 @@
compile
+
+ compile
+
+
+ compile
+
compile
diff --git a/EmbeddedSystemsTHM/StopLight.c b/EmbeddedSystemsTHM/StopLight.c
new file mode 100644
index 0000000..94be753
--- /dev/null
+++ b/EmbeddedSystemsTHM/StopLight.c
@@ -0,0 +1,238 @@
+/*
+ * StopLight.c
+ *
+ * Created: 11/11/2021 16:26:22
+ * Author: n0x
+ */
+
+#include "Led.h";
+#include "Taster.h";
+#include ;
+
+/* enums */
+enum{
+ S1_GREEN, S1_YELLOW, S1_RED, S1_RED_YELLOW,
+ S2_GREEN, S2_YELLOW, S2_RED, S2_RED_YELLOW,
+ INIT,
+ } StoplightState = INIT; /* Emus for the stoplight task (task 3) */
+
+
+/* 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
+ | GREEN | YELLOW | RED
+----------------------------------
+SL 1 | Led1 | Led4 | Led6
+SL 2 | Led2 | Led5 | Led7
+
+General rundown:
+ - State is entered
+ - Wait till delay is passed
+ - Switch off running LEDs
+ - Switch on new LEDs
+ - Set next state
+*/
+
+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();
+
+ if(Timer_getTick() - g_startMS >= delayLong) {
+ g_startMS=Timer_getTick();
+ StoplightState = S1_YELLOW;
+ }
+ break;
+
+ case S1_YELLOW:
+ Led1_Off();
+ Led4_On();
+
+ if(Timer_getTick() - g_startMS >= delayShort) {
+ g_startMS=Timer_getTick();
+ StoplightState = S1_RED;
+ }
+ break;
+
+ case S1_RED:
+ Led4_Off();
+ Led6_On();
+
+ if(Timer_getTick() - g_startMS >= delayShort) {
+ g_startMS=Timer_getTick();
+ StoplightState = S2_RED_YELLOW;
+ }
+ break;
+
+ case S2_RED_YELLOW:
+ Led5_On();
+ Led7_On();
+
+ if(Timer_getTick() - g_startMS >= delayShort) {
+ g_startMS=Timer_getTick();
+ StoplightState = S2_GREEN;
+ }
+ break;
+
+ case S2_GREEN:
+ Led5_Off();
+ Led7_Off();
+ Led2_On();
+
+ 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();
+ if(Timer_getTick() - g_startMS >= delayShort) {
+ g_startMS=Timer_getTick();
+ StoplightState = S1_RED_YELLOW;
+ }
+ break;
+
+ case S1_RED_YELLOW:
+ Led4_On();
+ Led6_On();
+ if(Timer_getTick() - g_startMS >= delayShort) {
+ g_startMS=Timer_getTick();
+ StoplightState = S1_GREEN;
+ }
+ break;
+
+ case S1_GREEN:
+ Led4_Off();
+ Led6_Off();
+ Led1_On();
+ if(Timer_getTick() - g_startMS >= delayLong) {
+ g_startMS=Timer_getTick();
+ StoplightState = S1_YELLOW;
+ }
+ break;
+ }
+}
+
+void
+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();
+
+ if(Taster1_get()) {
+ StoplightState = S1_YELLOW;
+ }
+ break;
+
+ case S1_YELLOW:
+ Led1_Off();
+ Led4_On();
+
+ if(Timer_getTick() - g_startMS >= delayShort) {
+ g_startMS=Timer_getTick();
+ StoplightState = S1_RED;
+ }
+ break;
+
+ case S1_RED:
+ Led4_Off();
+ Led6_On();
+
+ if(Timer_getTick() - g_startMS >= delayShort) {
+ g_startMS=Timer_getTick();
+ StoplightState = S2_GREEN;
+ }
+ break;
+
+ case S2_GREEN:
+ Led5_Off();
+ Led7_Off();
+ Led2_On();
+
+ 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();
+ if(Timer_getTick() - g_startMS >= delayShort) {
+ g_startMS=Timer_getTick();
+ StoplightState = S1_RED_YELLOW;
+ }
+ break;
+
+ case S1_RED_YELLOW:
+ Led4_On();
+ Led6_On();
+ if(Timer_getTick() - g_startMS >= delayShort) {
+ g_startMS=Timer_getTick();
+ StoplightState = S1_GREEN;
+ }
+ break;
+
+ case S1_GREEN:
+ Led4_Off();
+ Led6_Off();
+ Led1_On();
+
+ if( (Timer_getTick() - g_startMS >= delayLong) && Taster1_get()) {
+ g_startMS=Timer_getTick();
+ StoplightState = S1_YELLOW;
+ }
+ break;
+ }
+}
+//*/
\ No newline at end of file
diff --git a/EmbeddedSystemsTHM/StopLight.h b/EmbeddedSystemsTHM/StopLight.h
new file mode 100644
index 0000000..2a4236a
--- /dev/null
+++ b/EmbeddedSystemsTHM/StopLight.h
@@ -0,0 +1,14 @@
+/*
+ * StopLight.h
+ *
+ * Created: 11/11/2021 16:24:16
+ * Author: n0x
+ */
+
+#ifndef STOPLIGHT_H_
+#define STOPLIGHT_H_
+
+void runStopLightTask1(void);
+void runStopLightTask2(void);
+
+#endif /* STOPLIGHT_H_ */
\ No newline at end of file
diff --git a/EmbeddedSystemsTHM/main.c b/EmbeddedSystemsTHM/main.c
index 138080c..830d006 100644
--- a/EmbeddedSystemsTHM/main.c
+++ b/EmbeddedSystemsTHM/main.c
@@ -8,6 +8,7 @@
#include "Led.h"
#include "Taster.h"
#include "Timer.h"
+#include "StopLight.h"
#include
#define F_CPU 1000000
#include
@@ -16,20 +17,14 @@
void buttonTest (void);
void task1 (void);
void task2 (void);
-void task3 (void);
-/* enums */
-enum{
- S1_GREEN, S1_YELLOW, S1_RED, S1_RED_YELLOW,
- S2_GREEN, S2_YELLOW, S2_RED, S2_RED_YELLOW,
- INIT,
-} StoplightState = INIT; /* Emus for the stoplight task (task 3) */
/* global variables */
volatile int g_counter = 0;
-int g_ledStatus = 0;
+uint8_t g_ledStatus = 0;
uint16_t g_startMS;
+
/* functions */
int
main (void)
@@ -48,7 +43,8 @@ main (void)
//buttonTest();
//task1();
//task2();
- task3();
+ //runStopLightTask1();
+ runStopLightTask2();
}
}
@@ -98,7 +94,7 @@ task1 (void)
g_counter >= 8 ? Led8_On() : Led8_Off();
}
-/* Task 2 (2021-10-28) */
+/* Task 2 (2021-10-28) */
void
task2 (void)
{
@@ -114,87 +110,3 @@ task2 (void)
}
}
-
-/* Task 3 */
-void
-task3 (void)
-{
- /* Programmieren eine Ampelsteuerung auf einer Straßenkreuzung */
-
- /*
- int delayShort = 5000;
- int delayLong = 30000;
- //*/
- int delayShort = 2500;
- int delayLong = 7500;
-
- /* Stoplight and the corresponding Leds
- | GREEN | YELLOW | RED
- ----------------------------------
- SL 1 | Led1 | Led4 | Led6
- SL 2 | Led2 | Led5 | Led7
- */
- 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();
- StoplightState = S1_YELLOW;
-
- case S1_YELLOW: // Set stoplight 2 to yellow
- _delay_ms(delayLong);
- Led1_Off();
- Led4_On();
- StoplightState = S1_RED;
-
- case S1_RED: // Set stoplight 2 to red
- _delay_ms(delayShort);
- Led4_Off();
- Led6_On();
- StoplightState = S2_RED_YELLOW;
-
- case S2_RED_YELLOW:
- _delay_ms(delayShort);
- Led5_On();
- Led7_On();
- StoplightState = S2_GREEN;
-
- case S2_GREEN:
- _delay_ms(delayShort);
- Led5_Off();
- Led7_Off();
- Led2_On();
- StoplightState = S2_YELLOW;
-
- case S2_YELLOW:
- _delay_ms(delayLong);
- Led2_Off();
- Led5_On();
- StoplightState = S2_RED;
-
- case S2_RED:
- _delay_ms(delayShort);
- Led5_Off();
- Led7_On();
- StoplightState = S1_RED_YELLOW;
-
- case S1_RED_YELLOW:
- _delay_ms(delayShort);
- Led4_On();
- Led6_On();
- StoplightState = S1_GREEN;
-
- case S1_GREEN:
- _delay_ms(delayShort);
- Led4_Off();
- Led6_Off();
- Led1_On();
- StoplightState = S1_YELLOW;
-
- }
-}
\ No newline at end of file