#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
 
#define SCREEN_WIDTH 128 // OLED 寬度像素
#define SCREEN_HEIGHT 64 // OLED 高度像素
#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16
#define test_width 50
#define test_height 50
#define txt_WIDTH 50
#define txt_HEIGHT 50
#include "DHTesp.h"
DHTesp dhtSensor;
const int DHT_PIN = 13;
 
#include "RTClib.h"
RTC_DS1307 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
// 設定OLED
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
static const uint8_t degreeIcon[] PROGMEM = {
   B0000000,
  B0001100,
  B0010010,
  B0010010,
  B0010010,
  B0001100,
  B0000000
};
/*static const unsigned char PROGMEM txt_s[] ={0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 
0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 
0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 
0xc0, 0xff, 0xff, 0xfe, 0xff, 0xdf, 0xff, 0xc0, 0xff, 0xfb, 0xfe, 0xff, 0xff, 0xff, 0xc0, 0xff, 
0xff, 0xff, 0xff, 0xbf, 0xff, 0xc0, 0xff, 0xfd, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 
0xff, 0x7f, 0xff, 0xc0, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 
0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xdf, 0xff, 0xc0, 0xfd, 0xef, 0xff, 0xff, 0xff, 0xff, 0xc0, 
0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xbf, 
0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf, 0xc0, 0xff, 0xff, 0xff, 0xff, 
0xfc, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
0xc0, 0xfe, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xef, 0xcf, 0xff, 0xff, 0xc0, 0xff, 
0xff, 0xcf, 0xcf, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xcf, 0xcf, 0xfd, 0x7f, 0xc0, 0xff, 0xff, 0xef, 
0xff, 0xff, 0xff, 0xc0, 0x80, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xf8, 0x7f, 0xff, 
0xff, 0xc0, 0xff, 0xff, 0xfc, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 
0xff, 0xbf, 0xff, 0xff, 0xfb, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 
0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xdf, 0xc0, 0xff, 0xef, 0xff, 0xff, 
0xff, 0xff, 0xc0, 0xff, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xbf, 0xff, 0xff, 0xef, 0xff, 
0xc0, 0xff, 0x7d, 0xff, 0xff, 0xf7, 0xff, 0xc0, 0xfe, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xfd, 
0xff, 0xff, 0xfd, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xbf, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xef, 
0xbe, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xbe, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xdf, 0xff, 0xff, 
0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff, 0xc0, 0xff, 0xff, 0xbf, 0xff, 0xff, 0xff, 0xc0, 
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0};*/
/*static const unsigned char PROGMEM logo_r[] ={0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 
0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 
0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xf8, 0x8f, 0xff, 0xff, 
0xc0, 0xff, 0xff, 0xf7, 0xf3, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xef, 0xfd, 0xff, 0xff, 0xc0, 0xff, 
0xff, 0xdf, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xbf, 0xfe, 0xff, 0xff, 0xc0, 0xff, 0xe7, 0x7f, 
0xff, 0x7b, 0xff, 0xc0, 0xff, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xbf, 0xff, 0xff, 0xfe, 
0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xc0, 0xff, 0x7f, 0xff, 0xff, 0xff, 0x7f, 0xc0, 
0xff, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xfb, 0xff, 0xff, 0xff, 0xff, 0xe7, 0xc0, 0xef, 0xff, 
0xff, 0xff, 0xff, 0xfb, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd, 0xc0, 0xdf, 0xff, 0xc3, 0xf0, 
0xff, 0xfd, 0xc0, 0xdf, 0xff, 0xbb, 0xee, 0x7f, 0xff, 0xc0, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 
0xc0, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xfe, 0xbf, 0xff, 0xfd, 0xc0, 0xef, 
0xff, 0xff, 0xff, 0xff, 0xfb, 0xc0, 0xf3, 0xff, 0xff, 0xff, 0xff, 0xf7, 0xc0, 0xfe, 0xff, 0xff, 
0xff, 0xff, 0x9f, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbf, 0xc0, 0xff, 0x7f, 0xff, 0xff, 0xff, 
0xff, 0xc0, 0xff, 0xbf, 0xff, 0xff, 0xff, 0x7f, 0xc0, 0xff, 0xc0, 0xff, 0xff, 0x80, 0xff, 0xc0, 
0xff, 0xff, 0xbf, 0xfe, 0x7f, 0xff, 0xc0, 0xff, 0xff, 0xe3, 0xf3, 0xff, 0xff, 0xc0, 0xff, 0xff, 
0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0x9f, 0xfc, 0xff, 
0xcf, 0xff, 0xc0, 0xff, 0x5f, 0xfa, 0xff, 0xaf, 0xff, 0xc0, 0xfe, 0xdf, 0xf5, 0xff, 0xaf, 0xff, 
0xc0, 0xff, 0x3f, 0xf1, 0xff, 0x8f, 0xff, 0xc0, 0xff, 0xff, 0xfb, 0xff, 0xff, 0xff, 0xc0, 0xff, 
0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xfd, 0xff, 
0xef, 0xff, 0xff, 0xc0, 0xff, 0xf9, 0xff, 0xcf, 0xff, 0xff, 0xc0, 0xff, 0xf5, 0xff, 0xaf, 0xff, 
0xff, 0xc0, 0xff, 0xf5, 0xff, 0xaf, 0xff, 0xff, 0xc0, 0xff, 0xfb, 0xff, 0xdf, 0xff, 0xff, 0xc0, 
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0
};*/
/*static const unsigned char PROGMEM logo_v[] = {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, 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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 
0xff, 0xc0, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xf0, 
0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xf8, 0x00, 0x00, 
0x00, 0x0f, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x0f, 
0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x0f, 0xfd, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x0f, 0xfc, 0xff, 
0xf8, 0x00, 0x00, 0x00, 0x0f, 0xfc, 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x0f, 0xfc, 0x3f, 0xf8, 0x00, 
0x00, 0x00, 0x0f, 0xfc, 0x1f, 0xf8, 0x00, 0x00, 0x00, 0x0f, 0xfc, 0x0f, 0xf8, 0x00, 0x00, 0x00, 
0x0f, 0xfc, 0x0f, 0xf8, 0x00, 0x00, 0x00, 0x0f, 0xfc, 0x1f, 0xf8, 0x00, 0x00, 0x00, 0x0f, 0xfc, 
0x3f, 0xf8, 0x00, 0x00, 0x00, 0x0f, 0xfc, 0x7f, 0xf8, 0x00, 0x00, 0x00, 0x0f, 0xfc, 0xff, 0xf8, 
0x00, 0x00, 0x00, 0x0f, 0xfd, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xf8, 0x00, 0x00, 
0x00, 0x0f, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x0f, 
0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 
0xf0, 0x00, 0x00, 0x00, 0x07, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xe0, 0x00, 
0x00, 0x00, 0x00, 0x0f, 0xf8, 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, 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, 0x00, 0x00, 0x00, 0x00, 0x00};
*/
/*static const unsigned char PROGMEM logo_bmp[] =
{ B00000000, B11000000,
  B00000001, B11000000,
  B00000001, B11000000,
  B00000011, B11100000,
  B11110011, B11100000,
  B11111110, B11111000,
  B01111110, B11111111,
  B00110011, B10011111,
  B00011111, B11111100,
  B00001101, B01110000,
  B00011011, B10100000,
  B00111111, B11100000,
  B00111111, B11110000,
  B01111100, B11110000,
  B01110000, B01110000,
  B00000000, B00110000 };
  */
