#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
#include "Button.h"

#define SPEAKER_PIN 23
RTC_DS1307 rtc;
LiquidCrystal_I2C lcd (0x27,28,4);
Button modelButton(27);
Button hourButton(26);
Button minButton(25);
Button startButton(33);
Button stopButton(32);

int key_press;  //按键返回值
int model=0;    //0显示时间  1设置闹钟   2计时
int set_hour=20;
int set_min=26;
int set_count_hour=0;
int set_count_min=0;
int set_count_sec=0;
int now_sec=0;
int last_sec=0;
int start_flag=0;   //开始计时标志  1开始计时
void setup () {
  Serial.begin(115200);
  lcd.init();
  lcd.backlight();

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    abort();
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running, let's set the time!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  Button_Init();
  pinMode(SPEAKER_PIN, OUTPUT);
   tone(SPEAKER_PIN, 0);
}

void loop() 
{
   DateTime time = rtc.now();
   if(start_flag)
{
last_sec=now_sec;
now_sec=time.second();
if(last_sec!=now_sec)
{
set_count_sec++;
if(set_count_sec>59)
{
set_count_sec=0;
set_count_min++;
if(set_count_min>59)
{
set_count_hour++;
set_count_min=0;
}
}
}
}
  key_deal();
if(model==0)
show_time();
else if(model==1)
show_clock();
else if(model==2)
show_time_count();
}

void Button_Init()
{
modelButton.begin ();
modelButton.set_repeat(500,200);
hourButton.begin ();
hourButton.set_repeat(500,200);
minButton.begin ();
minButton.set_repeat(500,200);
startButton.begin ();
startButton.set_repeat(500,200);
stopButton.begin ();
stopButton.set_repeat(500,200);
}

int key_get()
{
if(modelButton.pressed())
return 1;
else if(hourButton.pressed())
return 2;
else if(minButton.pressed())
return 3;
else if(startButton.pressed())
return 4;
else if(stopButton.pressed())
return 5;
else
return 0;
}

void key_deal()
{
key_press=key_get();
if(key_press)
{
 Serial.println(key_press);
 if(key_press==1)
 {
   lcd.init();
model++;
if(model>2)
model=0;
start_flag=0;
set_count_hour=0;
set_count_min=0;
set_count_sec=0;
 }

 if(key_press==2)
 {
 if(model==1)
 {
 set_hour++;
 if(set_hour>23)
 set_hour=0;
 }
 }

  if(key_press==3)
 {
 if(model==1)
 {
 set_min++;
 if(set_min>59)
 set_min=0;
 }
 }

   if(key_press==4)
 {
 if(model==2)
 {
start_flag=1;
 }
 }

    if(key_press==5)
 {
 if(model==2)
 {
start_flag=0;
 }
 }
}

}

void show_time()
{
 DateTime time = rtc.now();
 lcd.setCursor(2,0);
 lcd.print(time.timestamp(DateTime::TIMESTAMP_DATE));
 lcd.setCursor(4,1);
 lcd.print(time.timestamp(DateTime::TIMESTAMP_TIME));
 if(time.hour()==set_hour&&time.minute()==set_min)
 {
tone(SPEAKER_PIN, 450);
 }
 else
 {
  noTone(SPEAKER_PIN);
 }
}

void show_clock()
{
 lcd.setCursor(2,0);
 lcd.print("clock:");
 lcd.setCursor(4,1);
 if(set_hour<10)
  lcd.print("0");
 lcd.print(set_hour);
  lcd.print(":");
   if(set_min<10)
  lcd.print("0");
 lcd.print(set_min);
}

void show_time_count()
{
 lcd.setCursor(2,0);
 lcd.print("time:");
 lcd.setCursor(4,1);
 if(set_count_hour<10)
  lcd.print("0");
 lcd.print(set_count_hour);
  lcd.print(":");
   if(set_count_min<10)
  lcd.print("0");
 lcd.print(set_count_min);
   lcd.print(":");
   if(set_count_sec<10)
  lcd.print("0");
 lcd.print(set_count_sec);

}
GND5VSDASCLSQWRTCDS1307+