Viking Skull Lamp  V1.0.1
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1/*
2 * Created on May 28 2022
3 *
4 * Copyright (c) 2022 - Daniel Hajnal
5 * hajnal.daniel96@gmail.com
6 * This file is part of the Viking Skull Lamp project.
7 * Modified 2022.06.27
8*/
9
10/*
11MIT License
12Copyright (c) 2022 Daniel Hajnal
13Permission is hereby granted, free of charge, to any person obtaining a copy
14of this software and associated documentation files (the "Software"), to deal
15in the Software without restriction, including without limitation the rights
16to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17copies of the Software, and to permit persons to whom the Software is
18furnished to do so, subject to the following conditions:
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27SOFTWARE.
28*/
29
30#include "main.hpp"
31
40
48
52
59
60};
61
69
72
74uint8_t buttonPressCntr = 0;
75
77bool buttonPressed = false;
78
80void setup() {
81
82 // Just in case we enable the watchdog with 4-sec timeout.
83 wdt_enable( WDTO_4S );
84
85 // Initialize the Serial port.
86 Serial.begin( 115200 );
87
88 // Try to load the configuration from the EEPROM.
89 if( !config.loadConfig() ){
90
91 // If the configuration loading is not succesful,
92 // we set the config variables to the default values.
99
100 // Also we try to save the config again.
102
103 }
104
105 // Try to initialize the oled display.
106 if( !display.begin() ){
107
108 // If we can't, we print the error.
109 Serial.println( F( "Display Error!" ) );
110
111 }
112
113 // Initialize motor logic.
114 motorInit();
115
116 // Initialize encoder pins.
117 pinMode( ENCODER_CLK, INPUT );
118 pinMode( ENCODER_BTN, INPUT );
119 pinMode( ENCODER_DATA, INPUT );
120
121 // For encoder rotation detection we use an interrupt,
122 // so we have to register that to the @ref ENCODER_CLK pin.
123 // Every time a rising endge arrives ti @ref ENCODER_CLK pin,
124 // the @ref encoderISR function will be called.
125 attachInterrupt( digitalPinToInterrupt( ENCODER_CLK ), encoderISR, RISING );
126
127 // Initialize menu system.
128 menuInit();
129
130 // Initialize lighting.
131 lightInit();
132
133 // Initialize the humidifier.
135
136}
137
141void loop() {
142
143 // Update menu data.
145
146 // Update button handler.
148
149 // Update lighting effects.
150 lightUpdate();
151
152 // Update the humidifier module.
154
155 // Feed the watchdog.
156 wdt_reset();
157
158}
159
161
162 // We only allowed to use the menu,
163 // when the front panel is opened.
164 if( frontState != FRONT_OPEN ){
165
166 return;
167
168 }
169
170 // We have to detect the direction of the rotation.
171 // If we rotate it to one direction the state of the
172 // data line and the clock line always be equal. If we
173 // change direction they always be different.
174 if( digitalRead( ENCODER_CLK ) == digitalRead( ENCODER_DATA ) ){
175
178
179 }
180
181 else{
182
185
186 }
187
188}
189
191
192 // The encoder button uses inverted logic. If it is pressed
193 // the output of the button will be low.
194 if( digitalRead( ENCODER_BTN ) == 0 && !buttonPressed ){
195
196 // A tiny delay required to debounce the signal.
197 delay( 15 );
198
199 // Increment the buttonPressCntr.
201
202 // Save system time.
203 lastButtonPress = millis();
204
205 // Set the flag to true. It is required for event generation.
206 buttonPressed = true;
207 }
208
209 // Check if the button was released.
210 if( digitalRead( ENCODER_BTN ) == 1 ){
211
212 // Reset the flag.
213 buttonPressed = false;
214
215 }
216
217 // To detect multiple button presses we have to be quick. The time between
218 // two button presses should be less than 250ms. Otherwise it will be detected
219 // as a single press.
220 if( ( ( millis() - lastButtonPress ) > 250 ) && buttonPressCntr ){
221
222 // Decide action according to the number of the button pressed.
223 switch( buttonPressCntr ){
224
225 // Button pressed once.
226 case 1:
227
228 // If the front is opened we use the button to navigate
229 // in the menu. Also we save the config with every button
230 // press. If there are no changes in the configuration
231 // data it will skip the writing anyway.
232 if( frontState == FRONT_OPEN ){
233
237
238 }
239
240 // If the front is closed we use the button to turn on or
241 // off the lighting.
242 else{
243
244 lightToggle();
245 turnOnMelody();
246
247 }
248
249 break;
250
251 // Button pressed twice.
252 case 2:
253
254 // Open or close the front depending on it's state.
256 if( frontState == FRONT_OPEN ){
257
258 closeFront();
259
260 }
261
262 else{
263
264 openFront();
265
266 }
267
268 break;
269
270 }
271
272 // Reset the buttonPressCntr variable for the next round.
273 buttonPressCntr = 0;
274
275 }
276
277}
void encoderRotateSound()
Generate encoder rotate sound.
void turnOnMelody()
Generate turn on sound.
void buttonClickSound()
Generate button click sound.
void openCloseMelody()
Generate open-close melody.
Configuration Manager Class.
bool loadConfig()
Load configuration from EEPROM.
void saveConfig()
Save configuration to EEPROM.
Menu Controller Class.
Definition: menu.hpp:121
void draw()
Redraw the menu to the display.
Definition: menu.cpp:114
void up()
Step te menu upwards if possible.
Definition: menu.cpp:271
void down()
Step te menu downwards if possible.
Definition: menu.cpp:304
void next()
Enter to the next menu if possible.
Definition: menu.cpp:337
Display driver object.
Definition: oled.hpp:156
bool begin()
Init function for the display.
Definition: oled.cpp:38
#define createData(x)
void fogMachineInit()
Initialize the fog machine( humidifier ).
Definition: fogMachine.cpp:50
void fogMachineUpdate()
Update the fog machine state.
Definition: fogMachine.cpp:57
#define OLED_ADDRESS
menu menuController
Object for the menu.
void lightInit()
Initialize the LEDs.
void lightUpdate()
Update function for the lighting.
void lightToggle()
Toggle the lighting.
long lastButtonPress
Last system time when the button was pressed.
Definition: main.cpp:71
void encoderButtonUpdate()
This function handles the button press detection.
Definition: main.cpp:190
ssd1306 display(OLED_ADDRESS)
Object for the display.
Definition: main.cpp:47
void setup()
Device initialization section after reset.
Definition: main.cpp:80
bool buttonPressed
Flag for button press detection.
Definition: main.cpp:77
uint8_t buttonPressCntr
Counts how many button press was detected in a period.
Definition: main.cpp:74
configurationManager::configurationData_t configTree[]
This array contains all the variables, that required for configuring the device.
Definition: main.cpp:51
void encoderISR()
Interrupt service for the encoder rotation events.
Definition: main.cpp:160
configurationManager config(configTree,(sizeof(configTree)/sizeof(configTree[0])))
Object for configuration saving and loading.
void loop()
This is the infinite loop of the code.
Definition: main.cpp:141
#define ENCODER_DATA
The encoder data line is connected to pin 4 on the Arduino.
Definition: main.hpp:48
#define ENCODER_CLK
The encoder clock line is connected to pin 3 on the Arduino.
Definition: main.hpp:51
#define DEFAULT_CLAP_SWITCH_STATE
By default the clap switch is enabled.
Definition: main.hpp:79
#define DEFAULT_LIGHT_BRIGHTNESS
Default value for the brightness.
Definition: main.hpp:73
#define DEFAULT_SELECTED_COLOR
Default value for the user selected color.
Definition: main.hpp:70
#define DEFAULT_BUZZER_STATE
By default the buzzer is enabled.
Definition: main.hpp:76
#define DEFAULT_LIGHT_MODE
Default value for the light mode.
Definition: main.hpp:67
#define ENCODER_BTN
The encoder button is connected to pin 2 on the Arduino.
Definition: main.hpp:45
#define DEFAULT_FOG_STATE
By default the fog machine is enabled.
Definition: main.hpp:82
menuItem lightMenuItem
Light Menu item.
enum lightMode_t lightMode
It will store the user selected light mode.
Definition: menu.cpp:44
uint8_t lightBrightness
It will store the user selected brightness.
Definition: menu.cpp:48
bool fogMachineEnabled
It will store the user selected state of the fog machine.
Definition: menu.cpp:54
uint8_t selectedColor
It will store the user selected color.
Definition: menu.cpp:46
void menuInit()
Initialize the menu system.
Definition: menu.cpp:807
bool buzzerEnabled
It will store the user selected state of the buzzer.
Definition: menu.cpp:50
bool clapSwitchEnabled
It will store the user selected state of the clap switch.
Definition: menu.cpp:52
void closeFront()
Close the font.
enum frontState_t frontState
Store the state of the front panel.
void motorInit()
Initialize the motor.
void openFront()
Open the font.
@ FRONT_OPEN
Front is opened.