void testdrawstyles(void) {
 
  /*display.clearDisplay();
  display.setTextSize(1);          // 設定文字大小
  display.setTextColor(WHITE);         // 1:OLED預設的顏色(這個會依該OLED的顏色來決定)
  display.setCursor(5,0);          // 設定起始座標
  display.print("Hello OLED");     // 要顯示的字串
  display.setCursor(26,40);        // 設定起始座標
  display.print("STUST");        // 要顯示的字串
  display.display();               // 要有這行才會把文字顯示出來*/
 
  //delay(2000);
  //int16_t i;
  //display.clearDisplay(); // Clear display buffer
 //i = 100;
 // for(i=0; i<max(display.width(),display.height())/2; i+=2) {
    //display.drawCircle(display.width()/2,display.height()/2-i, i, SSD1306_WHITE);
    //display.fillCircle(display.width()/2,display.height()/2+i, i, SSD1306_WHITE);
    //display.drawCircle(display.width()/2, display.height()/2, i, SSD1306_WHITE);
    // display.drawLine(0,0,display.width()-1, display.height()-1,SSD1306_WHITE);
    //display.drawLine(display.width()-1, 0,0,display.height()-1,SSD1306_WHITE);
    //display.drawBitmap(0,0, logo_v, LOGO_WIDTH, LOGO_HEIGHT, SSD1306_WHITE);
    //display.drawBitmap(0,0, logo_v, test_width, test_height, SSD1306_WHITE);
    //display.drawRect(0, 0, display.width()/2,10, SSD1306_WHITE);
    //display.fillRect(0,display.height()/2,display.width()/2,display.height()-1, SSD1306_WHITE);
    //display.drawRoundRect(0, 0, display.width()/2,10,30 ,SSD1306_WHITE);
    //display.fillRoundRect(0, 50, display.width()/2,60,30, SSD1306_WHITE);
    //display.display();
    //display.drawBitmap(0,0, txt_s, txt_WIDTH, txt_HEIGHT, SSD1306_WHITE);
    delay(1);
 // }
  delay(100);
}
 
void setup() {
  
 
  Serial.begin(115200);
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
   if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.clearDisplay();
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  delay(1000);
}
 
void loop() {
  TempAndHumidity data = dhtSensor.getTempAndHumidity();
  DateTime now = rtc.now();
  
  display.setTextSize(1.5);
  display.setCursor(0, 0);
  display.print(now.year(), DEC); 
  display.print('/');
  display.print(now.month(), DEC);
  display.print('/');
  display.print(now.day(), DEC);
  display.print(' ');
  display.print(daysOfTheWeek[now.dayOfTheWeek()]);
  display.println(' ');
  display.setCursor(0, 10);
  if (now.hour() < 10)
    display.print('0');
  display.print(now.hour(), DEC);
  display.print(':');
  if (now.minute() < 10)
    display.print('0');
  display.print(now.minute(), DEC);
  display.print(':');
  if (now.second() < 10)
    display.print('0');
  display.println(now.second(), DEC);
  display.display();
  delay(1000);
  
  displayTemperatureAndHumidity(data.temperature, data.humidity);
}
void displayTemperatureAndHumidity(float temperature, float humidity) {
  display.clearDisplay();
  display.setTextSize(1.5);
  display.setCursor(0, 0);
  display.print("Temperature=");
  display.print(String(temperature, 2));
  display.drawBitmap(display.getCursorX(), display.getCursorY() - 1.5, degreeIcon, 7, 7, SSD1306_WHITE);
  display.setCursor(display.getCursorX() + 9, display.getCursorY());
  display.print("C");
  display.setCursor(0, 10);
  display.print("Humidity=");
  display.print(String(humidity, 2));
  display.print("%");
  display.display();
  delay(1000);
}