From 9556c5b811ea3e8c237843e0eed5d1544f23cd14 Mon Sep 17 00:00:00 2001 From: Martin Budden Date: Mon, 25 Sep 2017 05:55:46 +0100 Subject: [PATCH 1/2] RC Split tidy --- src/main/fc/fc_init.c | 8 +++---- src/main/fc/fc_tasks.c | 5 ++++- src/main/io/rcsplit.c | 51 ++++++++++++------------------------------ src/main/io/rcsplit.h | 10 ++++----- 4 files changed, 27 insertions(+), 47 deletions(-) diff --git a/src/main/fc/fc_init.c b/src/main/fc/fc_init.c index ec8e011381..3f79412896 100644 --- a/src/main/fc/fc_init.c +++ b/src/main/fc/fc_init.c @@ -714,6 +714,10 @@ void init(void) LED2_ON; #endif +#ifdef USE_RCSPLIT + rcSplitInit(); +#endif // USE_RCSPLIT + // Latch active features AGAIN since some may be modified by init(). latchActiveFeatures(); pwmEnableMotors(); @@ -726,9 +730,5 @@ void init(void) fcTasksInit(); #endif -#ifdef USE_RCSPLIT - rcSplitInit(); -#endif // USE_RCSPLIT - systemState |= SYSTEM_STATE_READY; } diff --git a/src/main/fc/fc_tasks.c b/src/main/fc/fc_tasks.c index 2e21e54549..3af829bf9c 100644 --- a/src/main/fc/fc_tasks.c +++ b/src/main/fc/fc_tasks.c @@ -368,6 +368,9 @@ void fcTasksInit(void) #ifdef USE_CAMERA_CONTROL setTaskEnabled(TASK_CAMCTRL, true); #endif +#ifdef USE_RCSPLIT + setTaskEnabled(TASK_RCSPLIT, rcSplitIsEnabled()); +#endif } #endif @@ -614,7 +617,7 @@ cfTask_t cfTasks[TASK_COUNT] = { #ifdef USE_RCSPLIT [TASK_RCSPLIT] = { .taskName = "RCSPLIT", - .taskFunc = rcSplitProcess, + .taskFunc = rcSplitUpdate, .desiredPeriod = TASK_PERIOD_HZ(10), // 10 Hz, 100ms .staticPriority = TASK_PRIORITY_MEDIUM, }, diff --git a/src/main/io/rcsplit.c b/src/main/io/rcsplit.c index aada0181ab..a593890961 100644 --- a/src/main/io/rcsplit.c +++ b/src/main/io/rcsplit.c @@ -17,63 +17,48 @@ #include #include -#include -#include #include #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_modes.h" -#include "io/beeper.h" #include "io/rcsplit.h" #include "io/serial.h" -#include "scheduler/scheduler.h" - // communicate with camera device variables STATIC_UNIT_TESTED serialPort_t *rcSplitSerialPort = NULL; -STATIC_UNIT_TESTED rcsplitState_e cameraState = RCSPLIT_STATE_UNKNOWN; // only for unit test STATIC_UNIT_TESTED rcsplitSwitchState_t switchStates[BOXCAMERA3 - BOXCAMERA1 + 1]; 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--) { crc ^= *ptr++; - for (i=8; i>0; --i) { - if (crc & 0x80) + for (int i=8; i>0; --i) { + if (crc & 0x80) { crc = (crc << 1) ^ 0x31; - else + } else { crc = (crc << 1); + } } } - return (crc); + return crc; } static void sendCtrlCommand(rcsplit_ctrl_argument_e argument) { - if (!rcSplitSerialPort) - return ; - uint8_t uart_buffer[5] = {0}; - uint8_t crc = 0; uart_buffer[0] = RCSPLIT_PACKET_HEADER; uart_buffer[1] = RCSPLIT_PACKET_CMD_CTRL; uart_buffer[2] = argument; 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] uart_buffer[3] = crc; @@ -85,12 +70,8 @@ static void sendCtrlCommand(rcsplit_ctrl_argument_e argument) 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++) { - uint8_t switchIndex = i - BOXCAMERA1; + const uint8_t switchIndex = i - BOXCAMERA1; if (IS_RC_MODE_ACTIVE(i)) { // 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 @@ -124,6 +105,11 @@ static void rcSplitProcessMode(void) } } +bool rcSplitIsEnabled(void) +{ + return rcSplitSerialPort ? true : false; +} + bool rcSplitInit(void) { // found the port config with FUNCTION_RUNCAM_SPLIT_CONTROL @@ -143,22 +129,13 @@ bool rcSplitInit(void) switchStates[switchIndex].isActivated = true; } - cameraState = RCSPLIT_STATE_IS_READY; - -#ifdef USE_RCSPLIT - setTaskEnabled(TASK_RCSPLIT, true); -#endif - return true; } -void rcSplitProcess(timeUs_t currentTimeUs) +void rcSplitUpdate(timeUs_t currentTimeUs) { UNUSED(currentTimeUs); - if (rcSplitSerialPort == NULL) - return ; - // process rcsplit custom mode if has any changed rcSplitProcessMode(); } diff --git a/src/main/io/rcsplit.h b/src/main/io/rcsplit.h index 56f0efb913..7ec320fd7e 100644 --- a/src/main/io/rcsplit.h +++ b/src/main/io/rcsplit.h @@ -19,9 +19,8 @@ #include #include "common/time.h" -#include "fc/fc_msp.h" -typedef struct { +typedef struct rcsplitSwitchState_s { bool isActivated; } rcsplitSwitchState_t; @@ -32,8 +31,8 @@ typedef enum { } rcsplitState_e; // packet header and tail -#define RCSPLIT_PACKET_HEADER 0x55 -#define RCSPLIT_PACKET_CMD_CTRL 0x01 +#define RCSPLIT_PACKET_HEADER 0x55 +#define RCSPLIT_PACKET_CMD_CTRL 0x01 #define RCSPLIT_PACKET_TAIL 0xaa @@ -47,4 +46,5 @@ typedef enum { } rcsplit_ctrl_argument_e; bool rcSplitInit(void); -void rcSplitProcess(timeUs_t currentTimeUs); +bool rcSplitIsEnabled(void); +void rcSplitUpdate(timeUs_t currentTimeUs); From 170e2f5c07b558ec67768fb49790569e5c305e23 Mon Sep 17 00:00:00 2001 From: Martin Budden Date: Mon, 25 Sep 2017 07:58:30 +0100 Subject: [PATCH 2/2] Fixed test code --- src/test/unit/rcsplit_unittest.cc | 49 +++++++++++++------------------ 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/src/test/unit/rcsplit_unittest.cc b/src/test/unit/rcsplit_unittest.cc index fafa274013..3c8ac3ca8a 100644 --- a/src/test/unit/rcsplit_unittest.cc +++ b/src/test/unit/rcsplit_unittest.cc @@ -25,9 +25,10 @@ extern "C" { #include "platform.h" - #include "common/utils.h" - #include "common/maths.h" #include "common/bitarray.h" + #include "common/maths.h" + #include "common/streambuf.h" + #include "common/utils.h" #include "config/parameter_group.h" #include "config/parameter_group_ids.h" @@ -37,25 +38,16 @@ extern "C" { #include "io/beeper.h" - #include "io/serial.h" - - #include "scheduler/scheduler.h" - #include "drivers/serial.h" #include "io/rcsplit.h" + #include "io/serial.h" #include "rx/rx.h" - extern rcsplitState_e cameraState; extern serialPort_t *rcSplitSerialPort; extern rcsplitSwitchState_t switchStates[BOXCAMERA3 - BOXCAMERA1 + 1]; int16_t rcData[MAX_SUPPORTED_RC_CHANNEL_COUNT]; // interval [1000;2000] - rcsplitState_e unitTestRCsplitState() - { - return cameraState; - } - bool unitTestIsSwitchActivited(boxId_e boxId) { uint8_t adjustBoxID = boxId - BOXCAMERA1; @@ -66,7 +58,6 @@ extern "C" { void unitTestResetRCSplit() { rcSplitSerialPort = NULL; - cameraState = RCSPLIT_STATE_UNKNOWN; } } @@ -85,7 +76,7 @@ TEST(RCSplitTest, TestRCSplitInitWithoutPortConfigurated) unitTestResetRCSplit(); bool result = rcSplitInit(); EXPECT_EQ(false, result); - EXPECT_EQ(RCSPLIT_STATE_UNKNOWN, unitTestRCsplitState()); + EXPECT_EQ(false, rcSplitIsEnabled()); } TEST(RCSplitTest, TestRCSplitInitWithoutOpenPortConfigurated) @@ -97,7 +88,7 @@ TEST(RCSplitTest, TestRCSplitInitWithoutOpenPortConfigurated) bool result = rcSplitInit(); EXPECT_EQ(false, result); - EXPECT_EQ(RCSPLIT_STATE_UNKNOWN, unitTestRCsplitState()); + EXPECT_EQ(false, rcSplitIsEnabled()); } TEST(RCSplitTest, TestRCSplitInit) @@ -109,7 +100,7 @@ TEST(RCSplitTest, TestRCSplitInit) bool result = rcSplitInit(); EXPECT_EQ(true, result); - EXPECT_EQ(RCSPLIT_STATE_IS_READY, unitTestRCsplitState()); + EXPECT_EQ(true, rcSplitIsEnabled()); } TEST(RCSplitTest, TestRecvWhoAreYouResponse) @@ -126,9 +117,9 @@ TEST(RCSplitTest, TestRecvWhoAreYouResponse) // so the "who are you response" will full received, and cause the state change to RCSPLIT_STATE_IS_READY; int8_t randNum = rand() % 127 + 6; testData.maxTimesOfRespDataAvailable = randNum; - rcSplitProcess((timeUs_t)0); + rcSplitUpdate((timeUs_t)0); - EXPECT_EQ(RCSPLIT_STATE_IS_READY, unitTestRCsplitState()); + EXPECT_EQ(true, rcSplitIsEnabled()); } TEST(RCSplitTest, TestWifiModeChangeWithDeviceUnready) @@ -172,8 +163,8 @@ TEST(RCSplitTest, TestWifiModeChangeWithDeviceUnready) updateActivatedModes(); - // runn process loop - rcSplitProcess(0); + // run update + rcSplitUpdate(0); EXPECT_EQ(false, unitTestIsSwitchActivited(BOXCAMERA1)); EXPECT_EQ(false, unitTestIsSwitchActivited(BOXCAMERA2)); @@ -221,12 +212,12 @@ TEST(RCSplitTest, TestWifiModeChangeWithDeviceReady) updateActivatedModes(); - // runn process loop + // run update int8_t randNum = rand() % 127 + 6; testData.maxTimesOfRespDataAvailable = randNum; - rcSplitProcess((timeUs_t)0); + rcSplitUpdate((timeUs_t)0); - EXPECT_EQ(RCSPLIT_STATE_IS_READY, unitTestRCsplitState()); + EXPECT_EQ(true, rcSplitIsEnabled()); EXPECT_EQ(false, unitTestIsSwitchActivited(BOXCAMERA1)); EXPECT_EQ(true, unitTestIsSwitchActivited(BOXCAMERA2)); @@ -274,12 +265,12 @@ TEST(RCSplitTest, TestWifiModeChangeCombine) rcData[modeActivationConditions(2)->auxChannelIndex + NON_AUX_CHANNEL_COUNT] = 1700; updateActivatedModes(); - // runn process loop + // run update int8_t randNum = rand() % 127 + 6; testData.maxTimesOfRespDataAvailable = randNum; - rcSplitProcess((timeUs_t)0); + rcSplitUpdate((timeUs_t)0); - EXPECT_EQ(RCSPLIT_STATE_IS_READY, unitTestRCsplitState()); + EXPECT_EQ(true, rcSplitIsEnabled()); EXPECT_EQ(false, unitTestIsSwitchActivited(BOXCAMERA1)); EXPECT_EQ(true, unitTestIsSwitchActivited(BOXCAMERA2)); @@ -291,7 +282,7 @@ TEST(RCSplitTest, TestWifiModeChangeCombine) rcData[modeActivationConditions(1)->auxChannelIndex + NON_AUX_CHANNEL_COUNT] = 1300; rcData[modeActivationConditions(2)->auxChannelIndex + NON_AUX_CHANNEL_COUNT] = 1900; updateActivatedModes(); - rcSplitProcess((timeUs_t)0); + rcSplitUpdate((timeUs_t)0); EXPECT_EQ(true, unitTestIsSwitchActivited(BOXCAMERA1)); EXPECT_EQ(false, unitTestIsSwitchActivited(BOXCAMERA2)); EXPECT_EQ(true, unitTestIsSwitchActivited(BOXCAMERA3)); @@ -299,12 +290,12 @@ TEST(RCSplitTest, TestWifiModeChangeCombine) rcData[modeActivationConditions(2)->auxChannelIndex + NON_AUX_CHANNEL_COUNT] = 1899; updateActivatedModes(); - rcSplitProcess((timeUs_t)0); + rcSplitUpdate((timeUs_t)0); EXPECT_EQ(false, unitTestIsSwitchActivited(BOXCAMERA3)); rcData[modeActivationConditions(1)->auxChannelIndex + NON_AUX_CHANNEL_COUNT] = 2001; updateActivatedModes(); - rcSplitProcess((timeUs_t)0); + rcSplitUpdate((timeUs_t)0); EXPECT_EQ(true, unitTestIsSwitchActivited(BOXCAMERA1)); EXPECT_EQ(true, unitTestIsSwitchActivited(BOXCAMERA2)); EXPECT_EQ(false, unitTestIsSwitchActivited(BOXCAMERA3));