feat: add inverted sources (#4722)
Co-authored-by: elecpower <neilh713@tpg.com.au>
This commit is contained in:
@@ -193,7 +193,7 @@ void CurveDataEdit::update()
|
||||
}
|
||||
}
|
||||
|
||||
void CurveEdit::SetCurrentSource(uint32_t source)
|
||||
void CurveEdit::SetCurrentSource(mixsrc_t source)
|
||||
{
|
||||
CurveEdit::currentSource = source;
|
||||
if (source)
|
||||
|
||||
@@ -31,7 +31,7 @@ class CurveEdit: public Window
|
||||
{
|
||||
public:
|
||||
CurveEdit(Window * parent, const rect_t & rect, uint8_t index);
|
||||
static void SetCurrentSource(uint32_t source);
|
||||
static void SetCurrentSource(mixsrc_t source);
|
||||
|
||||
void deleteLater(bool detach = true, bool trash = true) override
|
||||
{
|
||||
|
||||
@@ -95,14 +95,14 @@ InputSource::InputSource(Window *parent, ExpoData *input) :
|
||||
lv_obj_set_flex_flow(lvobj, LV_FLEX_FLOW_COLUMN);
|
||||
lv_obj_set_size(lvobj, lv_pct(100), LV_SIZE_CONTENT);
|
||||
|
||||
new SourceChoice(this, rect_t{}, INPUTSRC_FIRST, INPUTSRC_LAST,
|
||||
GET_DEFAULT(input->srcRaw), [=](int32_t newValue) {
|
||||
input->srcRaw = newValue;
|
||||
lv_event_send(lvobj, LV_EVENT_VALUE_CHANGED, nullptr);
|
||||
SET_DIRTY();
|
||||
});
|
||||
lv_obj_add_event_cb(lvobj, InputSource::value_changed, LV_EVENT_VALUE_CHANGED,
|
||||
nullptr);
|
||||
new SourceChoice(
|
||||
this, rect_t{}, INPUTSRC_FIRST, INPUTSRC_LAST, GET_DEFAULT(input->srcRaw),
|
||||
[=](int32_t newValue) {
|
||||
input->srcRaw = newValue;
|
||||
lv_event_send(lvobj, LV_EVENT_VALUE_CHANGED, nullptr);
|
||||
SET_DIRTY();
|
||||
}, true);
|
||||
lv_obj_add_event_cb(lvobj, InputSource::value_changed, LV_EVENT_VALUE_CHANGED, nullptr);
|
||||
|
||||
sensor_form = new Window(this, rect_t{});
|
||||
sensor_form->padAll(PAD_TINY);
|
||||
|
||||
@@ -110,7 +110,7 @@ void MixEditWindow::buildBody(Window *form)
|
||||
line = form->newLine(grid);
|
||||
new StaticText(line, rect_t{}, STR_SOURCE);
|
||||
new SourceChoice(line, rect_t{}, 0, MIXSRC_LAST,
|
||||
GET_SET_DEFAULT(mix->srcRaw));
|
||||
GET_SET_DEFAULT(mix->srcRaw), true);
|
||||
CurveEdit::SetCurrentSource(mix->srcRaw);
|
||||
|
||||
// Weight
|
||||
|
||||
@@ -122,7 +122,7 @@ class LogicalSwitchEditPage : public Page
|
||||
break;
|
||||
case LS_FAMILY_COMP:
|
||||
new SourceChoice(line, rect_t{}, 0, MIXSRC_LAST_TELEM,
|
||||
GET_SET_DEFAULT(cs->v1));
|
||||
GET_SET_DEFAULT(cs->v1), true);
|
||||
break;
|
||||
case LS_FAMILY_TIMER:
|
||||
timer =
|
||||
@@ -144,7 +144,7 @@ class LogicalSwitchEditPage : public Page
|
||||
v2Edit->setValue(cs->v2);
|
||||
}
|
||||
SET_DIRTY();
|
||||
});
|
||||
}, true);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -191,7 +191,7 @@ class LogicalSwitchEditPage : public Page
|
||||
} break;
|
||||
case LS_FAMILY_COMP:
|
||||
new SourceChoice(line, rect_t{}, 0, MIXSRC_LAST_TELEM,
|
||||
GET_SET_DEFAULT(cs->v2));
|
||||
GET_SET_DEFAULT(cs->v2), true);
|
||||
break;
|
||||
case LS_FAMILY_TIMER:
|
||||
timer =
|
||||
@@ -208,7 +208,7 @@ class LogicalSwitchEditPage : public Page
|
||||
GET_SET_DEFAULT(cs->v2));
|
||||
|
||||
v2Edit->setDisplayHandler([=](int value) -> std::string {
|
||||
if (cs->v1 <= MIXSRC_LAST_CH) value = calc100toRESX(value);
|
||||
if (abs(cs->v1) <= MIXSRC_LAST_CH) value = calc100toRESX(value);
|
||||
std::string txt = getSourceCustomValueString(cs->v1, value, 0);
|
||||
return txt;
|
||||
});
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
class SourceChoiceMenuToolbar : public MenuToolbar
|
||||
{
|
||||
public:
|
||||
SourceChoiceMenuToolbar(SourceChoice *choice, Menu *menu) :
|
||||
SourceChoiceMenuToolbar(SourceChoice* choice, Menu* menu) :
|
||||
MenuToolbar(choice, menu, FILTER_COLUMNS)
|
||||
{
|
||||
addButton(STR_CHAR_INPUT, MIXSRC_FIRST_INPUT, MIXSRC_LAST_INPUT, nullptr,
|
||||
@@ -104,9 +104,79 @@ class SourceChoiceMenuToolbar : public MenuToolbar
|
||||
if ((nxtBtnPos > filterColumns) && choice->isValueAvailable &&
|
||||
choice->isValueAvailable(0))
|
||||
addButton(STR_SELECT_MENU_CLR, 0, 0, nullptr, nullptr, true);
|
||||
|
||||
#if defined(HARDWARE_TOUCH)
|
||||
if (choice->canInvert) {
|
||||
invertBtn = new MenuToolbarButton(this, {0, 0, LV_PCT(100), 0},
|
||||
STR_SELECT_MENU_INV);
|
||||
invertBtn->check(choice->inverted);
|
||||
lv_obj_align(invertBtn->getLvObj(), LV_ALIGN_BOTTOM_MID, 0, 0);
|
||||
|
||||
invertBtn->setPressHandler([=]() {
|
||||
lv_obj_clear_state(invertBtn->getLvObj(), LV_STATE_FOCUSED);
|
||||
invertChoice();
|
||||
return choice->inverted;
|
||||
});
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void invertChoice()
|
||||
{
|
||||
SourceChoice* sourceChoice = (SourceChoice*)choice;
|
||||
if (sourceChoice->canInvert) {
|
||||
sourceChoice->inverted = !sourceChoice->inverted;
|
||||
auto idx = menu->selection();
|
||||
sourceChoice->fillMenu(menu, filter);
|
||||
menu->select(idx);
|
||||
#if defined(HARDWARE_TOUCH)
|
||||
invertBtn->check(sourceChoice->inverted);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
void longPress()
|
||||
{
|
||||
lv_indev_t* indev = lv_indev_get_act();
|
||||
if (indev->driver->type == LV_INDEV_TYPE_KEYPAD) {
|
||||
invertChoice();
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
#if defined(HARDWARE_TOUCH)
|
||||
MenuToolbarButton* invertBtn = nullptr;
|
||||
#endif
|
||||
};
|
||||
|
||||
void SourceChoice::LongPressHandler(void* data)
|
||||
{
|
||||
SourceChoice* src = (SourceChoice*)data;
|
||||
if (src && src->canInvert) {
|
||||
int16_t val = src->_getValue();
|
||||
if (src->isValueAvailable && src->isValueAvailable(-val)) {
|
||||
src->setValue(-val);
|
||||
src->invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SourceChoice::setValue(int value)
|
||||
{
|
||||
if (inMenu) {
|
||||
if (inverted) value = -value;
|
||||
inMenu = false;
|
||||
}
|
||||
Choice::setValue(value);
|
||||
}
|
||||
|
||||
int SourceChoice::getIntValue() const
|
||||
{
|
||||
int value = Choice::getIntValue();
|
||||
if (inMenu) value = abs(value);
|
||||
return value;
|
||||
}
|
||||
|
||||
// defined in gui/gui_common.cpp
|
||||
uint8_t switchToMix(uint8_t source);
|
||||
|
||||
@@ -114,6 +184,9 @@ void SourceChoice::openMenu()
|
||||
{
|
||||
setEditMode(true); // this needs to be done first before menu is created.
|
||||
|
||||
inverted = getIntValue() < 0;
|
||||
inMenu = true;
|
||||
|
||||
auto menu = new Menu(this);
|
||||
if (menuTitle) menu->setTitle(menuTitle);
|
||||
|
||||
@@ -149,15 +222,19 @@ void SourceChoice::openMenu()
|
||||
|
||||
SourceChoice::SourceChoice(Window *parent, const rect_t &rect, int16_t vmin,
|
||||
int16_t vmax, std::function<int16_t()> getValue,
|
||||
std::function<void(int16_t)> setValue) :
|
||||
Choice(parent, rect, vmin, vmax, getValue, setValue, STR_SOURCE)
|
||||
std::function<void(int16_t)> setValue, bool allowInvert) :
|
||||
Choice(parent, rect, vmin, vmax, getValue, setValue, STR_SOURCE), canInvert(allowInvert)
|
||||
{
|
||||
setTextHandler([=](int value) {
|
||||
if (inMenu && inverted) value = -value;
|
||||
|
||||
if (isValueAvailable && !isValueAvailable(value))
|
||||
return std::to_string(0); // we will fix this later
|
||||
|
||||
return std::string(getSourceString(value));
|
||||
});
|
||||
|
||||
set_lv_LongPressHandler(LongPressHandler, this);
|
||||
|
||||
setAvailableHandler([](int v) { return isSourceAvailable(v); });
|
||||
}
|
||||
|
||||
@@ -33,11 +33,21 @@ class SourceChoice : public Choice
|
||||
public:
|
||||
SourceChoice(Window* parent, const rect_t& rect, int16_t vmin, int16_t vmax,
|
||||
std::function<int16_t()> getValue,
|
||||
std::function<void(int16_t)> setValue);
|
||||
std::function<void(int16_t)> setValue, bool allowInvert = false);
|
||||
|
||||
static void LongPressHandler(void* data);
|
||||
|
||||
protected:
|
||||
friend SourceChoiceMenuToolbar;
|
||||
|
||||
bool inMenu = false;
|
||||
bool canInvert = false;
|
||||
|
||||
void setValue(int value) override;
|
||||
int getIntValue() const override;
|
||||
|
||||
void invertChoice();
|
||||
|
||||
void openMenu() override;
|
||||
|
||||
#if defined(DEBUG_WINDOWS)
|
||||
|
||||
@@ -373,7 +373,7 @@ void FunctionEditPage::addSourceChoice(FormLine *line, const char *title,
|
||||
CustomFunctionData *cfn, int16_t vmax)
|
||||
{
|
||||
new StaticText(line, rect_t{}, title);
|
||||
new SourceChoice(line, rect_t{}, 0, vmax, GET_SET_DEFAULT(CFN_PARAM(cfn)));
|
||||
new SourceChoice(line, rect_t{}, 0, vmax, GET_SET_DEFAULT(CFN_PARAM(cfn)), true);
|
||||
}
|
||||
|
||||
NumberEdit *FunctionEditPage::addNumberEdit(FormLine *line, const char *title,
|
||||
|
||||
@@ -98,7 +98,9 @@ class SwitchChoiceMenuToolbar : public MenuToolbar
|
||||
}
|
||||
|
||||
protected:
|
||||
#if defined(HARDWARE_TOUCH)
|
||||
MenuToolbarButton* invertBtn = nullptr;
|
||||
#endif
|
||||
};
|
||||
|
||||
void SwitchChoice::LongPressHandler(void* data)
|
||||
|
||||
Reference in New Issue
Block a user