#include <TFT.h> // Hardware-specific library
#include <SPI.h>

#define CS   10
#define DC   9
#define RESET  4

TFT myScreen = TFT(CS, DC, RESET);

const int relay_pin = 6;  // nastavení pinu relé
const int button3_pin = 7;  //nastavení pinu tlačítka 3
const int breath_pin = 8 ;  //nastavení pinu vstupu z optopřevodníku ze simulátoru dýchání

int switch3_state=0; //nastavení počátečního stavu proměnné tlačítka 3
int switch3_state_last = LOW;
int relay_state = HIGH;
int last_relay_state = LOW;
long casLastSwitch=0;
long casLastSwitchON=0;
int breath_state=1;
int breath_state_last=1;
long breath_time=0;
long last_breath_time=0;
int breath_delay=0;
int button_ON=LOW;
long nebulization_start_time=0;
long duration=0;
long last_time=0;

void setup() {
  myScreen.begin();  // inicializace displeje
  myScreen.background(0,0,0); // clear the screen
  myScreen.setRotation(2); //otočí dislpej o 90°
  Serial.begin(9600);
  pinMode(button3_pin, INPUT);  // nastavení pinu tlačítka jako vstup
  pinMode(relay_pin, OUTPUT); //nastavení pinu relé jako výstup
  pinMode(breath_pin, INPUT_PULLUP);
  digitalWrite(relay_pin, relay_state);

  //Text který se nemění
  myScreen.setCursor(0, 0);  // set the cursor to column X, line Y 
  myScreen.print("Actual time:"); // time Actual
  myScreen.setCursor(0, 24);
  myScreen.print("Last breath start:");  //Time Last breath start
  myScreen.setCursor(0, 50);
  myScreen.print("Nebulization delay:");  //Delay
  myScreen.setCursor(0, 76);
  myScreen.print("Nebulization duration:"); //Duration
  myScreen.setCursor(0, 102);
  myScreen.print("Relay state:"); //stav relé
  
}


void button3_action(){
  switch3_state=digitalRead(button3_pin); //  vyčte stav tlačítka3
  
  if (millis()>=casLastSwitch+100 && switch3_state != switch3_state_last){   //provede dále pokud od posledního zmáčknutí tlačítka uběhlo alespoň....ms a změnil se stav tlačítka 
    if (switch3_state == HIGH && relay_state == HIGH) {
        relay_state=LOW;  //zapne relé
        button_ON=HIGH; //zapnuto tlačítkem
        casLastSwitchON=millis();
    }   else if (switch3_state == HIGH && relay_state == LOW) {
        relay_state=HIGH; //vypne relé
        button_ON=LOW;  //vypnuto tlačítkem
        }
    casLastSwitch=millis();
    switch3_state_last=switch3_state;
  }
  digitalWrite(relay_pin, relay_state);
}


void breath_action(){
    breath_state=digitalRead(breath_pin);

  if (breath_state==LOW && breath_state != breath_state_last){  
    breath_time=millis();
    }
  breath_state_last=breath_state;
}

    
void loop(){
  breath_action();
  button3_action();
  nebulization_start_time = breath_time + breath_delay;

  if(button_ON==LOW){ //pokud nebylo relé zapnuto tlačítkem může relé ovládat simulátor
    if (breath_state==LOW && millis()>=nebulization_start_time){
      relay_state=LOW;
      casLastSwitchON=millis();
      } else if(breath_state==HIGH){
        relay_state=HIGH;
        }
  }
  
  myScreen.stroke(255,255,255); //bílá barva
  myScreen.setTextSize(2);
  
  if(last_time/1000 != millis() / 1000){
    myScreen.setCursor(0, 8);  // set the cursor to column X, line Y
    myScreen.stroke(0,0,0); 
    myScreen.print(last_time / 1000); // print the number of seconds since reset
    myScreen.print("s");
    myScreen.stroke(255,255,255);
    myScreen.setCursor(0, 8);  // set the cursor to column X, line Y 
    myScreen.print(millis() / 1000); // print the number of seconds since reset
    last_time=millis();
    myScreen.print("s");
    }

  if(last_breath_time != breath_time){
    myScreen.setCursor(0, 34);  // set the cursor to column X, line Y
    myScreen.stroke(0,0,0); 
    myScreen.print(last_breath_time/1000.0,3); // print the number of seconds since reset
    myScreen.print("s");
    myScreen.stroke(255,255,255);
    myScreen.setCursor(0, 34);  // set the cursor to column X, line Y 
    myScreen.print(breath_time/1000.0,3); // print the number of seconds since reset
    last_breath_time=breath_time;
    myScreen.print("s");
    }


  myScreen.setCursor(0, 60);
  myScreen.print(breath_delay);
  myScreen.print("ms");
  
  myScreen.setCursor(0, 86);
  myScreen.print(duration);
  myScreen.print("ms");

  
  if(last_relay_state != relay_state){
    myScreen.setCursor(0, 112);  // set the cursor to column X, line Y
    myScreen.stroke(0,0,0);
    if (!last_relay_state == 1){
      myScreen.print("ON"); //možnost !inverze hodnoty
      } else{
        myScreen.print("OFF");
        } 
    myScreen.stroke(255,255,255);
    myScreen.setCursor(0, 112);  // set the cursor to column X, line Y 
    if (!relay_state == 1){
      myScreen.print("ON"); //možnost !inverze hodnoty
    } else{
      myScreen.print("OFF");
      }
      last_relay_state=relay_state;
    }

    
//  myScreen.setCursor(0, 112);
//  if (!relay_state == 1){
//  myScreen.print("ON"); //možnost !inverze hodnoty
//  } else{
//    myScreen.print("OFF");
//    }

 
}
