Refactoring. CAUTION: ERASE your EEPROM again!

This commit is contained in:
Bertrand Songis
2019-02-22 13:15:23 +01:00
parent 62f8e36d90
commit e567643652
12 changed files with 197 additions and 146 deletions
+3 -3
View File
@@ -337,10 +337,10 @@ void setCustomFailsafe(uint8_t moduleIndex)
if (moduleIndex < NUM_MODULES) {
for (int ch=0; ch<MAX_OUTPUT_CHANNELS; ch++) {
if (ch < g_model.moduleData[moduleIndex].channelsStart || ch >= sentModuleChannels(moduleIndex) + g_model.moduleData[moduleIndex].channelsStart) {
g_model.moduleData[moduleIndex].failsafeChannels[ch] = 0;
g_model.failsafeChannels[ch] = 0;
}
else if (g_model.moduleData[moduleIndex].failsafeChannels[ch] < FAILSAFE_CHANNEL_HOLD) {
g_model.moduleData[moduleIndex].failsafeChannels[ch] = channelOutputs[ch];
else if (g_model.failsafeChannels[ch] < FAILSAFE_CHANNEL_HOLD) {
g_model.failsafeChannels[ch] = channelOutputs[ch];
}
}
}
-101
View File
@@ -1,101 +0,0 @@
/*
* 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"
template <class PxxTransport>
void PxxPulses<PxxTransport>::addChannels(uint8_t port, uint8_t sendFailsafe, uint8_t sendUpperChannels)
{
uint16_t pulseValue = 0;
uint16_t pulseValueLow = 0;
for (int8_t i=0; i<8; i++) {
if (sendFailsafe) {
if (g_model.moduleData[port].failsafeMode == FAILSAFE_HOLD) {
pulseValue = (i < sendUpperChannels ? 4095 : 2047);
}
else if (g_model.moduleData[port].failsafeMode == FAILSAFE_NOPULSES) {
pulseValue = (i < sendUpperChannels ? 2048 : 0);
}
else {
if (i < sendUpperChannels) {
int16_t failsafeValue = g_model.moduleData[port].failsafeChannels[8+i];
if (failsafeValue == FAILSAFE_CHANNEL_HOLD) {
pulseValue = 4095;
}
else if (failsafeValue == FAILSAFE_CHANNEL_NOPULSE) {
pulseValue = 2048;
}
else {
failsafeValue += 2*PPM_CH_CENTER(8+g_model.moduleData[port].channelsStart+i) - 2*PPM_CENTER;
pulseValue = limit(2049, (failsafeValue * 512 / 682) + 3072, 4094);
}
}
else {
int16_t failsafeValue = g_model.moduleData[port].failsafeChannels[i];
if (failsafeValue == FAILSAFE_CHANNEL_HOLD) {
pulseValue = 2047;
}
else if (failsafeValue == FAILSAFE_CHANNEL_NOPULSE) {
pulseValue = 0;
}
else {
failsafeValue += 2*PPM_CH_CENTER(g_model.moduleData[port].channelsStart+i) - 2*PPM_CENTER;
pulseValue = limit(1, (failsafeValue * 512 / 682) + 1024, 2046);
}
}
}
}
else {
if (i < sendUpperChannels) {
int channel = 8 + g_model.moduleData[port].channelsStart + i;
int value = channelOutputs[channel] + 2*PPM_CH_CENTER(channel) - 2*PPM_CENTER;
pulseValue = limit(2049, (value * 512 / 682) + 3072, 4094);
}
else if (i < sentModuleChannels(port)) {
int channel = g_model.moduleData[port].channelsStart + i;
int value = channelOutputs[channel] + 2*PPM_CH_CENTER(channel) - 2*PPM_CENTER;
pulseValue = limit(1, (value * 512 / 682) + 1024, 2046);
}
else {
pulseValue = 1024;
}
}
if (i & 1) {
PxxTransport::addByte(pulseValueLow); // Low byte of channel
PxxTransport::addByte(((pulseValueLow >> 8) & 0x0F) | (pulseValue << 4)); // 4 bits each from 2 channels
PxxTransport::addByte(pulseValue >> 4); // High byte of channel
}
else {
pulseValueLow = pulseValue;
}
}
}
#if defined(PXX1)
template class PxxPulses<StandardPxx1Transport<PwmPxxBitTransport> >;
template class PxxPulses<StandardPxx1Transport<SerialPxxBitTransport> >;
template class PxxPulses<UartPxx1Transport>;
#endif
#if defined(PXX2)
template class PxxPulses<Pxx2Transport>;
#endif
+71 -1
View File
@@ -110,6 +110,76 @@ void Pxx1Pulses<PxxTransport>::addExtraFlags(uint8_t module)
PxxTransport::addByte(extra_flags);
}
template <class PxxTransport>
void Pxx1Pulses<PxxTransport>::addChannels(uint8_t port, uint8_t sendFailsafe, uint8_t sendUpperChannels)
{
uint16_t pulseValue = 0;
uint16_t pulseValueLow = 0;
for (int8_t i=0; i<8; i++) {
if (sendFailsafe) {
if (g_model.moduleData[port].failsafeMode == FAILSAFE_HOLD) {
pulseValue = (i < sendUpperChannels ? 4095 : 2047);
}
else if (g_model.moduleData[port].failsafeMode == FAILSAFE_NOPULSES) {
pulseValue = (i < sendUpperChannels ? 2048 : 0);
}
else {
if (i < sendUpperChannels) {
int16_t failsafeValue = g_model.failsafeChannels[8+i];
if (failsafeValue == FAILSAFE_CHANNEL_HOLD) {
pulseValue = 4095;
}
else if (failsafeValue == FAILSAFE_CHANNEL_NOPULSE) {
pulseValue = 2048;
}
else {
failsafeValue += 2*PPM_CH_CENTER(8+g_model.moduleData[port].channelsStart+i) - 2*PPM_CENTER;
pulseValue = limit(2049, (failsafeValue * 512 / 682) + 3072, 4094);
}
}
else {
int16_t failsafeValue = g_model.failsafeChannels[i];
if (failsafeValue == FAILSAFE_CHANNEL_HOLD) {
pulseValue = 2047;
}
else if (failsafeValue == FAILSAFE_CHANNEL_NOPULSE) {
pulseValue = 0;
}
else {
failsafeValue += 2*PPM_CH_CENTER(g_model.moduleData[port].channelsStart+i) - 2*PPM_CENTER;
pulseValue = limit(1, (failsafeValue * 512 / 682) + 1024, 2046);
}
}
}
}
else {
if (i < sendUpperChannels) {
int channel = 8 + g_model.moduleData[port].channelsStart + i;
int value = channelOutputs[channel] + 2*PPM_CH_CENTER(channel) - 2*PPM_CENTER;
pulseValue = limit(2049, (value * 512 / 682) + 3072, 4094);
}
else if (i < sentModuleChannels(port)) {
int channel = g_model.moduleData[port].channelsStart + i;
int value = channelOutputs[channel] + 2*PPM_CH_CENTER(channel) - 2*PPM_CENTER;
pulseValue = limit(1, (value * 512 / 682) + 1024, 2046);
}
else {
pulseValue = 1024;
}
}
if (i & 1) {
PxxTransport::addByte(pulseValueLow); // Low byte of channel
PxxTransport::addByte(((pulseValueLow >> 8) & 0x0F) | (pulseValue << 4)); // 4 bits each from 2 channels
PxxTransport::addByte(pulseValue >> 4); // High byte of channel
}
else {
pulseValueLow = pulseValue;
}
}
}
template <class PxxTransport>
void Pxx1Pulses<PxxTransport>::add8ChannelsFrame(uint8_t module, uint8_t sendUpperChannels)
{
@@ -128,7 +198,7 @@ void Pxx1Pulses<PxxTransport>::add8ChannelsFrame(uint8_t module, uint8_t sendUpp
PxxTransport::addByte(0);
// Channels
PxxPulses<PxxTransport>::addChannels(module, flag1 & PXX_SEND_FAILSAFE, sendUpperChannels);
addChannels(module, flag1 & PXX_SEND_FAILSAFE, sendUpperChannels);
// Extra flags
addExtraFlags(module);
+1 -1
View File
@@ -156,7 +156,7 @@ class Pxx1Pulses: public PxxPulses<PxxTransport>
uint8_t addFlag1(uint8_t port);
void addExtraFlags(uint8_t port);
void addChannels(uint8_t port, uint8_t sendFailsafe, uint8_t sendUpperChannels);
void add8ChannelsFrame(uint8_t port, uint8_t sendUpperChannels);
};
+52 -2
View File
@@ -39,6 +39,50 @@ void Pxx2Pulses::addFlag1(uint8_t module)
Pxx2Transport::addByte(flag1);
}
void Pxx2Pulses::addChannels(uint8_t module, uint8_t sendFailsafe, uint8_t firstChannel)
{
uint16_t pulseValue = 0;
uint16_t pulseValueLow = 0;
for (int8_t i=0; i<8; i++) {
uint8_t channel = firstChannel + i;
if (sendFailsafe) {
if (g_model.moduleData[module].failsafeMode == FAILSAFE_HOLD) {
pulseValue = 2047;
}
else if (g_model.moduleData[module].failsafeMode == FAILSAFE_NOPULSES) {
pulseValue = 0;
}
else {
int16_t failsafeValue = g_model.failsafeChannels[channel];
if (failsafeValue == FAILSAFE_CHANNEL_HOLD) {
pulseValue = 2047;
}
else if (failsafeValue == FAILSAFE_CHANNEL_NOPULSE) {
pulseValue = 0;
}
else {
failsafeValue += 2*PPM_CH_CENTER(channel) - 2*PPM_CENTER;
pulseValue = limit(1, (failsafeValue * 512 / 682) + 1024, 2046);
}
}
}
else {
int value = channelOutputs[channel] + 2*PPM_CH_CENTER(channel) - 2*PPM_CENTER;
pulseValue = limit(1, (value * 512 / 682) + 1024, 2046);
}
if (i & 1) {
Pxx2Transport::addByte(pulseValueLow); // Low byte of channel
Pxx2Transport::addByte(((pulseValueLow >> 8) & 0x0F) | (pulseValue << 4)); // 4 bits each from 2 channels
Pxx2Transport::addByte(pulseValue >> 4); // High byte of channel
}
else {
pulseValueLow = pulseValue;
}
}
}
void Pxx2Pulses::setupChannelsFrame(uint8_t module)
{
addFrameType(PXX2_TYPE_C_MODULE, PXX2_TYPE_ID_CHANNELS);
@@ -50,8 +94,14 @@ void Pxx2Pulses::setupChannelsFrame(uint8_t module)
addFlag1(module);
// Channels
addChannels(module, flag0 & PXX2_FLAG0_FAILSAFE, 0);
addChannels(module, flag0 & PXX2_FLAG0_FAILSAFE, 1);
uint8_t channelsCount = sentModuleChannels(module);
addChannels(module, flag0 & PXX2_FLAG0_FAILSAFE, g_model.moduleData[module].channelsStart);
if (channelsCount > 8) {
addChannels(module, flag0 & PXX2_FLAG0_FAILSAFE, g_model.moduleData[module].channelsStart + 8);
if (channelsCount > 16) {
addChannels(module, flag0 & PXX2_FLAG0_FAILSAFE, g_model.moduleData[module].channelsStart + 16);
}
}
}
bool Pxx2Pulses::setupRegisterFrame(uint8_t module)
+2
View File
@@ -132,6 +132,8 @@ class Pxx2Pulses: public PxxPulses<Pxx2Transport> {
void addFlag1(uint8_t module);
void addChannels(uint8_t module, uint8_t sendFailsafe, uint8_t firstChannel);
void addCrc()
{
Pxx2Transport::addByteWithoutCrc(Pxx2CrcMixin::crc >> 8);