#include "TwoLedSignal.h"
#include "defines.h"

// #define ESP8266


#include <EEPROM.h>

#include "configuration.h"
#include "productionMode.h"
#include "configurationMode.h"




bool configMode = false;

uint32_t startTime = 0;


void setup() {
  startTime = millis();
#ifdef DEBUG
  Serial.begin(115200);
  Serial.println("Starting ...");
#endif

  pinMode(CONFIG_SWITCH, INPUT_PULLUP);


  EEPROM.begin(512);
  EEPROM.get(EEPROM_ADDR, configBlock);


  Serial.print("Magic: ");
  Serial.println(configBlock.magic);

  configMode = ((LOW == digitalRead(CONFIG_SWITCH)) || (configBlock.magic != MAGIC));

  if (configMode) {
#ifdef DEBUG
  Serial.println("Configuration mode");
#endif
    setupConfigurationNetwork();
    setupConfigurationServer();
  } else {
#ifdef DEBUG
    Serial.println("Production mode");
    Serial.println();
    Serial.println();
    showConfiguration();
#endif

    setupProduction();
    setupConfigurationServer();
  }

#ifdef DEBUG
  Serial.println("Started.");
#endif
}

void loop() {
  if (configMode) {
    loopConfiguration();
  } else {
	loopConfiguration();
    loopProduction();
  }
}
