-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArcConfigMenu.h
158 lines (123 loc) · 3.79 KB
/
ArcConfigMenu.h
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef ArcConfigMenu_h
#define ArcConfigMenu_h
// https://iotcircuithub.com/esp32-preferences-library-tutorial/
#include <Preferences.h>
#include "Arduino.h"
// https://github.com/arduino-libraries/WiFi/tree/master
#include "WiFi.h"
#include "Fonts.h"
#include "TimezoneDef.h"
#define ARC_CONFIG_VERSION 2
// Root menu
#define ROOT_MENU_SIZE 3
#define ROOT_MENU_INDEX__WIFI_CONFIG 0
#define ROOT_MENU_INDEX__TIME_CONFIG 1
#define ROOT_MENU_INDEX__FONT_CONFIG 2
// Sub menus
#define SUB_MENU_SIZE__WIFI 4
#define SUB_MENU_SIZE__TIME 3
#define SUB_MENU_SIZE__FONT 4
#define SUB_MENU__BACK 0
// Wifi Config
#define SUB_MENU__WIFI_CONFIG__VIEW 1
#define SUB_MENU__WIFI_CONFIG__LIST_NETWORKS 2
#define SUB_MENU__WIFI_CONFIG__SET 3
// Time Config
#define SUB_MENU__TIME_CONFIG__VIEW 11
#define SUB_MENU__TIME_CONFIG__SET 12
// Font Config
#define SUB_MENU__FONT_CONFIG__VIEW 21
#define SUB_MENU__FONT_CONFIG__TOGGLE_BLINK 22
#define SUB_MENU__FONT_CONFIG__SET_FONT 23
#define INT_UNINITIALIZED -1
#define STRING_UNINITIALIZED "?no value?"
#define INPUT_PROMPT__CHOICE "Choice"
// Types for user inputs
#define EXPECTED_INPUT_TYPE_PROMPT__STRING '>'
#define EXPECTED_INPUT_TYPE_PROMPT__PASSWORD '*'
#define EXPECTED_INPUT_TYPE_PROMPT__INT '#'
#define EXPECTED_INPUT_TYPE_PROMPT__BOOL '&'
// Identifiers for preferences. Max 15 chars
#define CONFIG_NAMESPACE "ArcConfig"
#define CONFIG_OPTION__CONFIG_VERSION "configVersion"
#define CONFIG_OPTION__WIFI__TYPE "wifiType"
#define CONFIG_OPTION__WIFI__SSID "wifiSSID"
#define CONFIG_OPTION__WIFI__PASSWORD "wifiPassword"
#define CONFIG_OPTION__WIFI__USERNAME "wifiUser" // needed for "Enterprise" wifi
#define CONFIG_OPTION__TIME__TIMEZONE "timeZone"
#define CONFIG_OPTION__FONT__BLINKING_COLON "fontBlkColon"
#define CONFIG_OPTION__FONT__NO "fontNo"
// Supported wifi types
#define WIFI_TYPE__HOME 0
#define WIFI_TYPE__ENTERPRISE 1
// To store Wifi Configuration
struct ArcWifiConfig
{
short type;
char ssid[30];
char password[50];
char username[50];
};
// To store time configuration
struct ArcTimeConfig
{
short timezone;
};
// To store Font configuration
struct ArcFontConfig
{
bool colonBlink;
short fontNo;
};
// Structure to store whole Arc Clock configuration
struct ArcConfig
{
short version;
ArcWifiConfig wifi;
ArcTimeConfig time;
ArcFontConfig font;
};
// ---------------------------------------
// Class definition
class ArcConfigMenu
{
public:
ArcConfigMenu();
void begin(FontInfos fontList[], TimezoneInfos timezoneList[]);
short handleInput(WiFiClass* wifiAdapter);
bool isConfigFormatValid();
short getWifiType();
char* getWifiSSID();
char* getWifiUsername();
char* getWifiPassword();
TimezoneInfos getTimezoneInfos();
FontInfos getFontInfos();
bool doesColonHaveToBlink();
void toggleFont();
protected:
short _currentMenuIndex, _currentSubMenuIndex;
bool _displayPrompt;
short _userInputStep; // What input we are expecting
bool _configFormatIsValid;
FontInfos* _fontList;
TimezoneInfos* _timezoneList;
// To store configuration
ArcConfig _config;
String _nextInputPrompt;
char _nextExpectedInputTypePrompt;
char* _menu[ROOT_MENU_SIZE];
char* _subMenu[ROOT_MENU_SIZE][SUB_MENU_SIZE__WIFI];
// Utilities
void convertStringToCharArray(String text, char* output);
void printCurrentMenu();
void setNextInputPrompt(String txt, char type=EXPECTED_INPUT_TYPE_PROMPT__STRING);
void printNextInputPrompt();
short handleSubMenu(String lastUserInput, WiFiClass* wifiAdapter);
void printInvalidConfigMessage();
short getSubMenuIndexFromId(short rootMenuIndex, short subMenuId);
short getSubMenuIdFromIndex(short rootMenuIndex, short subMenuIndex);
short getCurrentSubMenuSize();
void loadConfig();
void saveConfig();
};
#endif