-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalarm-clock.ino
110 lines (84 loc) · 2.78 KB
/
alarm-clock.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Third-party libraries.
#include <OneButton.h> // OneButton by Matthias Hertel (https://github.com/mathertel/OneButton)
#include <WiFiManager.h> // WiFiManager by tzapu (https://github.com/tzapu/WiFiManager.git)
// Local libraries.
#include "DisplayController.h"
#include "SoundController.h"
#include "TimeController.h"
// Configuration.
#include "config.h"
// Global feature controllers.
WiFiManager wifi;
DisplayController display;
SoundController sound;
TimeController ntpClient;
OneButton btn;
// Global variables.
unsigned long lastTimeUpdate = millis();
unsigned long lastBlinkUpdate = millis();
void setup()
{
// Setup serial connection.
Serial.begin(115200);
Serial.println(F("Starting..."));
// Initialize the display.
display.init();
display.displayBootMessage();
// Initialize the MP3 player.
sound.init(FROM_PLAYER_PIN, TO_PLAYER_PIN, BOOT_SOUND_FILE_NUMBER); // RX, TX
// Configure the buttons.
btn = OneButton(BUTTON_PIN, true, true); // true: button is active LOW (connected to GND), true: internal pull-up resistor enabled.
btn.attachClick(onButtonClicked);
btn.attachDoubleClick(onButtonDoubleClicked);
// Connect to the wireless network.
Serial.printf("[Network] Initializing with MAC address %s (SSID: %s)...\n", WiFi.macAddress().c_str(), WIFI_AP_SSID);
wifi.autoConnect(WIFI_AP_SSID, WIFI_AP_PASSWORD);
Serial.printf("[Network] Connected. IP address: %s, MAC address: %s\n", WiFi.localIP().toString().c_str(), WiFi.macAddress().c_str());
// Connect to the NTP server.
ntpClient.init(TIMEZONE_NAME);
setAlarmTrigger();
displayCurrentDateTime();
}
void loop()
{
// Update the button state.
btn.tick();
// Send player debug information.
sound.printStatus();
// Update the NTP clock and trigger alarms.
ntpClient.processEvents();
updateClockDisplay();
}
void displayCurrentDateTime() {
display.displayDateTime(ntpClient.getCurrentDate(), ntpClient.getCurrentTime(), ntpClient.getCurrentDayOfWeek());
}
void setAlarmTrigger() {
ntpClient.setEvent(onAlarmTriggered);
}
void onAlarmTriggered() {
Serial.println(F("[NTP] Alarm triggered!"));
// sound.play(ALARM_SOUND_FILE_NUMBER);
// Event is fired only once, so we have to set it up again.
setAlarmTrigger();
}
void onButtonClicked() {
Serial.println(F("[Button] Clicked"));
sound.pause();
}
void onButtonDoubleClicked() {
Serial.println(F("[Button] Double clicked"));
sound.start();
}
void updateClockDisplay() {
unsigned long currentTime = millis();
// Update the date and time in every minute.
if(currentTime - lastTimeUpdate > 60000) {
displayCurrentDateTime();
lastTimeUpdate = currentTime;
}
// Make the ":" blink in every second.
if(currentTime - lastBlinkUpdate > 1000) {
display.blink();
lastBlinkUpdate = currentTime;
}
}