makes GPIO_PIN_RST optional for the sx1276
[ExpressLRS.git] / src / lib / SCREEN / devScreen.cpp
blobe216b8052b75235af2d2d6cf19ef2094119e4310
1 #include "targets.h"
2 #include "common.h"
3 #include "device.h"
5 #if defined(USE_OLED_SPI) || defined(USE_OLED_SPI_SMALL) || defined(USE_OLED_I2C) || defined(HAS_TFT_SCREEN)
7 #include "logging.h"
8 #include "Wire.h"
9 #include "config.h"
10 #include "POWERMGNT.h"
11 #include "hwTimer.h"
13 #ifdef HAS_TFT_SCREEN
14 #include "TFT/tftscreen.h"
15 TFTScreen screen;
16 #else
17 #include "OLED/oledscreen.h"
18 OLEDScreen screen;
19 #endif
21 #ifdef HAS_FIVE_WAY_BUTTON
22 #include "FiveWayButton/FiveWayButton.h"
23 FiveWayButton fivewaybutton;
25 static uint32_t none_input_start_time = 0;
26 static bool isUserInputCheck = false;
27 #endif
29 #ifdef HAS_GSENSOR
30 #include "gsensor.h"
31 extern Gsensor gsensor;
32 static bool is_screen_flipped = false;
33 static bool is_pre_screen_flipped = false;
34 #endif
36 #ifdef HAS_THERMAL
37 #include "thermal.h"
38 extern Thermal thermal;
40 #define UPDATE_TEMP_TIMEOUT 5000
41 uint32_t update_temp_start_time = 0;
42 #endif
44 #define SCREEN_DURATION 20
46 #define LOGO_DISPLAY_TIMEOUT 5000
47 static bool isLogoDisplayed = false;
49 #define SCREEN_IDLE_TIMEOUT 20000
51 extern bool ICACHE_RAM_ATTR IsArmed();
52 extern void EnterBindingMode();
53 extern void ExitBindingMode();
55 #if defined(TARGET_TX)
56 extern TxConfig config;
57 #else
58 extern RxConfig config;
59 #endif
61 #ifdef PLATFORM_ESP32
62 extern unsigned long rebootTime;
63 #endif
65 #define BINDING_MODE_TIME_OUT 5000
66 static uint32_t binding_mode_start_time = 0;
69 static void ScreenUpdateCallback(int updateType)
71 switch(updateType)
73 case USER_UPDATE_TYPE_RATE:
74 DBGLN("User set AirRate %d", screen.getUserRateIndex());
75 config.SetRate(screen.getUserRateIndex());
76 break;
77 case USER_UPDATE_TYPE_POWER:
78 DBGLN("User set Power %d", screen.getUserPowerIndex());
79 config.SetPower(screen.getUserPowerIndex());
80 break;
81 case USER_UPDATE_TYPE_RATIO:
82 DBGLN("User set TLM RATIO %d", screen.getUserRatioIndex());
83 config.SetTlm(screen.getUserRatioIndex());
84 break;
85 case USER_UPDATE_TYPE_BINDING:
86 DBGLN("User request binding!");
87 EnterBindingMode();
88 binding_mode_start_time = millis();
89 break;
90 case USER_UPDATE_TYPE_EXIT_BINDING:
91 DBGLN("User request exit binding!");
92 ExitBindingMode();
93 break;
94 case USER_UPDATE_TYPE_WIFI:
95 DBGLN("User request Wifi Update Mode!");
96 connectionState = wifiUpdate;
97 break;
98 case USER_UPDATE_TYPE_EXIT_WIFI:
99 DBGLN("User request exit Wifi Update Mode!");
100 #ifdef PLATFORM_ESP32
101 if (connectionState == wifiUpdate) {
102 rebootTime = millis() + 200;
104 #endif
105 break;
106 #ifdef HAS_THERMAL
107 case USER_UPDATE_TYPE_SMARTFAN:
108 DBGLN("User request SMART FAN Mode!");
109 config.SetFanMode(screen.getUserSmartFanIndex());
110 break;
111 #endif
112 #ifdef HAS_GSENSOR
113 case USER_UPDATE_TYPE_POWERSAVING:
114 DBGLN("User request Power Saving Mode!");
115 config.SetMotionMode(screen.getUserPowerSavingIndex());
116 break;
117 #endif
118 default:
119 DBGLN("Error handle user request %d", updateType);
120 break;
124 #ifdef HAS_FIVE_WAY_BUTTON
125 static int handle(void)
127 fivewaybutton.handle();
129 if(!IsArmed())
131 int key;
132 bool isLongPressed;
133 fivewaybutton.getKeyState(&key, &isLongPressed);
134 if(screen.getScreenStatus() == SCREEN_STATUS_IDLE)
136 #ifdef HAS_THERMAL
137 if(millis() - update_temp_start_time > UPDATE_TEMP_TIMEOUT)
139 screen.doTemperatureUpdate(thermal.getTempValue());
140 update_temp_start_time = millis();
142 #endif
143 if(isLongPressed)
145 screen.activeScreen();
148 else if(screen.getScreenStatus() == SCREEN_STATUS_WORK)
150 if(!isUserInputCheck)
152 none_input_start_time = millis();
153 isUserInputCheck = true;
156 if(key != INPUT_KEY_NO_PRESS)
158 DBGLN("user key = %d", key);
159 isUserInputCheck = false;
160 if(key == INPUT_KEY_DOWN_PRESS)
162 screen.doUserAction(USER_ACTION_DOWN);
164 else if(key == INPUT_KEY_UP_PRESS)
166 screen.doUserAction(USER_ACTION_UP);
168 else if(key == INPUT_KEY_LEFT_PRESS)
170 screen.doUserAction(USER_ACTION_LEFT);
172 else if(key == INPUT_KEY_RIGHT_PRESS)
174 screen.doUserAction(USER_ACTION_RIGHT);
176 else if(key == INPUT_KEY_OK_PRESS)
178 screen.doUserAction(USER_ACTION_CONFIRM);
181 else
183 if((millis() - none_input_start_time) > SCREEN_IDLE_TIMEOUT)
185 isUserInputCheck = false;
186 screen.idleScreen();
190 else if(screen.getScreenStatus() == SCREEN_STATUS_BINDING)
192 if((millis() - binding_mode_start_time) > BINDING_MODE_TIME_OUT)
194 screen.doUserAction(USER_ACTION_LEFT);
198 #ifdef HAS_GSENSOR
199 is_screen_flipped = gsensor.isFlipped();
201 if((is_screen_flipped == true) && (is_pre_screen_flipped == false))
203 screen.doScreenBackLight(SCREEN_BACKLIGHT_OFF);
205 else if((is_screen_flipped == false) && (is_pre_screen_flipped == true))
207 screen.doScreenBackLight(SCREEN_BACKLIGHT_ON);
209 is_pre_screen_flipped = is_screen_flipped;
210 #endif
211 return SCREEN_DURATION;
213 #else
214 static int handle(void)
216 return DURATION_NEVER;
218 #endif
220 static void initialize()
222 #ifdef HAS_FIVE_WAY_BUTTON
223 fivewaybutton.init();
224 #endif
225 screen.updatecallback = &ScreenUpdateCallback;
226 #if defined(PLATFORM_ESP32)
227 screen.init(esp_reset_reason() == ESP_RST_SW);
228 #else
229 screen.init(false);
230 #endif
233 static int start()
235 if (screen.getScreenStatus() == SCREEN_STATUS_INIT)
237 screen.doParamUpdate(config.GetRate(), (uint8_t)(POWERMGNT::currPower()), config.GetTlm(), config.GetMotionMode(), config.GetFanMode());
238 return LOGO_DISPLAY_TIMEOUT;
240 return DURATION_IMMEDIATELY;
243 static int event()
245 if(connectionState != wifiUpdate)
247 screen.doParamUpdate(config.GetRate(), (uint8_t)(POWERMGNT::currPower()), config.GetTlm(), config.GetMotionMode(), config.GetFanMode());
250 return DURATION_IGNORE;
253 static int timeout()
255 if(screen.getScreenStatus() == SCREEN_STATUS_INIT)
257 isLogoDisplayed = true;
258 screen.idleScreen();
261 return handle();
264 device_t Screen_device = {
265 .initialize = initialize,
266 .start = start,
267 .event = event,
268 .timeout = timeout
270 #endif