/*
 * Weiche.c
 *
 * Created: 04.04.2021 13:50:24
 * Author : bma2
 */ 

#include <stdbool.h> // für Typ "bool"

#include <avr/io.h>

void setup(void)

{

	// PB2, PB6, PC1, PD0, PD1: Ausgänge 1b rechts, 1b links, 1b Spitze, rechts aktiv, links aktiv

	DDRB = _BV(2) | _BV(6);
	DDRC = _BV(1);
	DDRD = _BV(0) | _BV(1);
	
	// PB3, PB7, PC0: Eingänge 1a rechts, 1a links, 1a Spitze, aktiviere Pullups

	PORTB = _BV(3) | _BV(7);
	PORTC = _BV(0);

}

bool get_a(void)

{

	bool a = PINB & _BV(3); // PB3 abfragen: 1a rechts

	return a;

}

bool get_b(void)

{

	bool b = PINB & _BV(7); // PB7 abfragen: 1a links

	return b;

}

bool get_c(void)

{

	bool c = PINC & _BV(0); // PC0 abfragen: 1a Spitze

	return c;

}

void set_v(bool state)

{

	if (state)

	PORTB |= _BV(2); // PB2 setzen, 1b rechts aktiv

	else

	PORTB &= ~_BV(2); // PB2 löschen

}

void set_w(bool state)

{

	if (state)

	PORTB |= _BV(6); // PB6 setzen, 1b links aktiv

	else

	PORTB &= ~_BV(6); // PB6 löschen

}

void set_x(bool state)

{

	if (state)

	PORTC |= _BV(1); // PC1 setzen, 1b Spitze

	else

	PORTC &= ~_BV(1); // PC1 löschen

}

void set_y(bool state)

{

	if (state)

	PORTD |= _BV(0); // PD0 setzen, rechts aktiv

	else

	PORTD &= ~_BV(0); // PD0 löschen

}

void set_z(bool state)

{

	if (state)

	PORTD |= _BV(1); // PD1 setzen, links aktiv

	else

	PORTD &= ~_BV(1); // PD1 löschen

}

int

main(void)

{

	setup();

	bool sw1, sw2, sw3, sw4, sw5, sw6, sw7, sw8, sw9, sw10, sw11, sw12, sw13, sw14, sw15, sw16, sw17, sw18, sw19, sw20, sw21, sw22;

	for (;;) // Endlosschleife

	{

		// 1a Spitze UND Variable SW11 NICHT ergibt eine Variable SW1

		sw1 = get_c() && !sw11;

		// 1a Spitze UND Variable SW22 ergibt eine Variable SW2

		sw2 = get_c() && sw22;
		
		// Variable SW1 ODER Variable SW2 ergibt eine Variable SW3

		sw3 = sw1 || sw2;
		
		// Variable SW3 UND Variable SW21 NICHT ergibt eine Variable SW4

		sw4 = sw3 && !sw21;
		
		// 1a rechts UND Variable SW6 NICHT ergibt eine Variable SW5

		sw5 = get_a() && !sw6;
		
		// Variable SW21 ODER Variable SW8 ergibt eine Variable SW6

		sw6 = sw21 || sw8;

		// Variable SW3 UND Variable SW17 NICHT ergibt eine Variable SW7

		sw7 = sw3 && !sw17;
		
		// 1a links UND Variable SW9 NICHT ergibt eine Variable SW8

		sw8 = get_b() && !sw9;
		
		// Variable SW5 ODER Variable SW17 ergibt eine Variable SW9

		sw9 = sw5 || sw17;
		
		// Variable SW5 ODER Variable SW8 ergibt eine Variable SW10

		sw10 = sw5 || sw8;
		
		// Variable SW10 UND Variable SW1 NICHT ergibt eine Variable SW11

		sw11 = sw10 && !sw1;
		
		// Variable SW10 UND Variable SW22 ergibt eine Variable SW12

		sw12 = sw10 && sw22;
		
		// Variable SW12 ODER Variable SW11 ergibt eine Variable SW13

		sw13 = sw12 || sw11;
		
		// 1a Spitze ODER Variable SW5 ergibt eine Variable SW14

		sw14 = get_c() || sw5;
		
		// 1a Spitze UND Variable SW5 ergibt eine Variable SW15

		sw15 = get_c() && sw5;
		
		// Variable SW15 ODER Variable SW17 ergibt eine Variable SW16

		sw16 = sw15 || sw17;
		
		// Variable SW14 UND Variable SW16 ergibt eine Variable SW17

		sw17 = sw14 && sw16;
		
		// 1a Spitze ODER Variable SW8 ergibt eine Variable SW18

		sw18 = get_c() || sw8;
		
		// 1a Spitze UND Variable SW8 ergibt eine Variable SW19

		sw19 = get_c() && sw8;
		
		// Variable SW18 ODER Variable SW19 ergibt eine Variable SW20

		sw20 = sw18 || sw19;
		
		// Variable SW18 UND Variable SW20 ergibt eine Variable SW21

		sw21 = sw18 && sw20;
		
		// Variable SW17 ODER Variable SW21 ergibt eine Variable SW22

		sw22 = sw17 || sw21;
		
		// Variable SW4 auf einen Ausgang ausgeben

		set_v(sw4);
		
		// Variable SW7 auf einen Ausgang ausgeben

		set_w(sw7);
		
		// Variable SW13 auf einen Ausgang ausgeben

		set_x(sw13);
		
		// Variable SW17 auf einen Ausgang ausgeben

		set_y(sw17);

		// Variable SW21 auf einen Ausgang ausgeben

		set_z(sw21);

	}

}