optimise mavlink SS packet size (#3029)
[ExpressLRS.git] / src / lib / SCREEN / devScreen.cpp
bloba887e07ac30f5060a1389b1ff6fd772ac7b81d8e
1 #include "targets.h"
3 #if defined(PLATFORM_ESP32)
5 #include "devScreen.h"
7 #include "common.h"
8 #include "logging.h"
10 #include "OLED/oleddisplay.h"
11 #include "TFT/tftdisplay.h"
13 #include "devButton.h"
14 #include "handset.h"
16 FiniteStateMachine state_machine(entry_fsm);
18 Display *display;
20 #include "FiveWayButton/FiveWayButton.h"
21 FiveWayButton fivewaybutton;
23 #include "gsensor.h"
24 extern Gsensor gsensor;
25 static bool is_screen_flipped = false;
26 static bool is_pre_screen_flipped = false;
28 #define SCREEN_DURATION 20
30 extern void jumpToWifiRunning();
31 extern void jumpToBleRunning();
33 static bool jumpToBandSelect = false;
34 static bool jumpToChannelSelect = false;
36 static int handle(void)
38 is_screen_flipped = gsensor.isFlipped();
40 if ((is_screen_flipped == true) && (is_pre_screen_flipped == false))
42 display->doScreenBackLight(SCREEN_BACKLIGHT_OFF);
44 else if ((is_screen_flipped == false) && (is_pre_screen_flipped == true))
46 display->doScreenBackLight(SCREEN_BACKLIGHT_ON);
47 state_machine.start(millis(), STATE_IDLE);
49 is_pre_screen_flipped = is_screen_flipped;
50 if (is_screen_flipped)
52 return 100; // no need to check as often if the screen is off!
54 uint32_t now = millis();
56 if (state_machine.getParentState() != STATE_WIFI_TX && connectionState == wifiUpdate)
58 jumpToWifiRunning();
61 if (state_machine.getParentState() != STATE_JOYSTICK && connectionState == bleJoystick)
63 jumpToBleRunning();
66 if (!handset->IsArmed())
68 int key;
69 bool isLongPressed;
70 // if we are using analog joystick then we can't cancel because WiFi is using the ADC2 (i.e. channel >= 8)!
71 if (connectionState == wifiUpdate && digitalPinToAnalogChannel(GPIO_PIN_JOYSTICK) >= 8)
73 key = INPUT_KEY_NO_PRESS;
75 else
77 fivewaybutton.update(&key, &isLongPressed);
79 fsm_event_t fsm_event;
80 switch (key)
82 case INPUT_KEY_DOWN_PRESS:
83 fsm_event = EVENT_DOWN;
84 break;
85 case INPUT_KEY_UP_PRESS:
86 fsm_event = EVENT_UP;
87 break;
88 case INPUT_KEY_LEFT_PRESS:
89 fsm_event = EVENT_LEFT;
90 break;
91 case INPUT_KEY_RIGHT_PRESS:
92 fsm_event = EVENT_RIGHT;
93 break;
94 case INPUT_KEY_OK_PRESS:
95 fsm_event = EVENT_ENTER;
96 break;
97 default: // INPUT_KEY_NO_PRESS
98 fsm_event = EVENT_TIMEOUT;
100 if (fsm_event != EVENT_TIMEOUT && isLongPressed)
102 fsm_event = (fsm_event | LONG_PRESSED);
104 #if defined(DEBUG_SCREENSHOT)
105 if (key == INPUT_KEY_DOWN_PRESS && isLongPressed)
107 DBGLN("state_%d", state_machine.getCurrentState());
108 Display::printScreenshot();
110 #endif
111 if (jumpToBandSelect)
113 state_machine.jumpTo(vtx_menu_fsm, STATE_VTX_BAND);
114 state_machine.jumpTo(value_select_fsm, STATE_VALUE_INIT);
115 jumpToBandSelect = false;
117 else if (jumpToChannelSelect)
119 state_machine.jumpTo(vtx_menu_fsm, STATE_VTX_CHANNEL);
120 state_machine.jumpTo(value_select_fsm, STATE_VALUE_INIT);
121 jumpToChannelSelect = false;
123 else
125 state_machine.handleEvent(now, fsm_event);
128 else
130 state_machine.handleEvent(now, EVENT_TIMEOUT);
133 return SCREEN_DURATION;
136 static void initialize()
138 if (OPT_HAS_SCREEN)
140 fivewaybutton.init();
141 if (OPT_HAS_TFT_SCREEN)
143 display = new TFTDisplay();
145 else
147 display = new OLEDDisplay();
149 display->init();
150 state_machine.start(millis(), getInitialState());
152 registerButtonFunction(ACTION_GOTO_VTX_BAND, [](){
153 jumpToBandSelect = true;
154 devicesTriggerEvent();
156 registerButtonFunction(ACTION_GOTO_VTX_CHANNEL, [](){
157 jumpToChannelSelect = true;
158 devicesTriggerEvent();
163 static int start()
165 if (OPT_HAS_SCREEN)
167 return DURATION_IMMEDIATELY;
169 return DURATION_NEVER;
172 static int event()
174 if (OPT_HAS_SCREEN)
176 return handle();
178 return DURATION_NEVER;
181 static int timeout()
183 if (OPT_HAS_SCREEN)
185 return handle();
187 return DURATION_NEVER;
190 device_t Screen_device = {
191 .initialize = initialize,
192 .start = start,
193 .event = event,
194 .timeout = timeout};
195 #endif