Skip to content

Commit

Permalink
feat(color): add FILE type for widget options (#5605)
Browse files Browse the repository at this point in the history
  • Loading branch information
philmoz authored Oct 18, 2024
1 parent 3c3a2df commit 1deac60
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 85 deletions.
1 change: 1 addition & 0 deletions companion/src/firmwares/customisation_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ inline ZoneOptionValueEnum zoneValueEnumFromType(ZoneOption::Type type)
{
switch(type) {
case ZoneOption::String:
case ZoneOption::File:
return ZOV_String;

case ZoneOption::Integer:
Expand Down
1 change: 1 addition & 0 deletions companion/src/firmwares/customisation_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ struct ZoneOption
Align,
Slider,
Choice,
File,
};

const char * name;
Expand Down
14 changes: 7 additions & 7 deletions radio/src/datastructs.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ static inline void check_struct()
CHKSIZE(LimitData, 13);
CHKSIZE(TimerData, 17);
CHKSIZE(ModelHeader, 131);
CHKSIZE(CustomScreenData, 1452);
CHKSIZE(CustomScreenData, 1892);
#if defined(PCBNV14)
CHKTYPE(TopBarPersistentData, 540);
CHKTYPE(TopBarPersistentData, 704);
#else
CHKTYPE(TopBarPersistentData, 804);
CHKTYPE(TopBarPersistentData, 1048);
#endif
#else
#error CHKSIZE not set up
Expand All @@ -103,13 +103,13 @@ static inline void check_struct()
#elif defined(PCBX7) || defined(PCBXLITE) || defined(PCBX9LITE)
CHKSIZE(ModelData, 6329);
#elif defined(PCBNV14)
CHKSIZE(ModelData, 21899);
CHKSIZE(ModelData, 26463);
#elif defined(PCBPL18)
CHKSIZE(ModelData, 22201);
CHKSIZE(ModelData, 26845);
#elif defined(RADIO_T15)
CHKSIZE(ModelData, 22190);
CHKSIZE(ModelData, 26834);
#elif defined(PCBHORUS)
CHKSIZE(ModelData, 22165);
CHKSIZE(ModelData, 26809);
#else
#error CHKSIZE not set up
#endif
Expand Down
2 changes: 1 addition & 1 deletion radio/src/gui/colorlcd/libui/filechoice.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class FileChoice : public Choice
std::function<std::string()> getValue,
std::function<void(std::string)> setValue,
bool stripExtension = false,
const char* title = nullptr);
const char* title = "");

#if defined(DEBUG_WINDOWS)
std::string getName() const override { return "FileChoice"; }
Expand Down
15 changes: 15 additions & 0 deletions radio/src/gui/colorlcd/mainview/widget_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "sourcechoice.h"
#include "switchchoice.h"
#include "view_main.h"
#include "filechoice.h"

#define SET_DIRTY() storageDirty(EE_MODEL)

Expand Down Expand Up @@ -191,6 +192,20 @@ WidgetSettings::WidgetSettings(Widget* w) :
SET_DIRTY();
});
break;

case ZoneOption::File:
new FileChoice(line, rect_t{}, opt->fileSelectPath, nullptr, LEN_ZONE_OPTION_STRING,
[=]() {
char s[LEN_ZONE_OPTION_STRING + 1];
strncpy(s, optVal->stringValue, LEN_ZONE_OPTION_STRING);
s[LEN_ZONE_OPTION_STRING] = 0;
return std::string(s);
},
[=](std::string s) {
strncpy(optVal->stringValue, s.c_str(), LEN_ZONE_OPTION_STRING);
SET_DIRTY();
});
break;
}

optIdx++;
Expand Down
5 changes: 4 additions & 1 deletion radio/src/gui/colorlcd/mainview/zone.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "storage/yaml/yaml_defs.h"
#include <vector>

#define LEN_ZONE_OPTION_STRING 8
#define LEN_ZONE_OPTION_STRING 12

#if defined(_MSC_VER)
#define OPTION_VALUE_UNSIGNED(x) { uint32_t(x) }
Expand Down Expand Up @@ -74,6 +74,7 @@ struct ZoneOption
Align,
Slider,
Choice,
File,
};

const char * name;
Expand All @@ -82,6 +83,7 @@ struct ZoneOption
ZoneOptionValue min;
ZoneOptionValue max;
const char * displayName;
std::string fileSelectPath;
std::vector<std::string> choiceValues;
};

