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


// definições
#define DHT22_PIN 8
#define POT_PIN 0
#define BPM_MIN 5
#define BPM_MAX 120
#define SERVO_PIN 11

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

// classes
dht DHT;
RTC_DS1307 rtc;
Servo servo;

// variáveis globais
int8_t contador = 0;
// IMAGENS
// 'coracao_small', 33x32px
const unsigned char coracao_small [] PROGMEM = {
	0x80, 0xe0, 0xf0, 0xf8, 0xf8, 0xfc, 0xfc, 0xfc, 0xfc, 0xfc, 0xfc, 0xfc, 0xfc, 0xf8, 0xf0, 0xe0, 
	0x80, 0xe0, 0xf0, 0xf8, 0xf8, 0xfc, 0xfc, 0xfc, 0xfc, 0xfc, 0xfc, 0xfc, 0xf8, 0xf8, 0xf0, 0xe0, 
	0x80, 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, 0x3f, 0x00, 0x00, 0x03, 0x07, 0x0f, 0x3f, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x3f, 0x0f, 0x07, 
	0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 
	0x3f, 0x3f, 0x7f, 0xff, 0x7f, 0x3f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00
};

// 'coracao00', 44x40px
const unsigned char coracao [] PROGMEM = {
	0x00, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xfe, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfe, 
	0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xfe, 0xfe, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xfe, 0xfe, 0xfc, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x00, 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, 0x7e, 0x00, 0x03, 0x0f, 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, 0xff, 0x7f, 
	0x1f, 0x0f, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x07, 
	0x0f, 0x1f, 0x1f, 0x3f, 0x7f, 0xff, 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x0f, 0x07, 0x03, 0x01, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

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, 
	0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  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(DHT22_PIN, INPUT);

  // Configura o Servo motor
  servo.attach(SERVO_PIN);
  servo.write(0);

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

void loop(){
  controlaServo();
}

// implementação das funções
void controlaServo(){
  static uint16_t angulo = 0;
  static bool servo_desc = true;
  
  servo.write(angulo);
  if (angulo <= 0){
    angulo = 0;
    servo_desc = true;
  }
  if (angulo >= 90){
    angulo = 90;
    servo_desc = false;
  } 
  if (servo_desc){
    angulo += 10;
  } else {
      angulo -= 10;
  }
  delay(1000);
}
void menuBPM(){
  oled.setFont(FONT8X16);
  oled.setCursor(26, 2);
  oled.print(lerPot());
  oled.print("  ");
  oled.setCursor(18, 4);
  oled.print(" BPM ");
}
uint16_t lerPot(){
  static uint16_t valor_pot = 0;
  // ler potenciometro e condiciona entre 20:120
  valor_pot = analogRead(POT_PIN);
  valor_pot = map(valor_pot, 0, 1023, BPM_MIN, BPM_MAX);
  
  return valor_pot;
}
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(74, 1, 33+74, 32/8+1, coracao_small);
  delay(500);
  limpaDisplay();
  oled.bitmap(70, 1, 44+70, 40/8+1, coracao);
  delay(500);
  limpaDisplay();
}

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+