2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
20 #include "drivers/serial.h"
23 // The protocol for Runcam Device definition
25 #define RCDEVICE_PROTOCOL_HEADER 0xCC
27 #define RCDEVICE_PROTOCOL_MAX_PACKET_SIZE 64
28 #define RCDEVICE_PROTOCOL_MAX_DATA_SIZE 20
31 #define RCDEVICE_PROTOCOL_COMMAND_GET_DEVICE_INFO 0x00
33 #define RCDEVICE_PROTOCOL_COMMAND_CAMERA_CONTROL 0x01
34 // 5 key osd cable simulation
35 #define RCDEVICE_PROTOCOL_COMMAND_5KEY_SIMULATION_PRESS 0x02
36 #define RCDEVICE_PROTOCOL_COMMAND_5KEY_SIMULATION_RELEASE 0x03
37 #define RCDEVICE_PROTOCOL_COMMAND_5KEY_CONNECTION 0x04
39 // Old protocol defines
40 #define RCSPLIT_PACKET_HEADER 0x55
41 #define RCSPLIT_PACKET_CMD_CTRL 0x01
42 #define RCSPLIT_PACKET_TAIL 0xaa
44 // Feature Flag sets, it's a uint16_t flag
46 RCDEVICE_PROTOCOL_FEATURE_SIMULATE_POWER_BUTTON
= (1 << 0),
47 RCDEVICE_PROTOCOL_FEATURE_SIMULATE_WIFI_BUTTON
= (1 << 1),
48 RCDEVICE_PROTOCOL_FEATURE_CHANGE_MODE
= (1 << 2),
49 RCDEVICE_PROTOCOL_FEATURE_SIMULATE_5_KEY_OSD_CABLE
= (1 << 3),
50 RCDEVICE_PROTOCOL_FEATURE_START_RECORDING
= (1 << 6),
51 RCDEVICE_PROTOCOL_FEATURE_STOP_RECORDING
= (1 << 7),
52 RCDEVICE_PROTOCOL_FEATURE_CMS_MENU
= (1 << 8),
53 } rcdevice_features_e
;
55 // Operation of Camera Button Simulation
57 RCDEVICE_PROTOCOL_CAM_CTRL_SIMULATE_WIFI_BTN
= 0x00,
58 RCDEVICE_PROTOCOL_CAM_CTRL_SIMULATE_POWER_BTN
= 0x01,
59 RCDEVICE_PROTOCOL_CAM_CTRL_CHANGE_MODE
= 0x02,
60 RCDEVICE_PROTOCOL_CAM_CTRL_START_RECORDING
= 0x03,
61 RCDEVICE_PROTOCOL_CAM_CTRL_STOP_RECORDING
= 0x04,
62 RCDEVICE_PROTOCOL_CAM_CTRL_UNKNOWN_CAMERA_OPERATION
= 0xFF
63 } rcdevice_camera_control_opeation_e
;
65 // Operation Of 5 Key OSD Cable Simulation
67 RCDEVICE_PROTOCOL_5KEY_SIMULATION_NONE
= 0x00,
68 RCDEVICE_PROTOCOL_5KEY_SIMULATION_SET
= 0x01,
69 RCDEVICE_PROTOCOL_5KEY_SIMULATION_LEFT
= 0x02,
70 RCDEVICE_PROTOCOL_5KEY_SIMULATION_RIGHT
= 0x03,
71 RCDEVICE_PROTOCOL_5KEY_SIMULATION_UP
= 0x04,
72 RCDEVICE_PROTOCOL_5KEY_SIMULATION_DOWN
= 0x05
73 } rcdevice_5key_simulation_operation_e
;
75 // Operation of RCDEVICE_PROTOCOL_COMMAND_5KEY_CONNECTION
77 RCDEVICE_PROTOCOL_5KEY_CONNECTION_OPEN
= 0x01,
78 RCDEVICE_PROTOCOL_5KEY_CONNECTION_CLOSE
= 0x02
79 } RCDEVICE_5key_connection_event_e
;
82 RCDEVICE_CAM_KEY_NONE
,
83 RCDEVICE_CAM_KEY_ENTER
,
84 RCDEVICE_CAM_KEY_LEFT
,
86 RCDEVICE_CAM_KEY_RIGHT
,
87 RCDEVICE_CAM_KEY_DOWN
,
88 RCDEVICE_CAM_KEY_CONNECTION_CLOSE
,
89 RCDEVICE_CAM_KEY_CONNECTION_OPEN
,
90 RCDEVICE_CAM_KEY_RELEASE
,
91 } rcdeviceCamSimulationKeyEvent_e
;
94 RCDEVICE_PROTOCOL_RCSPLIT_VERSION
= 0x00, // this is used to indicate the
95 // device that using rcsplit
96 // firmware version that <= 1.1.0
97 RCDEVICE_PROTOCOL_VERSION_1_0
= 0x01,
98 RCDEVICE_PROTOCOL_UNKNOWN
99 } rcdevice_protocol_version_e
;
101 // end of Runcam Device definition
103 typedef struct runcamDeviceInfo_s
{
104 rcdevice_protocol_version_e protocolVersion
;
106 } runcamDeviceInfo_t
;
108 typedef struct runcamDevice_s
{
109 serialPort_t
*serialPort
;
110 uint8_t buffer
[RCDEVICE_PROTOCOL_MAX_PACKET_SIZE
];
111 runcamDeviceInfo_t info
;
115 #define MAX_WAITING_RESPONSES 2
118 RCDEVICE_RESP_SUCCESS
= 0,
119 RCDEVICE_RESP_INCORRECT_CRC
= 1,
120 RCDEVICE_RESP_TIMEOUT
= 2
121 } rcdeviceResponseStatus_e
;
123 typedef struct rcdeviceResponseParsingContext_s rcdeviceResponseParsingContext_t
;
124 typedef void(*rcdeviceResponseCallback
)(rcdeviceResponseParsingContext_t
* context
);
126 typedef struct rcdeviceResponseParsingContext_s
{
128 uint8_t expectedRespLen
; // total length of response data
129 uint8_t recvRespLen
; // length of the data received
130 uint8_t *recvBuf
; // response data buffer
132 timeMs_t timeoutTimestamp
; // if zero, it's means keep waiting for the response
133 rcdeviceResponseCallback parserFunc
;
134 runcamDevice_t
*device
;
135 uint8_t paramData
[RCDEVICE_PROTOCOL_MAX_DATA_SIZE
];
136 uint8_t paramDataLen
;
137 uint8_t protocolVersion
;
140 rcdeviceResponseStatus_e result
;
141 } rcdeviceResponseParsingContext_t
;
144 uint8_t headPos
; // current head position of the queue
146 uint8_t itemCount
; // the item count in the queue
147 rcdeviceResponseParsingContext_t buffer
[MAX_WAITING_RESPONSES
];
148 rcdeviceResponseCallback parseFunc
;
149 } rcdeviceWaitingResponseQueue
;
151 void runcamDeviceInit(runcamDevice_t
*device
);
152 void rcdeviceReceive(timeUs_t currentTimeUs
);
154 // camera button simulation
155 bool runcamDeviceSimulateCameraButton(runcamDevice_t
*device
, uint8_t operation
);
157 // 5 key osd cable simulation
158 void runcamDeviceOpen5KeyOSDCableConnection(runcamDevice_t
*device
, rcdeviceResponseCallback callback
);
159 void runcamDeviceClose5KeyOSDCableConnection(runcamDevice_t
*device
, rcdeviceResponseCallback callback
);
160 void runcamDeviceSimulate5KeyOSDCableButtonPress(runcamDevice_t
*device
, uint8_t operation
, rcdeviceResponseCallback callback
);
161 void runcamDeviceSimulate5KeyOSDCableButtonRelease(runcamDevice_t
*device
, rcdeviceResponseCallback callback
);