fix(track-core): sync track proto fields

This commit is contained in:
2026-06-17 15:05:37 +08:00
parent 5ed07253d8
commit 7a988778f4
5 changed files with 55 additions and 6 deletions
+7
View File
@@ -17,6 +17,9 @@ struct TrackSchemeDecoder {
[[nodiscard]] [[nodiscard]]
static TrackSchemeDecoder from_proto(const proto_type &proto); static TrackSchemeDecoder from_proto(const proto_type &proto);
[[nodiscard]]
proto_type to_proto() const;
[[nodiscard]] [[nodiscard]]
expected<track_core::DecodedScheme, error_t> decode_core() const; expected<track_core::DecodedScheme, error_t> decode_core() const;
@@ -37,6 +40,8 @@ struct TrackSchemeMgr {
error_t err{ESP_OK}; error_t err{ESP_OK};
static Add from_proto(const proto_type &proto); static Add from_proto(const proto_type &proto);
[[nodiscard]]
proto_type to_proto() const;
}; };
struct Clear {}; struct Clear {};
@@ -52,6 +57,8 @@ struct TrackSchemeMgr {
explicit TrackSchemeMgr() = default; explicit TrackSchemeMgr() = default;
static TrackSchemeMgr from_proto(const proto_type &proto); static TrackSchemeMgr from_proto(const proto_type &proto);
[[nodiscard]]
proto_type to_proto() const;
std::variant<Unknown, Add, Clear> choice{Unknown{}}; std::variant<Unknown, Add, Clear> choice{Unknown{}};
}; };
+9 -3
View File
@@ -809,6 +809,7 @@ struct TrackTestParameters {
uint16_t segment_led_start{0}; uint16_t segment_led_start{0};
uint16_t segment_led_count{0}; uint16_t segment_led_count{0};
uint16_t highlight_last_n_leds{0}; uint16_t highlight_last_n_leds{0};
uint8_t blink_palette_index{0};
static TrackTestParameters from_proto(const proto_type &proto) { static TrackTestParameters from_proto(const proto_type &proto) {
TrackTestParameters params; TrackTestParameters params;
@@ -821,6 +822,7 @@ struct TrackTestParameters {
params.segment_led_start = proto.segment_led_start; params.segment_led_start = proto.segment_led_start;
params.segment_led_count = proto.segment_led_count; params.segment_led_count = proto.segment_led_count;
params.highlight_last_n_leds = proto.highlight_last_n_leds; params.highlight_last_n_leds = proto.highlight_last_n_leds;
params.blink_palette_index = proto.blink_palette_index;
return params; return params;
} }
@@ -835,10 +837,10 @@ struct TrackTestParameters {
proto.segment_led_start = segment_led_start; proto.segment_led_start = segment_led_start;
proto.segment_led_count = segment_led_count; proto.segment_led_count = segment_led_count;
proto.highlight_last_n_leds = highlight_last_n_leds; proto.highlight_last_n_leds = highlight_last_n_leds;
proto.blink_palette_index = blink_palette_index;
return proto; return proto;
} }
void log(const char *tag, esp_log_level_t level) const { void log(const char *tag, esp_log_level_t level) const {
ESP_LOG_LEVEL(level, tag, ESP_LOG_LEVEL(level, tag,
"TrackTestParameters{" "TrackTestParameters{"
@@ -847,13 +849,17 @@ struct TrackTestParameters {
".blink_period_ms=%" PRIu32 ", " ".blink_period_ms=%" PRIu32 ", "
".rainbow_move_speed_leds_per_sec=%" PRIu32 ", " ".rainbow_move_speed_leds_per_sec=%" PRIu32 ", "
".segment_led_start=%" PRIu16 ", " ".segment_led_start=%" PRIu16 ", "
".segment_led_count=%" PRIu16 "}", ".segment_led_count=%" PRIu16 ", "
".highlight_last_n_leds=%" PRIu16 ", "
".blink_palette_index=%" PRIu8 "}",
blink_color.string().c_str(), blink_color.string().c_str(),
blink_duty_cycle_percent, blink_duty_cycle_percent,
blink_period_ms, blink_period_ms,
rainbow_move_speed_leds_per_sec, rainbow_move_speed_leds_per_sec,
segment_led_start, segment_led_start,
segment_led_count); segment_led_count,
highlight_last_n_leds,
blink_palette_index);
} }
}; };
+36
View File
@@ -1,6 +1,7 @@
#include "app_track_decoder.hpp" #include "app_track_decoder.hpp"
#include <algorithm> #include <algorithm>
#include <cstddef>
#include <cstdint> #include <cstdint>
namespace app::track { namespace app::track {
@@ -47,6 +48,18 @@ TrackSchemeDecoder TrackSchemeDecoder::from_proto(const proto_type &proto) {
return scheme; return scheme;
} }
TrackSchemeDecoder::proto_type TrackSchemeDecoder::to_proto() const {
proto_type proto = track_app_TrackScheme_init_default;
proto.id = id;
proto.has_color = true;
proto.color = color.to_proto();
const auto data = std::span<uint8_t>(proto.data.bytes);
const auto count = std::min(data.size(), binary.size());
std::ranges::copy(binary.begin(), binary.begin() + static_cast<std::ptrdiff_t>(count), data.begin());
proto.data.size = count;
return proto;
}
expected<track_core::DecodedScheme, error_t> TrackSchemeDecoder::decode_core() const { expected<track_core::DecodedScheme, error_t> TrackSchemeDecoder::decode_core() const {
using ue = unexpected<error_t>; using ue = unexpected<error_t>;
if (binary.empty()) { if (binary.empty()) {
@@ -67,6 +80,13 @@ TrackSchemeMgr::Add TrackSchemeMgr::Add::from_proto(const proto_type &proto) {
return add; return add;
} }
TrackSchemeMgr::Add::proto_type TrackSchemeMgr::Add::to_proto() const {
proto_type proto = track_app_TrackSchemeMgrAdd_init_default;
proto.has_scheme = true;
proto.scheme = scheme_decoder.to_proto();
return proto;
}
TrackSchemeMgr TrackSchemeMgr::from_proto(const proto_type &proto) { TrackSchemeMgr TrackSchemeMgr::from_proto(const proto_type &proto) {
TrackSchemeMgr mgmt; TrackSchemeMgr mgmt;
switch (proto.which_msg) { switch (proto.which_msg) {
@@ -82,4 +102,20 @@ TrackSchemeMgr TrackSchemeMgr::from_proto(const proto_type &proto) {
return mgmt; return mgmt;
} }
TrackSchemeMgr::proto_type TrackSchemeMgr::to_proto() const {
proto_type proto = track_app_TrackSchemeMgr_init_default;
std::visit(app::utils::overloads{
[](Unknown) {},
[&](const Add &add) {
proto.which_msg = track_app_TrackSchemeMgr_add_tag;
proto.msg.add = add.to_proto();
},
[&](Clear) {
proto.which_msg = track_app_TrackSchemeMgr_clear_tag;
proto.msg.clear = track_app_TrackSchemeMgrClear_init_default;
}},
choice);
return proto;
}
} // namespace app::track } // namespace app::track
+1 -1
View File
@@ -53,7 +53,7 @@ track_core::TrackState to_core(app::track::TrackState state) {
return track_core::TrackState::stop; return track_core::TrackState::stop;
case track_app_TrackState_RUN: case track_app_TrackState_RUN:
return track_core::TrackState::run; return track_core::TrackState::run;
case track_app_TrackState_TEST_RAINBOW: case track_app_TrackState_TEST_RAINBOW_CONTINUOUS:
return track_core::TrackState::test_rainbow; return track_core::TrackState::test_rainbow;
case track_app_TrackState_TEST_BLINK: case track_app_TrackState_TEST_BLINK:
return track_core::TrackState::test_blink; return track_core::TrackState::test_blink;
+2 -2
View File
@@ -15,8 +15,8 @@ const char *to_str(TrackState status) {
return "STOP"; return "STOP";
case track_app_TrackState_RUN: case track_app_TrackState_RUN:
return "RUN"; return "RUN";
case track_app_TrackState_TEST_RAINBOW: case track_app_TrackState_TEST_RAINBOW_CONTINUOUS:
return "TEST_RAINBOW"; return "TEST_RAINBOW_CONTINUOUS";
case track_app_TrackState_TEST_BLINK: case track_app_TrackState_TEST_BLINK:
return "TEST_BLINK"; return "TEST_BLINK";
} }