#ifndef _IO_HELPER_
#define _IO_HELPER_

#include <stdint.h>

#define LEVEL 0
#define EDGE 1
#define RISING 2
#define FALLING 3

#define OFF 0
#define ON 1
#define BLINK 2
#define TOGGLE 3


#define NUMBER_OF_INPUT_BYTES 4

extern volatile uint8_t outStates[NUMBER_OF_INPUT_BYTES]; 
extern volatile uint8_t outStatesBlinking[NUMBER_OF_INPUT_BYTES];
extern volatile uint8_t inStates[NUMBER_OF_INPUT_BYTES];
extern volatile uint8_t inStatesRaw[NUMBER_OF_INPUT_BYTES];
extern volatile uint8_t ioHelperDebounceTable[NUMBER_OF_INPUT_BYTES*8];
extern volatile uint8_t oldInstates[NUMBER_OF_INPUT_BYTES];
extern volatile uint8_t inStatesBothEdges[NUMBER_OF_INPUT_BYTES];
extern volatile uint8_t inStatesRisingEdge[NUMBER_OF_INPUT_BYTES];
extern volatile uint8_t inStatesFallingEdge[NUMBER_OF_INPUT_BYTES];


uint8_t read_Input(uint8_t nr, uint8_t type);
void set_Output(uint8_t nr, uint8_t state);

void ioHelperInitBuffer(void);
void ioHelperSetOuts(void);
void ioHelperReadPins(void);
void ioHelperIoConf(void);
void ioHelperSetBit(volatile uint8_t *list, uint8_t nr, uint8_t state);
unsigned char ioHelperReadBit(volatile uint8_t *list, uint8_t nr);
void ioHelperDebounce(void);
void ioHelperEdgeDetector(void);

//Outputs
//Verknüpfen von Pin | Bit mit Bitposition (0...n) outStates[0...n/8].
#define BitPE4 0  //D0
#define BitPE5 1  //D1
#define BitPG5 2  //D2
#define BitPE3 3  //D3
#define BitPH3 4  //D4
#define BitPH4 5  //D5
#define BitPH5 6  //D6
#define BitPH6 7  //D7

#define BitPB4 8  //D8
#define BitPB5 9  //D9
#define BitPB6 10 //D10
#define BitPB7 11 //D11
#define BitPL7 12 //D12
#define BitPL6 13 //D13
#define BitPL5 14 //D14
#define BitPL4 15 //D15

#define BitPL3 16 //D16
#define BitPL2 17 //D17
#define BitPL1 18 //D18
#define BitPL0 19 //D19
#define BitPD4 20 //D20
#define BitPD5 21 //D21
#define BitPD6 22 //D22
#define BitPJ4 23 //D23


#define LED_GRN_NOTAUS_ANLAGE   BitPE4
#define LED_ROT_NOTAUS_ANLAGE   BitPE5
#define LED_GRN_NOTAUS_SCHRANK  BitPG5
#define LED_ROT_NOTAUS_SCHRANK  BitPE3
#define LED_PLC_OK              BitPH5
#define LED_BUS_OK              BitPD4


//Inputs
//Verknüpfen von Pin | Bit mit Bitposition (0...n) inStates[0...n/8].
#define BitPinF0 0  //A0
#define BitPinF1 1  //A1
#define BitPinF2 2  //A2
#define BitPinF3 3  //A3
#define BitPinF4 4  //A4
#define BitPinF5 5  //A5
#define BitPinF6 6  //A6
#define BitPinF7 7  //A7

#define BitPinK0 8  //A8
#define BitPinK1 9  //A9
#define BitPinD3 10 //INO
#define BitPinD2 11 //IN1

#define IN_ANLAGE_EIN_INV       BitPinF5
#define IN_NOTAUS_ANLAGE        BitPinF7
#define IN_NOTAUS_SCHRANK       BitPinK0

#endif
