
// image converter:
// https://mischianti.org/images-to-byte-array-online-converter-cpp-arduino/


#include <Arduino.h>
#include <SPI.h>
#include <TFT_eSPI.h>
#include <XPT2046_Touchscreen.h>

TFT_eSPI tft = TFT_eSPI();

// touchscreen pins 
#define XPT2046_IRQ 36  // T_IRQ
#define XPT2046_MOSI 32 // T_DIN
#define XPT2046_MISO 39 // T_OUT
#define XPT2046_CLK 25  // T_CLK
#define XPT2046_CS 33   // T_CS
SPIClass touchscreenSPI = SPIClass(VSPI);
XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);

#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
#define FONT_SIZE 2

#include "cow-all.h"

// extra colors
#define NAVY tft.color565(0,0,160)   
#define DODGERBLUE tft.color565(30,144,255)
#define TURQUOISE tft.color565(64,224,208) 
#define CYAN tft.color565(0,255,255)  
#define GOLD tft.color565(255,215,0)
#define ORANGE tft.color565(255,165,0)
#define RED tft.color565(255,10,0)
#define MAROON tft.color565(123,0,0)
#define BROWN tft.color565(150,75,0)

// Global Variables
int x, y, z;     // touchscreen coordinates + pressure

// Set X and Y coordinates for center of display
int centerX, centerY;

//moodmeter & cow
 uint32_t moodcolor;
 int food, water, mood, mooddegree, waterlevel, foodlevel;
 int cowX,cowY;
 uint32_t cowImage;
 int saturation, thirst;
 

void drawStable() {
  //tft.drawRect(260, 10, 50, 14, TFT_RED);         //status
  tft.fillRoundRect(10, 80, 30, 70, 3, GOLD );          //food   TFT_DARKGREEN
  tft.fillRoundRect(10, 140, 50, 10, 5, TFT_DARKGREEN); //food

  tft.fillRoundRect(290, 80, 16, 60, 5, DODGERBLUE);  //water
  tft.drawRoundRect(290, 80, 16, 60, 5, TFT_WHITE);  //water
  tft.drawRoundRect(280, 138, 26, 2, 2, TFT_WHITE);  //water
}


void setup()
{
  Serial.begin(115200);

  // init RGB LED
  pinMode(4, OUTPUT); pinMode(16, OUTPUT); pinMode(17, OUTPUT);
  // turn off LED
  digitalWrite(4, HIGH); digitalWrite(16, HIGH); digitalWrite(17, HIGH);

  // Start the SPI for the touchscreen and init the touchscreen
  touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
  touchscreen.begin(touchscreenSPI);
  touchscreen.setRotation(3);

  // start display
  tft.init();
  tft.setRotation(3); // may be altered
  tft.fillScreen(TFT_BLACK);
  tft.setTextColor(TFT_WHITE, TFT_BLACK);

  // Set X and Y coordinates for center of display
  centerX = SCREEN_WIDTH / 2;
  centerY = SCREEN_HEIGHT / 2;

  tft.drawCentreString("Welcome to my stable", centerX, 30, FONT_SIZE);

  int x = (tft.width() - 116) / 2;
  int y = (tft.height() - 110) / 2;

food=60;
saturation = 90;
water= 80;

  //clear cow area
  tft.fillRect(40, 70, 249, 170, TFT_BLACK);
  // draw cow
  //tft.drawBitmap(40, 80, cow_well, 160, 140, TFT_WHITE);
  //tft.drawBitmap(45, 80, cow_eat, 160, 142, TFT_WHITE);
  tft.drawBitmap(70, 80, cow_smile, 160, 145, TFT_WHITE);
  delay(2000);
  tft.fillRect(50, 0, 200, 50, TFT_BLACK); //clear area
}

void loop()  {
  // Checks if Touchscreen is touched
  if (touchscreen.tirqTouched() && touchscreen.touched()) {
    // Get Touchscreen points
    TS_Point p = touchscreen.getPoint();
    // Calibrate Touchscreen points with map function to the correct width and height
    x = map(p.x, 200, 3700, 1, SCREEN_WIDTH);
    y = map(p.y, 240, 3800, 1, SCREEN_HEIGHT);
    z = p.z;

    printTouchToSerial(x, y, z);

       
    //food touch
    if ((x>5)&&(x<60)&&(y>70)&&(y<160)) food=food+20;
    if (food>=100) food=100;
    //water touch
    if ((x>270)&&(x<315)&&(y>60)&&(y<155)) water=water+20;
    if (water>=100) water=100;
    
    cowX=50; cowY = 80; //start coordinates of cow bmp
    //cow touch
    if ((x>cowX)&&(x<cowX+150)&&(y>cowY)&&(y<cowY+130)) mood=mood+20;
    if (mood>=100) mood=100;

    Serial.print(food); Serial.print("  "); Serial.print(water);  Serial.print("  "); Serial.println(mood); 
    delay(80);
  }
  moodmeter(mood);
  waterfill(water);
  foodfill(food);

/*
  //  food /water levels ********************
  if (cowImage = cow_eat) {
    food--;
    saturation = saturation + 6;
    
  }
  if (cowImage = cow_drink) {
    water--;
    thirst = thirst -7;
  }

  //hunger/thirst comes up  *******************
  if (millisnow - millispast = random(480000, 900000)) {  //8-15 minutes
    saturation = saturation-3;
    thirst = thirst + 2;
    if (saturation<=0) saturation=0;
    if (thirst<=0) thirst=0;
  }
  */
  food--; water--; mood--;
  if (food<=0) food=0;
  if (water<=0) water=0;
  if (mood<=0) mood=0;
  delay(200);
}


void waterfill(int water) {
  tft.fillRoundRect(290, 80, 16, 60, 5, TFT_BLACK); //clear area
  waterlevel = map(water, 0, 100, 140, 80);
  tft.fillRect(290, waterlevel, 16, (140-waterlevel), DODGERBLUE);  //water
  tft.drawRoundRect(289, 79, 18, 62, 5, TFT_WHITE);  //water frame
  tft.drawRoundRect(275, 138, 26, 3, 1, TFT_WHITE);  //water 
}

void foodfill(int food) {
  tft.fillRoundRect(10, 80, 40, 70, 5, TFT_BLACK); //clear area
  foodlevel = map(food, 0, 100, 150 , 80);
  tft.drawRoundRect(9, 79, 42, 72, 3, BROWN);     //food frame
  tft.fillRoundRect(10, foodlevel, 40, (150-foodlevel), 3, TFT_DARKGREEN);   //food   
  tft.fillTriangle(9, 150, 10, 157, 62, 150, BROWN);    //feeding dish
}

void moodmeter(int mood) {
  tft.fillRect(0, 0, 43, 43, TFT_BLACK); //clear area
  mooddegree = map(mood, 0, 100, 1, 360);
  if (mood < 15) moodcolor = RED;
  if ((mood >= 15) && (mood < 30)) moodcolor = ORANGE;
  if ((mood >= 30) && (mood < 60)) moodcolor = GOLD;
  if (mood >= 60) moodcolor = TFT_DARKGREEN;
  tft.drawSmoothArc(22, 22, 20, 8, 0, mooddegree, moodcolor, TFT_BLACK);
}

void printTouchToSerial(int x, int y, int z) {
  Serial.print("X = ");
  Serial.print(x);
  Serial.print(" | Y = ");
  Serial.print(y);
  Serial.print(" | Pressure = ");
  Serial.print(z);
  Serial.println();
}
