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

// definições
#define DHT22_PIN 8

// classes
dht DHT;

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

// 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,
};

// 'pikachu', 59x64px
const unsigned char pikachu [] PROGMEM = {
	0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 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, 0x7f, 0x9f, 
	0x9f, 0xef, 0xcf, 0xcf, 0xe3, 0x03, 0x03, 0x00, 0x80, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xfc, 0xfc, 0xfc, 0xf8, 0xf0, 0xe2, 0xc2, 0x82, 0x0e, 0x0c, 0x7d, 0xfd, 0xfd, 0xfd, 0xfd, 0xfd, 
	0xf3, 0xf3, 0xf3, 0xf3, 0xf3, 0xf3, 0xef, 0xef, 0xf3, 0xf3, 0xf3, 0xf3, 0xf3, 0xf3, 0xf3, 0xf3, 
	0xf3, 0xf3, 0xef, 0xef, 0xf3, 0xf3, 0xf3, 0xfd, 0x7c, 0x7e, 0xff, 0xff, 0x7f, 0x9f, 0x9f, 0xe7, 
	0xf0, 0xf0, 0xfc, 0xff, 0xff, 0xff, 0x03, 0x03, 0xe7, 0xcf, 0x9f, 0x7f, 0x7f, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xfc, 0x1c, 0xe3, 0xe1, 0xfc, 0xfc, 0xff, 0x1f, 0x07, 
	0x07, 0x67, 0x27, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x1f, 0x07, 
	0x27, 0x67, 0x1f, 0x1f, 0xff, 0xe0, 0xe0, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xfb, 0xf3, 0xe7, 0xdf, 0x9f, 0x3f, 0x7f, 
	0xff, 0x03, 0x03, 0xfc, 0xc7, 0xc7, 0x03, 0x03, 0x03, 0xc7, 0xec, 0xfc, 0xfc, 0xdc, 0xdf, 0x3f, 
	0x3f, 0x3f, 0x99, 0x99, 0xdb, 0x3f, 0x3f, 0x3f, 0x1f, 0x9f, 0xfc, 0xfc, 0xfc, 0xc7, 0x87, 0x03, 
	0x03, 0x03, 0xc4, 0x03, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xf8, 0xe7, 0xe7, 
	0xcf, 0x3f, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x9c, 0xcc, 0xe0, 0xf0, 0x18, 0x27, 
	0x27, 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, 0xe7, 0xe5, 0x18, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xf7, 0xf7, 0xcf, 0x9f, 
	0xbf, 0x7f, 0x7e, 0xfe, 0x79, 0x03, 0x07, 0x01, 0x01, 0xfe, 0xff, 0xff, 0xff, 0xe1, 0xe1, 0x0f, 
	0x0f, 0x7f, 0xff, 0xf8, 0xf8, 0x07, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x00, 0xf8, 
	0xff, 0xff, 0x7f, 0x0f, 0x09, 0xe1, 0xff, 0xff, 0xf8, 0x07, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd, 0xf9, 0xf0, 0xe0, 0xc1, 0xc0, 
	0xc0, 0x80, 0x80, 0x80, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfe, 0xf9, 0xf9, 0xf9, 
	0xfe, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xf8, 0xf9, 0xf9, 0xf8, 0xfe, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0x3f, 0xc0, 0xc0, 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, 0xfe, 
	0x8d, 0x8d, 0xb3, 0x97, 0x9f, 0xbf, 0x8f, 0x8f, 0xcf, 0xc3, 0xc3, 0xf3, 0xf3, 0xf3, 0xf3, 0xf3, 
	0xf3, 0xf3, 0xf3, 0xf3, 0xe3, 0xcf, 0x8f, 0x8f, 0x9f, 0x9f, 0x97, 0xb3, 0x8d, 0x8d, 0xfe, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};

