/*
   EnviroBox - 18-05-17
   By Chris Ritchie
*/

// Set pins for TFT display
#define sclk 4
#define mosi 5
#define cs   6
#define dc   7
#define rst  8  // Can also connect this to the Arduino reset (change to 0)

#define DHTPIN 2 // Pin 2 on DHT connects to D2 on Arduino
#define soilPin A0 // Soil moisture sensor connects to A0 on Arduino
#define DHTTYPE DHT22
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h> // 128x160
#include <SPI.h>
#include <DHT.h>

int tempC;
int tempF;
int humidity;
int moistureLevel;
int humidexF;
int humidexC;

DHT dht(DHTPIN, DHTTYPE);
Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, mosi, sclk, rst);



void setup(void) {
  Serial.begin(9600);
  dht.begin();

  tft.initR(INITR_BLACKTAB);
  
  tft.fillScreen(ST7735_BLACK);
  splashScreen();

  //tft.setRotation(tft.getRotation()+1); //uncomment to rotate display

  tft.drawRoundRect(5, 130, 120, 25, 5, ST7735_WHITE); // Box for moisture level
  tft.drawRoundRect(10, 40, 110, 70, 5, ST7735_WHITE); // Box for temperature
  tft.fillRect(0, 0, 160, 20, ST7735_WHITE); // Box for title

  // Title label
  tft.setCursor(12, 1);
  tft.setTextColor(ST7735_BLACK);
  tft.setTextSize(2);
  tft.print("EnviroBox");

  // Moisture label
  tft.fillRoundRect(23, 120, 80, 10, 5, ST7735_CYAN);
  tft.setCursor(40, 121);
  tft.setTextColor(ST7735_BLACK);
  tft.setTextSize(1);
  tft.print("Moisture");

  // Temperature label
  tft.fillRoundRect(27, 30, 80, 10, 5, ST7735_RED);
  tft.setCursor(35, 31);
  tft.setTextColor(ST7735_BLACK);
  tft.setTextSize(1);
  tft.print("Temperature");

}
void loop() {
  tempC = round((float)dht.readTemperature());
  tempF = round(dht.readTemperature(true));
  humidity = round((float)dht.readHumidity());
  humidexF = round((float)dht.computeHeatIndex(tempF, humidity));
  humidexC = round((float)dht.computeHeatIndex(tempC, humidity, false));
  moistureLevel = analogRead(soilPin);


  //Serial.println(humidexC);

  // Temperature box contents
  tft.setCursor(40, 50);
  tft.setTextColor(ST7735_GREEN);
  tft.setTextSize(2);
  tft.print(tempC);
  tft.setTextSize(1);
  tft.print("o");
  tft.setTextSize(2);
  tft.println("C");

  tft.setCursor(40, 70);
  tft.print(tempF);
  tft.setTextSize(1);
  tft.print("o");
  tft.setTextSize(2);
  tft.println("F");

  tft.setCursor(40, 90);
  tft.print(humidity);
  tft.println("%");

  displayMoistureBar(moistureLevel);

  delay(2000);
  clearScreen();
}


void clearScreen()
{
  // Draw rectangle over temperatures and moisture level instead of using fillScreen to avoid whole screen redrawing
  tft.fillRect(7, 132, 117, 22, ST7735_BLACK);
  tft.fillRect(11, 45, 100, 60, ST7735_BLACK);
}

void displayMoistureBar(int moisture)
{
  if (moisture < 200)
    tft.fillRect(7, 132, 5, 22, ST7735_BLUE);
  else
    tft.fillRect(7, 132, 15, 22, ST7735_BLUE);

  if (moisture >= 225)
    tft.fillRect(22, 132, 15, 22, ST7735_BLUE);
  if (moisture >= 250)
    tft.fillRect(37, 132, 15, 22, ST7735_BLUE);
  if (moisture >= 275)
    tft.fillRect(52, 132, 15, 22, ST7735_BLUE);
  if (moisture >= 300)
    tft.fillRect(67, 132, 15, 22, ST7735_BLUE);
  if (moisture >= 325)
    tft.fillRect(82, 132, 15, 22, ST7735_BLUE);
  if (moisture >= 350)
    tft.fillRect(97, 132, 15, 22, ST7735_BLUE);
  if (moisture >= 375)
    tft.fillRect(109, 132, 15, 22, ST7735_BLUE);
}

void splashScreen() {
  tft.setCursor(12, 50);
  tft.setTextColor(ST7735_YELLOW);
  tft.setTextSize(2);
  tft.println("EnviroBox");
  tft.setCursor(20, 70);
  tft.setTextSize(1);
  tft.print("by Chris Ritchie");
  delay(1000);
  tft.fillScreen(ST7735_BLACK);
}