DIY QlockTwo
Perfect DIY version of famous QlockTwo Classic with ESP8266
main.cpp
Go to the documentation of this file.
1 
5 #include <Arduino.h>
6 #include <Ticker.h>
7 #include "LedControlModule.h"
8 #include "ClockModule.h"
9 #include "WifiModule.h"
10 #include "Settings.h"
11 #include "AceButton.h"
12 #include "SimpleTime.h"
13 #include "ConfigModule.h"
14 
15 using namespace ace_button;
16 
17 //-----------------------------------------------------
18 // Member Variables
19 //-----------------------------------------------------
20 NeoTopology<MyPanelLayout> topo(PANEL_WIDTH, PANEL_HEIGHT);
23 
25 
27 
29 
30 AceButton buttonOne(new ButtonConfig());
31 AceButton buttonTwo(new ButtonConfig());
32 AceButton buttonThree(new ButtonConfig());
33 AceButton buttonFour(new ButtonConfig());
34 
35 unsigned long lastClockUpdate = 0;
36 unsigned long lastShowTime = 0;
37 bool showTimeDisabled = false;
38 
41 
43 
44 //-----------------------------------------------------
45 // Function Declarations
46 //-----------------------------------------------------
47 void handleButtonOneEvent(AceButton*, uint8_t, uint8_t);
48 void handleButtonTwoEvent(AceButton*, uint8_t, uint8_t);
49 void handleButtonThreeEvent(AceButton*, uint8_t, uint8_t);
50 void handleButtonFourEvent(AceButton*, uint8_t, uint8_t);
51 
52 void updateClock();
53 void updateLedColor();
54 
55 void setButtonConfig(ButtonConfig* buttonConfig, ButtonConfig::EventHandler eventHandler);
56 void setupButtons();
57 void showTime();
58 
59 void configModeCallback (WiFiManager *myWiFiManager);
60 void saveConfigCallback();
61 
62 
63 //-----------------------------------------------------
64 // Function Definitions
65 //-----------------------------------------------------
66 
70 void setup() {
71  Serial.begin(9600);
72 
73  pinMode(BUILTIN_LED, OUTPUT);
74 
75  Serial.println("Setup start.");
76 
77  Serial.println("loadConfig:");
82 
83  setupButtons();
84 
86  //wifiModule.reset();
88 
90 
92 
93  updateClock();
94  lastClockUpdate = millis();
95 
96  showTime();
97  lastShowTime = millis();
98 
99  Serial.println("Setup done.");
100 }
101 
107 void setButtonConfig(ButtonConfig* buttonConfig, ButtonConfig::EventHandler eventHandler) {
108  buttonConfig->setEventHandler(eventHandler);
109  buttonConfig->setFeature(ButtonConfig::kFeatureClick);
110  buttonConfig->setFeature(ButtonConfig::kFeatureDoubleClick);
111  buttonConfig->setFeature(ButtonConfig::kFeatureLongPress);
112  // buttonConfig->setFeature(ButtonConfig::kFeatureRepeatPress);
113 }
114 
118 void setupButtons() {
119  pinMode(BUTTON_ONE_PIN, INPUT);
120  buttonOne.init(BUTTON_ONE_PIN, LOW);
121 
122  pinMode(BUTTON_TWO_PIN, INPUT);
123  buttonTwo.init(BUTTON_TWO_PIN, LOW);
124 
125  pinMode(BUTTON_THREE_PIN, INPUT);
126  buttonThree.init(BUTTON_THREE_PIN, LOW);
127 
128  pinMode(BUTTON_FOUR_PIN, INPUT);
129  buttonFour.init(BUTTON_FOUR_PIN, LOW);
130 
131  setButtonConfig(buttonOne.getButtonConfig(), handleButtonOneEvent);
132  setButtonConfig(buttonTwo.getButtonConfig(), handleButtonTwoEvent);
135 }
136 
140 void loop() {
141  if((millis() - lastClockUpdate) > (CLOCK_UPDATE_INTERVAL * 1000)) {
142  updateClock();
143  lastClockUpdate = millis();
144  }
145 
146  if((millis() - lastShowTime) > (TIME_UPDATE_INTERVAL * 1000) && !showTimeDisabled) {
147  showTime();
148  lastShowTime = millis();
149  }
150 
151  buttonOne.check();
152  buttonTwo.check();
153  buttonThree.check();
154  buttonFour.check();
155 }
156 
161 void configModeCallback (WiFiManager *myWiFiManager) {
162  Serial.println("Entered config mode");
163 
164  showTimeDisabled = true;
166 }
167 
172  Serial.println("Save callback.");
176 
177  showTimeDisabled = false;
178  showTime();
179 }
180 
184 void showTime() {
185  Serial.println("disableTime: " + config.disableTime.toString());
186  Serial.println("enableTime: " + config.enableTime.toString());
187 
189  updateClock();
190  }
191 
193 
195  !(((config.disableTime > config.enableTime) && (config.disableTime <= st && config.enableTime < st)) ||
197  Serial.println("Show Time: " + st.toString());
199  } else {
200  Serial.println("Show Time: LED DISABLED");
202  }
203 }
204 
208 void updateClock() {
209  Serial.println("Connect to wifi and update clock.");
210  if (!wifiModule.isConnected()) {
212  }
213 
215 }
216 
222 }
223 
230 void handleButtonOneEvent(AceButton* button, uint8_t eventType,
231  uint8_t buttonState) {
232  switch (eventType) {
233  case AceButton::kEventClicked:
234  Serial.println("Button One Clicked");
236 
239 
240  updateLedColor();
241  showTime();
242  break;
243  case AceButton::kEventLongPressed:
244  Serial.println("Button One Long Press");
245  if(!showTimeDisabled) {
246  Serial.println("Disable LED");
248  showTimeDisabled = true;
249  } else {
250  Serial.println("Enable LED");
251  showTime();
252  showTimeDisabled = false;
253  }
254  break;
255  }
256 }
257 
264 void handleButtonTwoEvent(AceButton* button, uint8_t eventType,
265  uint8_t buttonState) {
266  switch (eventType) {
267  case AceButton::kEventClicked:
268  Serial.println("Button Two Clicked");
269  break;
270  case AceButton::kEventLongPressed:
271  Serial.println("Button Two Long Press");
272  break;
273  }
274 }
275 
282 void handleButtonThreeEvent(AceButton* button, uint8_t eventType,
283  uint8_t buttonState) {
284  switch (eventType) {
285  case AceButton::kEventClicked:
286  Serial.println("Button Three Clicked");
287  break;
288  case AceButton::kEventLongPressed:
289  Serial.println("Button Three Long Press");
290  break;
291  }
292 }
293 
300 void handleButtonFourEvent(AceButton* button, uint8_t eventType,
301  uint8_t buttonState) {
302  switch (eventType) {
303  case AceButton::kEventClicked:
304  Serial.println("Button Four Clicked");
305  updateClock();
306  break;
307  case AceButton::kEventLongPressed:
308  Serial.println("Button Four Long Press");
309  wifiModule.reset();
310  delay(1000);
312  break;
313  }
314 }
Control the LEDs resp show the words and minute dots.
void showTime()
Get Time from RTC and update if it is not correct from NTP.
Definition: main.cpp:184
bool showTimeDisabled
Definition: main.cpp:37
bool isDateTimeValid()
Either true if rtc time is not valid or the updateInterval is reached.
Definition: ClockModule.cpp:47
int currentLedColorId
Definition: main.cpp:39
void setup(void(*configModeCallback)(WiFiManager *myWiFiManager), void(*saveConfig)(void))
Setup WifiManger.
Definition: WifiModule.cpp:20
SimpleTime getEnableTime()
Return EnableTime from set Parameters.
Definition: WifiModule.cpp:73
const Timezone LOCAL_TIMEZONE(TIME_CHANGE_RULE_DST, TIME_CHANGE_RULE_STD)
NeoPixelBus< NeoGrbwFeature, Neo800KbpsMethod > NeoPixelBusType
void update()
Get current time from NTP server and update RTC.
Definition: ClockModule.cpp:93
void showTime(const SimpleTime &simpleTime, const RgbwColor &ledColor=RgbwColor(0, 0, 0, 255))
Show Time with LEDs.
void handleButtonFourEvent(AceButton *, uint8_t, uint8_t)
Handle clicks of 4.
Definition: main.cpp:300
void loop()
Main Loop.
Definition: main.cpp:140
SimpleTime getDisableTime()
Return DisableTime from set Parameters.
Definition: WifiModule.cpp:81
const Config loadConfig()
Load config JSON from SPIFF and deserialize it.
const String DEVICE_NAME
Definition: Settings.h:27
bool connect()
Try to connect to presaved Wifi, otherwise go into AP mode.
Definition: WifiModule.cpp:45
const int PANEL_HEIGHT
Definition: Settings.h:9
const long TIME_UPDATE_INTERVAL
Definition: Settings.h:19
void setup()
Setup all Modules.
Definition: main.cpp:70
AceButton buttonOne(new ButtonConfig())
const RgbwColor LED_COLORS[]
Definition: Settings.h:33
void saveConfigCallback()
Gets called when WifiManager when custom parameters have been set AND a connection has been establish...
Definition: main.cpp:171
bool isConnected()
Check if Wifi is connected.
Definition: WifiModule.cpp:37
const String NTP_SERVER_NAME
Definition: Settings.h:22
Handling connection to Wifi and setting up Wifi credentials by creating an Access Point.
Definition: WifiModule.h:23
void disableLeds()
Show on all LEDs black.
ClockModule is handling the RTC and keeps it updated over NTP.
Definition: ClockModule.h:24
WifiModule wifiModule(DEVICE_NAME)
void updateLedColor()
Apply ambient light by dimming currentLedColor.
Definition: main.cpp:220
const int BUTTON_ONE_PIN
Definition: Settings.h:12
const int LED_COLORS_SIZE
Definition: Settings.h:31
ConfigModule configModule(CONFIG_FILE_PATH)
ClockModule clockModule(Wire, LOCAL_TIMEZONE, NTP_SERVER_NAME)
const int PIXEL_COUNT
Definition: Settings.h:10
A simple representation of time with hour and minutes.
Definition: SimpleTime.h:13
const int BUTTON_TWO_PIN
Definition: Settings.h:13
unsigned long lastShowTime
Definition: main.cpp:36
void handleButtonThreeEvent(AceButton *, uint8_t, uint8_t)
Handle clicks of 3.
Definition: main.cpp:282
SimpleTime getLocalSimpleTime()
Get current time from RTC in local time.
unsigned long lastClockUpdate
Definition: main.cpp:35
int setLedColor
Definition: ConfigModule.h:21
RgbwColor currentLedColor
Definition: main.cpp:40
const char *const CONFIG_FILE_PATH
Definition: Settings.h:29
void configModeCallback(WiFiManager *myWiFiManager)
Gets called when WiFiManager enters configuration mode.
Definition: main.cpp:161
void setup(NeoPixelBusType *_pixelStrip)
Setup LEDs in NeoPixel library.
void setButtonConfig(ButtonConfig *buttonConfig, ButtonConfig::EventHandler eventHandler)
Setup Event Handlers of a Button.
Definition: main.cpp:107
AceButton buttonThree(new ButtonConfig())
const int BUTTON_THREE_PIN
Definition: Settings.h:14
void reset()
Delete saved Wifi network credentials.
Definition: WifiModule.cpp:64
void showConfigWifi(const RgbwColor &ledColor=RgbwColor(0, 0, 0, 255))
Show that clock is in Wifi configuration mode, in german show Word "Funk".
void updateClock()
Connect to Wifi and update RTC over NTP.
Definition: main.cpp:208
const long CLOCK_UPDATE_INTERVAL
Definition: Settings.h:20
SimpleTime disableTime
Definition: ConfigModule.h:20
void handleButtonOneEvent(AceButton *, uint8_t, uint8_t)
Handle clicks of 1.
Definition: main.cpp:230
const int PANEL_WIDTH
Definition: Settings.h:8
bool saveConfig(const Config &config)
Serialize Config to JSON and save to SPIFF.
Save and Load Config to/from SPIFF of the ESP.
Definition: ConfigModule.h:28
LedControlModule ledControlModule(topo)
const int BUTTON_FOUR_PIN
Definition: Settings.h:15
Config config
Definition: main.cpp:42
String toString() const
Definition: SimpleTime.h:76
NeoPixelBusType pixelStrip(PIXEL_COUNT)
AceButton buttonFour(new ButtonConfig())
NeoTopology< MyPanelLayout > topo(PANEL_WIDTH, PANEL_HEIGHT)
SimpleTime enableTime
Definition: ConfigModule.h:19
void handleButtonTwoEvent(AceButton *, uint8_t, uint8_t)
Handle clicks of 2.
Definition: main.cpp:264
void setupButtons()
Setup all four Buttons.
Definition: main.cpp:118
AceButton buttonTwo(new ButtonConfig())
void setup()
Setup NTP client and RTC connection.
Definition: ClockModule.cpp:32