#include <Arduino.h>
#include <ESPDMX.h>

#include <WiFi.h>

// WiFi AP credentials
const char* ssid     = "AtlasAP";
const char* password = "feuilles";

DMXESPSerial dmx;


//////////////////////////////////    WIFI    //////////////////////////////////

void ClientConnected(WiFiEvent_t event, WiFiEventInfo_t info){
  Serial.println("New client connected to AP");
}

void ClientDisconnected(WiFiEvent_t event, WiFiEventInfo_t info){
  Serial.println("Client disconnected from AP");
}

void ClientIP(WiFiEvent_t event, WiFiEventInfo_t info){
  Serial.println("Client has IP");
}




//////////////////////////////////    SETUP   //////////////////////////////////



void setup() {
  Serial.begin(115200);
  Serial.println("started");
  
  Serial.println("starting Wifi AP \"Atlas-AP\" with pw \"feuille\"");
  //Register events
  WiFi.onEvent(ClientConnected, SYSTEM_EVENT_AP_STACONNECTED);
  WiFi.onEvent(ClientDisconnected, SYSTEM_EVENT_AP_STADISCONNECTED);
  WiFi.onEvent(ClientIP, SYSTEM_EVENT_AP_STAIPASSIGNED);
  //start AP
  WiFi.softAP(ssid, password);

  IPAddress IP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(IP);

  Serial.println("Starting DMX and sending blackout");

  dmx.init(512, 17);           // initialization for complete bus
  delay(10);
  for (int i = 0; i < 512; i++)
  {
    dmx.write(i, 0);
  }

  Serial.println("initialized...");
  delay(200);               // wait a while (not necessary)
}


//////////////////////////////////    LOOP   //////////////////////////////////


void loop() {

  for (int i = 0; i < 512; i++)
  {
    dmx.write(i, 255);
  }
  Serial.print("turning on...");
  dmx.update();           // update the DMX bus
  Serial.println("updated!");
  delay(1000);            // wait for 1s

  for (int i = 0; i < 512; i++)
  {
    dmx.write(i, 0);
  }
  Serial.print("turning off...");
  dmx.update();           // update the DMX bus
  Serial.println("updated!");
  delay(1000);

}