#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <BluetoothSerial.h>

#define UI unsigned int
#define DHT_TYPE DHT22
#define DHT_PIN 5
#define LED_PIN 4
#define FPS 5

UI now = 0, prev = 0, last_update = 0, last_press = 0;
bool celsius = true;
int records = 0;
char dado;
float temperature, humidity, recs[4];

byte grau[8] = {0b00110, 0b01001, 0b01001, 0b00110,
                0b00000, 0b00000, 0b00000, 0b00000
               };
byte setaBaixo[8] = {0b00000, 0b00100, 0b00100, 0b00100,
                     0b10101, 0b01110, 0b00100, 0b00000
                    };
byte setaCima[8] = {0b00000, 0b00100, 0b01110, 0b10101,
                    0b00100, 0b00100, 0b00100, 0b00000
                   };

LiquidCrystal_I2C lcd(0x27, 16, 2);
DHT dht(DHT_PIN, DHT_TYPE);
BluetoothSerial SerialBT;

void setup() {
  Serial.begin(9600);
  Serial.println("Serial BEGIN");
  SerialBT.begin("UFFS-CC_ESP32");
  Serial.println("THE BLUETOOTH DEVICE IS READY TO PAIR");
  pinMode(LED_PIN, OUTPUT);
  lcd.init();
  lcd.backlight();
  lcd.createChar(0, grau);
  lcd.createChar(1, setaBaixo);
  lcd.createChar(2, setaCima);
  dht.begin();
  initialize();
}

void initialize() {
  temperature = recs[0] = recs[1] = dht.readTemperature();
  humidity = recs[2] = recs[3] = dht.readHumidity();
  Serial.println("INIT OK");
}

void loop() {
  now = millis();
  leBluetooth();
  updateTemp();
  printLCD();
  Serial.println("LOOPED\n");
  delay(20);
}

void leBluetooth() {
  if (SerialBT.available()) {
    dado = (SerialBT.read());
    Serial.write(dado);
    escreveBluetooth();
  }
}

void escreveBluetooth() {
  if (dado == 'L') {
    digitalWrite(LED_PIN, HIGH);
    SerialBT.print("Led ON via APP");
    Serial.println("Led ON via APP");
    SerialBT.print("*LR255G255B255*");
  } else if (dado == 'D') {
    digitalWrite(LED_PIN, LOW);
    SerialBT.print("Led OFF via APP");
    Serial.println("Led OFF via APP");
    SerialBT.print("*LR0G0B0*");
  } else if (dado == 'C') {
    celsius = !celsius;
    String c = celsius ? "Celsius" : "Fahrenheit";
    String comp = "Temperatura em " + c + " no LCD";
    SerialBT.print(comp);
    Serial.println(comp);
  } else if (dado == 'R') {
    records = (records + 1 > 2) ? 0 : records + 1;
    String r = "atuais";
    if (records) r = (records == 1) ? "mínimos" : "máximos";
    String comp = "Valores " + r + " no LCD";
    SerialBT.print(comp);
    Serial.println(comp);
  }
}

void updateTemp() {
  if (now - last_update < 1000) return; // DHT update rate 1s?
  temperature = dht.readTemperature();
  humidity = dht.readHumidity();
  SerialBT.print("*G"+String(temperature)+","+String(humidity)+"*");
  checkChanges();
  last_update = now;
}

void checkChanges() {
  if (temperature < recs[0]) recs[0] = temperature;
  if (temperature > recs[1]) recs[1] = temperature;
  if (humidity < recs[2]) recs[2] = humidity;
  if (humidity > recs[3]) recs[3] = humidity;
}

void printLCD() {
  if (now - prev < (1000 / FPS)) return; // FPS
  int t = temperature;
  if (records) t = (records == 1) ? recs[0] : recs[1];
  int h = humidity;
  if (records) h = (records == 1) ? recs[2] : recs[3];
  lcd.clear();
  lcd.setCursor(0, 0);
  if (records == 1) lcd.print("\x01");
  if (records == 2) lcd.print("\x02");
  if (celsius) lcd.print("Temp.: " + String(t, 1) + "\x08" + "C");
  else lcd.print("Temp.: " + String((t * 9 / 5) + 32) + "F");
  lcd.setCursor(0, 1);
  if (records == 1) lcd.print("\x01");
  if (records == 2) lcd.print("\x02");
  lcd.print("Umid.: " + String(h, 1) + " %");
  prev = now;
}