#include <Arduino.h>
#include "train.h"
#include "relais.h"
#include "taster.h"
#include "leds.h"
#include "mp3.h"
#include "display.h"
#include <TridentTD_ESP32NVS.h>
//#include "eeprom.h"

//eeprom flash;
const unsigned long SZENE_TIMEOUT_TO_SET_TO_DEFAULT_MS = 30000; 

///---- OLED ----

const int PIN_MONI_SDA = 21 ; //12
const int PIN_MONI_SCL = 22 ; //14
const uint8_t ADDR_MONI = 0x3c;
display moni(ADDR_MONI, PIN_MONI_SDA, PIN_MONI_SCL);

///---- MP3 ----
const size_t PIN_MP3_RX = 25 ; //D2;
const size_t PIN_MP3_TX = 26 ; //D3;

const uint8_t SONG_EINE_INSEL = 1;
const uint8_t SONG_WEIHNACHTSBAECKEREI = 2;
const uint8_t SONG_WEIHNACHTSBAHN = 3;
const uint8_t SONG_OH_TANNEBAUM = 4;
const uint8_t SONG_DISCO_MARYSBOYCHILD = 5;
const uint8_t SONG_DISCO_LASTCHRISTMAS = 6;
const uint8_t SONG_DISCO_DOTHEYKNOWITSCHRISTMAS = 7;

mp3 mp3ply(PIN_MP3_RX, PIN_MP3_TX);

///---- RELAIS ----
enum relais_t {RNONE = 0, RZUGOBEN=1, RZUGUNTEN=2, RWOLKE=4, RSPIEGEL=8};
const size_t PIN_RELAIS_0 = 5;
const size_t PIN_RELAIS_1 = 17;
const size_t PIN_RELAIS_2 = 16;
const size_t PIN_RELAIS_3 = 4;
relais relais_wolke_schiene(PIN_RELAIS_2);
relais relais_spiegel(PIN_RELAIS_3);

///---- Spannungsregler
uint32_t spannungsregler_state_flag = 0;
const size_t PIN_TRAIN_UNTEN = 32;
const size_t PIN_TRAIN_OBEN = 33;
const size_t PIN_LICHT = 15; //------------------------
#define PWM_CHANNEL_OBEN 5
#define PWM_CHANNEL_UNTEN 6	
#define PWM_CHANNEL_LICHT 7	
train zugunten("Zugunten", PIN_TRAIN_UNTEN, PWM_CHANNEL_UNTEN, PIN_RELAIS_1,5);
train zugoben("Zugoben", PIN_TRAIN_OBEN, PWM_CHANNEL_OBEN, PIN_RELAIS_0,5 );	
train licht("Licht", PIN_LICHT, PWM_CHANNEL_LICHT, -1 ,5);	

///--- IOs ----
const size_t PIN_IO0 = 23;
const size_t PIN_IO1 = 10;
const size_t PIN_IO2 = 9;
const size_t PIN_IO3 = 18;

///--- Taster ----
const unsigned long BOUNCING_TIME_MS = 300;
const size_t PIN_TASTER_AUSSEN_LICHT = 36;
const size_t PIN_TASTER_AUSSEN_MOVE = 39;  //-----------------
const size_t PIN_TASTER_TRAIN_UNTEN = 35;
const size_t PIN_TASTER_TRAIN_OBEN = 13;
taster taster_aussen_licht(PIN_TASTER_AUSSEN_LICHT);
taster taster_aussen_move(PIN_TASTER_AUSSEN_MOVE);
taster taster_train_unten(PIN_TASTER_TRAIN_UNTEN);
taster taster_train_oben(PIN_TASTER_TRAIN_OBEN);
taster taster_schiene(PIN_IO0);

///--- RGB LEDs ---
uint32_t licht_state_flag = 0;
const size_t PIN_RGBLEDS = 19;
#define NUMRGBLEDS 109
Adafruit_NeoPixel rgb_leds(NUMRGBLEDS, PIN_RGBLEDS, NEO_GRB + NEO_KHZ800);
enum led_t {LTANNE=1, LBACK=2, LBAUHOF=4, LTEICH=8, LBURG=16, LSTERNE1=32, LSTERNE2=64, LSTERNBILD1 = 128, LSTERNBILD2=256, LSTERNBILD3=512, LHAEUSER=1024, LSPIEGEL=2048};

leds led_back(&rgb_leds, &licht_state_flag, LBACK, 5000,220, 0, 6 );
leds led_burg(&rgb_leds, &licht_state_flag, LBURG, 5000,220, 7, 9);
leds led_teich(&rgb_leds, &licht_state_flag, LTEICH, 5000,220, 10, 16 );
leds led_sternbilder1(&rgb_leds, &licht_state_flag, LSTERNBILD1, 5000,220, 17, 17 );
leds led_sterne1(&rgb_leds, &licht_state_flag, LSTERNE1, 5000,220, 18, 18 );
leds led_sternbilder2(&rgb_leds, &licht_state_flag, LSTERNBILD2, 5000,220, 19, 19 );
leds led_sterne2(&rgb_leds, &licht_state_flag, LSTERNE2, 5000,220, 20, 20 );
leds led_sternbilder3(&rgb_leds, &licht_state_flag, LSTERNBILD3, 5000,220, 21, 21 );
leds led_bauhof(&rgb_leds, &licht_state_flag, LBAUHOF, 5000,220, 22, 24);
leds led_tanne(&rgb_leds, &licht_state_flag, LTANNE, 5000,220, 25, 25 );

/*
leds led_back(&rgb_leds, &licht_state_flag, LBACK, 5000,220, 0, 69 );
leds led_burg(&rgb_leds, &licht_state_flag, LBURG, 5000,220, 70, 76);
leds led_teich(&rgb_leds, &licht_state_flag, LTEICH, 5000,220, 77, 88 );
leds led_sternbilder1(&rgb_leds, &licht_state_flag, LSTERNBILD1, 5000,220, 89, 90 );
leds led_sterne1(&rgb_leds, &licht_state_flag, LSTERNE1, 5000,220, 91, 92 );
leds led_sternbilder2(&rgb_leds, &licht_state_flag, LSTERNBILD2, 5000,220, 93, 94 );
leds led_sterne2(&rgb_leds, &licht_state_flag, LSTERNE2, 5000,220, 95, 96 );
leds led_sternbilder3(&rgb_leds, &licht_state_flag, LSTERNBILD3, 5000,220, 97, 98 );
leds led_bauhof(&rgb_leds, &licht_state_flag, LBAUHOF, 5000,220, 99, 106);
leds led_tanne(&rgb_leds, &licht_state_flag, LTANNE, 5000,220, 107, 108 );
*/
