218 lines
5.1 KiB
C++
218 lines
5.1 KiB
C++
//Test if Alarm was triggerd and start Alarm, test if Alarm Time should be changed (Summer/Winter Time)
|
|
void run_alarm(long currentMillis){
|
|
if (test_alarm())
|
|
{
|
|
write_log("Start Alarm");
|
|
start_wake_light(currentMillis);
|
|
setAlarmTimer(wakeup_time + 5);
|
|
}
|
|
else{
|
|
if (run_wake_up(currentMillis)){
|
|
run_wake_up_beep(currentMillis);
|
|
}
|
|
}
|
|
//Test if Summer->Winter time change (this should also change the Alarm times!)
|
|
bool dst = summertime_EU(y,M,d,h,1);
|
|
//write_log(String(dst) + ":" + String(summer));
|
|
if (dst != summer)
|
|
{
|
|
summer = dst;
|
|
set_alarm1();
|
|
set_alarm2();
|
|
write_eeprom();
|
|
}
|
|
}
|
|
|
|
//Stop Alarm if timeout reached
|
|
void stop_alarm(){
|
|
write_log("Stop Alarm");
|
|
stop_wake_light();
|
|
stop_alarm_beep();
|
|
setAlarmTimer(0);
|
|
}
|
|
|
|
//Set Alarm Timeout to stop alarm if nobody is at home
|
|
void setAlarmTimer(int Minutes){
|
|
if (Minutes == 0)
|
|
{
|
|
time_on = -1;
|
|
minutes_sleep = -1;
|
|
}
|
|
else{
|
|
unsigned long currentMillis = millis();
|
|
time_on = currentMillis + ((Minutes *60) *1000);
|
|
}
|
|
}
|
|
|
|
//Test if timeout reached
|
|
void handleSleep(long intervall, long currentMillis){
|
|
if(time_on < 0){
|
|
return;
|
|
}
|
|
else if (currentMillis > time_on)
|
|
{
|
|
stop_alarm();
|
|
}
|
|
}
|
|
|
|
//####################################################################################################
|
|
//Turn Alarm on/off
|
|
void switch_alarm_mode(){
|
|
bool A1 = Clock.checkAlarmEnabled(1);
|
|
bool A2 = Clock.checkAlarmEnabled(2);
|
|
if (A1 && A2){
|
|
Clock.turnOffAlarm(1);
|
|
Clock.turnOffAlarm(2);
|
|
}
|
|
else if (A1 && !A2){
|
|
Clock.turnOffAlarm(1);
|
|
Clock.turnOnAlarm(2);
|
|
}
|
|
else if (!A1 && A2){
|
|
Clock.turnOnAlarm(1);
|
|
Clock.turnOnAlarm(2);
|
|
}
|
|
else if (!A1 && !A2){
|
|
Clock.turnOnAlarm(1);
|
|
Clock.turnOffAlarm(2);
|
|
}
|
|
A1 = Clock.checkAlarmEnabled(1);
|
|
A2 = Clock.checkAlarmEnabled(2);
|
|
if (A1 && A2)
|
|
{
|
|
display_text("Alarm 1 an", "Alarm 2 an");
|
|
}
|
|
else if (A1)
|
|
{
|
|
display_text("Alarm 1 an", "Alarm 2 aus");
|
|
}
|
|
else if (A2)
|
|
{
|
|
display_text("Alarm 1 aus", "Alarm 2 an");
|
|
}
|
|
else
|
|
{
|
|
display_text("Alarm 1 aus", "Alarm 2 aus");
|
|
}
|
|
delay_gui(1000);
|
|
}
|
|
//Enter Alarm Menu
|
|
void set_alarm_item(){
|
|
if (setItem == 0)
|
|
{
|
|
setItem = 10;
|
|
}
|
|
setItem = setItem + 1;
|
|
if(setItem == 13)
|
|
{
|
|
set_alarm1();
|
|
}
|
|
else if (setItem > 14)
|
|
{
|
|
set_alarm2();
|
|
setItem = 0;
|
|
}
|
|
}
|
|
//Get Alarm Info for Display
|
|
String get_alarm_string(){
|
|
String alarm_string_1 = "";
|
|
String alarm_string_2 = "";
|
|
bool A1_en = Clock.checkAlarmEnabled(1);
|
|
bool A2_en = Clock.checkAlarmEnabled(2);
|
|
if (A1_en == true){
|
|
if (al1m < 10)
|
|
alarm_string_1 = "A1:" + String(al1h) + ":0" + String(al1m);
|
|
else
|
|
alarm_string_1 = "A1:" + String(al1h) + ":" + String(al1m);
|
|
}
|
|
if (A2_en == true){
|
|
if (al2m < 10)
|
|
alarm_string_2 = "A2:" + String(al2h) + ":0" + String(al2m);
|
|
else
|
|
alarm_string_2 = "A2:" + String(al2h) + ":" + String(al2m);
|
|
}
|
|
return alarm_string_1 + " " + alarm_string_2;
|
|
}
|
|
|
|
//####################################################################################################
|
|
//Test if Alarm is triggerd
|
|
bool test_alarm(){
|
|
bool A1 = Clock.checkIfAlarm(1);
|
|
bool A2 = Clock.checkIfAlarm(2);
|
|
bool A1_en = Clock.checkAlarmEnabled(1);
|
|
bool A2_en = Clock.checkAlarmEnabled(2);
|
|
return ((A1 && A1_en) || (A2 && A2_en));
|
|
}
|
|
|
|
//########################################################################################################
|
|
//Helpers
|
|
|
|
|
|
void add_al1_hour(int i){
|
|
al1h = al1h + i;
|
|
if (al1h > 23){
|
|
al1h = 0;
|
|
}
|
|
}
|
|
|
|
void add_al1_minute(int i){
|
|
al1m = al1m + i;
|
|
if (al1m > 59){
|
|
al1m = al1m - 60;
|
|
}
|
|
}
|
|
|
|
void add_al2_hour(int i){
|
|
al2h = al2h + i;
|
|
if (al2h > 23){
|
|
al2h = 0;
|
|
}
|
|
}
|
|
|
|
void add_al2_minute(int i){
|
|
al2m = al2m + i;
|
|
if (al2m > 59){
|
|
al2m = al2m - 60;
|
|
}
|
|
}
|
|
|
|
//Save Alarm 1
|
|
void set_alarm1(){
|
|
int al1h_utc = al1h;
|
|
if (summertime_EU(y,M,d,h,1))
|
|
{
|
|
al1h_utc = al1h - 1;
|
|
if (al1h_utc < 0){
|
|
al1h_utc = 23;
|
|
}
|
|
}
|
|
Clock.setA1Time(0, al1h_utc, al1m, 0, 1000, true, false, false);
|
|
//Clock.turnOnAlarm(1);
|
|
byte A1Day, A1Hour, A1Minute, A1Second, AlarmBits;
|
|
bool A1Dy, A1h12, A1PM;
|
|
Clock.getA1Time(A1Day, A1Hour, A1Minute, A1Second, AlarmBits, A1Dy, A1h12, A1PM);
|
|
write_log("Alarm1 Set:" + String(A1Day) + ", " + String(A1Hour) + ", " + String(A1Minute) + ", " + String(A1Second) + ", " + String(AlarmBits) + ", " + String(A1Dy) + ", " + String(A1h12) + ", ");
|
|
//display_text("Alarm 1", String(A1Hour) + ":" +String(A1Minute));
|
|
//delay_gui(1000);
|
|
}
|
|
|
|
//Save Alarm 2
|
|
void set_alarm2(){
|
|
int al2h_utc = al2h;
|
|
if (summertime_EU(y,M,d,h,1))
|
|
{
|
|
al2h_utc = al2h - 1;
|
|
if (al2h_utc < 0){
|
|
al2h_utc = 23;
|
|
}
|
|
}
|
|
Clock.setA2Time(0, al2h_utc, al2m, 0b1000000, true, false, false);
|
|
//Clock.turnOnAlarm(1);
|
|
byte A1Day, A1Hour, A1Minute, AlarmBits;
|
|
bool A1Dy, A1h12, A1PM;
|
|
Clock.getA2Time(A1Day, A1Hour, A1Minute, AlarmBits, A1Dy, A1h12, A1PM);
|
|
write_log("Alarm2 Set:" + String(A1Day) + ", " + String(A1Hour) + ", " + String(A1Minute) + ", " + String(AlarmBits) + ", " + String(A1Dy) + ", " + String(A1h12) + ", ");
|
|
//display_text("Alarm 2", String(A1Hour) + ":" +String(A1Minute));
|
|
//delay_gui(1000);
|
|
}
|