/*  Color Clock
    E-Fabrik' 2018 - Pop [lab]
 
    ########
    WIRING :
    ########
    Plug a DS3231 chip DS3231 on 5V/GND
    SDA > A4
    SCL > A5
 
    Ledstrips on 5V/GND
    DI > pin 8
 
    Simple switch on 5V/GND
    reading > pin 2
    --> HIGH : Summer time, no daylight saving
    --> LOW : Winter time, daylight saving (-1h)
 
    Last update : 18/05/2018
*/
 
//libs
#include <DS3231.h>
#include <Wire.h>
#include <Adafruit_NeoPixel.h>
 
//constants
#define SWITCH 2
#define DATA_PIN 8
 
//variables
DS3231 Clock;
bool Century = false;
bool h12;
bool PM;
byte ADay, AHour, AMinute, ASecond, ABits;
bool ADy, A12h, Apm;
 
Adafruit_NeoPixel ledstrip = Adafruit_NeoPixel(4, DATA_PIN, NEO_GRB + NEO_KHZ800);
 
uint32_t color = (0, 0, 0);
 
void setup() {
  //Start I2C interface with DS3231
  Wire.begin();
 
  pinMode(SWITCH, INPUT);
 
  //Initialize ledstrip
  ledstrip.begin();
  ledstrip.show();
  ledstrip.setBrightness(100);
 
  //Start serial interface
  Serial.begin(9600);
}
 
void loop() {
  // Read & show date and time from DS3231 (RTC)
  Serial.print("Date : ");
  Serial.print(Clock.getDate(), DEC);
  Serial.print("/");
  Serial.print(Clock.getMonth(Century), DEC);
  Serial.print("/");
  Serial.print("20");
  Serial.print(Clock.getYear(), DEC);
  Serial.print(" - Time : ");
  Serial.print(Clock.getHour(h12, PM), DEC);
  Serial.print(':');
  Serial.print(Clock.getMinute(), DEC);
  Serial.print(':');
  Serial.print(Clock.getSecond(), DEC);
 
  int buttonState = digitalRead(SWITCH);
  int hour;
 
  if (buttonState == 1) {
    //Summertime > Do nothing to time
    hour = Clock.getHour(h12, PM);
  }
 
  if (buttonState == 0) {
    //Wintertime > Remove 1 hour from time read
    if (Clock.getHour(h12, PM) == 0) {
      //Exception when midnight
    hour = 23;
  }
  else {
    hour = Clock.getHour(h12, PM) - 1;
    }
  }
 
  Serial.print(" - hour var : " );
  Serial.print(hour);
 
 
 
  if (hour >= 6 && hour < 12) {
    color = ledstrip.Color(255, 255, 0);
    lightLeds();
  }
 
 
  else if (hour >= 12 && hour < 14) {
    color = ledstrip.Color(0, 255, 0);
    lightLeds();
  }
 
  else if (hour >= 14 && hour < 17) {
    color = ledstrip.Color(0, 0, 255);
    lightLeds();
  }
 
  else if (hour >= 17 && hour < 20) {
    color = ledstrip.Color(255, 0, 0);
    lightLeds();
  }
 
  else if (hour >= 20) {
    color = ledstrip.Color(255, 255, 255);
    lightLeds();
  }
 
  else {
    //do nothing
  }
 
 
  Serial.print('\n');
 
}
 
void lightLeds() {
    for (uint16_t i = 0; i < ledstrip.numPixels(); i++) {
    ledstrip.setPixelColor(i, color);
    ledstrip.show();
  }
}