forked from ramapcsx2/gbs-control
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OLEDMenuImplementation.cpp
417 lines (397 loc) · 14.6 KB
/
OLEDMenuImplementation.cpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#define OSD_TIMEOUT 8000
#include <ESP8266WiFi.h>
#include "OLEDMenuImplementation.h"
#include "options.h"
#include "tv5725.h"
#include "slot.h"
#include "src/WebSockets.h"
#include "src/WebSocketsServer.h"
#include "fonts.h"
#include "OSDManager.h"
typedef TV5725<GBS_ADDR> GBS;
extern void applyPresets(uint8_t videoMode);
extern void setOutModeHdBypass(bool bypass);
extern void saveUserPrefs();
extern float getOutputFrameRate();
extern void loadDefaultUserOptions();
extern uint8_t getVideoMode();
extern runTimeOptions *rto;
extern userOptions *uopt;
extern const char *ap_ssid;
extern const char *ap_password;
extern const char *device_hostname_full;
extern WebSocketsServer webSocket;
extern OLEDMenuManager oledMenu;
extern OSDManager osdManager;
unsigned long oledMenuFreezeStartTime;
unsigned long oledMenuFreezeTimeoutInMS;
bool resolutionMenuHandler(OLEDMenuManager *manager, OLEDMenuItem *item, OLEDMenuNav, bool isFirstTime)
{
if (!isFirstTime) {
if (millis() - oledMenuFreezeStartTime >= oledMenuFreezeTimeoutInMS) {
manager->unfreeze();
}
return false;
}
oledMenuFreezeTimeoutInMS = 1000; // freeze for 1s
oledMenuFreezeStartTime = millis();
OLEDDisplay *display = manager->getDisplay();
display->clear();
display->setColor(OLEDDISPLAY_COLOR::WHITE);
display->setFont(ArialMT_Plain_16);
display->setTextAlignment(OLEDDISPLAY_TEXT_ALIGNMENT::TEXT_ALIGN_CENTER);
display->drawString(OLED_MENU_WIDTH / 2, 16, item->str);
display->drawXbm((OLED_MENU_WIDTH - TEXT_LOADED_WIDTH) / 2, OLED_MENU_HEIGHT / 2, IMAGE_ITEM(TEXT_LOADED));
display->display();
uint8_t videoMode = getVideoMode();
PresetPreference preset = PresetPreference::Output1080P;
switch (item->tag) {
case MT_1280x960:
preset = PresetPreference::Output960P;
break;
case MT1280x1024:
preset = PresetPreference::Output1024P;
break;
case MT1280x720:
preset = PresetPreference::Output720P;
break;
case MT1920x1080:
preset = PresetPreference::Output1080P;
break;
case MT_480s576:
preset = PresetPreference::Output480P;
break;
case MT_DOWNSCALE:
preset = PresetPreference::OutputDownscale;
break;
case MT_BYPASS:
preset = PresetPreference::OutputCustomized;
break;
default:
break;
}
if (videoMode == 0 && GBS::STATUS_SYNC_PROC_HSACT::read()) {
videoMode = rto->videoStandardInput;
}
if (item->tag != MT_BYPASS) {
uopt->presetPreference = preset;
rto->useHdmiSyncFix = 1;
if (rto->videoStandardInput == 14) {
rto->videoStandardInput = 15;
} else {
applyPresets(videoMode);
}
} else {
setOutModeHdBypass(false);
uopt->presetPreference = preset;
if (rto->videoStandardInput != 15) {
rto->autoBestHtotalEnabled = 0;
if (rto->applyPresetDoneStage == 11) {
rto->applyPresetDoneStage = 1;
} else {
rto->applyPresetDoneStage = 10;
}
} else {
rto->applyPresetDoneStage = 1;
}
}
saveUserPrefs();
manager->freeze();
return false;
}
bool presetSelectionMenuHandler(OLEDMenuManager *manager, OLEDMenuItem *item, OLEDMenuNav, bool isFirstTime)
{
if (!isFirstTime) {
if (millis() - oledMenuFreezeStartTime >= oledMenuFreezeTimeoutInMS) {
manager->unfreeze();
}
return false;
}
OLEDDisplay *display = manager->getDisplay();
display->clear();
display->setColor(OLEDDISPLAY_COLOR::WHITE);
display->setFont(ArialMT_Plain_16);
display->setTextAlignment(OLEDDISPLAY_TEXT_ALIGNMENT::TEXT_ALIGN_CENTER);
display->drawString(OLED_MENU_WIDTH / 2, 16, item->str);
display->drawXbm((OLED_MENU_WIDTH - TEXT_LOADED_WIDTH) / 2, OLED_MENU_HEIGHT / 2, IMAGE_ITEM(TEXT_LOADED));
display->display();
uopt->presetSlot = 'A' + item->tag; // ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~()!*:,
uopt->presetPreference = PresetPreference::OutputCustomized;
saveUserPrefs();
if (rto->videoStandardInput == 14) {
// vga upscale path: let synwatcher handle it
rto->videoStandardInput = 15;
} else {
// normal path
applyPresets(rto->videoStandardInput);
}
saveUserPrefs();
manager->freeze();
oledMenuFreezeTimeoutInMS = 2000;
oledMenuFreezeStartTime = millis();
return false;
}
bool presetsCreationMenuHandler(OLEDMenuManager *manager, OLEDMenuItem *item, OLEDMenuNav, bool)
{
SlotMetaArray slotsObject;
File slotsBinaryFileRead = SPIFFS.open(SLOTS_FILE, "r");
manager->clearSubItems(item);
int curNumSlot = 0;
if (slotsBinaryFileRead) {
slotsBinaryFileRead.read((byte *)&slotsObject, sizeof(slotsObject));
slotsBinaryFileRead.close();
for (int i; i < SLOTS_TOTAL; ++i) {
const SlotMeta &slot = slotsObject.slot[i];
if (strcmp(EMPTY_SLOT_NAME, slot.name) == 0 || !strlen(slot.name)) {
continue;
}
curNumSlot++;
if (curNumSlot > OLED_MENU_MAX_SUBITEMS_NUM) {
break;
}
manager->registerItem(item, slot.slot, slot.name, presetSelectionMenuHandler);
}
}
if (curNumSlot > OLED_MENU_MAX_SUBITEMS_NUM) {
manager->registerItem(item, 0, IMAGE_ITEM(TEXT_TOO_MANY_PRESETS));
}
if (!item->numSubItem) {
manager->registerItem(item, 0, IMAGE_ITEM(TEXT_NO_PRESETS));
}
return true;
}
bool resetMenuHandler(OLEDMenuManager *manager, OLEDMenuItem *item, OLEDMenuNav, bool isFirstTime)
{
if (!isFirstTime) {
// not precise
if (millis() - oledMenuFreezeStartTime >= oledMenuFreezeTimeoutInMS) {
manager->unfreeze();
ESP.reset();
return false;
}
return false;
}
OLEDDisplay *display = manager->getDisplay();
display->clear();
display->setColor(OLEDDISPLAY_COLOR::WHITE);
switch (item->tag) {
case MT_RESET_GBS:
display->drawXbm(CENTER_IMAGE(TEXT_RESETTING_GBS));
break;
case MT_RESTORE_FACTORY:
display->drawXbm(CENTER_IMAGE(TEXT_RESTORING));
break;
case MT_RESET_WIFI:
display->drawXbm(CENTER_IMAGE(TEXT_RESETTING_WIFI));
break;
}
display->display();
webSocket.close();
delay(50);
switch (item->tag) {
case MT_RESET_WIFI:
WiFi.disconnect();
break;
case MT_RESTORE_FACTORY:
loadDefaultUserOptions();
saveUserPrefs();
break;
}
manager->freeze();
oledMenuFreezeStartTime = millis();
oledMenuFreezeTimeoutInMS = 2000; // freeze for 2 seconds
return false;
}
bool currentSettingHandler(OLEDMenuManager *manager, OLEDMenuItem *, OLEDMenuNav nav, bool isFirstTime)
{
static unsigned long lastUpdateTime = 0;
if (isFirstTime) {
lastUpdateTime = 0;
oledMenuFreezeStartTime = millis();
oledMenuFreezeTimeoutInMS = 2000; // freeze for 2 seconds if no input
manager->freeze();
} else if (nav != OLEDMenuNav::IDLE) {
manager->unfreeze();
return false;
}
if (millis() - lastUpdateTime <= 200) {
return false;
}
OLEDDisplay &display = *manager->getDisplay();
display.clear();
display.setColor(OLEDDISPLAY_COLOR::WHITE);
display.setFont(ArialMT_Plain_16);
if (rto->sourceDisconnected || !rto->boardHasPower) {
if (millis() - oledMenuFreezeStartTime >= oledMenuFreezeTimeoutInMS) {
manager->unfreeze();
return false;
}
display.setTextAlignment(OLEDDISPLAY_TEXT_ALIGNMENT::TEXT_ALIGN_CENTER);
display.drawXbm(CENTER_IMAGE(TEXT_NO_INPUT));
} else {
// TODO translations
boolean vsyncActive = 0;
boolean hsyncActive = 0;
float ofr = getOutputFrameRate();
uint8_t currentInput = GBS::ADC_INPUT_SEL::read();
rto->presetID = GBS::GBS_PRESET_ID::read();
display.setFont(URW_Gothic_L_Book_20);
display.setTextAlignment(TEXT_ALIGN_LEFT);
if (rto->presetID == 0x01 || rto->presetID == 0x11) {
display.drawString(0, 0, "1280x960");
} else if (rto->presetID == 0x02 || rto->presetID == 0x12) {
display.drawString(0, 0, "1280x1024");
} else if (rto->presetID == 0x03 || rto->presetID == 0x13) {
display.drawString(0, 0, "1280x720");
} else if (rto->presetID == 0x05 || rto->presetID == 0x15) {
display.drawString(0, 0, "1920x1080");
} else if (rto->presetID == 0x06 || rto->presetID == 0x16) {
display.drawString(0, 0, "Downscale");
} else if (rto->presetID == 0x04) {
display.drawString(0, 0, "720x480");
} else if (rto->presetID == 0x14) {
display.drawString(0, 0, "768x576");
} else {
display.drawString(0, 0, "bypass");
}
display.drawString(0, 20, String(ofr, 5) + "Hz");
if (currentInput == 1) {
display.drawString(0, 41, "RGB");
} else {
display.drawString(0, 41, "YpBpR");
}
if (currentInput == 1) {
vsyncActive = GBS::STATUS_SYNC_PROC_VSACT::read();
if (vsyncActive) {
display.drawString(70, 41, "V");
hsyncActive = GBS::STATUS_SYNC_PROC_HSACT::read();
if (hsyncActive) {
display.drawString(53, 41, "H");
}
}
}
}
display.display();
lastUpdateTime = millis();
return false;
}
bool wifiMenuHandler(OLEDMenuManager *manager, OLEDMenuItem *item, OLEDMenuNav, bool)
{
static char ssid[64];
static char ip[25];
static char domain[25];
WiFiMode_t wifiMode = WiFi.getMode();
manager->clearSubItems(item);
if (wifiMode == WIFI_STA) {
sprintf(ssid, "SSID: %s", WiFi.SSID().c_str());
manager->registerItem(item, 0, ssid);
if (WiFi.isConnected()) {
manager->registerItem(item, 0, IMAGE_ITEM(TEXT_WIFI_CONNECTED));
manager->registerItem(item, 0, IMAGE_ITEM(TEXT_WIFI_URL));
sprintf(ip, "http://%s", WiFi.localIP().toString().c_str());
manager->registerItem(item, 0, ip);
sprintf(domain, "http://%s", device_hostname_full);
manager->registerItem(item, 0, domain);
} else {
// shouldn't happen?
manager->registerItem(item, 0, IMAGE_ITEM(TEXT_WIFI_DISCONNECTED));
}
} else if (wifiMode == WIFI_AP) {
manager->registerItem(item, 0, IMAGE_ITEM(TEXT_WIFI_CONNECT_TO));
sprintf(ssid, "SSID: %s (%s)", ap_ssid, ap_password);
manager->registerItem(item, 0, ssid);
manager->registerItem(item, 0, IMAGE_ITEM(TEXT_WIFI_URL));
manager->registerItem(item, 0, "http://192.168.4.1");
sprintf(domain, "http://%s", device_hostname_full);
manager->registerItem(item, 0, domain);
} else {
// shouldn't happen?
manager->registerItem(item, 0, IMAGE_ITEM(TEXT_WIFI_DISCONNECTED));
}
return true;
}
bool osdMenuHanlder(OLEDMenuManager *manager, OLEDMenuItem *, OLEDMenuNav nav, bool isFirstTime)
{
static unsigned long start;
static long left;
char buf[30];
auto display = manager->getDisplay();
if (isFirstTime) {
left = OSD_TIMEOUT;
start = millis();
manager->freeze();
osdManager.tick(OSDNav::ENTER);
} else {
display->clear();
display->setColor(OLEDDISPLAY_COLOR::WHITE);
display->setFont(ArialMT_Plain_16);
display->setTextAlignment(OLEDDISPLAY_TEXT_ALIGNMENT::TEXT_ALIGN_CENTER);
display->drawStringf(OLED_MENU_WIDTH / 2, 16, buf, "OSD (%ds)", left / 1000 + 1);
display->display();
if (REVERSE_ROTARY_ENCODER_FOR_OLED_MENU){
// reverse nav back to normal
if(nav == OLEDMenuNav::DOWN) {
nav = OLEDMenuNav::UP;
} else if(nav == OLEDMenuNav::UP) {
nav = OLEDMenuNav::DOWN;
}
}
switch (nav) {
case OLEDMenuNav::ENTER:
osdManager.tick(OSDNav::ENTER);
start = millis();
break;
case OLEDMenuNav::DOWN:
if(REVERSE_ROTARY_ENCODER_FOR_OSD) {
osdManager.tick(OSDNav::RIGHT);
} else {
osdManager.tick(OSDNav::LEFT);
}
start = millis();
break;
case OLEDMenuNav::UP:
if(REVERSE_ROTARY_ENCODER_FOR_OSD) {
osdManager.tick(OSDNav::LEFT);
} else {
osdManager.tick(OSDNav::RIGHT);
}
start = millis();
break;
default:
break;
}
left = OSD_TIMEOUT - (millis() - start);
if (left <= 0) {
manager->unfreeze();
osdManager.menuOff();
}
}
return true;
}
void initOLEDMenu()
{
OLEDMenuItem *root = oledMenu.rootItem;
// OSD Menu
oledMenu.registerItem(root, MT_NULL, IMAGE_ITEM(OM_OSD), osdMenuHanlder);
// Resolutions
OLEDMenuItem *resMenu = oledMenu.registerItem(root, MT_NULL, IMAGE_ITEM(OM_RESOLUTION));
const char *resolutions[5] = {"1280x960", "1280x1024", "1280x720", "1920x1080", "480/576"};
uint8_t tags[5] = {MT_1280x960, MT1280x1024, MT1280x720, MT1920x1080, MT_480s576};
for (int i = 0; i < 5; ++i) {
oledMenu.registerItem(resMenu, tags[i], resolutions[i], resolutionMenuHandler);
}
// downscale and passthrough
oledMenu.registerItem(resMenu, MT_DOWNSCALE, IMAGE_ITEM(OM_DOWNSCALE), resolutionMenuHandler);
oledMenu.registerItem(resMenu, MT_BYPASS, IMAGE_ITEM(OM_PASSTHROUGH), resolutionMenuHandler);
// Presets
oledMenu.registerItem(root, MT_NULL, IMAGE_ITEM(OM_PRESET), presetsCreationMenuHandler);
// WiFi
oledMenu.registerItem(root, MT_NULL, IMAGE_ITEM(OM_WIFI), wifiMenuHandler);
// Current Settings
oledMenu.registerItem(root, MT_NULL, IMAGE_ITEM(OM_CURRENT), currentSettingHandler);
// Reset (Misc.)
OLEDMenuItem *resetMenu = oledMenu.registerItem(root, MT_NULL, IMAGE_ITEM(OM_RESET_RESTORE));
oledMenu.registerItem(resetMenu, MT_RESET_GBS, IMAGE_ITEM(OM_RESET_GBS), resetMenuHandler);
oledMenu.registerItem(resetMenu, MT_RESTORE_FACTORY, IMAGE_ITEM(OM_RESTORE_FACTORY), resetMenuHandler);
oledMenu.registerItem(resetMenu, MT_RESET_WIFI, IMAGE_ITEM(OM_RESET_WIFI), resetMenuHandler);
}