Skip to content

Commit

Permalink
feat(color): additions to LVGL for Lua scripts (#5179)
Browse files Browse the repository at this point in the history
  • Loading branch information
philmoz authored Jun 30, 2024
1 parent 15b0878 commit 4d29d5d
Show file tree
Hide file tree
Showing 54 changed files with 653 additions and 404 deletions.
20 changes: 18 additions & 2 deletions companion/src/firmwares/edgetx/yaml_screendata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ Node convert<ZoneOptionValueTyped>::encode(const ZoneOptionValueTyped& rhs)
value["source"] = rhs.value.sourceValue;
break;
case ZOV_Color:
value["color"] = color_to_hex(rhs.value.colorValue);
if (rhs.value.colorValue & 0x80000000) {
value["color"] = color_to_hex(rhs.value.colorValue & 0xFFFFFF);
} else {
std::string s("COLIDX");
s += std::to_string(rhs.value.colorValue);
value["color"] = s;
}
break;
default:
break;
Expand All @@ -86,7 +92,17 @@ bool convert<ZoneOptionValueTyped>::decode(const Node& node,
value["boolValue"] >> rhs.value.boolValue;
value["stringValue"] >> rhs.value.stringValue;
value["source"] >> rhs.value.sourceValue;
value["color"] >> rhs.value.colorValue;
std::string s;
value["color"] >> s;
if (s.substr(0, 6) == "COLIDX") {
// Index color
rhs.value.colorValue = stoi(s.substr(6));
} else {
unsigned int c;
sscanf(s.c_str(), "0x%x", &c);
// Mark as RGB color
rhs.value.colorValue = c | 0x80000000;
}
}
}
return true;
Expand Down
42 changes: 14 additions & 28 deletions radio/src/gui/colorlcd/LvglWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,29 +173,13 @@ static void keyboardDriverRead(lv_indev_drv_t *drv, lv_indev_data_t *data)
}
}

static void copy_ts_to_indev_data(const TouchState &st, lv_indev_data_t *data)
{
data->point.x = st.x;
data->point.y = st.y;
}

static lv_indev_data_t touch_data_backup;

static void backup_touch_data(lv_indev_data_t* data)
{
memcpy(&touch_data_backup, data, sizeof(lv_indev_data_t));
}

static void copy_touch_data_backup(lv_indev_data_t* data)
{
memcpy(data, &touch_data_backup, sizeof(lv_indev_data_t));
}

extern "C" void touchDriverRead(lv_indev_drv_t *drv, lv_indev_data_t *data)
{
#if defined(HARDWARE_TOUCH)
static lv_indev_data_t touch_data_backup;

if(!touchPanelEventOccured()) {
copy_touch_data_backup(data);
memcpy(data, &touch_data_backup, sizeof(lv_indev_data_t));
return;
}

Expand All @@ -218,14 +202,16 @@ extern "C" void touchDriverRead(lv_indev_drv_t *drv, lv_indev_data_t *data)

if(st.event == TE_NONE) {
TRACE("TE_NONE");
} else if(st.event == TE_DOWN || st.event == TE_SLIDE) {
TRACE("INDEV_STATE_PRESSED");
data->state = LV_INDEV_STATE_PRESSED;
copy_ts_to_indev_data(st, data);
} else {
TRACE("INDEV_STATE_RELEASED");
data->state = LV_INDEV_STATE_RELEASED;
copy_ts_to_indev_data(st, data);
if(st.event == TE_DOWN || st.event == TE_SLIDE) {
TRACE("TE_PRESSED");
data->state = LV_INDEV_STATE_PRESSED;
} else {
TRACE("TE_RELEASED");
data->state = LV_INDEV_STATE_RELEASED;
}
data->point.x = st.x;
data->point.y = st.y;
}

static bool onebeep=true; // TODO... This probably needs to be fixed in the driver it's sending two events
Expand All @@ -237,8 +223,8 @@ extern "C" void touchDriverRead(lv_indev_drv_t *drv, lv_indev_data_t *data)
} else {
onebeep = true;
}
backup_touch_data(data);

