Skip to content
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

fix(color): New inputs disappear until restart. #4317

Merged
merged 1 commit into from
Nov 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 85 additions & 73 deletions radio/src/gui/colorlcd/model_inputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,29 @@
*/

#include "model_inputs.h"
#include "opentx.h"
#include "gvar_numberedit.h"
#include "libopenui.h"

#include <algorithm>

#include "choice.h"
#include "model_curves.h"
#include "gvar_numberedit.h"
#include "hal/adc_driver.h"
#include "input_edit.h"
#include "input_mix_group.h"
#include "input_mix_button.h"
#include "input_mapping.h"

#include "input_mix_button.h"
#include "input_mix_group.h"
#include "libopenui.h"
#include "model_curves.h"
#include "opentx.h"
#include "tasks/mixer_task.h"
#include "hal/adc_driver.h"

#include <algorithm>

#define SET_DIRTY() storageDirty(EE_MODEL)

uint8_t getExposCount()
{
uint8_t count = 0;
uint8_t ch ;
uint8_t ch;

for (int i=MAX_EXPOS-1 ; i>=0; i--) {
for (int i = MAX_EXPOS - 1; i >= 0; i--) {
ch = EXPO_VALID(expoAddress(i));
if (ch != 0) {
count++;
Expand All @@ -59,7 +59,7 @@ void copyExpo(uint8_t source, uint8_t dest, uint8_t input)
mixerTaskStop();
ExpoData sourceExpo;
memcpy(&sourceExpo, expoAddress(source), sizeof(ExpoData));
ExpoData *expo = expoAddress(dest);
ExpoData* expo = expoAddress(dest);
size_t trailingExpos = MAX_EXPOS - (dest + 1);
memmove(expo + 1, expo, trailingExpos * sizeof(ExpoData));
memcpy(expo, &sourceExpo, sizeof(ExpoData));
Expand All @@ -71,10 +71,10 @@ void copyExpo(uint8_t source, uint8_t dest, uint8_t input)
void deleteExpo(uint8_t idx)
{
mixerTaskStop();
ExpoData * expo = expoAddress(idx);
ExpoData* expo = expoAddress(idx);
int input = expo->chn;
memmove(expo, expo+1, (MAX_EXPOS-(idx+1))*sizeof(ExpoData));
memclear(&g_model.expoData[MAX_EXPOS-1], sizeof(ExpoData));
memmove(expo, expo + 1, (MAX_EXPOS - (idx + 1)) * sizeof(ExpoData));
memclear(&g_model.expoData[MAX_EXPOS - 1], sizeof(ExpoData));
if (!isInputAvailable(input)) {
memclear(&g_model.inputNames[input], LEN_INPUT_NAME);
}
Expand All @@ -90,16 +90,16 @@ int8_t s_copySrcRow;
void insertExpo(uint8_t idx, uint8_t input)
{
mixerTaskStop();
ExpoData * expo = expoAddress(idx);
memmove(expo+1, expo, (MAX_EXPOS-(idx+1))*sizeof(ExpoData));
ExpoData* expo = expoAddress(idx);
memmove(expo + 1, expo, (MAX_EXPOS - (idx + 1)) * sizeof(ExpoData));
memclear(expo, sizeof(ExpoData));
if (input >= adcGetMaxInputs(ADC_INPUT_MAIN)) {
expo->srcRaw = MIXSRC_FIRST_STICK + input;
} else {
expo->srcRaw = MIXSRC_FIRST_STICK + inputMappingChannelOrder(input);
}
}
expo->curve.type = CURVE_REF_EXPO;
expo->mode = 3; // pos+neg
expo->mode = 3; // pos+neg
expo->chn = input;
expo->weight = 100;
mixerTaskStart();
Expand All @@ -113,47 +113,59 @@ class InputLineButton : public InputMixButton

void refresh() override
{
const ExpoData &line = g_model.expoData[index];
const ExpoData& line = g_model.expoData[index];
setWeight(line.weight, -100, 100);
setSource(line.srcRaw);

char tmp_str[64];
size_t maxlen = sizeof(tmp_str);

char *s = tmp_str;
char* s = tmp_str;
*s = '\0';

if (line.name[0]) {
int cnt = lv_snprintf(s, maxlen, "%.*s ", (int)sizeof(line.name), line.name);
if ((size_t)cnt >= maxlen) maxlen = 0;
else { maxlen -= cnt; s += cnt; }
int cnt =
lv_snprintf(s, maxlen, "%.*s ", (int)sizeof(line.name), line.name);
if ((size_t)cnt >= maxlen)
maxlen = 0;
else {
maxlen -= cnt;
s += cnt;
}
}

if (line.swtch || line.curve.value) {
if (line.swtch) {
char* sw_pos = getSwitchPositionName(line.swtch);
int cnt = lv_snprintf(s, maxlen, "%s ", sw_pos);
if ((size_t)cnt >= maxlen) maxlen = 0;
else { maxlen -= cnt; s += cnt; }
}
if (line.curve.value != 0) {
getCurveRefString(s, maxlen, line.curve);
int cnt = strnlen(s, maxlen);
if ((size_t)cnt >= maxlen) maxlen = 0;
else { maxlen -= cnt; s += cnt; }
}
if (line.swtch) {
char* sw_pos = getSwitchPositionName(line.swtch);
int cnt = lv_snprintf(s, maxlen, "%s ", sw_pos);
if ((size_t)cnt >= maxlen)
maxlen = 0;
else {
maxlen -= cnt;
s += cnt;
}
}
if (line.curve.value != 0) {
getCurveRefString(s, maxlen, line.curve);
int cnt = strnlen(s, maxlen);
if ((size_t)cnt >= maxlen)
maxlen = 0;
else {
maxlen -= cnt;
s += cnt;
}
}
}
lv_label_set_text_fmt(opts, "%.*s", (int)sizeof(tmp_str), tmp_str);

setFlightModes(line.flightModes);
}

protected:
protected:
bool isActive() const override { return isExpoActive(index); }
};

ModelInputsPage::ModelInputsPage():
PageTab(STR_MENUINPUTS, ICON_MODEL_INPUTS)
ModelInputsPage::ModelInputsPage() : PageTab(STR_MENUINPUTS, ICON_MODEL_INPUTS)
{
setOnSetVisibleHandler([=]() {
// reset clipboard
Expand All @@ -172,10 +184,9 @@ bool ModelInputsPage::reachExposLimit()

InputMixGroup* ModelInputsPage::getGroupBySrc(mixsrc_t src)
{
auto g =
std::find_if(groups.begin(), groups.end(), [=](InputMixGroup* g) -> bool {
return g->getMixSrc() == src;
});
auto g = std::find_if(
groups.begin(), groups.end(),
[=](InputMixGroup* g) -> bool { return g->getMixSrc() == src; });

if (g != groups.end()) return *g;

Expand Down Expand Up @@ -212,9 +223,9 @@ void ModelInputsPage::removeGroup(InputMixGroup* g)
void ModelInputsPage::removeLine(InputMixButton* l)
{
auto line = std::find_if(lines.begin(), lines.end(),
[=](InputMixButton* lh) -> bool { return lh == l; });
[=](InputMixButton* lh) -> bool { return lh == l; });
if (line == lines.end()) return;

line = lines.erase(line);
while (line != lines.end()) {
(*line)->setIndex((*line)->getIndex() - 1);
Expand All @@ -227,18 +238,18 @@ InputMixGroup* ModelInputsPage::createGroup(FormWindow* form, mixsrc_t src)
return new InputMixGroup(form, src);
}

InputMixButton* ModelInputsPage::createLineButton(InputMixGroup *group,
InputMixButton* ModelInputsPage::createLineButton(InputMixGroup* group,
uint8_t index)
{
auto button = new InputLineButton(group, index);
button->refresh();

lines.emplace_back(button);
group->addLine(button);

uint8_t input = group->getMixSrc() - MIXSRC_FIRST_INPUT;
button->setPressHandler([=]() -> uint8_t {
Menu *menu = new Menu(form);
Menu* menu = new Menu(form);
menu->addLine(STR_EDIT, [=]() {
uint8_t idx = button->getIndex();
editInput(input, idx);
Expand Down Expand Up @@ -293,18 +304,20 @@ void ModelInputsPage::addLineButton(uint8_t index)
void ModelInputsPage::addLineButton(mixsrc_t src, uint8_t index)
{
InputMixGroup* group_w = getGroupBySrc(src);
if (!group_w) {
if (!group_w) {
group_w = createGroup(form, src);
// insertion sort
groups.emplace_back(group_w);
auto g = groups.rbegin();
if (g != groups.rend()) {
auto g_prev = g; ++g_prev;
auto g_prev = g;
++g_prev;
while (g_prev != groups.rend()) {
if ((*g_prev)->getMixSrc() < (*g)->getMixSrc()) break;
lv_obj_swap((*g)->getLvObj(), (*g_prev)->getLvObj());
std::swap(*g, *g_prev);
++g; ++g_prev;
++g;
++g_prev;
}
}
}
Expand All @@ -316,7 +329,8 @@ void ModelInputsPage::addLineButton(mixsrc_t src, uint8_t index)
// insertion sort for the focus group
auto l = lines.rbegin();
if (l != lines.rend()) {
auto l_prev = l; ++l_prev;
auto l_prev = l;
++l_prev;
while (l_prev != lines.rend()) {
if ((*l_prev)->getIndex() < (*l)->getIndex()) break;
// Swap elements (focus + line list)
Expand All @@ -330,9 +344,10 @@ void ModelInputsPage::addLineButton(mixsrc_t src, uint8_t index)
lv_group_swap_obj(obj1, obj2);
}
std::swap(*l, *l_prev);
// Inc index of elements after
// Inc index of elements after
(*l)->setIndex((*l)->getIndex() + 1);
++l; ++l_prev;
++l;
++l_prev;
}
}
}
Expand All @@ -343,26 +358,27 @@ void ModelInputsPage::newInput()
menu->setTitle(STR_MENU_INPUTS);

uint8_t chn = 0;
uint8_t index = 0;
ExpoData* line = g_model.expoData;

// search for unused channels
for (uint8_t i = 0; i < MAX_EXPOS; i++) {
for (uint8_t i = 0; i < MAX_EXPOS && chn < MAX_INPUTS; i++) {
if (!EXPO_VALID(line) || (line->chn > chn)) {
if (chn >= MAX_INPUTS) break;
std::string name(getSourceString(chn+1));
menu->addLineBuffered(name.c_str(), [=]() { insertInput(chn, i); });
std::string name(getSourceString(chn + 1));
menu->addLineBuffered(name.c_str(), [=]() { insertInput(chn, index); });
}
if (EXPO_VALID(line))
if (EXPO_VALID(line)) {
chn = line->chn + 1;
else
index += 1;
} else {
chn += 1;
}
++line;
}
pfeerick marked this conversation as resolved.
Show resolved Hide resolved

menu->updateLines();
}


void ModelInputsPage::editInput(uint8_t input, uint8_t index)
{
_copyMode = 0;
Expand All @@ -377,9 +393,9 @@ void ModelInputsPage::editInput(uint8_t input, uint8_t index)
auto group_obj = group->getLvObj();
auto edit = new InputEditWindow(input, index);
edit->setCloseHandler([=]() {
lv_event_send(line_obj, LV_EVENT_VALUE_CHANGED, nullptr);
lv_event_send(group_obj, LV_EVENT_VALUE_CHANGED, nullptr);
});
lv_event_send(line_obj, LV_EVENT_VALUE_CHANGED, nullptr);
lv_event_send(group_obj, LV_EVENT_VALUE_CHANGED, nullptr);
});
}

void ModelInputsPage::insertInput(uint8_t input, uint8_t index)
Expand Down Expand Up @@ -408,7 +424,7 @@ void ModelInputsPage::deleteInput(uint8_t index)
line->deleteLater();
removeLine(line);
}

::deleteExpo(index);
}

Expand Down Expand Up @@ -447,10 +463,10 @@ void ModelInputsPage::pasteInputAfter(uint8_t dst_idx)
pasteInput(dst_idx + 1, input);
}

void ModelInputsPage::build(FormWindow *window)
void ModelInputsPage::build(FormWindow* window)
{
window->setFlexLayout(LV_FLEX_FLOW_COLUMN, 3);

form = new FormWindow(window, rect_t{});
form->setFlexLayout(LV_FLEX_FLOW_COLUMN, 3);

Expand All @@ -469,7 +485,6 @@ void ModelInputsPage::build(FormWindow *window)
uint8_t index = 0;
ExpoData* line = g_model.expoData;
for (uint8_t input = 0; input < MAX_INPUTS; input++) {

if (index >= MAX_EXPOS) break;

if (line->chn == input && EXPO_VALID(line)) {
Expand All @@ -486,12 +501,9 @@ void ModelInputsPage::build(FormWindow *window)
++index;
++line;
}
} else if (line->chn > input && EXPO_VALID(line)) {
TRACE("missing input for channel #%d", input);
} else if (!EXPO_VALID(line)) {
TRACE("invalid line #%d", index);
// End of list
break;
}
}
}