1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-17 21:35:44 +03:00

RC Split tidy

This commit is contained in:
Martin Budden 2017-09-25 05:55:46 +01:00
parent db8698d801
commit 9556c5b811
4 changed files with 27 additions and 47 deletions

View file

@ -714,6 +714,10 @@ void init(void)
LED2_ON; LED2_ON;
#endif #endif
#ifdef USE_RCSPLIT
rcSplitInit();
#endif // USE_RCSPLIT
// Latch active features AGAIN since some may be modified by init(). // Latch active features AGAIN since some may be modified by init().
latchActiveFeatures(); latchActiveFeatures();
pwmEnableMotors(); pwmEnableMotors();
@ -726,9 +730,5 @@ void init(void)
fcTasksInit(); fcTasksInit();
#endif #endif
#ifdef USE_RCSPLIT
rcSplitInit();
#endif // USE_RCSPLIT
systemState |= SYSTEM_STATE_READY; systemState |= SYSTEM_STATE_READY;
} }

View file

@ -368,6 +368,9 @@ void fcTasksInit(void)
#ifdef USE_CAMERA_CONTROL #ifdef USE_CAMERA_CONTROL
setTaskEnabled(TASK_CAMCTRL, true); setTaskEnabled(TASK_CAMCTRL, true);
#endif #endif
#ifdef USE_RCSPLIT
setTaskEnabled(TASK_RCSPLIT, rcSplitIsEnabled());
#endif
} }
#endif #endif
@ -614,7 +617,7 @@ cfTask_t cfTasks[TASK_COUNT] = {
#ifdef USE_RCSPLIT #ifdef USE_RCSPLIT
[TASK_RCSPLIT] = { [TASK_RCSPLIT] = {
.taskName = "RCSPLIT", .taskName = "RCSPLIT",
.taskFunc = rcSplitProcess, .taskFunc = rcSplitUpdate,
.desiredPeriod = TASK_PERIOD_HZ(10), // 10 Hz, 100ms .desiredPeriod = TASK_PERIOD_HZ(10), // 10 Hz, 100ms
.staticPriority = TASK_PRIORITY_MEDIUM, .staticPriority = TASK_PRIORITY_MEDIUM,
}, },

View file

@ -17,63 +17,48 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
#include <ctype.h>
#include <platform.h> #include <platform.h>
#include "common/utils.h" #include "common/utils.h"
#include "config/parameter_group.h"
#include "config/parameter_group_ids.h"
#include "drivers/serial.h"
#include "fc/rc_controls.h" #include "fc/rc_controls.h"
#include "fc/rc_modes.h" #include "fc/rc_modes.h"
#include "io/beeper.h"
#include "io/rcsplit.h" #include "io/rcsplit.h"
#include "io/serial.h" #include "io/serial.h"
#include "scheduler/scheduler.h"
// communicate with camera device variables // communicate with camera device variables
STATIC_UNIT_TESTED serialPort_t *rcSplitSerialPort = NULL; STATIC_UNIT_TESTED serialPort_t *rcSplitSerialPort = NULL;
STATIC_UNIT_TESTED rcsplitState_e cameraState = RCSPLIT_STATE_UNKNOWN;
// only for unit test // only for unit test
STATIC_UNIT_TESTED rcsplitSwitchState_t switchStates[BOXCAMERA3 - BOXCAMERA1 + 1]; STATIC_UNIT_TESTED rcsplitSwitchState_t switchStates[BOXCAMERA3 - BOXCAMERA1 + 1];
static uint8_t crc_high_first(uint8_t *ptr, uint8_t len) static uint8_t crc_high_first(uint8_t *ptr, uint8_t len)
{ {
uint8_t i; uint8_t crc = 0x00;
uint8_t crc=0x00;
while (len--) { while (len--) {
crc ^= *ptr++; crc ^= *ptr++;
for (i=8; i>0; --i) { for (int i=8; i>0; --i) {
if (crc & 0x80) if (crc & 0x80) {
crc = (crc << 1) ^ 0x31; crc = (crc << 1) ^ 0x31;
else } else {
crc = (crc << 1); crc = (crc << 1);
}
} }
} }
return (crc); return crc;
} }
static void sendCtrlCommand(rcsplit_ctrl_argument_e argument) static void sendCtrlCommand(rcsplit_ctrl_argument_e argument)
{ {
if (!rcSplitSerialPort)
return ;
uint8_t uart_buffer[5] = {0}; uint8_t uart_buffer[5] = {0};
uint8_t crc = 0;
uart_buffer[0] = RCSPLIT_PACKET_HEADER; uart_buffer[0] = RCSPLIT_PACKET_HEADER;
uart_buffer[1] = RCSPLIT_PACKET_CMD_CTRL; uart_buffer[1] = RCSPLIT_PACKET_CMD_CTRL;
uart_buffer[2] = argument; uart_buffer[2] = argument;
uart_buffer[3] = RCSPLIT_PACKET_TAIL; uart_buffer[3] = RCSPLIT_PACKET_TAIL;
crc = crc_high_first(uart_buffer, 4); uint8_t crc = crc_high_first(uart_buffer, 4);
// build up a full request [header]+[command]+[argument]+[crc]+[tail] // build up a full request [header]+[command]+[argument]+[crc]+[tail]
uart_buffer[3] = crc; uart_buffer[3] = crc;
@ -85,12 +70,8 @@ static void sendCtrlCommand(rcsplit_ctrl_argument_e argument)
static void rcSplitProcessMode(void) static void rcSplitProcessMode(void)
{ {
// if the device not ready, do not handle any mode change event
if (RCSPLIT_STATE_IS_READY != cameraState)
return ;
for (boxId_e i = BOXCAMERA1; i <= BOXCAMERA3; i++) { for (boxId_e i = BOXCAMERA1; i <= BOXCAMERA3; i++) {
uint8_t switchIndex = i - BOXCAMERA1; const uint8_t switchIndex = i - BOXCAMERA1;
if (IS_RC_MODE_ACTIVE(i)) { if (IS_RC_MODE_ACTIVE(i)) {
// check last state of this mode, if it's true, then ignore it. // check last state of this mode, if it's true, then ignore it.
// Here is a logic to make a toggle control for this mode // Here is a logic to make a toggle control for this mode
@ -124,6 +105,11 @@ static void rcSplitProcessMode(void)
} }
} }
bool rcSplitIsEnabled(void)
{
return rcSplitSerialPort ? true : false;
}
bool rcSplitInit(void) bool rcSplitInit(void)
{ {
// found the port config with FUNCTION_RUNCAM_SPLIT_CONTROL // found the port config with FUNCTION_RUNCAM_SPLIT_CONTROL
@ -143,22 +129,13 @@ bool rcSplitInit(void)
switchStates[switchIndex].isActivated = true; switchStates[switchIndex].isActivated = true;
} }
cameraState = RCSPLIT_STATE_IS_READY;
#ifdef USE_RCSPLIT
setTaskEnabled(TASK_RCSPLIT, true);
#endif
return true; return true;
} }
void rcSplitProcess(timeUs_t currentTimeUs) void rcSplitUpdate(timeUs_t currentTimeUs)
{ {
UNUSED(currentTimeUs); UNUSED(currentTimeUs);
if (rcSplitSerialPort == NULL)
return ;
// process rcsplit custom mode if has any changed // process rcsplit custom mode if has any changed
rcSplitProcessMode(); rcSplitProcessMode();
} }

View file

@ -19,9 +19,8 @@
#include <stdbool.h> #include <stdbool.h>
#include "common/time.h" #include "common/time.h"
#include "fc/fc_msp.h"
typedef struct { typedef struct rcsplitSwitchState_s {
bool isActivated; bool isActivated;
} rcsplitSwitchState_t; } rcsplitSwitchState_t;
@ -32,8 +31,8 @@ typedef enum {
} rcsplitState_e; } rcsplitState_e;
// packet header and tail // packet header and tail
#define RCSPLIT_PACKET_HEADER 0x55 #define RCSPLIT_PACKET_HEADER 0x55
#define RCSPLIT_PACKET_CMD_CTRL 0x01 #define RCSPLIT_PACKET_CMD_CTRL 0x01
#define RCSPLIT_PACKET_TAIL 0xaa #define RCSPLIT_PACKET_TAIL 0xaa
@ -47,4 +46,5 @@ typedef enum {
} rcsplit_ctrl_argument_e; } rcsplit_ctrl_argument_e;
bool rcSplitInit(void); bool rcSplitInit(void);
void rcSplitProcess(timeUs_t currentTimeUs); bool rcSplitIsEnabled(void);
void rcSplitUpdate(timeUs_t currentTimeUs);