Logging of the S -term values in blackbox for Fixed wings. (#14012)
[betaflight.git] / src / main / io / rcdevice_cam.c
bloba5e7a6ae3906cb9702314cb184ca2336effb32b3
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)
49 static runcamDevice_t runcamDevice;
50 runcamDevice_t *camDevice = &runcamDevice;
51 rcdeviceSwitchState_t switchStates[BOXCAMERA3 - BOXCAMERA1 + 1];
52 bool rcdeviceInMenu = false;
53 bool isButtonPressed = false;
54 bool waitingDeviceResponse = false;
56 static bool isFeatureSupported(uint16_t feature)
58 if (camDevice->info.features & feature || rcdeviceConfig()->feature & feature) {
59 return true;
62 return false;
65 bool rcdeviceIsEnabled(void)
67 return camDevice->serialPort != NULL;
70 static void rcdeviceCameraControlProcess(void)
72 for (boxId_e i = BOXCAMERA1; i <= BOXCAMERA3; i++) {
73 uint8_t switchIndex = i - BOXCAMERA1;
75 if (IS_RC_MODE_ACTIVE(i)) {
77 // check last state of this mode, if it's true, then ignore it.
78 // Here is a logic to make a toggle control for this mode
79 if (switchStates[switchIndex].isActivated) {
80 continue;
83 uint8_t behavior = RCDEVICE_PROTOCOL_CAM_CTRL_UNKNOWN_CAMERA_OPERATION;
84 switch (i) {
85 case BOXCAMERA1:
86 if (isFeatureSupported(RCDEVICE_PROTOCOL_FEATURE_SIMULATE_WIFI_BUTTON)) {
87 // avoid display wifi page when arming, in the next firmware(>2.0) of rcsplit we have change the wifi page logic:
88 // when the wifi was turn on it won't turn off the analog video output,
89 // and just put a wifi indicator on the right top of the video output. here is for the old split firmware
90 if (!ARMING_FLAG(ARMED) && !(getArmingDisableFlags() & (ARMING_DISABLED_RUNAWAY_TAKEOFF | ARMING_DISABLED_CRASH_DETECTED))) {
91 behavior = RCDEVICE_PROTOCOL_CAM_CTRL_SIMULATE_WIFI_BTN;
94 break;
95 case BOXCAMERA2:
96 if (isFeatureSupported(RCDEVICE_PROTOCOL_FEATURE_SIMULATE_POWER_BUTTON)) {
97 behavior = RCDEVICE_PROTOCOL_CAM_CTRL_SIMULATE_POWER_BTN;
99 break;
100 case BOXCAMERA3:
101 if (isFeatureSupported(RCDEVICE_PROTOCOL_FEATURE_CHANGE_MODE)) {
102 // avoid change camera mode when arming
103 if (!ARMING_FLAG(ARMED) && !(getArmingDisableFlags() & (ARMING_DISABLED_RUNAWAY_TAKEOFF | ARMING_DISABLED_CRASH_DETECTED))) {
104 behavior = RCDEVICE_PROTOCOL_CAM_CTRL_CHANGE_MODE;
107 break;
108 default:
109 break;
111 if (behavior != RCDEVICE_PROTOCOL_CAM_CTRL_UNKNOWN_CAMERA_OPERATION) {
112 runcamDeviceSimulateCameraButton(camDevice, behavior);
113 switchStates[switchIndex].isActivated = true;
115 } else {
116 switchStates[switchIndex].isActivated = false;
121 static void rcdeviceSimulationOSDCableFailed(rcdeviceResponseParseContext_t *ctx)
123 waitingDeviceResponse = false;
124 if (ctx->command == RCDEVICE_PROTOCOL_COMMAND_5KEY_CONNECTION) {
125 uint8_t operationID = ctx->paramData[0];
126 if (operationID == RCDEVICE_PROTOCOL_5KEY_CONNECTION_CLOSE) {
127 return;
132 static void rcdeviceSimulationRespHandle(rcdeviceResponseParseContext_t *ctx)
134 if (ctx->result != RCDEVICE_RESP_SUCCESS) {
135 rcdeviceSimulationOSDCableFailed(ctx);
136 waitingDeviceResponse = false;
137 return;
140 switch (ctx->command) {
141 case RCDEVICE_PROTOCOL_COMMAND_5KEY_SIMULATION_RELEASE:
142 isButtonPressed = false;
143 break;
144 case RCDEVICE_PROTOCOL_COMMAND_5KEY_CONNECTION:
146 // the high 4 bits is the operationID that we sent
147 // the low 4 bits is the result code
148 isButtonPressed = true;
149 uint8_t operationID = ctx->paramData[0];
150 bool errorCode = (ctx->recvBuf[1] & 0x0F);
151 if (operationID == RCDEVICE_PROTOCOL_5KEY_CONNECTION_OPEN) {
152 if (errorCode == 1) {
153 rcdeviceInMenu = true;
154 beeper(BEEPER_CAM_CONNECTION_OPEN);
155 } else {
156 beeper(BEEPER_CAM_CONNECTION_CLOSE);
158 } else if (operationID == RCDEVICE_PROTOCOL_5KEY_CONNECTION_CLOSE) {
159 if (errorCode == 1) {
160 rcdeviceInMenu = false;
161 beeper(BEEPER_CAM_CONNECTION_CLOSE);
165 break;
166 case RCDEVICE_PROTOCOL_COMMAND_5KEY_SIMULATION_PRESS:
167 isButtonPressed = true;
168 break;
171 waitingDeviceResponse = false;
174 static void rcdeviceCamSimulate5KeyCablePress(rcdeviceCamSimulationKeyEvent_e key)
176 uint8_t operation = RCDEVICE_PROTOCOL_5KEY_SIMULATION_NONE;
177 switch (key) {
178 case RCDEVICE_CAM_KEY_LEFT:
179 operation = RCDEVICE_PROTOCOL_5KEY_SIMULATION_LEFT;
180 break;
181 case RCDEVICE_CAM_KEY_UP:
182 operation = RCDEVICE_PROTOCOL_5KEY_SIMULATION_UP;
183 break;
184 case RCDEVICE_CAM_KEY_RIGHT:
185 operation = RCDEVICE_PROTOCOL_5KEY_SIMULATION_RIGHT;
186 break;
187 case RCDEVICE_CAM_KEY_DOWN:
188 operation = RCDEVICE_PROTOCOL_5KEY_SIMULATION_DOWN;
189 break;
190 case RCDEVICE_CAM_KEY_ENTER:
191 operation = RCDEVICE_PROTOCOL_5KEY_SIMULATION_SET;
192 break;
193 case RCDEVICE_CAM_KEY_NONE:
194 default:
195 operation = RCDEVICE_PROTOCOL_5KEY_SIMULATION_NONE;
196 break;
199 runcamDeviceSimulate5KeyOSDCableButtonPress(camDevice, operation, rcdeviceSimulationRespHandle);
202 void rcdeviceSend5KeyOSDCableSimualtionEvent(rcdeviceCamSimulationKeyEvent_e key)
204 switch (key) {
205 case RCDEVICE_CAM_KEY_CONNECTION_OPEN:
206 runcamDeviceOpen5KeyOSDCableConnection(camDevice, rcdeviceSimulationRespHandle);
207 break;
208 case RCDEVICE_CAM_KEY_CONNECTION_CLOSE:
209 runcamDeviceClose5KeyOSDCableConnection(camDevice, rcdeviceSimulationRespHandle);
210 break;
211 case RCDEVICE_CAM_KEY_ENTER:
212 case RCDEVICE_CAM_KEY_LEFT:
213 case RCDEVICE_CAM_KEY_UP:
214 case RCDEVICE_CAM_KEY_RIGHT:
215 case RCDEVICE_CAM_KEY_DOWN:
216 rcdeviceCamSimulate5KeyCablePress(key);
217 break;
218 case RCDEVICE_CAM_KEY_RELEASE:
219 runcamDeviceSimulate5KeyOSDCableButtonRelease(camDevice, rcdeviceSimulationRespHandle);
220 break;
221 case RCDEVICE_CAM_KEY_NONE:
222 default:
223 break;
227 static void rcdevice5KeySimulationProcess(timeUs_t currentTimeUs)
229 UNUSED(currentTimeUs);
231 #ifdef USE_CMS
232 if (cmsInMenu) {
233 return;
235 #endif
237 if (ARMING_FLAG(ARMED) || IS_RC_MODE_ACTIVE(BOXSTICKCOMMANDDISABLE) || (getArmingDisableFlags() & (ARMING_DISABLED_RUNAWAY_TAKEOFF | ARMING_DISABLED_CRASH_DETECTED))) {
238 return;
241 if (isButtonPressed) {
242 if (IS_MID(YAW) && IS_MID(PITCH) && IS_MID(ROLL)) {
243 rcdeviceSend5KeyOSDCableSimualtionEvent(RCDEVICE_CAM_KEY_RELEASE);
244 waitingDeviceResponse = true;
246 } else {
247 if (waitingDeviceResponse) {
248 return;
251 rcdeviceCamSimulationKeyEvent_e key = RCDEVICE_CAM_KEY_NONE;
253 if (IS_MID(THROTTLE) && IS_MID(ROLL) && IS_MID(PITCH) && IS_LO(YAW)) { // Disconnect Lo YAW
254 if (rcdeviceInMenu) {
255 key = RCDEVICE_CAM_KEY_CONNECTION_CLOSE;
257 } else {
258 if (rcdeviceInMenu) {
259 if (IS_LO(ROLL)) { // Left LO ROLL
260 key = RCDEVICE_CAM_KEY_LEFT;
261 } else if (IS_HI(PITCH)) { // Up HI PITCH
262 key = RCDEVICE_CAM_KEY_UP;
263 } else if (IS_HI(ROLL)) { // Right HI ROLL
264 key = RCDEVICE_CAM_KEY_RIGHT;
265 } else if (IS_LO(PITCH)) { // Down LO PITCH
266 key = RCDEVICE_CAM_KEY_DOWN;
267 } else if (IS_MID(THROTTLE) && IS_MID(ROLL) && IS_MID(PITCH) && IS_HI(YAW)) { // Enter HI YAW
268 key = RCDEVICE_CAM_KEY_ENTER;
270 } else {
271 if (IS_MID(THROTTLE) && IS_MID(ROLL) && IS_MID(PITCH) && IS_HI(YAW)) { // Enter HI YAW
272 key = RCDEVICE_CAM_KEY_CONNECTION_OPEN;
277 if (key != RCDEVICE_CAM_KEY_NONE) {
278 rcdeviceSend5KeyOSDCableSimualtionEvent(key);
279 isButtonPressed = true;
280 waitingDeviceResponse = true;
285 static void rcdeviceProcessDeviceRequest(runcamDeviceRequest_t *request)
287 switch (request->command) {
288 case RCDEVICE_PROTOCOL_COMMAND_REQUEST_FC_ATTITUDE:
289 runcamDeviceSendAttitude(camDevice);
290 break;
294 void rcdeviceUpdate(timeUs_t currentTimeUs)
296 rcdeviceReceive(currentTimeUs);
298 rcdeviceCameraControlProcess();
300 rcdevice5KeySimulationProcess(currentTimeUs);
302 if (isFeatureSupported(RCDEVICE_PROTOCOL_FEATURE_FC_ATTITUDE)) {
303 runcamDeviceRequest_t *request = rcdeviceGetRequest();
304 if (request) {
305 rcdeviceProcessDeviceRequest(request);
310 void rcdeviceInit(void)
312 // open serial port
313 runcamDeviceInit(camDevice);
315 for (boxId_e i = BOXCAMERA1; i <= BOXCAMERA3; i++) {
316 uint8_t switchIndex = i - BOXCAMERA1;
317 switchStates[switchIndex].isActivated = true;
321 #endif