//  Both_RX-8025NB  by kkurahashi 2016-06-21
//
//  thanks:
//    秋月のリアルタイムクロック(RTC)モジュール
//    http://s2jp.com/2015/03/rtc-module/
//
//

#include <Wire.h>
#include <Time.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>

extern "C" {
#include "user_interface.h"
#include "sntp.h"
}



#define LCD_ADRS  0x3E
#define RTC_ADRS  0x32


void setupRTC() {
  Wire.beginTransmission(RTC_ADRS);
  Wire.write(0xE0);
  Wire.write(0x20);  // 24 hour mode
  Wire.write(0x00);  // PON Clear
  Wire.endTransmission();
  delay(1);
}

void writeRTC(time_t *inTime) {
  int yy, mm, dd, HH, MM, SS;

  setTime(*inTime);

  Serial.print("year = ");
  Serial.println(year());

  yy   = year() % 100;
  mm   = month();
  dd   = day();
  HH   = hour();
  MM   = minute();
  SS   = second();

  Wire.beginTransmission(RTC_ADRS);
  Wire.write(0x00);
  Wire.write(toClockFormat(SS));
  Wire.write(toClockFormat(MM));
  Wire.write(toClockFormat(HH));
  Wire.write(0x00);
  Wire.write(toClockFormat(dd));
  Wire.write(toClockFormat(mm));
  Wire.write(toClockFormat(yy));
  Wire.endTransmission();
}


void readRTC(time_t *outTime) {
  int yyyy, mm, dd, HH, MM, SS;

  Wire.requestFrom(RTC_ADRS, 8);

  Wire.read();
  SS   = fromClockFormat(Wire.read());
  MM   = fromClockFormat(Wire.read());
  HH   = fromClockFormat(Wire.read());
  Wire.read();  //  dummy read
  dd   = fromClockFormat(Wire.read());
  mm   = fromClockFormat(Wire.read());
  yyyy = 2000 + fromClockFormat(Wire.read());

  setTime(HH, MM, SS, dd, mm, yyyy);
  *outTime = now();
}

byte fromClockFormat(int inClock) {
  return ((inClock & 0xf0) >> 4) * 10 + (inClock & 0x0f);
}

byte toClockFormat(int inValue) {
  return abs(inValue / 10) * 16 + (inValue % 10);
}

//
// Wifi
//
#include "../../private_ssid.h"
//const char *ssid     = "*************";
//const char *password = "*************";
WiFiClient client;

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

  startWifi();

  pinMode(13, OUTPUT);
  Wire.begin();

  setupRTC();

  time_t tt = getTimestamp();
  Serial.print("get sntp = ");
  Serial.println(tt);
  writeRTC(&tt);
}

void loop()
{
  digitalWrite(13, HIGH);

  time_t tt;
  readRTC(&tt);

  Serial.print(tt);
  Serial.print(" ");
  Serial.print(year());
  Serial.print("-");
  Serial.print(month());
  Serial.print("-");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(hour());
  Serial.print(":");
  Serial.print(minute());
  Serial.print(":");
  Serial.println(second());

  digitalWrite(13, LOW);
  delay(1000);
}


//
//  WiFi
//
bool startWifi() {
  WiFi.begin ( ssid, password );
  Serial.println("Started");

  // Wait for connection

  while ( WiFi.status() != WL_CONNECTED ) {
    delay ( 500 );
    Serial.print ( "." );
  }
  Serial.println("Wifi Connected");


  return true;
}


//
//  sntp
//
uint32_t getTimestamp() {
  configTime(9 * 3600, 0, "ntp.nict.jp", NULL, NULL);

  uint32_t result = 0;
  int cnt = 0;
  while (result == 0) {
    result = sntp_get_current_timestamp();
    delay(100);
    if (++cnt > 10) {
      break;
    }
  }

  return result;
}