#include <Tiny4kOLED.h>
#include <dht.h>
#include <RTClib.h>

// definições
#define DHT22_PIN 8

// protótipo da função
void iniciaDisplay();
void limpaDisplay();
void incrementa();
void decrementa();
void menuTemperatura();
void menuImagem();
void menuRTC();

// classes
dht DHT;
RTC_DS1307 rtc;

// variáveis globais
int8_t contador = 0;

const uint8_t termometro[] PROGMEM = {
  0x00, 0xfe, 0x03, 0xfe, 0x50,
  0x00, 0xff, 0x00, 0xff, 0x55,
  0x60, 0x9f, 0x80, 0x9f, 0x65,
};

const unsigned char pikachu[] PROGMEM = {
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x3f, 0x1f, 0x1f, 0x0f, 
	0x0f, 0x0f, 0x07, 0x07, 0x87, 0x87, 0x87, 0x87, 0x07, 0x07, 0x0f, 0x0f, 0x0f, 0x0f, 0x1f, 0x1f, 
	0x3f, 0x7f, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0x1f, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x3c, 0x3e, 0x1f, 
	0x07, 0x07, 0x07, 0x07, 0x07, 0x0f, 0x1f, 0x3e, 0x3c, 0x38, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 
	0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf, 0x3f, 
	0x1f, 0x08, 0x00, 0x08, 0x0c, 0xfc, 0xdc, 0x5e, 0x9e, 0x8e, 0xaf, 0x6f, 0x0f, 0x8f, 0x07, 0x07, 
	0x07, 0x87, 0x8f, 0x6f, 0x2f, 0x8f, 0x9e, 0x46, 0xae, 0x6c, 0xcc, 0xc0, 0x00, 0x00, 0x0f, 0x1f, 
	0x1f, 0xbf, 0xff, 0x3f, 0x9f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf3, 0xe1, 0xe0, 0xe0, 0x80, 0x00, 0x1e, 
	0x18, 0x3c, 0x3f, 0xfe, 0xfe, 0xff, 0xe1, 0xf9, 0xff, 0xfe, 0xff, 0xfc, 0xfe, 0xff, 0xff, 0xff, 
	0xfe, 0xff, 0xe9, 0xfd, 0xff, 0xfc, 0x3f, 0x3c, 0xdd, 0xcf, 0xfe, 0xfe, 0xfc, 0x7c, 0x78, 0xf8, 
	0xdf, 0xf7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x10, 
	0xc0, 0xfb, 0x1b, 0x17, 0x07, 0x0f, 0x0f, 0x0f, 0x8f, 0x1f, 0x1f, 0x1f, 0x8f, 0x0f, 0x0f, 0x07, 
	0x03, 0x1f, 0x3f, 0x7f, 0x47, 0xf7, 0xff, 0xff, 0xbf, 0xff, 0xf3, 0xf9, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xf8, 0xf8, 0xfc, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xe7, 0xfc, 0xf7, 0x0b, 0x08, 0x08, 0x08, 0x00, 0x00, 0x3e, 
	0x3c, 0x3a, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x03, 0x03, 0x02, 0x02, 
	0x02, 0x82, 0xc2, 0xe3, 0xe1, 0xc3, 0x82, 0x02, 0x02, 0x02, 0x02, 0x01, 0x00, 0xfc, 0xfe, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcf, 0xcf, 0xc0, 0xc0, 0xc0, 0xe0, 0xf8, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xc0, 0xc0, 0xc0, 0xc0, 0xcf, 0xcf, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 
	0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 
	0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 
	0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 
	0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 
	0x0f, 0x0f, 0x0f, 0x0f, 0x0f
};
void setup(){
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(DHT22_PIN, INPUT);

  // configura INT nos pinos
  // 2 e 3 
  attachInterrupt(digitalPinToInterrupt(3), incrementa, RISING);
  attachInterrupt(digitalPinToInterrupt(2), decrementa, RISING);
  
  iniciaDisplay();
  delay(2000);
  limpaDisplay();
  
}

void loop(){
  menuRTC();
}

// implementação das funções
void menuRTC(){
  DateTime now = rtc.now();
  DateTime data0 (2012, 05, 19, 0, 0, 0);

 	oled.setCursor(18, 2);
  oled.print("Data: ");
  oled.print(now.day(), DEC);
  oled.print("/");
  oled.print(now.month(), DEC);
  oled.print("/");
  oled.print(now.year(), DEC);
  oled.print(" ");

  oled.setCursor(18, 4);
  oled.print("Hora: ");
  oled.print(now.hour(), DEC);
  oled.print(":");
  oled.print(now.minute(), DEC);
  oled.print(":");
  oled.print(now.second(), DEC);
  oled.print(" ");
}
void menuImagem(){
  oled.bitmap(20, 0, 77+20, 68/8, pikachu);
}

void menuTemperatura(){
  // leitura do sensor de temperatura
  DHT.read22(DHT22_PIN);
  oled.setCursor(0, 4);
  oled.print("Temp: ");
  oled.print(DHT.temperature, 1);
  oled.print((char)247);
  oled.print("C ");

  // leitura do sensor de humidade
  oled.setCursor(0, 6);
  oled.print("Humidade: ");
  oled.print(DHT.humidity, 1);
  oled.print("% ");

  // inserindo imagem
  oled.bitmap(100, 0, 5+100, 3+0, termometro);
}

void iniciaDisplay(){
  oled.begin();
  oled.clear();
  oled.setFont(FONT8X16);
  oled.setCursor(44, 1);
  oled.print(F("IFPB"));
  oled.setFont(FONT6X8);
  oled.setCursor(28, 4);
  oled.print(F("Prof. Cicero"));
  oled.setCursor(12, 6);
  oled.print(F("Sistemas Digitais"));
}

void limpaDisplay(){
  oled.clear();
  oled.switchRenderFrame();
  oled.clear();
  oled.switchRenderFrame();
}

void incrementa(){
  // variável local
  static long tempo_anterior = 0;
  long tempo_atual = millis();

  if ( (tempo_atual - tempo_anterior) > 200){
    // contador++
    contador = contador + 1; 
  }
  tempo_anterior = tempo_atual;
}
void decrementa(){
  // variável local
  static long tempo_anterior = 0;
  long tempo_atual = millis();

  if ( (tempo_atual - tempo_anterior) > 200){
    contador--;
  }
  tempo_anterior = tempo_atual;
}
GND5VSDASCLSQWRTCDS1307+