From b223f95bdf2557136d72bfacadeedf89b158ce99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fukan=20Korkmazt=C3=BCrk?= Date: Wed, 27 Sep 2023 21:23:18 -0400 Subject: [PATCH] Update the packet submission logic Packet submission logic is updated to provide a lower channel update latency. --- src/src/rx-serial/SerialSatellite.cpp | 44 +++++++++++++++------------ src/src/rx-serial/SerialSatellite.h | 9 +++--- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/src/rx-serial/SerialSatellite.cpp b/src/src/rx-serial/SerialSatellite.cpp index 294fa0e9a8..2976e4e1f6 100644 --- a/src/src/rx-serial/SerialSatellite.cpp +++ b/src/src/rx-serial/SerialSatellite.cpp @@ -65,29 +65,35 @@ uint32_t SerialSatellite::sendRCFrame(bool frameAvailable, uint32_t *channelData } sendPackets = true; - uint16_t outgoingPacket[SATELLITE_CHANNEL_DATA_LENGTH]; - for (uint8_t ii = 0; ii < SATELLITE_CHANNEL_DATA_LENGTH; ++ii) + uint16_t outgoingPacket[SATELLITE_CHANNELS_PER_PACKET]; + + // Our bandwidth is limited, so try to send updates only for the channels + // with new values to reduce the channel update latency. Updates are + // checked in round-robin fashion to make sure that the channels do not + // starve. + for (uint8_t channelCount = 0, packetDataCount = 0; + (channelCount < SATELLITE_MAX_CHANNELS) && (packetDataCount < SATELLITE_CHANNELS_PER_PACKET); + ++channelCount) { - // These channels are sent in every packet - if (ii < SATELLITE_FIRST_RR_CHANNEL) + // If the channel data is updated, add it to the packet. Or, if the + // space remaining in the packet is equal to the number of remaining + // channels to be checked for updates, just fill the packet with the + // remaining channels no matter if they have updates or not because we + // need to send exactly 7 channel data in every packet. + if ((prevChannelData[roundRobinIndex] != channelData[roundRobinIndex]) || + ((SATELLITE_CHANNELS_PER_PACKET - packetDataCount) == (SATELLITE_MAX_CHANNELS - channelCount))) { - outgoingPacket[ii] = crsfToSatellite(channelData[ii], - CRSF_TO_SATELLITE_CH_MAP[ii], - satelliteSystem); + prevChannelData[roundRobinIndex] = channelData[roundRobinIndex]; + outgoingPacket[packetDataCount++] = + crsfToSatellite(channelData[roundRobinIndex], + CRSF_TO_SATELLITE_CH_MAP[roundRobinIndex], + satelliteSystem); } - // Round-robin channels - else + + ++roundRobinIndex; + if (roundRobinIndex >= SATELLITE_MAX_CHANNELS) { - outgoingPacket[ii] = crsfToSatellite(channelData[rr], - CRSF_TO_SATELLITE_CH_MAP[rr], - satelliteSystem); - - // Update the round-robin index - ++rr; - if (rr >= SATELLITE_NUM_CHANNELS) - { - rr = SATELLITE_FIRST_RR_CHANNEL; - } + roundRobinIndex = 0; } } diff --git a/src/src/rx-serial/SerialSatellite.h b/src/src/rx-serial/SerialSatellite.h index 6e63d62c4e..46171a836d 100644 --- a/src/src/rx-serial/SerialSatellite.h +++ b/src/src/rx-serial/SerialSatellite.h @@ -13,13 +13,14 @@ class SerialSatellite : public SerialIO uint32_t sendRCFrame(bool frameAvailable, uint32_t *channelData) override; private: - static constexpr uint16_t SATELLITE_NUM_CHANNELS = 12; - static constexpr uint8_t SATELLITE_FIRST_RR_CHANNEL = 4; - static constexpr uint8_t SATELLITE_CHANNEL_DATA_LENGTH = 7; + static constexpr uint16_t SATELLITE_MAX_CHANNELS = 12; + static constexpr uint8_t SATELLITE_CHANNELS_PER_PACKET = 7; void processBytes(uint8_t *bytes, uint16_t size) override{}; + uint32_t prevChannelData[SATELLITE_CHANNELS_PER_PACKET] = {0}; + uint8_t fadeCount{0}; - uint8_t rr{SATELLITE_FIRST_RR_CHANNEL}; + uint8_t roundRobinIndex{0}; bool sendPackets{false}; }; \ No newline at end of file