// 'coringa', 64x64px
const unsigned char coringa [] PROGMEM = {
	0x00, 0x00, 0x00, 0x00, 0xfe, 0x01, 0xe0, 0xa0, 0x3e, 0x0f, 0x1f, 0x3f, 0x1f, 0x5f, 0x3f, 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, 0xf8, 0xf8, 0xc0, 0x80, 0x30, 0xe0, 0xe0, 0xc0, 0x90, 0x08, 0x20, 0x00, 0x00, 0x00, 0x00, 
	0x20, 0x00, 0x40, 0x41, 0x20, 0x80, 0xff, 0xc7, 0xc7, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc0, 0x00, 
	0x83, 0x83, 0x1f, 0x3f, 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, 0x7f, 0x7f, 0x00, 0xff, 0xff, 0xff, 0xff, 0x04, 0x82, 0x80, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xfb, 0xf9, 0xf1, 0x90, 0x80, 0x80, 0x80, 0x83, 0x07, 
	0x06, 0x04, 0x06, 0x06, 0x04, 0x00, 0xc0, 0xc1, 0x9f, 0xff, 0xff, 0xff, 0xff, 0x8f, 0x1f, 0x1f, 
	0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 
	0x3f, 0xff, 0xff, 0x78, 0x38, 0x37, 0xef, 0x9f, 0x87, 0xc3, 0x00, 0x07, 0x07, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 
	0xfe, 0xfe, 0xfe, 0xf0, 0xff, 0xfc, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf3, 0xdd, 0x40, 
	0x60, 0x00, 0x03, 0x03, 0x07, 0x0f, 0x0c, 0x0d, 0x0c, 0x0c, 0x0c, 0x0e, 0x0c, 0x00, 0x00, 0x00, 
	0xe0, 0x00, 0x38, 0xbc, 0xff, 0xff, 0xff, 0x7f, 0x1f, 0x9f, 0xff, 0x7e, 0x60, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x3b, 0xff, 0x6f, 0xff, 0xff, 0xff, 0xff, 0xe3, 0xbf, 
	0x7d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xff, 0xfe, 
	0xfe, 0xff, 0xfe, 0xfe, 0xf8, 0xf0, 0xe6, 0xf8, 0xe4, 0xe4, 0xe4, 0xe6, 0xfc, 0xfc, 0xfc, 0xfc, 
	0xfb, 0xff, 0x7f, 0x7f, 0xff, 0x7f, 0x0f, 0x00, 0x00, 0x03, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x23, 0x8b, 0x0f, 0x1f, 0x1f, 0x1f, 0x1e, 
	0x7f, 0x6f, 0x6f, 0xd7, 0xef, 0xf7, 0xf7, 0xff, 0xdf, 0xdf, 0xef, 0xff, 0x7f, 0xfe, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0x7f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x1f, 0xdf, 0x8f, 0xef, 0xe7, 0x73, 
	0x39, 0x18, 0x0f, 0x07, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x3f, 0x7e, 0xfe, 0xf0, 0xf0, 
	0xe0, 0xe0, 0xe0, 0xc0, 0xc0, 0xc0, 0xc0, 0xe1, 0xe1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf9, 0xe1, 
	0xe1, 0x61, 0x61, 0x38, 0x38, 0x38, 0x0c, 0x1e, 0x07, 0x03, 0x03, 0x03, 0x01, 0x01, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x05, 
	0x07, 0x07, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x07, 0x07, 0x07, 0x03, 0x03, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

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(){
/*
  oled.setCursor(18, 3);
  oled.print(F("Contador: "));
  oled.print(contador);
  oled.print(" ");
*/
  oled.setCursor(0, 0);
  oled.bitmap(35, 0, 64+35, 64/8+0, coringa);
}

// implementação das funções
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 menuTemperatura(){
  DHT.read22(DHT22_PIN);
  oled.setFont(FONT8X16);
  oled.setCursor(12, 1);
  oled.print("Temp: ");
  oled.print(DHT.temperature);
  oled.print(" ");
  oled.setCursor(12, 4);
  oled.print(" Hum: ");
  oled.print(DHT.humidity);
  oled.print("% ");
  oled.bitmap(115, 1, 5+115, 3+1, termometro);
}

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;
}