Expand All @@ -105,6 +107,7 @@ inline ZoneOptionValueEnum zoneValueEnumFromType(ZoneOption::Type type)
{
switch(type) {
case ZoneOption::String:
case ZoneOption::File:
return ZOV_String;

case ZoneOption::Integer:
Expand Down
1 change: 1 addition & 0 deletions radio/src/lua/api_general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3118,6 +3118,7 @@ LROT_BEGIN(etxcst, NULL, 0)
LROT_NUMENTRY( SWITCH, ZoneOption::Switch )
LROT_NUMENTRY( SLIDER, ZoneOption::Slider )
LROT_NUMENTRY( CHOICE, ZoneOption::Choice )
LROT_NUMENTRY( FILE, ZoneOption::File )
LROT_NUMENTRY( MENU_HEADER_HEIGHT, COLOR2FLAGS(EdgeTxStyles::MENU_HEADER_HEIGHT) )

// Colors gui/colorlcd/colors.h
Expand Down
1 change: 1 addition & 0 deletions radio/src/lua/lua_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ void LuaWidget::update()
auto optVal = getOptionValue(i);
switch (option->type) {
case ZoneOption::String:
case ZoneOption::File:
{
char str[LEN_ZONE_OPTION_STRING + 1] = {0};
strncpy(str, optVal->stringValue, LEN_ZONE_OPTION_STRING);
Expand Down
2 changes: 1 addition & 1 deletion radio/src/lua/lua_widget_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Widget* LuaWidgetFactory::create(Window* parent, const rect_t& rect,
lua_newtable(lsWidgets);
int i = 0;
for (const ZoneOption* option = options; option->name; option++, i++) {
if (option->type == ZoneOption::String) {
if (option->type == ZoneOption::String || option->type == ZoneOption::File) {
lua_pushstring(lsWidgets, option->name);
// Zero-terminated string for Lua
char str[LEN_ZONE_OPTION_STRING + 1] = {0};
Expand Down
34 changes: 14 additions & 20 deletions radio/src/lua/widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,21 +134,18 @@ ZoneOption *createOptionsArray(int reference, uint8_t maxOptions)
luaL_checktype(lsWidgets, -2, LUA_TNUMBER); // key is number
switch (field) {
case 0:
luaL_checktype(lsWidgets, -1, LUA_TSTRING); // value is string
option->name = lua_tostring(lsWidgets, -1);
option->name = luaL_checkstring(lsWidgets, -1);
option->displayName = nullptr;
// TRACE("name = %s", option->name);
break;
case 1:
luaL_checktype(lsWidgets, -1, LUA_TNUMBER); // value is number
option->type = (ZoneOption::Type)lua_tointeger(lsWidgets, -1);
option->type = (ZoneOption::Type)luaL_checkinteger(lsWidgets, -1);
option->deflt.unsignedValue = 0;
// set some sensible defaults
if (option->type == ZoneOption::Integer) {
// set some sensible defaults
option->min.signedValue = -100;
option->max.signedValue = 100;
} else if (option->type == ZoneOption::Switch) {
// set some sensible defaults
option->min.signedValue = SWSRC_FIRST;
option->max.signedValue = SWSRC_LAST;
} else if (option->type == ZoneOption::Timer) {
Expand All @@ -157,7 +154,7 @@ ZoneOption *createOptionsArray(int reference, uint8_t maxOptions)
} else if (option->type == ZoneOption::TextSize) {
option->min.unsignedValue = FONT_STD_INDEX;
option->max.unsignedValue = FONTS_COUNT - 1;
} else if (option->type == ZoneOption::String) {
} else if (option->type == ZoneOption::String || option->type == ZoneOption::File) {
option->deflt.stringValue[0] = 0;
} else if (option->type == ZoneOption::Slider) {
option->min.unsignedValue = 0;
Expand All @@ -166,34 +163,31 @@ ZoneOption *createOptionsArray(int reference, uint8_t maxOptions)
break;
case 2:
if (option->type == ZoneOption::Integer || option->type == ZoneOption::Switch) {
luaL_checktype(lsWidgets, -1, LUA_TNUMBER); // value is number
option->deflt.signedValue = lua_tointeger(lsWidgets, -1);
option->deflt.signedValue = luaL_checkinteger(lsWidgets, -1);
} else if (option->type == ZoneOption::Bool) {
luaL_checktype(lsWidgets, -1, LUA_TNUMBER); // value is number
option->deflt.boolValue = (lua_tounsigned(lsWidgets, -1) != 0);
} else if (option->type == ZoneOption::String) {
strncpy(option->deflt.stringValue, lua_tostring(lsWidgets, -1),
option->deflt.boolValue = (luaL_checkunsigned(lsWidgets, -1) != 0);
} else if (option->type == ZoneOption::String || option->type == ZoneOption::File) {
strncpy(option->deflt.stringValue, luaL_checkstring(lsWidgets, -1),
LEN_ZONE_OPTION_STRING);
} else {
luaL_checktype(lsWidgets, -1, LUA_TNUMBER); // value is number
option->deflt.unsignedValue = lua_tounsigned(lsWidgets, -1);
option->deflt.unsignedValue = luaL_checkunsigned(lsWidgets, -1);
}
break;
case 3:
if (option->type == ZoneOption::Integer || option->type == ZoneOption::Switch || option->type == ZoneOption::Slider) {
luaL_checktype(lsWidgets, -1, LUA_TNUMBER); // value is number
option->min.signedValue = lua_tointeger(lsWidgets, -1);
option->min.signedValue = luaL_checkinteger(lsWidgets, -1);
} else if (option->type == ZoneOption::Choice) {
luaL_checktype(lsWidgets, -1, LUA_TTABLE); // value is a table
for (lua_pushnil(lsWidgets); lua_next(lsWidgets, -2); lua_pop(lsWidgets, 1)) {
option->choiceValues.push_back(lua_tostring(lsWidgets, -1));
option->choiceValues.push_back(luaL_checkstring(lsWidgets, -1));
}
} else if (option->type == ZoneOption::File) {
option->fileSelectPath = luaL_checkstring(lsWidgets, -1);
}
break;
case 4:
if (option->type == ZoneOption::Integer || option->type == ZoneOption::Switch || option->type == ZoneOption::Slider) {
luaL_checktype(lsWidgets, -1, LUA_TNUMBER); // value is number
option->max.signedValue = lua_tointeger(lsWidgets, -1);
option->max.signedValue = luaL_checkinteger(lsWidgets, -1);
}
break;
}
Expand Down
22 changes: 11 additions & 11 deletions radio/src/storage/yaml/yaml_datastructs_f16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -767,41 +767,41 @@ static const struct YamlNode union_ZoneOptionValue_elmts[] = {
YAML_UNSIGNED( "unsignedValue", 32 ),
YAML_SIGNED( "signedValue", 32 ),
YAML_UNSIGNED( "boolValue", 32 ),
YAML_STRING("stringValue", 8),
YAML_STRING("stringValue", 12),
YAML_CUSTOM("source",r_zov_source,w_zov_source),
YAML_CUSTOM("color",r_zov_color,w_zov_color),
YAML_END
};
static const struct YamlNode struct_ZoneOptionValueTyped[] = {
YAML_IDX,
YAML_ENUM("type", 32, enum_ZoneOptionValueEnum),
YAML_UNION("value", 64, union_ZoneOptionValue_elmts, select_zov),
YAML_UNION("value", 96, union_ZoneOptionValue_elmts, select_zov),
YAML_END
};
static const struct YamlNode struct_WidgetPersistentData[] = {
YAML_ARRAY("options", 96, 10, struct_ZoneOptionValueTyped, NULL),
YAML_ARRAY("options", 128, 10, struct_ZoneOptionValueTyped, NULL),
YAML_END
};
static const struct YamlNode struct_ZonePersistentData[] = {
YAML_IDX,
YAML_STRING("widgetName", 12),
YAML_STRUCT("widgetData", 960, struct_WidgetPersistentData, NULL),
YAML_STRUCT("widgetData", 1280, struct_WidgetPersistentData, NULL),
YAML_END
};
static const struct YamlNode struct_LayoutPersistentData[] = {
YAML_ARRAY("zones", 1056, 10, struct_ZonePersistentData, NULL),
YAML_ARRAY("options", 96, 10, struct_ZoneOptionValueTyped, NULL),
YAML_ARRAY("zones", 1376, 10, struct_ZonePersistentData, NULL),
YAML_ARRAY("options", 128, 10, struct_ZoneOptionValueTyped, NULL),
YAML_END
};
static const struct YamlNode struct_CustomScreenData[] = {
YAML_IDX,
YAML_STRING("LayoutId", 12),
YAML_STRUCT("layoutData", 11520, struct_LayoutPersistentData, NULL),
YAML_STRUCT("layoutData", 15040, struct_LayoutPersistentData, NULL),
YAML_END
};
static const struct YamlNode struct_TopBarPersistentData[] = {
YAML_ARRAY("zones", 1056, 6, struct_ZonePersistentData, NULL),
YAML_ARRAY("options", 96, 1, struct_ZoneOptionValueTyped, NULL),
YAML_ARRAY("zones", 1376, 6, struct_ZonePersistentData, NULL),
YAML_ARRAY("options", 128, 1, struct_ZoneOptionValueTyped, NULL),
YAML_END
};
static const struct YamlNode struct_USBJoystickChData[] = {
Expand Down Expand Up @@ -865,8 +865,8 @@ static const struct YamlNode struct_ModelData[] = {
YAML_UNSIGNED( "potsWarnEnabled", 16 ),
YAML_ARRAY("potsWarnPosition", 8, 16, struct_signed_8, NULL),
YAML_ARRAY("telemetrySensors", 112, 60, struct_TelemetrySensor, NULL),
YAML_ARRAY("screenData", 11616, 10, struct_CustomScreenData, NULL),
YAML_STRUCT("topbarData", 6432, struct_TopBarPersistentData, NULL),
YAML_ARRAY("screenData", 15136, 10, struct_CustomScreenData, NULL),
YAML_STRUCT("topbarData", 8384, struct_TopBarPersistentData, NULL),
YAML_ARRAY("topbarWidgetWidth", 8, 6, struct_unsigned_8, NULL),
YAML_UNSIGNED( "view", 8 ),
YAML_STRING("modelRegistrationID", 8),
Expand Down
22 changes: 11 additions & 11 deletions radio/src/storage/yaml/yaml_datastructs_nv14.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,41 +764,41 @@ static const struct YamlNode union_ZoneOptionValue_elmts[] = {
YAML_UNSIGNED( "unsignedValue", 32 ),
YAML_SIGNED( "signedValue", 32 ),
YAML_UNSIGNED( "boolValue", 32 ),
YAML_STRING("stringValue", 8),
YAML_STRING("stringValue", 12),
YAML_CUSTOM("source",r_zov_source,w_zov_source),
YAML_CUSTOM("color",r_zov_color,w_zov_color),
YAML_END
};
static const struct YamlNode struct_ZoneOptionValueTyped[] = {
YAML_IDX,
YAML_ENUM("type", 32, enum_ZoneOptionValueEnum),
YAML_UNION("value", 64, union_ZoneOptionValue_elmts, select_zov),
YAML_UNION("value", 96, union_ZoneOptionValue_elmts, select_zov),
YAML_END
};
static const struct YamlNode struct_WidgetPersistentData[] = {
YAML_ARRAY("options", 96, 10, struct_ZoneOptionValueTyped, NULL),
YAML_ARRAY("options", 128, 10, struct_ZoneOptionValueTyped, NULL),
YAML_END
};
static const struct YamlNode struct_ZonePersistentData[] = {
YAML_IDX,
YAML_STRING("widgetName", 12),
YAML_STRUCT("widgetData", 960, struct_WidgetPersistentData, NULL),
YAML_STRUCT("widgetData", 1280, struct_WidgetPersistentData, NULL),
YAML_END
};
static const struct YamlNode struct_LayoutPersistentData[] = {
YAML_ARRAY("zones", 1056, 10, struct_ZonePersistentData, NULL),
YAML_ARRAY("options", 96, 10, struct_ZoneOptionValueTyped, NULL),
YAML_ARRAY("zones", 1376, 10, struct_ZonePersistentData, NULL),
YAML_ARRAY("options", 128, 10, struct_ZoneOptionValueTyped, NULL),
YAML_END
};
static const struct YamlNode struct_CustomScreenData[] = {
YAML_IDX,
YAML_STRING("LayoutId", 12),
YAML_STRUCT("layoutData", 11520, struct_LayoutPersistentData, NULL),
YAML_STRUCT("layoutData", 15040, struct_LayoutPersistentData, NULL),
YAML_END
};
static const struct YamlNode struct_TopBarPersistentData[] = {
YAML_ARRAY("zones", 1056, 4, struct_ZonePersistentData, NULL),
YAML_ARRAY("options", 96, 1, struct_ZoneOptionValueTyped, NULL),
YAML_ARRAY("zones", 1376, 4, struct_ZonePersistentData, NULL),
YAML_ARRAY("options", 128, 1, struct_ZoneOptionValueTyped, NULL),
YAML_END
};
static const struct YamlNode struct_USBJoystickChData[] = {
Expand Down Expand Up @@ -863,8 +863,8 @@ static const struct YamlNode struct_ModelData[] = {
YAML_UNSIGNED( "potsWarnEnabled", 16 ),
YAML_ARRAY("potsWarnPosition", 8, 16, struct_signed_8, NULL),
YAML_ARRAY("telemetrySensors", 112, 60, struct_TelemetrySensor, NULL),
YAML_ARRAY("screenData", 11616, 10, struct_CustomScreenData, NULL),
YAML_STRUCT("topbarData", 4320, struct_TopBarPersistentData, NULL),
YAML_ARRAY("screenData", 15136, 10, struct_CustomScreenData, NULL),
YAML_STRUCT("topbarData", 5632, struct_TopBarPersistentData, NULL),
YAML_ARRAY("topbarWidgetWidth", 8, 4, struct_unsigned_8, NULL),
YAML_UNSIGNED( "view", 8 ),
YAML_STRING("modelRegistrationID", 8),
Expand Down
Loading

0 comments on commit 1deac60

Please sign in to comment.