
//********************************//

#define TEST_PWM_STATE_INIT     0
#define TEST_PWM_STATE_CH1_INC  1
#define TEST_PWM_STATE_CH1_DEC  2
#define TEST_PWM_STATE_CH2_INC  3
#define TEST_PWM_STATE_CH2_DEC  4
#define TEST_PWM_STATE_CH3_INC  5
#define TEST_PWM_STATE_CH3_DEC  6
#define TEST_PWM_STATE_CH4_INC  7
#define TEST_PWM_STATE_CH4_DEC  8

//********************************//

bool     test_pwm = false;
uint32_t test_pwm_lastcheck_ms = 0;
uint8_t  test_pwm_state = TEST_PWM_STATE_INIT;

//********************************//

void test_pwm_main()
{
  if (test_pwm == false)
  {
    return;
  } else if ((test_pwm_lastcheck_ms + PWM_TEST_INTERVA_MS) <= millis())
  {
    test_pwm_lastcheck_ms = millis();

    switch (test_pwm_state)
    {
      // ----------------------- //
      case TEST_PWM_STATE_INIT:
        // disable the lights
        for (uint8_t i = 0; i < LIGHTS_COUNT; i++)
        {
          light_state[i]    = false;
          bri[i]            = 0;
          current_bri[i]    = 0;
          current_pwm[i]    = 0;
          transitiontime[i] = 0;
          process_lightdata(i, transitiontime[i]);
        }

        light_state[0] = true;
        test_pwm_state = TEST_PWM_STATE_CH1_INC;
        break;

      // ----------------------- //
      case TEST_PWM_STATE_CH1_INC:

        if (bri[0] < 255)
        {
          bri[0] += TEST_PWM_CHG_CNT;
          transitiontime[0] = 1;
          process_lightdata(0, transitiontime[0]);

        } else {

          test_pwm_state = TEST_PWM_STATE_CH1_DEC;
        }
        break;

      case TEST_PWM_STATE_CH1_DEC:

        if (bri[0] > 0)
        {
          bri[0] -= TEST_PWM_CHG_CNT;
          transitiontime[0] = 1;
          process_lightdata(0, transitiontime[0]);

        } else {

          light_state[0] = false;
          bri[0]            = 0;
          current_bri[0]    = 0;
          current_pwm[0]    = 0;
          transitiontime[0] = default_transitiontime;
          process_lightdata(0, transitiontime[0]);

          light_state[1] = true;
          test_pwm_state = TEST_PWM_STATE_CH2_INC;
        }
        break;

      // ----------------------- //
      case TEST_PWM_STATE_CH2_INC:

        if (bri[1] < 255)
        {
          bri[1] += TEST_PWM_CHG_CNT;
          transitiontime[1] = 1;
          process_lightdata(1, transitiontime[1]);

        } else {

          test_pwm_state = TEST_PWM_STATE_CH2_DEC;
        }
        break;

      case TEST_PWM_STATE_CH2_DEC:

        if (bri[1] > 0)
        {
          bri[1] -= TEST_PWM_CHG_CNT;
          transitiontime[1] = 1;
          process_lightdata(1, transitiontime[1]);

        } else {

          light_state[1] = false;
          bri[1]            = 0;
          current_bri[1]    = 0;
          current_pwm[1]    = 0;
          transitiontime[1] = default_transitiontime;
          process_lightdata(1, transitiontime[1]);

          light_state[2] = true;
          test_pwm_state = TEST_PWM_STATE_CH3_INC;
        }
        break;

      // ----------------------- //
      case TEST_PWM_STATE_CH3_INC:

        if (bri[2] < 255)
        {
          bri[2] += TEST_PWM_CHG_CNT;
          transitiontime[2] = 1;
          process_lightdata(2, transitiontime[2]);

        } else {

          test_pwm_state = TEST_PWM_STATE_CH3_DEC;
        }
        break;

      case TEST_PWM_STATE_CH3_DEC:

        if (bri[2] > 0)
        {
          bri[2] -= TEST_PWM_CHG_CNT;
          transitiontime[2] = 1;
          process_lightdata(2, transitiontime[2]);

        } else {

          light_state[2]    = false;
          bri[2]            = 0;
          current_bri[2]    = 0;
          current_pwm[2]    = 0;
          transitiontime[2] = default_transitiontime;
          process_lightdata(2, transitiontime[2]);

          light_state[3] = true;
          test_pwm_state = TEST_PWM_STATE_CH4_INC;
        }
        break;

      // ----------------------- //
      case TEST_PWM_STATE_CH4_INC:

        if (bri[3] < 255)
        {
          bri[3] += TEST_PWM_CHG_CNT;
          transitiontime[3] = 1;
          process_lightdata(3, transitiontime[3]);

        } else {

          test_pwm_state = TEST_PWM_STATE_CH4_DEC;
        }
        break;

      case TEST_PWM_STATE_CH4_DEC:

        if (bri[3] > 0)
        {
          bri[3] -= TEST_PWM_CHG_CNT;
          transitiontime[3] = 1;
          process_lightdata(3, transitiontime[3]);

        } else {

          test_pwm = false;
          tc_enabled = tc_enabled_old;

          for (uint8_t i = 0; i < LIGHTS_COUNT; i++)
          {
            light_state[i]    = false;
            bri[i]            = 0;
            current_bri[i]    = 0;
            current_pwm[i]    = 0;
            transitiontime[i] = default_transitiontime;
            process_lightdata(i, transitiontime[i]);
          }

          tc_reset();
          tc_update_main(); // load the tc if required

          test_pwm_state = TEST_PWM_STATE_INIT;
        }
        break;

      // ----------------------- //
      default:
        test_pwm_state = TEST_PWM_STATE_INIT;
    }

    Serial.println("---");
    for (uint8_t i = 0; i < LIGHTS_COUNT; i++)
    {
      Serial.println("light_state[" + (String)i + "]    = " + (String)light_state[i]);
      Serial.println("bri[" + (String)i + "]            = " + (String)bri[i]);
      Serial.println("current_bri[" + (String)i + "]    = " + (String)current_bri[i]);
      Serial.println("current_pwm[" + (String)i + "]    = " + (String)current_pwm[i]);
      Serial.println("transitiontime[" + (String)i + "] = " + (String)transitiontime[i]);
    }

  }

}

//********************************//
