/**
 * Application setup
 */
void setup()
{
    bcu.begin(4, 0x7054, 2); // We are a "Jung 2118" device, version 0.2

    pinMode(PIO_LED, OUTPUT);
    digitalWrite(PIO_LED, 1);

    // Configure the input pins and initialize the debouncers with the current
    // value of the pin.
    for (int channel = 0; channel < NUM_CHANNELS; ++channel)
    {
        pinMode(inputPins[channel], INPUT | HYSTERESIS | PULL_UP);
        inputDebouncer[channel].init(digitalRead(inputPins[channel]));
    }

    // Handle configured power-up delay
    unsigned int startupTimeout = calculateTime
            ( userEeprom[EE_BUS_RETURN_DELAY_BASE] >> 4
            , userEeprom[EE_BUS_RETURN_DELAY_FACT] &  0x7F
            );
    Timeout delay;
    int debounceTime = userEeprom[EE_INPUT_DEBOUNCE_TIME] >> 1;
    delay.start(startupTimeout);
    while (delay.started() && !delay.expired())
    {   // while we wait for the power on delay to expire we debounce the input channels
        for (int channel = 0; channel < NUM_CHANNELS; ++channel)
        {
            inputDebouncer[channel].debounce(digitalRead(inputPins[channel]), debounceTime);
        }
        waitForInterrupt();
    }

    initApplication();
}
void checkPeriodicFuntions(void)
{
	if(msTimeout.started() && msTimeout.expired()) {
		msTimeout.start(1);
		for (unsigned int i = 0; i < NO_OF_CHANNELS; i++)
		{
//			setChannelPWMRatio(i, pwm&0xffff);
//			pwm+=1;
			Channel * chn = channels[i];
			if (chn)
				chn->periodic();
		}
		memMapper.doFlash();
	}
	if(inBetriebTimeout.started() && inBetriebTimeout.expired()) {
		inBetriebTimeout.start(inBetriebZeit*1000);
		objectWrite(COMObj_In_Operation, inBetriebValue_0);
	}
	if(timeoutTest.started() && timeoutTest.expired()) {
		timeoutTest.start(1000);
//		objectWrite(209, inBetriebValue_0);
	}
}
static void tc_setup(void)
{
    REQUIRE(to.started() == false);
}
static void _check_expired_yes(void * refState, unsigned int param)
{
	REQUIRE(to.expired() == true);
	REQUIRE(to.started() == false);
	REQUIRE(to.stopped() == true);
}
static void _check_expired_yes(void * refState)
{
	REQUIRE(to.expired() == true);
	REQUIRE(to.started() == false);
	REQUIRE(to.stopped() == true);
}