Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / io / rcdevice_cam.c
bloba31d8117598b2e74c0ddee6c6ddbff9b3c6a82bd
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #include <stdbool.h>
22 #include <stdint.h>
24 #include "platform.h"
26 #include "pg/rx.h"
28 #include "common/time.h"
30 #include "cms/cms.h"
32 #include "fc/rc_controls.h"
33 #include "fc/runtime_config.h"
35 #include "io/beeper.h"
36 #include "io/serial.h"
37 #include "io/rcdevice_cam.h"
39 #include "rx/rx.h"
41 #include "pg/rcdevice.h"
43 #ifdef USE_RCDEVICE
45 #define IS_HI(X) (rcData[X] > FIVE_KEY_CABLE_JOYSTICK_MAX)
46 #define IS_LO(X) (rcData[X] < FIVE_KEY_CABLE_JOYSTICK_MIN)
47 #define IS_MID(X) (rcData[X] > FIVE_KEY_CABLE_JOYSTICK_MID_START && rcData[X] < FIVE_KEY_CABLE_JOYSTICK_MID_END)
50 static runcamDevice_t runcamDevice;
51 runcamDevice_t *camDevice = &runcamDevice;
52 rcdeviceSwitchState_t switchStates[BOXCAMERA3 - BOXCAMERA1 + 1];
53 bool rcdeviceInMenu = false;
54 bool isButtonPressed = false;
55 bool waitingDeviceResponse = false;
58 static bool isFeatureSupported(uint16_t feature)
60 if (camDevice->info.features & feature || rcdeviceConfig()->feature & feature) {
61 return true;
64 return false;
67 bool rcdeviceIsEnabled(void)
69 return camDevice->serialPort != NULL;
72 static void rcdeviceCameraControlProcess(void)
74 for (boxId_e i = BOXCAMERA1; i <= BOXCAMERA3; i++) {
75 uint8_t switchIndex = i - BOXCAMERA1;
77 if (IS_RC_MODE_ACTIVE(i)) {
79 // check last state of this mode, if it's true, then ignore it.
80 // Here is a logic to make a toggle control for this mode
81 if (switchStates[switchIndex].isActivated) {
82 continue;
85 uint8_t behavior = RCDEVICE_PROTOCOL_CAM_CTRL_UNKNOWN_CAMERA_OPERATION;
86 switch (i) {
87 case BOXCAMERA1:
88 if (isFeatureSupported(RCDEVICE_PROTOCOL_FEATURE_SIMULATE_WIFI_BUTTON)) {
89 // avoid display wifi page when arming, in the next firmware(>2.0) of rcsplit we have change the wifi page logic:
90 // when the wifi was turn on it won't turn off the analog video output,
91 // and just put a wifi indicator on the right top of the video output. here is for the old split firmware
92 if (!ARMING_FLAG(ARMED) && !(getArmingDisableFlags() & (ARMING_DISABLED_RUNAWAY_TAKEOFF | ARMING_DISABLED_CRASH_DETECTED))) {
93 behavior = RCDEVICE_PROTOCOL_CAM_CTRL_SIMULATE_WIFI_BTN;
96 break;
97 case BOXCAMERA2:
98 if (isFeatureSupported(RCDEVICE_PROTOCOL_FEATURE_SIMULATE_POWER_BUTTON)) {
99 behavior = RCDEVICE_PROTOCOL_CAM_CTRL_SIMULATE_POWER_BTN;
101 break;
102 case BOXCAMERA3:
103 if (isFeatureSupported(RCDEVICE_PROTOCOL_FEATURE_CHANGE_MODE)) {
104 // avoid change camera mode when arming
105 if (!ARMING_FLAG(ARMED) && !(getArmingDisableFlags() & (ARMING_DISABLED_RUNAWAY_TAKEOFF | ARMING_DISABLED_CRASH_DETECTED))) {
106 behavior = RCDEVICE_PROTOCOL_CAM_CTRL_CHANGE_MODE;
109 break;
110 default:
111 break;
113 if (behavior != RCDEVICE_PROTOCOL_CAM_CTRL_UNKNOWN_CAMERA_OPERATION) {
114 runcamDeviceSimulateCameraButton(camDevice, behavior);
115 switchStates[switchIndex].isActivated = true;
117 } else {
118 switchStates[switchIndex].isActivated = false;
123 static void rcdeviceSimulationOSDCableFailed(rcdeviceResponseParseContext_t *ctx)
125 waitingDeviceResponse = false;
126 if (ctx->command == RCDEVICE_PROTOCOL_COMMAND_5KEY_CONNECTION) {
127 uint8_t operationID = ctx->paramData[0];
128 if (operationID == RCDEVICE_PROTOCOL_5KEY_CONNECTION_CLOSE) {
129 return;
134 static void rcdeviceSimulationRespHandle(rcdeviceResponseParseContext_t *ctx)
136 if (ctx->result != RCDEVICE_RESP_SUCCESS) {
137 rcdeviceSimulationOSDCableFailed(ctx);
138 waitingDeviceResponse = false;
139 return;
142 switch (ctx->command) {
143 case RCDEVICE_PROTOCOL_COMMAND_5KEY_SIMULATION_RELEASE:
144 isButtonPressed = false;
145 break;
146 case RCDEVICE_PROTOCOL_COMMAND_5KEY_CONNECTION:
148 // the high 4 bits is the operationID that we sent
149 // the low 4 bits is the result code
150 isButtonPressed = true;
151 uint8_t operationID = ctx->paramData[0];
152 bool errorCode = (ctx->recvBuf[1] & 0x0F);
153 if (operationID == RCDEVICE_PROTOCOL_5KEY_CONNECTION_OPEN) {
154 if (errorCode == 1) {
155 rcdeviceInMenu = true;
156 beeper(BEEPER_CAM_CONNECTION_OPEN);
157 } else {
158 beeper(BEEPER_CAM_CONNECTION_CLOSE);
160 } else if (operationID == RCDEVICE_PROTOCOL_5KEY_CONNECTION_CLOSE) {
161 if (errorCode == 1) {
162 rcdeviceInMenu = false;
163 beeper(BEEPER_CAM_CONNECTION_CLOSE);
167 break;
168 case RCDEVICE_PROTOCOL_COMMAND_5KEY_SIMULATION_PRESS:
169 isButtonPressed = true;
170 break;
173 waitingDeviceResponse = false;
176 static void rcdeviceCamSimulate5KeyCablePress(rcdeviceCamSimulationKeyEvent_e key)
178 uint8_t operation = RCDEVICE_PROTOCOL_5KEY_SIMULATION_NONE;
179 switch (key) {
180 case RCDEVICE_CAM_KEY_LEFT:
181 operation = RCDEVICE_PROTOCOL_5KEY_SIMULATION_LEFT;
182 break;
183 case RCDEVICE_CAM_KEY_UP:
184 operation = RCDEVICE_PROTOCOL_5KEY_SIMULATION_UP;
185 break;
186 case RCDEVICE_CAM_KEY_RIGHT:
187 operation = RCDEVICE_PROTOCOL_5KEY_SIMULATION_RIGHT;
188 break;
189 case RCDEVICE_CAM_KEY_DOWN:
190 operation = RCDEVICE_PROTOCOL_5KEY_SIMULATION_DOWN;
191 break;
192 case RCDEVICE_CAM_KEY_ENTER:
193 operation = RCDEVICE_PROTOCOL_5KEY_SIMULATION_SET;
194 break;
195 case RCDEVICE_CAM_KEY_NONE:
196 default:
197 operation = RCDEVICE_PROTOCOL_5KEY_SIMULATION_NONE;
198 break;
201 runcamDeviceSimulate5KeyOSDCableButtonPress(camDevice, operation, rcdeviceSimulationRespHandle);
204 void rcdeviceSend5KeyOSDCableSimualtionEvent(rcdeviceCamSimulationKeyEvent_e key)
206 switch (key) {
207 case RCDEVICE_CAM_KEY_CONNECTION_OPEN:
208 runcamDeviceOpen5KeyOSDCableConnection(camDevice, rcdeviceSimulationRespHandle);
209 break;
210 case RCDEVICE_CAM_KEY_CONNECTION_CLOSE:
211 runcamDeviceClose5KeyOSDCableConnection(camDevice, rcdeviceSimulationRespHandle);
212 break;
213 case RCDEVICE_CAM_KEY_ENTER:
214 case RCDEVICE_CAM_KEY_LEFT:
215 case RCDEVICE_CAM_KEY_UP:
216 case RCDEVICE_CAM_KEY_RIGHT:
217 case RCDEVICE_CAM_KEY_DOWN:
218 rcdeviceCamSimulate5KeyCablePress(key);
219 break;
220 case RCDEVICE_CAM_KEY_RELEASE:
221 runcamDeviceSimulate5KeyOSDCableButtonRelease(camDevice, rcdeviceSimulationRespHandle);
222 break;
223 case RCDEVICE_CAM_KEY_NONE:
224 default:
225 break;
229 static void rcdevice5KeySimulationProcess(timeUs_t currentTimeUs)
231 UNUSED(currentTimeUs);
233 #ifdef USE_CMS
234 if (cmsInMenu) {
235 return;
237 #endif
239 if (ARMING_FLAG(ARMED) || IS_RC_MODE_ACTIVE(BOXSTICKCOMMANDDISABLE) || (getArmingDisableFlags() & (ARMING_DISABLED_RUNAWAY_TAKEOFF | ARMING_DISABLED_CRASH_DETECTED))) {
240 return;
243 if (isButtonPressed) {
244 if (IS_MID(YAW) && IS_MID(PITCH) && IS_MID(ROLL)) {
245 rcdeviceSend5KeyOSDCableSimualtionEvent(RCDEVICE_CAM_KEY_RELEASE);
246 waitingDeviceResponse = true;
248 } else {
249 if (waitingDeviceResponse) {
250 return;
253 rcdeviceCamSimulationKeyEvent_e key = RCDEVICE_CAM_KEY_NONE;
255 if (IS_MID(THROTTLE) && IS_MID(ROLL) && IS_MID(PITCH) && IS_LO(YAW)) { // Disconnect Lo YAW
256 if (rcdeviceInMenu) {
257 key = RCDEVICE_CAM_KEY_CONNECTION_CLOSE;
259 } else {
260 if (rcdeviceInMenu) {
261 if (IS_LO(ROLL)) { // Left LO ROLL
262 key = RCDEVICE_CAM_KEY_LEFT;
263 } else if (IS_HI(PITCH)) { // Up HI PITCH
264 key = RCDEVICE_CAM_KEY_UP;
265 } else if (IS_HI(ROLL)) { // Right HI ROLL
266 key = RCDEVICE_CAM_KEY_RIGHT;
267 } else if (IS_LO(PITCH)) { // Down LO PITCH
268 key = RCDEVICE_CAM_KEY_DOWN;
269 } else if (IS_MID(THROTTLE) && IS_MID(ROLL) && IS_MID(PITCH) && IS_HI(YAW)) { // Enter HI YAW
270 key = RCDEVICE_CAM_KEY_ENTER;
272 } else {
273 if (IS_MID(THROTTLE) && IS_MID(ROLL) && IS_MID(PITCH) && IS_HI(YAW)) { // Enter HI YAW
274 key = RCDEVICE_CAM_KEY_CONNECTION_OPEN;
279 if (key != RCDEVICE_CAM_KEY_NONE) {
280 rcdeviceSend5KeyOSDCableSimualtionEvent(key);
281 isButtonPressed = true;
282 waitingDeviceResponse = true;
287 static void rcdeviceProcessDeviceRequest(runcamDeviceRequest_t *request)
289 switch (request->command) {
290 case RCDEVICE_PROTOCOL_COMMAND_REQUEST_FC_ATTITUDE:
291 runcamDeviceSendAttitude(camDevice);
292 break;
296 void rcdeviceUpdate(timeUs_t currentTimeUs)
298 rcdeviceReceive(currentTimeUs);
300 rcdeviceCameraControlProcess();
302 rcdevice5KeySimulationProcess(currentTimeUs);
304 if (isFeatureSupported(RCDEVICE_PROTOCOL_FEATURE_FC_ATTITUDE)) {
305 runcamDeviceRequest_t *request = rcdeviceGetRequest();
306 if (request) {
307 rcdeviceProcessDeviceRequest(request);
312 void rcdeviceInit(void)
314 // open serial port
315 runcamDeviceInit(camDevice);
317 for (boxId_e i = BOXCAMERA1; i <= BOXCAMERA3; i++) {
318 uint8_t switchIndex = i - BOXCAMERA1;
319 switchStates[switchIndex].isActivated = true;
323 #endif