memcpy(&touch_data_backup, data, sizeof(lv_indev_data_t));
#endif
}

Expand Down
17 changes: 10 additions & 7 deletions radio/src/gui/colorlcd/color_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ColorBar : public FormField
nullptr);

etx_std_style(lvobj, LV_PART_MAIN, PAD_ZERO);
etx_obj_add_style(lvobj, styles->border_color_edit, LV_PART_MAIN | LV_STATE_EDITED);
etx_obj_add_style(lvobj, styles->border_color[COLOR_THEME_EDIT_INDEX], LV_PART_MAIN | LV_STATE_EDITED);
etx_obj_add_style(lvobj, styles->outline_color_edit, LV_PART_MAIN | LV_STATE_EDITED);
}

Expand Down Expand Up @@ -289,7 +289,9 @@ class HSVColorType : public BarColorType
public:
HSVColorType(Window* parent, uint32_t color) : BarColorType(parent)
{
auto r = GET_RED(color), g = GET_GREEN(color), b = GET_BLUE(color);
auto rgb = COLOR_VAL(colorToRGB(color));

auto r = GET_RED(rgb), g = GET_GREEN(rgb), b = GET_BLUE(rgb);
float values[MAX_BARS];
RGBtoHSV(r, g, b, values[0], values[1], values[2]);
values[1] *= MAX_SATURATION; // convert the proper base
Expand Down Expand Up @@ -322,7 +324,7 @@ class HSVColorType : public BarColorType

uint32_t getRGB() override
{
return HSVtoRGB(bars[0]->value, bars[1]->value, bars[2]->value);
return COLOR2FLAGS(HSVtoRGB(bars[0]->value, bars[1]->value, bars[2]->value)) | RGB_FLAG;
}

protected:
Expand All @@ -335,7 +337,9 @@ class RGBColorType : public BarColorType
public:
RGBColorType(Window* parent, uint32_t color) : BarColorType(parent)
{
auto r = GET_RED(color), g = GET_GREEN(color), b = GET_BLUE(color);
auto rgb = COLOR_VAL(colorToRGB(color));

auto r = GET_RED(rgb), g = GET_GREEN(rgb), b = GET_BLUE(rgb);
float values[MAX_BARS];
values[0] = r;
values[1] = g;
Expand All @@ -354,7 +358,7 @@ class RGBColorType : public BarColorType

uint32_t getRGB() override
{
return RGB(bars[0]->value, bars[1]->value, bars[2]->value);
return RGB2FLAGS(bars[0]->value, bars[1]->value, bars[2]->value);
}

protected:
Expand Down Expand Up @@ -392,8 +396,7 @@ class ThemeColorType : public ColorType
auto btn = new TextButton(parent, rect_t{}, " ");
etx_bg_color(btn->getLvObj(), (LcdColorIndex)color);
btn->setPressHandler([=]() {
auto cv = lcdColorTable[color];
m_color = RGB(GET_RED(cv), GET_GREEN(cv), GET_BLUE(cv));
m_color = COLOR2FLAGS(color);
lv_event_send(parent->getParent()->getParent()->getLvObj(),
LV_EVENT_VALUE_CHANGED, nullptr);
return 0;
Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/color_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static void color_swatch_constructor(const lv_obj_class_t *class_p,
{
etx_obj_add_style(obj, styles->bg_opacity_cover, LV_PART_MAIN);
etx_obj_add_style(obj, styles->border_thin, LV_PART_MAIN);
etx_obj_add_style(obj, styles->border_color_black, LV_PART_MAIN);
etx_obj_add_style(obj, styles->border_color[COLOR_BLACK_INDEX], LV_PART_MAIN);
}

static const lv_obj_class_t color_swatch_class = {
Expand Down
12 changes: 8 additions & 4 deletions radio/src/gui/colorlcd/color_picker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ class ColorEditorPopup : public BaseDialog
void updateColor(uint32_t c)
{
m_color = c;
uint8_t r = GET_RED(c), g = GET_GREEN(c), b = GET_BLUE(c);

auto rgb = COLOR_VAL(colorToRGB(m_color));

uint8_t r = GET_RED(rgb), g = GET_GREEN(rgb), b = GET_BLUE(rgb);

colorPad->setColor(r, g, b);

Expand Down Expand Up @@ -157,8 +160,8 @@ class ColorEditorPopup : public BaseDialog
};

ColorPicker::ColorPicker(Window* parent, const rect_t& rect,
std::function<uint16_t()> getValue,
std::function<void(uint16_t)> setValue) :
std::function<uint32_t()> getValue,
std::function<void(uint32_t)> setValue) :
Button(parent, {rect.x, rect.y, ColorEditorPopup::COLOR_PAD_WIDTH, ColorEditorPopup::COLOR_PAD_HEIGHT}),
setValue(std::move(setValue))
{
Expand All @@ -180,7 +183,8 @@ void ColorPicker::updateColor(uint32_t c)
{
color = c;

auto rgb = COLOR_VAL(colorToRGB(color));
auto lvcolor =
lv_color_make(GET_RED(color), GET_GREEN(color), GET_BLUE(color));
lv_color_make(GET_RED(rgb), GET_GREEN(rgb), GET_BLUE(rgb));
lv_obj_set_style_bg_color(lvobj, lvcolor, LV_PART_MAIN);
}
6 changes: 3 additions & 3 deletions radio/src/gui/colorlcd/color_picker.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
class ColorPicker : public Button
{
uint32_t color;
std::function<void(uint16_t)> setValue;
std::function<void(uint32_t)> setValue;

void updateColor(uint32_t c);

public:
ColorPicker(Window* parent, const rect_t& rect,
std::function<uint16_t()> getValue,
std::function<void(uint16_t)> setValue = nullptr);
std::function<uint32_t()> getValue,
std::function<void(uint32_t)> setValue = nullptr);

void setColor(uint32_t c);
uint32_t getColor() const { return color; }
Expand Down
10 changes: 10 additions & 0 deletions radio/src/gui/colorlcd/colors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ LcdColorIndex indexFromColor(uint32_t lcdFlags)
return CUSTOM_COLOR_INDEX;
}

// Return flags with RGB color value instead of indexed theme color
LcdFlags colorToRGB(LcdFlags colorFlags)
{
// RGB or indexed color?
if (colorFlags & RGB_FLAG)
return colorFlags;

return (colorFlags & 0xFFFF) | COLOR(COLOR_VAL(colorFlags)) | RGB_FLAG;
}

/**
* Helper function to translate a colorFlags value to a lv_color_t suitable
* for passing to an lv_obj function
Expand Down
2 changes: 2 additions & 0 deletions radio/src/gui/colorlcd/colors.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,6 @@ extern uint32_t HSVtoRGB(float H, float S, float V);
extern void RGBtoHSV(uint8_t R, uint8_t G, uint8_t B, float& fH, float& fS,
float& fV);

LcdFlags colorToRGB(LcdFlags colorFlags);

lv_color_t makeLvColor(uint32_t colorFlags);
4 changes: 2 additions & 2 deletions radio/src/gui/colorlcd/curve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ Curve::Curve(Window* parent, const rect_t& rect,
etx_solid_bg(p, COLOR_THEME_PRIMARY2_INDEX);
etx_obj_add_style(p, styles->circle, LV_PART_MAIN);
etx_obj_add_style(p, styles->border, LV_PART_MAIN);
etx_obj_add_style(p, styles->border_color_dark, LV_PART_MAIN);
etx_obj_add_style(p, styles->border_color[COLOR_THEME_SECONDARY1_INDEX], LV_PART_MAIN);
lv_obj_set_size(p, 9, 9);
lv_obj_add_flag(p, LV_OBJ_FLAG_HIDDEN);
pointDots[i] = p;
Expand All @@ -174,7 +174,7 @@ Curve::Curve(Window* parent, const rect_t& rect,
etx_solid_bg(posPoint, COLOR_THEME_PRIMARY2_INDEX);
etx_obj_add_style(posPoint, styles->circle, LV_PART_MAIN);
etx_obj_add_style(posPoint, styles->border, LV_PART_MAIN);
etx_obj_add_style(posPoint, styles->border_color_active, LV_PART_MAIN);
etx_obj_add_style(posPoint, styles->border_color[COLOR_THEME_ACTIVE_INDEX], LV_PART_MAIN);
lv_obj_set_size(posPoint, 9, 9);

updatePosition();
Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/custom_failsafe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ChannelFailsafeBargraph : public Window
Window(parent, rect), channel(channel)
{
etx_obj_add_style(lvobj, styles->border_thin, LV_PART_MAIN);
etx_obj_add_style(lvobj, styles->border_color_black, LV_PART_MAIN);
etx_obj_add_style(lvobj, styles->border_color[COLOR_BLACK_INDEX], LV_PART_MAIN);

outputsBar = new OutputChannelBar(this, {0, 1, width() - 2, ChannelBar::BAR_HEIGHT},
channel, false, false);
Expand Down
17 changes: 4 additions & 13 deletions radio/src/gui/colorlcd/layouts/layout2x4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
const ZoneOption OPTIONS_LAYOUT_2x4[] = {
LAYOUT_COMMON_OPTIONS,
{"Panel1 background", ZoneOption::Bool, OPTION_VALUE_BOOL(true)},
{" Color", ZoneOption::Color, OPTION_VALUE_UNSIGNED(RGB(77, 112, 203))},
{" Color", ZoneOption::Color, RGB2FLAGS(77, 112, 203)},
{"Panel2 background", ZoneOption::Bool, OPTION_VALUE_BOOL(true)},
{" Color", ZoneOption::Color, OPTION_VALUE_UNSIGNED(RGB(77, 112, 203))},
{" Color", ZoneOption::Color, RGB2FLAGS(77, 112, 203)},
LAYOUT_OPTIONS_END};

class Layout2x4 : public Layout
Expand Down Expand Up @@ -92,17 +92,8 @@ class Layout2x4 : public Layout
lv_obj_add_flag(panel2, LV_OBJ_FLAG_HIDDEN);
}

LcdFlags color =
COLOR2FLAGS(getOptionValue(OPTION_PANEL1_COLOR)->unsignedValue);
if (color != panel1Color) {
panel1Color = color;
lv_obj_set_style_bg_color(panel1, makeLvColor(panel1Color), LV_PART_MAIN);
}
color = COLOR2FLAGS(getOptionValue(OPTION_PANEL2_COLOR)->unsignedValue);
if (color != panel2Color) {
panel2Color = color;
lv_obj_set_style_bg_color(panel2, makeLvColor(panel2Color), LV_PART_MAIN);
}
etx_bg_color_from_flags(panel1, getOptionValue(OPTION_PANEL1_COLOR)->unsignedValue);
etx_bg_color_from_flags(panel2, getOptionValue(OPTION_PANEL2_COLOR)->unsignedValue);
}
};

Expand Down
20 changes: 17 additions & 3 deletions radio/src/gui/colorlcd/page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@
#include "themes/etx_lv_theme.h"
#include "view_main.h"

PageHeader::PageHeader(Page* parent, EdgeTxIcon icon) :
Window(parent, {0, 0, LCD_W, EdgeTxStyles::MENU_HEADER_HEIGHT}),
icon(icon)
PageHeader::PageHeader(Window* parent, EdgeTxIcon icon) :
Window(parent, {0, 0, LCD_W, EdgeTxStyles::MENU_HEADER_HEIGHT})
{
setWindowFlag(NO_FOCUS | OPAQUE);

Expand All @@ -41,6 +40,21 @@ PageHeader::PageHeader(Page* parent, EdgeTxIcon icon) :
"", COLOR_THEME_PRIMARY2);
}

PageHeader::PageHeader(Window* parent, const char* iconFile) :
Window(parent, {0, 0, LCD_W, EdgeTxStyles::MENU_HEADER_HEIGHT})
{
setWindowFlag(NO_FOCUS | OPAQUE);

etx_solid_bg(lvobj, COLOR_THEME_SECONDARY1_INDEX);

new HeaderIcon(this, iconFile);

title = new StaticText(this,
{PAGE_TITLE_LEFT, PAGE_TITLE_TOP,
LCD_W - PAGE_TITLE_LEFT, EdgeTxStyles::PAGE_LINE_HEIGHT},
"", COLOR_THEME_PRIMARY2);
}

StaticText* PageHeader::setTitle2(std::string txt)
{
if (title2 == nullptr) {
Expand Down
4 changes: 2 additions & 2 deletions radio/src/gui/colorlcd/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class Page;
class PageHeader : public Window
{
public:
PageHeader(Page* parent, EdgeTxIcon icon);
PageHeader(Window* parent, EdgeTxIcon icon);
PageHeader(Window* parent, const char* iconFile);

void setTitle(std::string txt) { title->setText(std::move(txt)); }
StaticText* setTitle2(std::string txt);
Expand All @@ -39,7 +40,6 @@ class PageHeader : public Window
static constexpr coord_t PAGE_TITLE_TOP = 2;

protected:
EdgeTxIcon icon;
StaticText* title;
StaticText* title2 = nullptr;
};
Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/popups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ static void bubble_popup_constructor(const lv_obj_class_t* class_p, lv_obj_t* ob
etx_obj_add_style(obj, bubble_popup, LV_PART_MAIN);
etx_bg_color(obj, COLOR_WHITE_INDEX, LV_PART_MAIN);
etx_txt_color(obj, COLOR_BLACK_INDEX, LV_PART_MAIN);
etx_obj_add_style(obj, styles->border_color_black, LV_PART_MAIN);
etx_obj_add_style(obj, styles->border_color[COLOR_BLACK_INDEX], LV_PART_MAIN);
}

static const lv_obj_class_t bubble_popup_class = {
Expand Down
4 changes: 2 additions & 2 deletions radio/src/gui/colorlcd/select_fab_carousel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static void etx_quick_button_constructor(const lv_obj_class_t* class_p,
etx_obj_add_style(obj, styles->pad_medium, LV_PART_MAIN);

etx_obj_add_style(obj, styles->border, LV_PART_MAIN | LV_STATE_FOCUSED);
etx_obj_add_style(obj, styles->border_color_white,
etx_obj_add_style(obj, styles->border_color[COLOR_WHITE_INDEX],
LV_PART_MAIN | LV_STATE_FOCUSED);
}

Expand All @@ -60,7 +60,7 @@ static void etx_quick_icon_constructor(const lv_obj_class_t* class_p,
lv_obj_t* obj)
{
etx_obj_add_style(obj, styles->border, LV_PART_MAIN);
etx_obj_add_style(obj, styles->border_color_white, LV_PART_MAIN);
etx_obj_add_style(obj, styles->border_color[COLOR_WHITE_INDEX], LV_PART_MAIN);
etx_obj_add_style(obj, styles->outline, LV_PART_MAIN);
lv_obj_set_style_outline_color(obj, lv_color_black(), LV_PART_MAIN);
etx_obj_add_style(obj, styles->circle, LV_PART_MAIN);
Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/special_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static void sf_enable_state_constructor(const lv_obj_class_t *class_p,
etx_obj_add_style(obj, sf_enable_state_style, LV_PART_MAIN);
etx_obj_add_style(obj, styles->outline_color_light, LV_PART_MAIN);
etx_obj_add_style(obj, styles->border, LV_PART_MAIN);
etx_obj_add_style(obj, styles->border_color_dark, LV_PART_MAIN);
etx_obj_add_style(obj, styles->border_color[COLOR_THEME_SECONDARY1_INDEX], LV_PART_MAIN);
etx_obj_add_style(obj, styles->bg_opacity_cover, LV_PART_MAIN);
etx_bg_color(obj, COLOR_THEME_ACTIVE_INDEX, LV_STATE_CHECKED);
}
Expand Down
Loading

0 comments on commit 4d29d5d

Please sign in to comment.