src_legacy/2017/pixelgateway.cpp
void loop()
{
  button1.tick(); // read button data

  bool state_changed = nextPoiState != poiState;

  // exit actions
  if (state_changed){
    if (logLevel != MUTE) printf("Poi State changed: %d -> %d\n", (poiState), (nextPoiState));

    switch(poiState){
      case POI_INIT:
      break;

      case POI_IP_CONFIG_OPTION:
      // switch off ip display
      //runner.displayOff();
      break;

      case POI_IP_CONFIG:
      runner.playWorm(RAINBOW, N_POIS, 1);
      break;

      case POI_NETWORK_SEARCH:
      break;

      case POI_CLIENT_CONNECTING:
      if (nextPoiState == POI_RECEIVING_DATA && !runner.isProgramActive()){
        runner.playWummer(GREEN, 3, 4);
      }
      break;

      case POI_RECEIVING_DATA:
        // switch off led if we leave this state
        digitalWrite(LED_PIN,LOW);
      break;

      case POI_AWAIT_PROGRAM_SYNC:
      break;

      case POI_PLAY_PROGRAM:
      // on exit stop the program
      runner.pauseProg();
      runner.displayOff();
      break;

      default:
      break;
    }
  }

  // update state
  // need to do this *here* since following functions may set nextPoiState
  poiState = nextPoiState;

  // entry and state actions of state machine
  switch (poiState){

    case POI_INIT:
    if (state_changed){
      // cleanup action
      runner.finishAction();
    }
    runner.playWorm(RAINBOW, N_PIXELS, 1);
    // proceed to next state
    nextPoiState = POI_IP_CONFIG_OPTION;
    break;

    case POI_IP_CONFIG_OPTION:
    // display pale white for 2 seconds
    // user needs to long press to set ip
    if (state_changed){
      poi_network_display_entered = millis();
      // show ip with pale white background
      runner.displayIp(ipIncrement, true);
    }
    currentTime = millis();
    if (currentTime-poi_network_display_entered > 5000){
      runner.playWorm(RAINBOW, N_POIS, 1);
      nextPoiState = POI_AWAIT_PROGRAM_SYNC;
    }
    break;

    case POI_IP_CONFIG:
    if (state_changed){
      runner.playWorm(RAINBOW, N_POIS, 1);
      delay (100); // otherwise the worm may overtake the displayIp
      runner.displayIp(ipIncrement, false);
    }
    // operation is done thru click1
    break;

    case POI_NETWORK_SEARCH:
    if (state_changed){
      digitalWrite(LED_PIN,LOW);
      wifi_connect_async_init();
      if (!runner.isProgramActive()){
        runner.playWummer(RED, 2, 0);
      }
    }
    //wifi_connect(); 
    wifi_connect_async();
    break;

    case POI_CLIENT_CONNECTING:
    if (state_changed){
      resetTimeout();
      // not when connection is lost during program
      if (!runner.isProgramActive()){
        runner.playWummer(YELLOW, 2, 0);
      }
      if (logLevel != MUTE) printf("Waiting for client...\n");
    }
    client_connect();
    break;

    case POI_RECEIVING_DATA:
    if (state_changed){
      digitalWrite(LED_PIN,HIGH);
    }
    protocoll_receive_data();
    if (protocoll_cmd_complete()){
      if (logLevel != MUTE){
        if (logLevel == CHATTY || ( !protocol_is_data() && !protocol_is_sendalive() )){
          print_cmd();
        }
      }

      if (protocol_is_data()){ // image data
        // only print once
        if (logLevel != MUTE && !loadingImgData){
          printf("Reading image data... \n");
//          runner.playWorm(PALE_WHITE, N_POIS, 0, false); // async forever
          // currently required since we write directly into image memory
          // TODO: add start command for image loading (with scene id)
          //       which will remove current image from memory
          // TODO: then remove this line again - it does not work anyway ;-)
          runner.clearImageMap();
        }
        loadingImgData = true;
      }
      else {
//        runner.pauseAction(); // finish worm
        loadingImgData = false;
      }

      // carry out and clean command
      realize_cmd();
      protocoll_clean_cmd();
    }
    break;

    case POI_AWAIT_PROGRAM_SYNC:
    if (state_changed){
      runner.displayOff();
      if (WiFi.status() == WL_CONNECTED){
        WiFi.disconnect();
      }
    }
    // just wait for click to start program
    break;

    case POI_PLAY_PROGRAM:
    if (state_changed){
      printf("  Starting program...\n" );
      runner.startProg();
    }
    else if (!runner.isProgramActive()){
      printf("Program finished\n" );
      nextPoiState = POI_AWAIT_PROGRAM_SYNC;
    }
    break;

    default:
    break;
  }

  runner.loop();
}
// state machine with entry actions, state actions and exit actions
state machine with entry actions, state actions and exit actions
state machine with entry actions state actions and exit actions
void loop()
{
  button1.tick(); 
  bool state_changed = nextPoiState != poiState;
  if (state_changed){
    if (logLevel != MUTE) printf("Poi State changed: %d -> %d\n", (poiState), (nextPoiState));
    switch(poiState){
      case POI_INIT:
      break;
      case POI_IP_CONFIG_OPTION:
      break;
      case POI_IP_CONFIG:
      runner.playWorm(RAINBOW, N_POIS, 1);
      break;
      case POI_NETWORK_SEARCH:
      break;
      case POI_CLIENT_CONNECTING:
      if (nextPoiState == POI_RECEIVING_DATA && !runner.isProgramActive()){
        runner.playWummer(GREEN, 3, 4);
      }
      break;
      case POI_RECEIVING_DATA:
        digitalWrite(LED_PIN,LOW);
      break;
      case POI_AWAIT_PROGRAM_SYNC:
      break;
      case POI_PLAY_PROGRAM:
      runner.pauseProg();
      runner.displayOff();
      break;
      default:
      break;
    }
  }
  poiState = nextPoiState;
  switch (poiState){
    case POI_INIT:
    if (state_changed){
      runner.finishAction();
    }
    runner.playWorm(RAINBOW, N_PIXELS, 1);
    nextPoiState = POI_IP_CONFIG_OPTION;
    break;
    case POI_IP_CONFIG_OPTION:
    if (state_changed){
      poi_network_display_entered = millis();
      runner.displayIp(ipIncrement, true);
    }
    currentTime = millis();
    if (currentTime-poi_network_display_entered > 5000){
      runner.playWorm(RAINBOW, N_POIS, 1);
      nextPoiState = POI_AWAIT_PROGRAM_SYNC;
    }
    break;
    case POI_IP_CONFIG:
    if (state_changed){
      runner.playWorm(RAINBOW, N_POIS, 1);
      delay (100); 
      runner.displayIp(ipIncrement, false);
    }
    break;
    case POI_NETWORK_SEARCH:
    if (state_changed){
      digitalWrite(LED_PIN,LOW);
      wifi_connect_async_init();
      if (!runner.isProgramActive()){
        runner.playWummer(RED, 2, 0);
      }
    }
    wifi_connect_async();
    break;
    case POI_CLIENT_CONNECTING:
    if (state_changed){
      resetTimeout();
      if (!runner.isProgramActive()){
        runner.playWummer(YELLOW, 2, 0);
      }
      if (logLevel != MUTE) printf("Waiting for client...\n");
    }
    client_connect();
    break;
    case POI_RECEIVING_DATA:
    if (state_changed){
      digitalWrite(LED_PIN,HIGH);
    }
    protocoll_receive_data();
    if (protocoll_cmd_complete()){
      if (logLevel != MUTE){
        if (logLevel == CHATTY || ( !protocol_is_data() && !protocol_is_sendalive() )){
          print_cmd();
        }
      }
      if (protocol_is_data()){ 
        if (logLevel != MUTE && !loadingImgData){
          printf("Reading image data... \n");
          runner.clearImageMap();
        }
        loadingImgData = true;
      }
      else {
        loadingImgData = false;
      }
      realize_cmd();
      protocoll_clean_cmd();
    }
    break;
    case POI_AWAIT_PROGRAM_SYNC:
    if (state_changed){
      runner.displayOff();
      if (WiFi.status() == WL_CONNECTED){
        WiFi.disconnect();
      }
    }
    break;
    case POI_PLAY_PROGRAM:
    if (state_changed){
      printf("  Starting program...\n" );
      runner.startProg();
    }
    else if (!runner.isProgramActive()){
      printf("Program finished\n" );
      nextPoiState = POI_AWAIT_PROGRAM_SYNC;
    }
    break;
    default:
    break;
  }
  runner.loop();
}
void loop ( ) { button1 . tick ( ) ; bool state_changed = nextPoiState != poiState ; if ( state_changed ) { if ( logLevel != MUTE ) printf ( " \n " , ( poiState ) , ( nextPoiState ) ) ; switch ( poiState ) { case POI_INIT : break ; case POI_IP_CONFIG_OPTION : break ; case POI_IP_CONFIG : runner . playWorm ( RAINBOW , N_POIS , 1 ) ; break ; case POI_NETWORK_SEARCH : break ; case POI_CLIENT_CONNECTING : if ( nextPoiState == POI_RECEIVING_DATA && ! runner . isProgramActive ( ) ) { runner . playWummer ( GREEN , 3 , 4 ) ; } break ; case POI_RECEIVING_DATA : digitalWrite ( LED_PIN , LOW ) ; break ; case POI_AWAIT_PROGRAM_SYNC : break ; case POI_PLAY_PROGRAM : runner . pauseProg ( ) ; runner . displayOff ( ) ; break ; default : break ; } } poiState = nextPoiState ; switch ( poiState ) { case POI_INIT : if ( state_changed ) { runner . finishAction ( ) ; } runner . playWorm ( RAINBOW , N_PIXELS , 1 ) ; nextPoiState = POI_IP_CONFIG_OPTION ; break ; case POI_IP_CONFIG_OPTION : if ( state_changed ) { poi_network_display_entered = millis ( ) ; runner . displayIp ( ipIncrement , true ) ; } currentTime = millis ( ) ; if ( currentTime - poi_network_display_entered > 5000 ) { runner . playWorm ( RAINBOW , N_POIS , 1 ) ; nextPoiState = POI_AWAIT_PROGRAM_SYNC ; } break ; case POI_IP_CONFIG : if ( state_changed ) { runner . playWorm ( RAINBOW , N_POIS , 1 ) ; delay ( 100 ) ; runner . displayIp ( ipIncrement , false ) ; } break ; case POI_NETWORK_SEARCH : if ( state_changed ) { digitalWrite ( LED_PIN , LOW ) ; wifi_connect_async_init ( ) ; if ( ! runner . isProgramActive ( ) ) { runner . playWummer ( RED , 2 , 0 ) ; } } wifi_connect_async ( ) ; break ; case POI_CLIENT_CONNECTING : if ( state_changed ) { resetTimeout ( ) ; if ( ! runner . isProgramActive ( ) ) { runner . playWummer ( YELLOW , 2 , 0 ) ; } if ( logLevel != MUTE ) printf ( " \n " ) ; } client_connect ( ) ; break ; case POI_RECEIVING_DATA : if ( state_changed ) { digitalWrite ( LED_PIN , HIGH ) ; } protocoll_receive_data ( ) ; if ( protocoll_cmd_complete ( ) ) { if ( logLevel != MUTE ) { if ( logLevel == CHATTY || ( ! protocol_is_data ( ) && ! protocol_is_sendalive ( ) ) ) { print_cmd ( ) ; } } if ( protocol_is_data ( ) ) { if ( logLevel != MUTE && ! loadingImgData ) { printf ( " \n " ) ; runner . clearImageMap ( ) ; } loadingImgData = true ; } else { loadingImgData = false ; } realize_cmd ( ) ; protocoll_clean_cmd ( ) ; } break ; case POI_AWAIT_PROGRAM_SYNC : if ( state_changed ) { runner . displayOff ( ) ; if ( WiFi . status ( ) == WL_CONNECTED ) { WiFi . disconnect ( ) ; } } break ; case POI_PLAY_PROGRAM : if ( state_changed ) { printf ( " \n " ) ; runner . startProg ( ) ; } else if ( ! runner . isProgramActive ( ) ) { printf ( " \n " ) ; nextPoiState = POI_AWAIT_PROGRAM_SYNC ; } break ; default : break ; } runner . loop ( ) ; }
state machine with entry actions, state actions and exit actions
state machine with entry actions state actions and exit actions
// read button data // exit actions // switch off ip display //runner.displayOff(); // switch off led if we leave this state // on exit stop the program // update state // need to do this *here* since following functions may set nextPoiState // entry and state actions of state machine // cleanup action // proceed to next state // display pale white for 2 seconds // user needs to long press to set ip // show ip with pale white background // otherwise the worm may overtake the displayIp // operation is done thru click1 //wifi_connect();  // not when connection is lost during program // image data // only print once //          runner.playWorm(PALE_WHITE, N_POIS, 0, false); // async forever // currently required since we write directly into image memory // TODO: add start command for image loading (with scene id) //       which will remove current image from memory // TODO: then remove this line again - it does not work anyway ;-) //        runner.pauseAction(); // finish worm // carry out and clean command // just wait for click to start program