/*
  Simple "Hello World" for ILI9341 LCD

  https://wokwi.com/arduino/projects/308024602434470466
*/

#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
//include "TFT_eSPI.h"
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define TFT_GREY 0x5AEB
void setup() {
  Serial.begin(115200);
  Serial.println("hello");
  tft.begin();
  tft.setRotation(1);
  tft.fillScreen(ILI9341_BLACK);
  tft.fillRect(0, 0, 319, 239, TFT_GREY);
  tft.fillRect(5, 5, 319 - 10, 239 - 10, ILI9341_BLACK);

  tft.setTextColor(ILI9341_ORANGE);
  tft.setTextSize(2);

  //tft.setFont(&FreeMonoBold9pt7b);
  tft.setCursor(0, 10);
  tft.setTextSize(3);

  tft.setTextColor(ILI9341_YELLOW);
  tft.print(" Tryb:");
  tft.setTextColor(ILI9341_GREEN);
  tft.print(" CO  ");
  tft.setTextColor(ILI9341_PURPLE);
  tft.print(" 20:15");
  tft.setCursor(0, 37);
  tft.setTextColor(ILI9341_ORANGE);
  tft.println(" Woda zas: 41.1*C");
  tft.setCursor(0, 64);
  tft.println(" Woda pow: 36.3*C");
  tft.setCursor(0, (64 + 27));
  tft.println(" Gaz zas:  45.1*C");
  tft.setCursor(0, 64 + 27 + 27);
  tft.println(" Gaz pow:  40.1*C");
  tft.setCursor(0, 64 + 27 + 27 + 27);
  tft.println(" Jedn zew:  5.1*C");
  tft.setCursor(0, 64 + 27 + 27 + 27 + 27);
  tft.setTextColor(ILI9341_RED);
  tft.println(" Przep: 65 l/min");
  tft.fillRoundRect(130, 200, 60, 30, 3, ILI9341_BLUE);
  tft.setTextSize(2);
  tft.setCursor(138, 209);
  tft.setTextColor(ILI9341_WHITE);
  tft.println("MENU");
  delay(2000);
  rysuj_schemat();
  // Meme reference: https://english.stackexchange.com/questions/20356/origin-of-i-can-haz
  pinMode(5, INPUT_PULLUP);
  pinMode(21, INPUT_PULLUP);
}
int oldstan, pwm, pwmold, licz;
uint8_t krok;
unsigned long newmillis;
void loop() {

  if (digitalRead(5) && (oldstan == 0)) {
    zawor(1);
    oldstan = 1;
    Serial.print("pin7=");
    Serial.println(1);
  }
  else if (!digitalRead(5) && (oldstan == 1)) {
    zawor(0);
    oldstan = 0;
    Serial.print("pin7=");
    Serial.println(0);

  }
  delay(10);
  //Serial.println(analogRead(1));
  pwm = map(analogRead(34), 0, 4095, 0, 99);
  if (pwm != pwmold) {
    pwmold = pwm;
    tft.fillRect(130, 145, 34, 15, ILI9341_BLACK);
    tft.setTextSize(2);
    tft.setCursor(130, 145);
    tft.setTextColor(ILI9341_YELLOW);
    tft.print(pwm);
    tft.print("% ");
  }
  if (millis() > newmillis) {
    newmillis = millis() + 400 - (pwm * 4);
    krok = 1;
  }
  if (digitalRead(21) && krok && pwm) {
    licz++;
    if (licz >= 4) licz = 0;
    Serial.println(licz);
    tft.fillRect(145 - 7, 171 - 7, 15, 15, ILI9341_BLACK);
    tft.drawCircle(145, 171, 8, ILI9341_WHITE);
    switch (licz) {
      case 0:  tft.fillTriangle(138, 171, 148, 165, 148, 177, ILI9341_WHITE);
        break;
      case 1:    tft.fillTriangle(145, 171 + 7, 145 - 6, 171 - 3, 145 + 6, 171 - 3, ILI9341_WHITE);
        break;
      case 2:  tft.fillTriangle(145 + 7, 171, 145 - 3, 171 - 6, 145 - 3, 171 + 6, ILI9341_WHITE);
        break;
      case 3:   tft.fillTriangle(145, 171 - 7, 145 - 6, 171 + 3, 145 + 6, 171 + 3, ILI9341_WHITE);
    }
    krok = 0;
  }

}
void rysuj_schemat() {
  tft.fillRect(5, 5, 319 - 10, 239 - 10, ILI9341_BLACK);
  tft.fillRoundRect(60, 40, 60, 120, 16, ILI9341_WHITE);
  tft.fillRoundRect(60 + 2, 40 + 2, 60 - 4, 120 - 4, 16, TFT_GREY);
  tft.fillCircle(60 + 15, 40 + 15, 5, ILI9341_WHITE);
  tft.fillCircle(60 - 15 + 60, 40 + 15, 7, ILI9341_WHITE);
  tft.fillCircle(60 + 15, 40 - 15 + 120, 5, ILI9341_WHITE);
  tft.fillCircle(60 - 15 + 60, 40 - 15 + 120, 7, ILI9341_WHITE);
  tft.fillRect(10, 54, 60, 3, ILI9341_PINK);
  tft.fillRect(10, 54 + 90, 60, 3, ILI9341_BLUE);
  tft.fillRect(105, 30, 62, 3, ILI9341_RED);
  tft.fillRect(104, 30, 3, 18, ILI9341_RED);

  zawor(1);

  tft.fillRect(104, 153, 3, 20, ILI9341_BLUE);
  tft.fillRect(105, 170, 32, 3, ILI9341_BLUE);
  tft.fillRect(153, 170, 84, 3, ILI9341_BLUE);
  tft.fillRect(180, 115, -15, 3, ILI9341_BLUE);
  tft.fillRect(165, 115, 3, 55, ILI9341_BLUE);
  tft.fillRect(237, 70 + 40, 10, 3, ILI9341_BLUE);
  tft.fillRect(237, 70 + 40, 3, 63, ILI9341_BLUE);


  tft.fillRoundRect(180, 50 + 10, 45, 80, 20, ILI9341_WHITE);
  tft.fillRoundRect(180 + 2, 50 + 12, 45 - 4, 80 - 4, 20, TFT_GREY);
  tft.drawLine(180, 70 + 10, 180 + 5, 70 + 10, ILI9341_WHITE);
  tft.drawLine(185, 70 + 10, 180 + 38, 70 + 16, ILI9341_WHITE);
  tft.drawLine(180 + 38, 70 + 16, 185, 70 + 22, ILI9341_WHITE);
  tft.drawLine(185, 70 + 22, 180 + 38, 70 + 28, ILI9341_WHITE);
  tft.drawLine(180 + 38, 70 + 28, 185, 70 + 34, ILI9341_WHITE);
  tft.drawLine(185, 70 + 34, 180 + 38, 70 + 40, ILI9341_WHITE);
  tft.drawLine(180 + 38, 70 + 40, 185, 70 + 46, ILI9341_WHITE);
  tft.drawLine(185, 70 + 46, 180, 70 + 46, ILI9341_WHITE);

  tft.fillRoundRect(247, 65, 61, 50, 2, ILI9341_DARKGREY);
  tft.drawRoundRect(247, 65, 61, 50, 2, ILI9341_WHITE);
  tft.drawRoundRect(247 + 1, 65 + 1, 61 - 2, 50 - 2, 2, ILI9341_WHITE);
  tft.drawLine(257, 65, 257, 114, ILI9341_WHITE);
  tft.drawLine(257 + 10, 65, 257 + 10, 114, ILI9341_WHITE);
  tft.drawLine(257 + 20, 65, 257 + 20, 114, ILI9341_WHITE);
  tft.drawLine(257 + 30, 65, 257 + 30, 114, ILI9341_WHITE);
  tft.drawLine(257 + 40, 65, 257 + 40, 114, ILI9341_WHITE);

  tft.drawCircle(165, 31, 8, ILI9341_WHITE);
  tft.drawCircle(145, 171, 8, ILI9341_WHITE);
  tft.fillTriangle(137 + 1, 171, 148, 165, 148, 177, ILI9341_WHITE);

  tft.setCursor(10, 35);
  tft.setTextSize(2);
  tft.setTextColor(ILI9341_YELLOW);
  tft.print("51.2");
  tft.setCursor(10, 124);
  tft.print("46.2");
  tft.setCursor(80, 13);
  tft.print("41.1");
  tft.setCursor(80, 178);
  tft.print("38.7");
  tft.setTextSize(2);
  tft.setCursor(130, 145);
  tft.print("20%");
  tft.setTextSize(2, 2);
  tft.setTextColor(ILI9341_YELLOW);
  tft.setCursor(180, 42);
  tft.print("48.5");

  tft.setTextSize(2, 2);
  tft.setTextColor(ILI9341_PURPLE);
  tft.setCursor(200, 10);
  tft.print("TRYB:");
  tft.setTextColor(ILI9341_RED);
  tft.print("AUTO ");
  

  tft.fillRect(0, 198, 320, 42, TFT_GREY);
  tft.fillRoundRect(5,200,73,35,3,ILI9341_DARKGREEN);
  tft.fillRoundRect(5+78,200,73,35,3,ILI9341_DARKGREEN);
  tft.fillRoundRect(5+156,200,73,35,3,ILI9341_DARKGREEN);
  tft.fillRoundRect(5+156+78,200,73,35,3,ILI9341_DARKGREEN);
  tft.setTextSize(2, 2);
  tft.setTextColor(ILI9341_BLACK);
  tft.setCursor(5, 210);
  tft.print(" INFO   STOP  TRYB  MENU");
}
void zawor(int stan) {
  uint16_t kolor1, kolor2;
  if (stan == 0) {
    kolor1 = ILI9341_RED;
    kolor2 = ILI9341_BLUE;
  }
  if (stan == 1) {
    kolor2 = ILI9341_RED;
    kolor1 = ILI9341_BLUE;
  }
  tft.fillRect(164, 33, 3, 48, kolor1);
  tft.fillRect(180, 79, -17, 3, kolor1);
  tft.fillRect(167, 30, 73, 3, kolor2);
  tft.fillRect(157 + 80, 33, 3, 40, kolor2);
  tft.fillRect(237, 70, 10, 3, kolor2);
  tft.drawCircle(165, 31, 8, ILI9341_WHITE);

}

void pompa(int stan) {

  tft.fillTriangle(137 + 1, 171, 148, 165, 148, 177, ILI9341_WHITE);
}