#include <PinButton.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "thermistor.h"
#include "HardwareSerial.h"
#include "Timer.h"

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64
#define BUTTON_PIN 5
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
#define NTC_PIN A1

THERMISTOR thermistor(NTC_PIN, 10000, 3950, 10000);
uint16_t waterTemp;

Timer timer;

PinButton mb(BUTTON_PIN);
bool tempMode = true;
int targetTemp = 180;
bool running = false;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


void setup() {
  Serial.begin(115200);

  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  timer.start;
  timer.stop;
  if (timer.state == STOPPED) Serial.println("stop");
  //tempModeDisplay();
}

void loop() {
  /*
  mb.update();
  actualTemp = 190;

  if (mb.isLongClick()) {
    switchMode();
  }


  if (mb.isDoubleClick() and tempMode) {
    Serial.println("decrease temp");
    if (waterTemp > 170) {
      waterTemp -= 5;
    }
    checkTemp();
    tempModeDisplay();
  }

  if (mb.isSingleClick() and tempMode) {
    Serial.println("increase temp");
    if (waterTemp < 210) {
      waterTemp += 5;
    }
    checkTemp();
    tempModeDisplay();
  }


  if (mb.isSingleClick() and (!tempMode)){
    if (running == false) {
      running = true;
      Serial.println("start timer");
    } else {
      running = false;
      Serial.println("stop timer");
    }
  }

  if ((!tempMode) and mb.isDoubleClick()) {
    Serial.println("change timer");
  }

}


void checkTemp() {
  if (waterTemp > targetTemp){
    tempStatus = "hot";
  }
  else if ((targetTemp - waterTemp) > 5) {
    tempStatus = "cold";
  }
  else {
    tempStatus = "ready for brewing"
  }
}

void checkTime() {
  if ( > targetTemp){
    tempStatus = "hot";
  }
  else if ((targetTemp - waterTemp) > 5) {
    tempStatus = "cold";
  }
  else {
    tempStatus = "ready for brewing"
  }
}

void tempModeDisplay() {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  display.println(tempStatus);
  display.display();
}


void timeModeDisplay() {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 10);
  display.println(timeStatus);
  display.display();
}

void switchMode() {
  if (tempMode) {
      tempMode = !tempMode;
      timeModeDisplay();
    }
    else {
      tempMode = !tempMode;
      tempModeDisplay();
    }
  Serial.println("mode changed");*/
}