68 lines
2.1 KiB
C++
68 lines
2.1 KiB
C++
/*
|
|
* Copyright (C) OpenTX
|
|
*
|
|
* Based on code named
|
|
* th9x - http://code.google.com/p/th9x
|
|
* er9x - http://code.google.com/p/er9x
|
|
* gruvin9x - http://code.google.com/p/gruvin9x
|
|
*
|
|
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
* published by the Free Software Foundation.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*/
|
|
|
|
#include "opentx.h"
|
|
|
|
#define CROSSFIRE_CH_CENTER 0x3E0
|
|
#define CROSSFIRE_CH_BITS 11
|
|
|
|
// Range for pulses (channels output) is [-1024:+1024]
|
|
uint8_t createCrossfireChannelsFrame(uint8_t * frame, int16_t * pulses)
|
|
{
|
|
uint8_t * buf = frame;
|
|
*buf++ = MODULE_ADDRESS;
|
|
*buf++ = 24; // 1(ID) + 22 + 1(CRC)
|
|
uint8_t * crc_start = buf;
|
|
*buf++ = CHANNELS_ID;
|
|
uint32_t bits = 0;
|
|
uint8_t bitsavailable = 0;
|
|
for (int i=0; i<CROSSFIRE_CHANNELS_COUNT; i++) {
|
|
uint32_t val = limit(0, CROSSFIRE_CH_CENTER + (((pulses[i]) * 4) / 5), 2*CROSSFIRE_CH_CENTER);
|
|
bits |= val << bitsavailable;
|
|
bitsavailable += CROSSFIRE_CH_BITS;
|
|
while (bitsavailable >= 8) {
|
|
*buf++ = bits;
|
|
bits >>= 8;
|
|
bitsavailable -= 8;
|
|
}
|
|
}
|
|
*buf++ = crc8(crc_start, 23);
|
|
return buf - frame;
|
|
}
|
|
|
|
void setupPulsesCrossfire()
|
|
{
|
|
if (telemetryProtocol == PROTOCOL_TELEMETRY_CROSSFIRE) {
|
|
uint8_t * pulses = extmodulePulsesData.crossfire.pulses;
|
|
#if defined(LUA)
|
|
if (outputTelemetryBufferTrigger != 0x00 && outputTelemetryBufferSize > 0) {
|
|
memcpy(pulses, outputTelemetryBuffer, outputTelemetryBufferSize);
|
|
extmodulePulsesData.crossfire.length = outputTelemetryBufferSize;
|
|
outputTelemetryBufferTrigger = 0x00;
|
|
outputTelemetryBufferSize = 0;
|
|
}
|
|
else
|
|
#endif
|
|
{
|
|
extmodulePulsesData.crossfire.length = createCrossfireChannelsFrame(pulses, &channelOutputs[g_model.moduleData[EXTERNAL_MODULE].channelsStart]);
|
|
}
|
|
}
|
|
}
|