Output widget colors as such

This commit is contained in:
Raphael Coeffic
2021-11-10 11:07:03 +01:00
parent a356b5199d
commit cc9b146713
14 changed files with 132 additions and 4 deletions
+5 -1
View File
@@ -46,6 +46,7 @@ union ZoneOptionValue
uint32_t boolValue; uint32_t boolValue;
char stringValue[LEN_ZONE_OPTION_STRING]; char stringValue[LEN_ZONE_OPTION_STRING];
CUST_ATTR(source, r_zov_source, w_zov_source); CUST_ATTR(source, r_zov_source, w_zov_source);
CUST_ATTR(color, r_zov_color, w_zov_color);
}; };
enum ZoneOptionValueEnum { enum ZoneOptionValueEnum {
@@ -53,7 +54,8 @@ enum ZoneOptionValueEnum {
ZOV_Signed, ZOV_Signed,
ZOV_Bool, ZOV_Bool,
ZOV_String, ZOV_String,
ZOV_Source ZOV_Source,
ZOV_Color
}; };
struct ZoneOption struct ZoneOption
@@ -101,6 +103,8 @@ inline ZoneOptionValueEnum zoneValueEnumFromType(ZoneOption::Type type)
return ZOV_Source; return ZOV_Source;
case ZoneOption::Color: case ZoneOption::Color:
return ZOV_Color;
case ZoneOption::Timer: case ZoneOption::Timer:
case ZoneOption::Switch: case ZoneOption::Switch:
case ZoneOption::TextSize: case ZoneOption::TextSize:
@@ -78,8 +78,18 @@ void patchWidgetOptions(ZonePersistentData* zones, unsigned n_zones)
if (widgetName.empty()) continue; if (widgetName.empty()) continue;
if (widgetName == "Value" || widgetName == "Gauge") { if (widgetName == "Value") {
zone.widgetData.options[0].type = ZOV_Source; zone.widgetData.options[0].type = ZOV_Source;
zone.widgetData.options[1].type = ZOV_Color;
} else if (widgetName == "Gauge") {
zone.widgetData.options[0].type = ZOV_Source;
zone.widgetData.options[3].type = ZOV_Color;
} else if (widgetName == "Outputs") {
zone.widgetData.options[2].type = ZOV_Color;
zone.widgetData.options[3].type = ZOV_Color;
zone.widgetData.options[4].type = ZOV_Color;
} else if (widgetName == "Text") {
zone.widgetData.options[1].type = ZOV_Color;
} }
} }
} }
@@ -168,7 +168,7 @@ uint8_t select_zov(void* user, uint8_t* data, uint32_t bitoffs)
data += bitoffs >> 3UL; data += bitoffs >> 3UL;
data -= sizeof(ZoneOptionValueEnum); data -= sizeof(ZoneOptionValueEnum);
ZoneOptionValueEnum* p_zovt = (ZoneOptionValueEnum*)data; ZoneOptionValueEnum* p_zovt = (ZoneOptionValueEnum*)data;
if (*p_zovt > ZOV_Source) return 0; if (*p_zovt > ZOV_Color) return 0;
return *p_zovt; return *p_zovt;
} }
@@ -181,6 +181,23 @@ bool w_zov_source(void* user, uint8_t* data, uint32_t bitoffs,
auto p_val = reinterpret_cast<ZoneOptionValue*>(data); auto p_val = reinterpret_cast<ZoneOptionValue*>(data);
return w_mixSrcRaw(nullptr, p_val->unsignedValue, wf, opaque); return w_mixSrcRaw(nullptr, p_val->unsignedValue, wf, opaque);
} }
#include "colors.h"
#define r_zov_color nullptr
bool w_zov_color(void* user, uint8_t* data, uint32_t bitoffs,
yaml_writer_func wf, void* opaque)
{
data += bitoffs >> 3UL;
auto p_val = reinterpret_cast<ZoneOptionValue*>(data);
uint32_t color = (uint32_t)GET_RED(p_val->unsignedValue) << 16 |
(uint32_t)GET_GREEN(p_val->unsignedValue) << 8 |
(uint32_t)GET_BLUE(p_val->unsignedValue);
if (!wf(opaque, "0x", 2)) return false;
return wf(opaque, yaml_rgb2hex(color), 3 * 2);
}
#endif #endif
uint8_t select_mod_type(void* user, uint8_t* data, uint32_t bitoffs) uint8_t select_mod_type(void* user, uint8_t* data, uint32_t bitoffs)
@@ -85,6 +85,7 @@ const struct YamlIdStr enum_ZoneOptionValueEnum[] = {
{ ZOV_Bool, "Bool" }, { ZOV_Bool, "Bool" },
{ ZOV_String, "String" }, { ZOV_String, "String" },
{ ZOV_Source, "Source" }, { ZOV_Source, "Source" },
{ ZOV_Color, "Color" },
{ 0, NULL } { 0, NULL }
}; };
const struct YamlIdStr enum_TimerModes[] = { const struct YamlIdStr enum_TimerModes[] = {
@@ -336,6 +337,7 @@ static const struct YamlNode union_ZoneOptionValue_elmts[] = {
YAML_UNSIGNED( "boolValue", 32 ), YAML_UNSIGNED( "boolValue", 32 ),
YAML_STRING("stringValue", 8), YAML_STRING("stringValue", 8),
YAML_CUSTOM("source",r_zov_source,w_zov_source), YAML_CUSTOM("source",r_zov_source,w_zov_source),
YAML_CUSTOM("color",r_zov_color,w_zov_color),
YAML_END YAML_END
}; };
static const struct YamlNode struct_ZoneOptionValueTyped[] = { static const struct YamlNode struct_ZoneOptionValueTyped[] = {
@@ -85,6 +85,7 @@ const struct YamlIdStr enum_ZoneOptionValueEnum[] = {
{ ZOV_Bool, "Bool" }, { ZOV_Bool, "Bool" },
{ ZOV_String, "String" }, { ZOV_String, "String" },
{ ZOV_Source, "Source" }, { ZOV_Source, "Source" },
{ ZOV_Color, "Color" },
{ 0, NULL } { 0, NULL }
}; };
const struct YamlIdStr enum_TimerModes[] = { const struct YamlIdStr enum_TimerModes[] = {
@@ -357,6 +358,7 @@ static const struct YamlNode union_ZoneOptionValue_elmts[] = {
YAML_UNSIGNED( "boolValue", 32 ), YAML_UNSIGNED( "boolValue", 32 ),
YAML_STRING("stringValue", 8), YAML_STRING("stringValue", 8),
YAML_CUSTOM("source",r_zov_source,w_zov_source), YAML_CUSTOM("source",r_zov_source,w_zov_source),
YAML_CUSTOM("color",r_zov_color,w_zov_color),
YAML_END YAML_END
}; };
static const struct YamlNode struct_ZoneOptionValueTyped[] = { static const struct YamlNode struct_ZoneOptionValueTyped[] = {
@@ -85,6 +85,7 @@ const struct YamlIdStr enum_ZoneOptionValueEnum[] = {
{ ZOV_Bool, "Bool" }, { ZOV_Bool, "Bool" },
{ ZOV_String, "String" }, { ZOV_String, "String" },
{ ZOV_Source, "Source" }, { ZOV_Source, "Source" },
{ ZOV_Color, "Color" },
{ 0, NULL } { 0, NULL }
}; };
const struct YamlIdStr enum_TimerModes[] = { const struct YamlIdStr enum_TimerModes[] = {
@@ -357,6 +358,7 @@ static const struct YamlNode union_ZoneOptionValue_elmts[] = {
YAML_UNSIGNED( "boolValue", 32 ), YAML_UNSIGNED( "boolValue", 32 ),
YAML_STRING("stringValue", 8), YAML_STRING("stringValue", 8),
YAML_CUSTOM("source",r_zov_source,w_zov_source), YAML_CUSTOM("source",r_zov_source,w_zov_source),
YAML_CUSTOM("color",r_zov_color,w_zov_color),
YAML_END YAML_END
}; };
static const struct YamlNode struct_ZoneOptionValueTyped[] = { static const struct YamlNode struct_ZoneOptionValueTyped[] = {
@@ -37,7 +37,8 @@ enum ZoneOptionValueEnum {
ZOV_Bool, ZOV_Bool,
ZOV_String, ZOV_String,
// added for conversion: // added for conversion:
ZOV_Source ZOV_Source,
ZOV_Color
}; };
struct ZoneOption struct ZoneOption
+48
View File
@@ -163,6 +163,29 @@ uint32_t yaml_str2uint(const char* val, uint8_t val_len)
return yaml_str2uint_ref(val, val_len); return yaml_str2uint_ref(val, val_len);
} }
uint32_t yaml_hex2uint(const char* val, uint8_t val_len)
{
uint32_t i_val = 0;
while(val_len > 0) {
if (*val >= '0' && *val <= '9') {
i_val <<= 4;
i_val |= (*val - '0') & 0xF;
} else if (*val >= 'A' && *val <= 'F') {
i_val <<= 4;
i_val |= (*val - 'A' + 10) & 0xF;
} else if (*val >= 'a' && *val <= 'f') {
i_val <<= 4;
i_val |= (*val - 'a' + 10) & 0xF;
} else {
break;
}
val++; val_len--;
}
return i_val;
}
static char int2str_buffer[MAX_STR] = {0}; static char int2str_buffer[MAX_STR] = {0};
static const char _int2str_lookup[] = { '0', '1', '2', '3', '4', '5', '6' , '7', '8', '9' }; static const char _int2str_lookup[] = { '0', '1', '2', '3', '4', '5', '6' , '7', '8', '9' };
@@ -188,6 +211,31 @@ char* yaml_signed2str(int32_t i)
return yaml_unsigned2str((uint32_t)i); return yaml_unsigned2str((uint32_t)i);
} }
static const char _int2hex_lookup[] = {'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'A', 'B',
'C', 'D', 'E', 'F'};
char* yaml_unsigned2hex(uint32_t i)
{
char* c = int2str_buffer;
for (int n = sizeof(uint32_t) * 2; n > 0; n--) {
*(c++) = _int2hex_lookup[(i >> ((n - 1) * 4)) & 0xF];
}
*c = '\0';
return int2str_buffer;
}
char* yaml_rgb2hex(uint32_t i)
{
char* c = int2str_buffer;
for (int n = 3 * 2; n > 0; n--) {
*(c++) = _int2hex_lookup[(i >> ((n - 1) * 4)) & 0xF];
}
*c = '\0';
return int2str_buffer;
}
int32_t yaml_to_signed(uint32_t i, uint32_t bits) int32_t yaml_to_signed(uint32_t i, uint32_t bits)
{ {
if (i & (1 << (bits-1))) { if (i & (1 << (bits-1))) {
+5
View File
@@ -36,9 +36,14 @@ uint32_t yaml_str2uint_ref(const char* val, uint8_t val_len);
int32_t yaml_str2int(const char* val, uint8_t val_len); int32_t yaml_str2int(const char* val, uint8_t val_len);
uint32_t yaml_str2uint(const char* val, uint8_t val_len); uint32_t yaml_str2uint(const char* val, uint8_t val_len);
uint32_t yaml_hex2uint(const char* val, uint8_t val_len);
char* yaml_unsigned2str(uint32_t i); char* yaml_unsigned2str(uint32_t i);
char* yaml_signed2str(int32_t i); char* yaml_signed2str(int32_t i);
char* yaml_unsigned2hex(uint32_t i);
char* yaml_rgb2hex(uint32_t i);
int32_t yaml_to_signed(uint32_t i, uint32_t bits); int32_t yaml_to_signed(uint32_t i, uint32_t bits);
#endif #endif
@@ -73,6 +73,9 @@ namespace yaml_conv_220 {
bool w_zov_source(void* user, uint8_t* data, uint32_t bitoffs, bool w_zov_source(void* user, uint8_t* data, uint32_t bitoffs,
yaml_writer_func wf, void* opaque); yaml_writer_func wf, void* opaque);
bool w_zov_color(void* user, uint8_t* data, uint32_t bitoffs,
yaml_writer_func wf, void* opaque);
}; };
// //
@@ -284,6 +287,30 @@ bool w_zov_source(void* user, uint8_t* data, uint32_t bitoffs,
{ {
return yaml_conv_220::w_zov_source(user, data, bitoffs, wf, opaque); return yaml_conv_220::w_zov_source(user, data, bitoffs, wf, opaque);
} }
void r_zov_color(void* user, uint8_t* data, uint32_t bitoffs,
const char* val, uint8_t val_len)
{
if (val_len < sizeof("0xFFFFFF")-1
|| val[0] != '0'
|| val[1] != 'x')
return;
val += 2; val_len -= 2;
data += bitoffs >> 3UL;
auto p_val = reinterpret_cast<ZoneOptionValue*>(data);
auto rgb24 = yaml_hex2uint(val, val_len);
p_val->unsignedValue =
RGB((rgb24 & 0xFF0000) >> 16, (rgb24 & 0xFF00) >> 8, rgb24 & 0xFF);
}
bool w_zov_color(void* user, uint8_t* data, uint32_t bitoffs,
yaml_writer_func wf, void* opaque)
{
return yaml_conv_220::w_zov_color(user, data, bitoffs, wf, opaque);
}
#endif #endif
static uint8_t select_mod_type(void* user, uint8_t* data, uint32_t bitoffs) static uint8_t select_mod_type(void* user, uint8_t* data, uint32_t bitoffs)
@@ -85,6 +85,7 @@ const struct YamlIdStr enum_ZoneOptionValueEnum[] = {
{ ZOV_Bool, "Bool" }, { ZOV_Bool, "Bool" },
{ ZOV_String, "String" }, { ZOV_String, "String" },
{ ZOV_Source, "Source" }, { ZOV_Source, "Source" },
{ ZOV_Color, "Color" },
{ 0, NULL } { 0, NULL }
}; };
const struct YamlIdStr enum_TimerModes[] = { const struct YamlIdStr enum_TimerModes[] = {
@@ -336,6 +337,7 @@ static const struct YamlNode union_ZoneOptionValue_elmts[] = {
YAML_UNSIGNED( "boolValue", 32 ), YAML_UNSIGNED( "boolValue", 32 ),
YAML_STRING("stringValue", 8), YAML_STRING("stringValue", 8),
YAML_CUSTOM("source",r_zov_source,w_zov_source), YAML_CUSTOM("source",r_zov_source,w_zov_source),
YAML_CUSTOM("color",r_zov_color,w_zov_color),
YAML_END YAML_END
}; };
static const struct YamlNode struct_ZoneOptionValueTyped[] = { static const struct YamlNode struct_ZoneOptionValueTyped[] = {
@@ -85,6 +85,7 @@ const struct YamlIdStr enum_ZoneOptionValueEnum[] = {
{ ZOV_Bool, "Bool" }, { ZOV_Bool, "Bool" },
{ ZOV_String, "String" }, { ZOV_String, "String" },
{ ZOV_Source, "Source" }, { ZOV_Source, "Source" },
{ ZOV_Color, "Color" },
{ 0, NULL } { 0, NULL }
}; };
const struct YamlIdStr enum_TimerModes[] = { const struct YamlIdStr enum_TimerModes[] = {
@@ -357,6 +358,7 @@ static const struct YamlNode union_ZoneOptionValue_elmts[] = {
YAML_UNSIGNED( "boolValue", 32 ), YAML_UNSIGNED( "boolValue", 32 ),
YAML_STRING("stringValue", 8), YAML_STRING("stringValue", 8),
YAML_CUSTOM("source",r_zov_source,w_zov_source), YAML_CUSTOM("source",r_zov_source,w_zov_source),
YAML_CUSTOM("color",r_zov_color,w_zov_color),
YAML_END YAML_END
}; };
static const struct YamlNode struct_ZoneOptionValueTyped[] = { static const struct YamlNode struct_ZoneOptionValueTyped[] = {
@@ -85,6 +85,7 @@ const struct YamlIdStr enum_ZoneOptionValueEnum[] = {
{ ZOV_Bool, "Bool" }, { ZOV_Bool, "Bool" },
{ ZOV_String, "String" }, { ZOV_String, "String" },
{ ZOV_Source, "Source" }, { ZOV_Source, "Source" },
{ ZOV_Color, "Color" },
{ 0, NULL } { 0, NULL }
}; };
const struct YamlIdStr enum_TimerModes[] = { const struct YamlIdStr enum_TimerModes[] = {
@@ -357,6 +358,7 @@ static const struct YamlNode union_ZoneOptionValue_elmts[] = {
YAML_UNSIGNED( "boolValue", 32 ), YAML_UNSIGNED( "boolValue", 32 ),
YAML_STRING("stringValue", 8), YAML_STRING("stringValue", 8),
YAML_CUSTOM("source",r_zov_source,w_zov_source), YAML_CUSTOM("source",r_zov_source,w_zov_source),
YAML_CUSTOM("color",r_zov_color,w_zov_color),
YAML_END YAML_END
}; };
static const struct YamlNode struct_ZoneOptionValueTyped[] = { static const struct YamlNode struct_ZoneOptionValueTyped[] = {
+4
View File
@@ -420,6 +420,10 @@ TEST(Conversions, ConversionTX16SFrom25)
const auto& top_option = top_widget.widgetData.options[0]; const auto& top_option = top_widget.widgetData.options[0];
EXPECT_EQ(ZOV_Source, top_option.type); EXPECT_EQ(ZOV_Source, top_option.type);
EXPECT_EQ(MIXSRC_TX_VOLTAGE, top_option.value.unsignedValue); EXPECT_EQ(MIXSRC_TX_VOLTAGE, top_option.value.unsignedValue);
const auto& color_option = top_widget.widgetData.options[1];
EXPECT_EQ(ZOV_Color, color_option.type);
EXPECT_EQ(0xFFFF, color_option.value.unsignedValue);
} }
#endif #endif