feat: use any source for expo/weight/offset (#5220)

Co-authored-by: philmoz <phil.a.mitchell@gmail.com>
Co-authored-by: 3djc <3djc@gh.com>
Co-authored-by: elecpower <elecpower@users.noreply.github.com>
This commit is contained in:
philmoz
2024-07-26 20:51:21 +10:00
committed by GitHub
co-authored by philmoz 3djc elecpower
parent 326c58a7fd
commit 4b917b762c
87 changed files with 2751 additions and 1671 deletions
+1
View File
@@ -111,6 +111,7 @@ set(GUI_SRC
radio_tools.cpp
radio_trainer.cpp
radio_version.cpp
source_numberedit.cpp
)
macro(add_gui_src src)
+5 -1
View File
@@ -243,7 +243,11 @@ void Curve::clearPoints()
update();
}
void Curve::update() { base.update(); }
void Curve::update()
{
base.update();
updatePosition();
}
void Curve::checkEvents()
{
+39 -24
View File
@@ -22,6 +22,7 @@
#include "curve_param.h"
#include "gvar_numberedit.h"
#include "source_numberedit.h"
#include "model_curves.h"
#include "opentx.h"
@@ -37,21 +38,12 @@ void CurveParam::LongPressHandler(void* data)
}
}
void CurveParam::value_changed(lv_event_t* e)
{
auto obj = lv_event_get_target(e);
auto param = (CurveParam*)lv_obj_get_user_data(obj);
if (!param) return;
param->update();
}
class CurveChoice : public Choice
{
public:
CurveChoice(Window* parent, CurveRef* ref, std::function<void(int32_t)> setRefValue, std::function<void(void)> refreshView) :
Choice(parent, rect_t{}, -MAX_CURVES, MAX_CURVES, GET_DEFAULT(ref->value), setRefValue),
ref(ref), refreshView(refreshView)
CurveChoice(Window* parent, std::function<int()> getRefValue, std::function<void(int32_t)> setRefValue, std::function<void(void)> refreshView) :
Choice(parent, rect_t{}, -MAX_CURVES, MAX_CURVES, getRefValue, setRefValue),
refreshView(std::move(refreshView))
{
setTextHandler([](int value) { return getCurveString(value); });
}
@@ -59,21 +51,21 @@ class CurveChoice : public Choice
bool onLongPress() override
{
if (modelCurvesEnabled()) {
if (ref->value) {
ModelCurvesPage::pushEditCurve(abs(ref->value) - 1, refreshView);
if (_getValue()) {
ModelCurvesPage::pushEditCurve(abs(_getValue()) - 1, refreshView);
}
}
return true;
}
protected:
CurveRef* ref;
std::function<void(void)> refreshView;
};
CurveParam::CurveParam(Window* parent, const rect_t& rect, CurveRef* ref,
std::function<void(int32_t)> setRefValue, std::function<void(void)> refreshView) :
Window(parent, rect), ref(ref)
std::function<void(int32_t)> setRefValue, int16_t sourceMin,
std::function<void(void)> refreshView) :
Window(parent, rect), ref(ref), refreshView(std::move(refreshView))
{
padAll(PAD_TINY);
lv_obj_set_flex_flow(lvobj, LV_FLEX_FLOW_ROW_WRAP);
@@ -85,23 +77,42 @@ CurveParam::CurveParam(Window* parent, const rect_t& rect, CurveRef* ref,
ref->type = newValue;
ref->value = 0;
SET_DIRTY();
lv_event_send(lvobj, LV_EVENT_VALUE_CHANGED, nullptr);
update();
});
lv_obj_add_event_cb(lvobj, CurveParam::value_changed, LV_EVENT_VALUE_CHANGED,
nullptr);
// CURVE_REF_DIFF
// CURVE_REF_EXPO
auto gv = new GVarNumberEdit(this, -100, 100, GET_DEFAULT(ref->value), setRefValue);
auto gv = new SourceNumberEdit(this, -100, 100, GET_DEFAULT(ref->value), setRefValue, sourceMin);
gv->setSuffix("%");
value_edit = gv;
// CURVE_REF_FUNC
func_choice = new Choice(this, rect_t{}, STR_VCURVEFUNC, 0, CURVE_BASE - 1,
GET_DEFAULT(ref->value), setRefValue);
[=]() {
SourceNumVal v;
v.rawValue = ref->value;
return v.value;
},
[=](int32_t newValue) {
SourceNumVal v;
v.isSource = false;
v.value = newValue;
setRefValue(v.rawValue);
});
// CURVE_REF_CUSTOM
cust_choice = new CurveChoice(this, ref, setRefValue, refreshView);
cust_choice = new CurveChoice(this,
[=]() {
SourceNumVal v;
v.rawValue = ref->value;
return v.value;
},
[=](int32_t newValue) {
SourceNumVal v;
v.isSource = false;
v.value = newValue;
setRefValue(v.rawValue);
}, refreshView);
update();
}
@@ -117,14 +128,17 @@ void CurveParam::update()
switch (ref->type) {
case CURVE_REF_DIFF:
case CURVE_REF_EXPO:
value_edit->update();
act_field = value_edit;
break;
case CURVE_REF_FUNC:
func_choice->update();
act_field = func_choice;
break;
case CURVE_REF_CUSTOM:
cust_choice->update();
act_field = cust_choice;
break;
@@ -133,10 +147,11 @@ void CurveParam::update()
}
act_field->show();
if (refreshView)
refreshView();
auto act_obj = act_field->getLvObj();
if (has_focus) {
lv_group_focus_obj(act_obj);
}
lv_event_send(act_obj, LV_EVENT_VALUE_CHANGED, nullptr);
}
+16 -10
View File
@@ -24,24 +24,30 @@
#include "window.h"
struct CurveRef;
class Choice;
class SourceNumberEdit;
class CurveParam : public Window
{
public:
CurveParam(Window* parent, const rect_t& rect, CurveRef* ref,
std::function<void(int32_t)> setRefValue,
int16_t sourceMin,
std::function<void(void)> refreshView = nullptr);
protected:
// Curve
CurveRef* ref;
// Controls
Window* value_edit;
Window* func_choice;
Window* cust_choice;
SourceNumberEdit* value_edit;
Choice* func_choice;
Choice* cust_choice;
Window* act_field = nullptr;
void update();
static void value_changed(lv_event_t* e);
static void LongPressHandler(void* data);
std::function<void(void)> refreshView;
public:
CurveParam(Window* parent, const rect_t& rect, CurveRef* ref,
std::function<void(int32_t)> setRefValue,
std::function<void(void)> refreshView = nullptr);
void update();
static void LongPressHandler(void* data);
};
+59 -15
View File
@@ -24,6 +24,7 @@
#include "curve_param.h"
#include "curveedit.h"
#include "gvar_numberedit.h"
#include "source_numberedit.h"
#include "input_edit_adv.h"
#include "input_source.h"
#include "opentx.h"
@@ -106,23 +107,23 @@ void InputEditWindow::buildBody(Window* form)
line = form->newLine(grid);
new StaticText(line, rect_t{}, STR_WEIGHT);
auto gvar =
new GVarNumberEdit(line, -100, 100, GET_DEFAULT(input->weight),
[=](int32_t newValue) {
input->weight = newValue;
preview->update();
SET_DIRTY();
});
new SourceNumberEdit(line, -100, 100, GET_DEFAULT(input->weight),
[=](int32_t newValue) {
input->weight = newValue;
preview->update();
SET_DIRTY();
}, INPUTSRC_FIRST);
gvar->setSuffix("%");
// Offset
line = form->newLine(grid);
new StaticText(line, rect_t{}, STR_OFFSET);
gvar = new GVarNumberEdit(line, -100, 100,
GET_DEFAULT(input->offset), [=](int32_t newValue) {
input->offset = newValue;
preview->update();
SET_DIRTY();
});
gvar = new SourceNumberEdit(line, -100, 100,
GET_DEFAULT(input->offset), [=](int32_t newValue) {
input->offset = newValue;
preview->update();
SET_DIRTY();
}, INPUTSRC_FIRST);
gvar->setSuffix("%");
// Switch
@@ -138,11 +139,13 @@ void InputEditWindow::buildBody(Window* form)
new CurveParam(line, rect_t{}, &input->curve,
[=](int32_t newValue) {
input->curve.value = newValue;
preview->update();
if (preview)
preview->update();
SET_DIRTY();
},
}, INPUTSRC_FIRST,
[=]() {
preview->update();
if (preview)
preview->update();
});
lv_obj_set_style_grid_cell_x_align(param->getLvObj(), LV_GRID_ALIGN_STRETCH,
0);
@@ -164,3 +167,44 @@ void InputEditWindow::deleteLater(bool detach, bool trash)
Page::deleteLater(detach, trash);
}
}
void InputEditWindow::checkEvents()
{
ExpoData* input = expoAddress(index);
bool updatePreview = false;
getvalue_t val;
SourceNumVal v;
v.rawValue = input->weight;
if (v.isSource) {
val = getValue(v.value);
if (val != lastWeightVal) {
lastWeightVal = val;
updatePreview = true;
}
}
v.rawValue = input->offset;
if (v.isSource) {
val = getValue(v.value);
if (val != lastOffsetVal) {
lastOffsetVal = val;
updatePreview = true;
}
}
v.rawValue = input->curve.value;
if (v.isSource) {
val = getValue(v.value);
if (val != lastCurveVal) {
lastCurveVal = val;
updatePreview = true;
}
}
if (updatePreview)
preview->update();
Page::checkEvents();
}
+5 -1
View File
@@ -38,9 +38,13 @@ class InputEditWindow : public Page
protected:
uint8_t input;
uint8_t index;
Curve* preview;
Curve* preview = nullptr;
getvalue_t lastWeightVal = 0;
getvalue_t lastOffsetVal = 0;
getvalue_t lastCurveVal = 0;
void buildBody(Window *window);
void checkEvents() override;
void deleteLater(bool detach = true, bool trash = true) override;
};
+18 -1
View File
@@ -71,6 +71,7 @@ InputMixButtonBase::InputMixButtonBase(Window* parent, uint8_t index) :
weight = lv_label_create(lvobj);
lv_obj_set_pos(weight, WGT_X, WGT_Y);
lv_obj_set_size(weight, WGT_W, WGT_H);
etx_font(weight, FONT_XS_INDEX, LV_STATE_USER_1);
source = lv_label_create(lvobj);
lv_obj_set_pos(source, SRC_X, SRC_Y);
@@ -79,6 +80,7 @@ InputMixButtonBase::InputMixButtonBase(Window* parent, uint8_t index) :
opts = lv_label_create(lvobj);
lv_obj_set_pos(opts, OPT_X, OPT_Y);
lv_obj_set_size(opts, OPT_W, OPT_H);
etx_font(opts, FONT_XS_INDEX, LV_STATE_USER_1);
}
InputMixButtonBase::~InputMixButtonBase()
@@ -89,7 +91,12 @@ InputMixButtonBase::~InputMixButtonBase()
void InputMixButtonBase::setWeight(gvar_t value, gvar_t min, gvar_t max)
{
char s[32];
getValueOrGVarString(s, sizeof(s), value, min, max, 0, "%");
getValueOrSrcVarString(s, sizeof(s), value, min, max, 0, "%");
if (getTextWidth(s, 0, FONT(STD)) > WGT_W)
lv_obj_add_state(weight, LV_STATE_USER_1);
else
lv_obj_clear_state(weight, LV_STATE_USER_1);
lv_label_set_text(weight, s);
}
@@ -99,6 +106,16 @@ void InputMixButtonBase::setSource(mixsrc_t idx)
lv_label_set_text(source, s);
}
void InputMixButtonBase::setOpts(const char* s)
{
if (getTextWidth(s, 0, FONT(STD)) > OPT_W)
lv_obj_add_state(opts, LV_STATE_USER_1);
else
lv_obj_clear_state(opts, LV_STATE_USER_1);
lv_label_set_text(opts, s);
}
void InputMixButtonBase::setFlightModes(uint16_t modes)
{
if (!modelFMEnabled()) return;
+3 -2
View File
@@ -54,6 +54,7 @@ class InputMixButtonBase : public ListLineButton
void setWeight(gvar_t value, gvar_t min, gvar_t max);
void setSource(mixsrc_t idx);
void setOpts(const char* s);
void setFlightModes(uint16_t modes);
virtual void updatePos(coord_t x, coord_t y) = 0;
@@ -66,7 +67,7 @@ class InputMixButtonBase : public ListLineButton
static LAYOUT_VAL(BTN_W, 389, 229)
static constexpr coord_t WGT_X = PAD_TINY;
static constexpr coord_t WGT_Y = PAD_TINY;
static LAYOUT_VAL(WGT_W, 43, 43)
static LAYOUT_VAL(WGT_W, 50, 50)
static LAYOUT_VAL(WGT_H, 21, 21)
static constexpr coord_t SRC_X = WGT_X + WGT_W + PAD_TINY;
static constexpr coord_t SRC_Y = WGT_Y;
@@ -74,7 +75,7 @@ class InputMixButtonBase : public ListLineButton
static constexpr coord_t SRC_H = WGT_H;
static constexpr coord_t OPT_X = SRC_X + SRC_W + PAD_TINY;
static constexpr coord_t OPT_Y = WGT_Y;
static LAYOUT_VAL(OPT_W, 171, 106)
static LAYOUT_VAL(OPT_W, 164, 99)
static constexpr coord_t OPT_H = WGT_H;
static LAYOUT_VAL(LN_X, 73, 73)
static LAYOUT_VAL(FM_X, (OPT_X + OPT_W + PAD_TINY), 12)
+8 -7
View File
@@ -22,8 +22,9 @@
#include "mixer_edit.h"
#include "channel_bar.h"
#include "curve_param.h"
#include "gvar_numberedit.h"
#include "source_numberedit.h"
#include "curve_param.h"
#include "mixer_edit_adv.h"
#include "mixes.h"
#include "opentx.h"
@@ -108,14 +109,14 @@ void MixEditWindow::buildBody(Window *form)
// Weight
line = form->newLine(grid);
new StaticText(line, rect_t{}, STR_WEIGHT);
auto gvar = new GVarNumberEdit(line, MIX_WEIGHT_MIN, MIX_WEIGHT_MAX,
GET_SET_DEFAULT(mix->weight));
gvar->setSuffix("%");
auto svar = new SourceNumberEdit(line, MIX_WEIGHT_MIN, MIX_WEIGHT_MAX,
GET_SET_DEFAULT(mix->weight), 0);
svar->setSuffix("%");
// Offset
new StaticText(line, rect_t{}, STR_OFFSET);
gvar = new GVarNumberEdit(line, MIX_OFFSET_MIN, MIX_OFFSET_MAX,
GET_SET_DEFAULT(mix->offset));
auto gvar = new SourceNumberEdit(line, MIX_OFFSET_MIN, MIX_OFFSET_MAX,
GET_SET_DEFAULT(mix->offset), 0);
gvar->setSuffix("%");
// Switch
@@ -126,7 +127,7 @@ void MixEditWindow::buildBody(Window *form)
// Curve
new StaticText(line, rect_t{}, STR_CURVE);
new CurveParam(line, rect_t{}, &mix->curve, SET_DEFAULT(mix->curve.value));
new CurveParam(line, rect_t{}, &mix->curve, SET_DEFAULT(mix->curve.value), 0);
line = form->newLine(grid);
line->padAll(PAD_LARGE);
+21 -21
View File
@@ -129,29 +129,29 @@ class InputLineButton : public InputMixButtonBase
}
}
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;
}
}
lv_label_set_text_fmt(opts, "%.*s", (int)sizeof(tmp_str), tmp_str);
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;
}
}
setOpts(s);
setFlightModes(line.flightModes);
}
+12 -19
View File
@@ -98,32 +98,25 @@ class MixLineButton : public InputMixButtonBase
setSource(line.srcRaw);
char tmp_str[64];
size_t maxlen = sizeof(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; }
s = strAppend(s, line.name, LEN_EXPOMIX_NAME);
}
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) {
if (tmp_str[0]) s = strAppend(s, " ");
char* sw_pos = getSwitchPositionName(line.swtch);
s = strAppend(s, sw_pos);
}
lv_label_set_text_fmt(opts, "%.*s", (int)sizeof(tmp_str), tmp_str);
if (line.curve.value != 0) {
if (tmp_str[0]) s = strAppend(s, " ");
getCurveRefString(s, sizeof(tmp_str) - (s - tmp_str), line.curve);
}
setOpts(tmp_str);
mplex->refresh();
@@ -0,0 +1,147 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* 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 "source_numberedit.h"
#include "opentx.h"
#include "sourcechoice.h"
void SourceNumberEdit::value_changed(lv_event_t* e)
{
auto obj = lv_event_get_target(e);
auto edit = (SourceNumberEdit*)lv_obj_get_user_data(obj);
if (!edit) return;
edit->update();
}
SourceNumberEdit::SourceNumberEdit(Window* parent,
int32_t vmin, int32_t vmax,
std::function<int32_t()> getValue,
std::function<void(int32_t)> setValue,
int16_t sourceMin,
LcdFlags textFlags, int32_t voffset,
int32_t vdefault) :
Window(parent, {0, 0, NUM_EDIT_W + SRC_BTN_W + PAD_TINY * 3, EdgeTxStyles::UI_ELEMENT_HEIGHT + PAD_TINY * 2}),
vmin(vmin),
vmax(vmax),
getValue(getValue),
setValue(setValue),
textFlags(textFlags),
voffset(voffset)
{
padAll(PAD_TINY);
lv_obj_set_flex_flow(lvobj, LV_FLEX_FLOW_ROW_WRAP);
lv_obj_set_style_flex_cross_place(lvobj, LV_FLEX_ALIGN_CENTER, 0);
lv_obj_set_size(lvobj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
// Source field
source_field = new SourceChoice(
this, {0, 0, NUM_EDIT_W, 0}, sourceMin, INPUTSRC_LAST,
[=]() {
SourceNumVal v;
v.rawValue = getValue();
return v.value;
},
[=](int newValue) {
SourceNumVal v = {(int16_t)newValue, true};
setValue(v.rawValue);
}, true);
num_field = new NumberEdit(
this, {0, 0, NUM_EDIT_W, 0}, vmin, vmax,
[=]() {
SourceNumVal v;
v.rawValue = getValue();
return v.value;
},
[=](int newValue) {
SourceNumVal v = {(int16_t)newValue, false};
setValue(v.rawValue);
},
textFlags);
num_field->setDefault(vdefault);
// The Source button
m_srcBtn = new TextButton(this, {NUM_EDIT_W + PAD_TINY, 0, SRC_BTN_W, 0}, "SRC", [=]() {
switchSourceMode();
return isSource();
});
m_srcBtn->check(isSource());
lv_obj_add_event_cb(lvobj, SourceNumberEdit::value_changed,
LV_EVENT_VALUE_CHANGED, nullptr);
// update field type based on value
update();
}
bool SourceNumberEdit::isSource()
{
SourceNumVal v;
v.rawValue = getValue();
return v.isSource;
}
void SourceNumberEdit::switchSourceMode()
{
SourceNumVal v;
v.rawValue = getValue();
v.isSource = !v.isSource;
// TODO: convert value???
v.value = 0;
setValue(v.rawValue);
// update field type based on value
update();
}
void SourceNumberEdit::setSuffix(std::string value)
{
num_field->setSuffix(value);
}
void SourceNumberEdit::update()
{
bool has_focus = act_field && act_field->hasFocus();
source_field->hide();
num_field->hide();
if (isSource()) {
// Source mode
act_field = source_field;
source_field->show();
source_field->update();
lv_event_send(source_field->getLvObj(), LV_EVENT_VALUE_CHANGED, nullptr);
} else {
// number edit mode
act_field = num_field;
num_field->show();
num_field->update();
}
m_srcBtn->check(isSource());
if (has_focus) {
lv_group_focus_obj(act_field->getLvObj());
}
}
@@ -0,0 +1,67 @@
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* 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.
*/
#pragma once
#include "choice.h"
#include "form.h"
#include "numberedit.h"
class TextButton;
class SourceNumberEdit : public Window
{
public:
SourceNumberEdit(Window* parent, int32_t vmin, int32_t vmax,
std::function<int32_t()> getValue,
std::function<void(int32_t)> setValue,
int16_t sourceMin,
LcdFlags textFlags = 0, int32_t voffset = 0,
int32_t vdefault = 0);
void switchSourceMode();
void setSuffix(std::string value);
void setFastStep(int value) { num_field->setFastStep(value); }
void setAccelFactor(int value) { num_field->setAccelFactor(value); }
void update();
static LAYOUT_VAL(NUM_EDIT_W, 84, 74)
static LAYOUT_VAL(SRC_BTN_W, 38, 38)
protected:
Choice* source_field = nullptr;
NumberEdit* num_field = nullptr;
FormField* act_field = nullptr;
TextButton* m_srcBtn = nullptr;
int32_t vmin;
int32_t vmax;
std::function<int32_t()> getValue;
std::function<void(int32_t)> setValue;
LcdFlags textFlags;
int32_t voffset;
bool isSource();
static void value_changed(lv_event_t* e);
};