-
-
Notifications
You must be signed in to change notification settings - Fork 347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable serial trainer for foreign protocols (Ibus/SumD) #1462
Closed
Closed
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
5df9806
first version
wimalopaan 67d24db
WIP: intermediate commit
wimalopaan 886a06a
fix taranis
wimalopaan 51dbaae
fix translations
wimalopaan e7bfec0
fixed use of 1ms tick
wimalopaan e416f16
do not call SBus::tick1ms() if not SBUS enabled
wimalopaan c0cbf9a
restored switches.cpp
wimalopaan 119d5a9
- cleaned code
wimalopaan c7b6173
wip: just compiles after rebase
wimalopaan 8a912d9
only allow one trainer-input via serial at a time
wimalopaan 4c4c6c1
fixed board.h (removed merge marker)
wimalopaan a4fc851
* removed IBUS trainer via ext module bay
wimalopaan a9c6615
after serial refactor get it working again
wimalopaan fce5bd3
added CRSF trainer input
wimalopaan cbe064a
changed baudrate to 420KB for CRSF trainer input
wimalopaan b53877d
fixed silly mistakes decoding CRSF
wimalopaan 1c22761
fixes the freeze problem on startup
wimalopaan 5cfb85e
follow-up to previous serial rewrite adaption (taranis radios)
wimalopaan d9fb50c
WIP for SumDV1/V3
wimalopaan c8d3d7f
refactored protocol adapters (sbus, ibus, crsf, sumd)
wimalopaan 67ab677
fixed taranis problems
wimalopaan 34cd38a
Now also SumDV3 fully functional and working
wimalopaan aa1a4d9
* refactored sbus/crsf/ibus/sumd v1/v3 trainer input
wimalopaan f2ddc71
fixed smaller radios w/o BT
wimalopaan 4ae789a
NV14 fix
wimalopaan 396de77
fixed simu and gtests
wimalopaan 6198be3
refactor class templates
wimalopaan cd7b87c
fixed lua api
wimalopaan c3339c8
improved LS switch handling
wimalopaan ba6fc3d
added EXTENDED_TARINER for X9e
wimalopaan 9eeb1ee
* little cleanup
wimalopaan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include "crsf.h" | ||
|
||
void crsfTrainerPauseCheck() { | ||
#if !defined(SIMU) | ||
# if defined(AUX_SERIAL) || defined(AUX2_SERIAL) | ||
if (hasSerialMode(UART_MODE_CRSF_TRAINER) >= 0) { | ||
CRSF::Servo<0>::tick1ms(); | ||
processCrsfInput(); | ||
} | ||
# endif | ||
#endif | ||
} | ||
|
||
void processCrsfInput() { | ||
#if !defined(SIMU) | ||
uint8_t rxchar; | ||
|
||
while (sbusAuxGetByte(&rxchar)) { | ||
CRSF::Servo<0>::process(rxchar, [&](){ | ||
CRSF::Servo<0>::convert(ppmInput); | ||
ppmInputValidityTimer = PPM_IN_VALID_TIMEOUT; | ||
}); | ||
} | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
#pragma once | ||
|
||
#include "opentx.h" | ||
#include "trainer.h" | ||
#include "crc.h" | ||
|
||
#include <algorithm> | ||
#include <limits> | ||
|
||
void processCrsfInput(); | ||
void crsfTrainerPauseCheck(); | ||
|
||
#ifdef __GNUC__ | ||
# pragma GCC diagnostic push | ||
# pragma GCC diagnostic error "-Wswitch" // unfortunately the project uses -Wnoswitch | ||
#endif | ||
|
||
namespace CRSF { | ||
static constexpr uint32_t baudrate{420000}; | ||
|
||
template<uint8_t Instance> | ||
struct Servo { | ||
using Crsf = Trainer::Protocol::Crsf; | ||
using MesgType = Crsf::MesgType; | ||
|
||
static constexpr uint8_t mPauseCount{2}; // 2ms | ||
|
||
static constexpr uint8_t CRSFChannels{16}; | ||
|
||
enum class State : uint8_t {Undefined, GotFCAddress, GotLength, Channels, Data, AwaitCRC, AwaitCRCAndDecode}; | ||
|
||
static inline int16_t convertCrsfToPuls(uint16_t const value) { | ||
const int16_t centered = value - Crsf::CenterValue; | ||
return Trainer::clamp((centered * 5) / 8); | ||
} | ||
|
||
static inline void tick1ms() { | ||
if (mPauseCounter > 0) { | ||
--mPauseCounter; | ||
} | ||
else { | ||
mState = State::Undefined; | ||
} | ||
} | ||
|
||
static inline void process(const uint8_t b, const std::function<void()> f) { | ||
mPauseCounter = mPauseCount; | ||
++mBytesCounter; | ||
switch(mState) { // enum-switch -> no default (intentional) | ||
case State::Undefined: | ||
csum.reset(); | ||
if (b == Crsf::FcAddress) { | ||
mState = State::GotFCAddress; | ||
} | ||
break; | ||
case State::GotFCAddress: | ||
if ((b > 2) && (b <= mData.size())) { | ||
mLength = b - 2; // only payload (not including type and crc) | ||
mIndex = 0; | ||
mState = State::GotLength; | ||
} | ||
else { | ||
mState = State::Undefined; | ||
} | ||
break; | ||
case State::GotLength: | ||
csum += b; | ||
if ((b == Crsf::FrametypeChannels) && (mLength == 22)) { | ||
mState = State::Channels; | ||
} | ||
else { | ||
mState = State::Data; | ||
} | ||
break; | ||
case State::Data: | ||
csum += b; | ||
if (++mIndex >= mLength) { | ||
mState = State::AwaitCRC; | ||
} | ||
break; | ||
case State::Channels: | ||
csum += b; | ||
mData[mIndex] = b; | ||
if (++mIndex >= mLength) { | ||
mState = State::AwaitCRCAndDecode; | ||
} | ||
break; | ||
case State::AwaitCRC: | ||
if (csum == b) { | ||
// only channel data is decoded, nothing todo here | ||
} | ||
mState = State::Undefined; | ||
break; | ||
case State::AwaitCRCAndDecode: | ||
if (csum == b) { | ||
++mPackagesCounter; | ||
f(); | ||
} | ||
mState = State::Undefined; | ||
break; | ||
} | ||
} | ||
template<uint8_t N> | ||
static inline void convert(int16_t (&pulses)[N]) { | ||
static_assert(N >= 16, "array too small"); | ||
pulses[0] = (uint16_t) (((mData[0] | mData[1] << 8)) & Crsf::ValueMask); | ||
pulses[1] = (uint16_t) ((mData[1]>>3 | mData[2] <<5) & Crsf::ValueMask); | ||
pulses[2] = (uint16_t) ((mData[2]>>6 | mData[3] <<2 | mData[4]<<10) & Crsf::ValueMask); | ||
pulses[3] = (uint16_t) ((mData[4]>>1 | mData[5] <<7) & Crsf::ValueMask); | ||
pulses[4] = (uint16_t) ((mData[5]>>4 | mData[6] <<4) & Crsf::ValueMask); | ||
pulses[5] = (uint16_t) ((mData[6]>>7 | mData[7] <<1 | mData[8]<<9) & Crsf::ValueMask); | ||
pulses[6] = (uint16_t) ((mData[8]>>2 | mData[9] <<6) & Crsf::ValueMask); | ||
pulses[7] = (uint16_t) ((mData[9]>>5 | mData[10]<<3) & Crsf::ValueMask); | ||
pulses[8] = (uint16_t) ((mData[11] | mData[12]<<8) & Crsf::ValueMask); | ||
pulses[9] = (uint16_t) ((mData[12]>>3 | mData[13]<<5) & Crsf::ValueMask); | ||
pulses[10] = (uint16_t) ((mData[13]>>6 | mData[14]<<2 | mData[15]<<10) & Crsf::ValueMask); | ||
pulses[11] = (uint16_t) ((mData[15]>>1 | mData[16]<<7) & Crsf::ValueMask); | ||
pulses[12] = (uint16_t) ((mData[16]>>4 | mData[17]<<4) & Crsf::ValueMask); | ||
pulses[13] = (uint16_t) ((mData[17]>>7 | mData[18]<<1 | mData[19]<<9) & Crsf::ValueMask); | ||
pulses[14] = (uint16_t) ((mData[19]>>2 | mData[20]<<6) & Crsf::ValueMask); | ||
pulses[15] = (uint16_t) ((mData[20]>>5 | mData[21]<<3) & Crsf::ValueMask); | ||
|
||
for(size_t i = 0; i < N; ++i) { | ||
pulses[i] = convertCrsfToPuls(pulses[i]); | ||
} | ||
} | ||
static inline uint16_t packages() { | ||
return mPackagesCounter; | ||
} | ||
static inline uint16_t getBytes() { | ||
return mBytesCounter; | ||
} | ||
private: | ||
static CRC8 csum; | ||
static State mState; | ||
static MesgType mData; | ||
static uint8_t mIndex; | ||
static uint8_t mLength; | ||
static uint16_t mPackagesCounter; | ||
static uint16_t mBytesCounter; | ||
static uint8_t mPauseCounter; | ||
}; | ||
// inline static member definitions not until c++17 | ||
template<uint8_t Instance> | ||
CRC8 Servo<Instance>::csum; | ||
template<uint8_t Instance> | ||
typename Servo<Instance>::State Servo<Instance>::mState{Servo::State::Undefined}; | ||
template<uint8_t Instance> | ||
typename Servo<Instance>::MesgType Servo<Instance>::mData; | ||
template<uint8_t Instance> | ||
uint8_t Servo<Instance>::mIndex{0}; | ||
template<uint8_t Instance> | ||
uint8_t Servo<Instance>::mLength{0}; | ||
template<uint8_t Instance> | ||
uint16_t Servo<Instance>::mPackagesCounter{0}; | ||
template<uint8_t Instance> | ||
uint16_t Servo<Instance>::mBytesCounter{0}; | ||
template<uint8_t Instance> | ||
uint8_t Servo<Instance>::mPauseCounter{Servo::mPauseCount}; // 2 ms | ||
} | ||
#ifdef __GNUC__ | ||
# pragma GCC diagnostic pop | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should group these under
UART_MODE_SERIAL_TRAINER
, and have an additional parameter to select the protocol.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hestitated doing that because that means more changes (gui-logic, yaml, ...)