@@ -65,29 +65,52 @@ uint32_t SerialSatellite::sendRCFrame(bool frameAvailable, uint32_t *channelData
65
65
}
66
66
sendPackets = true ;
67
67
68
- uint16_t outgoingPacket[SATELLITE_CHANNEL_DATA_LENGTH];
69
- for (uint8_t ii = 0 ; ii < SATELLITE_CHANNEL_DATA_LENGTH; ++ii)
68
+ uint16_t outgoingPacket[SATELLITE_CHANNELS_PER_PACKET];
69
+ uint8_t outgoingPacketIndex = 0 ;
70
+ const auto roundRobinIndexStartValue = roundRobinIndex;
71
+
72
+ // Our bandwidth is limited, so try to send updates only for the channels
73
+ // with new values to reduce the channel update latency. Updates are
74
+ // checked in round-robin fashion to make sure that the channels do not
75
+ // starve.
76
+ do
70
77
{
71
- // These channels are sent in every packet
72
- if (ii < SATELLITE_FIRST_RR_CHANNEL)
78
+ if (prevChannelData[roundRobinIndex] != channelData[roundRobinIndex])
73
79
{
74
- outgoingPacket[ii] = crsfToSatellite (channelData[ii],
75
- CRSF_TO_SATELLITE_CH_MAP[ii],
76
- satelliteSystem);
80
+ prevChannelData[roundRobinIndex] = channelData[roundRobinIndex];
81
+ outgoingPacket[outgoingPacketIndex++] =
82
+ crsfToSatellite (channelData[roundRobinIndex],
83
+ CRSF_TO_SATELLITE_CH_MAP[roundRobinIndex],
84
+ satelliteSystem);
85
+ // Mark the channel as "sent", so we wouldn't submit it more than
86
+ // once in a packet.
87
+ channelData[roundRobinIndex] = UINT32_MAX;
77
88
}
78
- // Round-robin channels
79
- else
89
+
90
+ ++roundRobinIndex;
91
+ if (roundRobinIndex >= SATELLITE_MAX_CHANNELS)
92
+ {
93
+ roundRobinIndex = 0 ;
94
+ }
95
+ if (outgoingPacketIndex >= SATELLITE_CHANNELS_PER_PACKET)
96
+ {
97
+ break ;
98
+ }
99
+ } while (roundRobinIndex != roundRobinIndexStartValue);
100
+
101
+ // If less than 7 channel values have been changed since the last update,
102
+ // fill the rest of the packet with data from arbitrary channels.
103
+ for (uint8_t index = 0 ;
104
+ (outgoingPacketIndex < SATELLITE_CHANNELS_PER_PACKET) && (index < SATELLITE_MAX_CHANNELS);
105
+ ++index)
106
+ {
107
+ // If the channel is not marked as "sent", add it to the packet.
108
+ if (channelData[index] != UINT32_MAX)
80
109
{
81
- outgoingPacket[ii] = crsfToSatellite (channelData[rr],
82
- CRSF_TO_SATELLITE_CH_MAP[rr],
83
- satelliteSystem);
84
-
85
- // Update the round-robin index
86
- ++rr;
87
- if (rr >= SATELLITE_NUM_CHANNELS)
88
- {
89
- rr = SATELLITE_FIRST_RR_CHANNEL;
90
- }
110
+ outgoingPacket[outgoingPacketIndex++] =
111
+ crsfToSatellite (channelData[index],
112
+ CRSF_TO_SATELLITE_CH_MAP[index],
113
+ satelliteSystem);
91
114
}
92
115
}
93
116
0 commit comments