Küchenuhr

/*
 Name:		küchenuhr.ino
 Created:	19.07.2018 13:34:14
 Author:	thomas
*/

#include <NTPtimeESP.h>
#include <WiFiUdp.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <WiFi.h>



#define _cs   4
#define _dc   2   // 4 goes to TFT DC
#define _mosi 14  // 5 goes to TFT MOSI
#define _sclk 12  // 6 goes to TFT SCK/CLK
#define _rst  13  // ESP RST to TFT RESET
#define _miso     // Not connected
//       3.3V     // Goes to TFT LED  
//       5v       // Goes to TFT Vcc
//       Gnd      // Goes to TFT Gnd        

Adafruit_ILI9341 tft = Adafruit_ILI9341(_cs, _dc, _mosi, _sclk, _rst);

const char* ssid = "thjansen";
const char* password = "9116805287641001";
float timezone = 1.0;
const char Day[7][10] = { "Sontag","Montag","Dienstag","Mittwoch","Donerstag","Freitag","Samstag" };
const char Month[12][6] = { "Jan.","Feb.","Märt","Apr.","Mai","Juni","Juli","Aug.","Sep.","Okt","Nov","Dez." };
strDateTime dateTime;
#define threshold 35
byte err = 0;
byte lastMin = -1;
//byte lastSek = -1;
//bool state = 0;
//int prev = 45;
//int mysek = 0;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
unsigned long wartedauer = 0;
int second = 0;

WiFiClient client;
NTPtime NTPch("ch.pool.ntp.org");

int threshold1 = 40;
bool touch1detected = false;
bool touch2detected = false;
bool touch3detected = false;
void gotTouch1() {
  touch1detected = true;
}
void gotTouch2() {
  touch2detected = true;
}
void gotTouch3() {
  touch3detected = true;
}

void setup() {
  Serial.begin(9600);
  touchAttachInterrupt(T9, gotTouch1, threshold1); //g32
  touchAttachInterrupt(T8, gotTouch3, threshold1); //g33
  touchAttachInterrupt(T3, gotTouch2, threshold1); //g15
  Serial.println("ILI9341 Test!");
  tft.begin();
  tft.setRotation(3);
  tft.fillScreen(ILI9341_BLACK);
  WiFi.begin(ssid, password);
  WiFi.setHostname("Kuechenuhr");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  ReadTime();
}

void Ausg_Zeit(void) {
  lastMin = dateTime.minute;
  String time;
  if (dateTime.hour<10)
    time += '0';
  time += String(dateTime.hour) + ':';
  if (dateTime.minute < 10) { time += '0'; }
  time += String(dateTime.minute);
  //tft.fillRect(0, 0, 200, 70, ILI9341_BLACK);
  tft.setCursor(0, 0); tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);  tft.setTextSize(3);
  tft.println(time); 
}

void Ausg_Sek(void) {
  String time;
  time += ':';
  if (second<10)
    time += '0';
  time += String(second); // +':';
  
  tft.setCursor(93, 0); tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);  tft.setTextSize(3);
  tft.println(time);
}

void Ausg_Datum(void) {
  String date;
  if (dateTime.day < 10)
    date += '0';
  date += dateTime.day;
  date += ' ';
  date += Month[dateTime.month - 1];
  date += ' ';
  date += dateTime.year;
  tft.setCursor(0, 40);
  tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);  tft.setTextSize(3);
  tft.println(date);
}
void ReadTime(void) {
retry:
  dateTime = NTPch.getNTPtime(timezone, 1);
  Serial.println("Lese Zeit");
  if (!dateTime.valid)
    goto retry;
}

void TimeLine(void) {
  currentMillis = millis();
  if (currentMillis - previousMillis > 1000) {
    previousMillis = currentMillis;
    //int n = wartedauer * (0.166); //(60000 * 12 Minuten)/120Pixel / 5 1Minute *10
    //tft.drawFastVLine(230, 5, n, ILI9341_YELLOW);
    /*wartedauer = 0;
    tft.drawFastVLine(1, 5, 128, BLACK);*/

    // die Sekundenleiste
    tft.drawFastVLine(305, 0, (second * 4) + 1, ILI9341_BLUE);
    tft.drawFastVLine(306, 0, (second * 4) + 1, ILI9341_BLUE);
    second++;
    //wartedauer = wartedauer + 1;
  }
  if (second == 60) { tft.drawFastVLine(305, 0, 240, ILI9341_BLACK); second = 0;
            tft.drawFastVLine(306, 0, 240, ILI9341_BLACK); }
}

void loop(void) {

  ReadTime();
  if (dateTime.valid == 0) {
    err++;
    if (err > 1500) {
      tft.fillScreen(ILI9341_BLACK);
      tft.setTextColor(ILI9341_WHITE);  tft.setTextSize(2);
      tft.println("Could not get\ndate and time,\nreset router");
      //ESP.deepSleep(1000000 * 60 * 24 * 10);
    }
    return;
  }
  if (dateTime.valid&&dateTime.minute != lastMin) {
    err = 0; second = 0;
    tft.drawFastVLine(305, 0, 240, ILI9341_BLACK);
    tft.drawFastVLine(306, 0, 240, ILI9341_BLACK);
    Ausg_Zeit();
    Ausg_Datum();
  }
  uint64_t started_at = millis();
  for (int i = started_at; i<started_at + 1000 * (60 - dateTime.second); i = millis()) {
    if (touch1detected) {
      touch1detected = false;
      Serial.println("Touch 1 detected");
      tft.setCursor(0, 100);
      tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);  tft.setTextSize(3);
      tft.println("Touch 1");
    }
    if (touch2detected) {
      touch2detected = false;
      Serial.println("Touch 2 detected");
      tft.setCursor(0, 100);
      tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);  tft.setTextSize(3);
      tft.println("Touch 2");
    }
    if (touch3detected) {
      touch3detected = false;
      Serial.println("Touch 3 detected");
      tft.fillRect(0, 100, 130, 120, ILI9341_BLACK);
    }
    
    TimeLine();
    //tft.println(second); tft.print(" ");
    Ausg_Sek();
      
  }
  
    
    
  
}



unsigned long testText() {
  tft.fillScreen(ILI9341_BLACK);
  
  tft.setCursor(0, 0);
  
  tft.setTextColor(ILI9341_WHITE);  tft.setTextSize(1);
  tft.println("Hello World!");
  tft.setTextColor(ILI9341_YELLOW); tft.setTextSize(2);
  tft.println(1234.56);
  tft.setTextColor(ILI9341_RED);    tft.setTextSize(3);
  tft.println(0xDEADBEEF, HEX);
  tft.println();
  tft.setTextColor(ILI9341_GREEN);
  tft.setTextSize(5);
  tft.println("Groop");
  tft.println(tft.getRotation());
  tft.setTextSize(2);
  tft.println(WiFi.getHostname());
  tft.setTextSize(1);
  
  tft.println("my foonting turlingdromes.");
  tft.setTextSize(2);
  tft.println(WiFi.localIP());
  //tft.drawFastVLine(tft.width()/2, tft.height()/2, 100, ILI9341_GREEN);
  tft.print(dateTime.hour); tft.print("\t"); tft.print(dateTime.minute); tft.print(dateTime.second);
  
}