Skip to content

Commit 4f17cea

Browse files
committed
Map YardForce button/hall id from string
1 parent 7463cbd commit 4f17cea

File tree

3 files changed

+45
-20
lines changed

3 files changed

+45
-20
lines changed

src/drivers/input/input_driver.hpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
namespace xbot::driver::input {
1212
struct Input {
1313
enum { VIRTUAL = 255 };
14-
enum class Type : uint8_t { BUTTON, HALL };
1514

1615
// Configuration
1716
uint8_t idx;
@@ -29,15 +28,8 @@ struct Input {
2928
} worx;
3029

3130
struct {
32-
Input::Type type;
33-
union {
34-
struct {
35-
uint8_t id;
36-
} button;
37-
struct {
38-
uint8_t bit;
39-
} hall;
40-
};
31+
bool button : 1;
32+
uint8_t id_or_bit : 7;
4133
} yardforce;
4234
};
4335

src/drivers/input/yard_force_input_driver.cpp

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,52 @@
22

33
#include <ulog.h>
44

5+
#include <crc_lookup_map.hpp>
56
#include <json_stream.hpp>
67

78
namespace xbot::driver::input {
89

10+
#pragma pack(push, 1)
11+
namespace {
12+
struct InputParams {
13+
uint16_t crc;
14+
bool button : 1;
15+
uint8_t id_or_bit : 7;
16+
};
17+
} // namespace
18+
#pragma pack(pop)
19+
20+
static constexpr InputParams available_inputs[] = {
21+
// Buttons
22+
{crc16("home"), true, 2},
23+
{crc16("play"), true, 3},
24+
{crc16("s1"), true, 4},
25+
{crc16("s2"), true, 5},
26+
{crc16("lock"), true, 6},
27+
28+
// Halls
29+
{crc16("stop1"), false, 1},
30+
{crc16("stop2"), false, 2},
31+
{crc16("lift"), false, 3},
32+
{crc16("bump"), false, 4},
33+
{crc16("liftx"), false, 5},
34+
{crc16("rbump"), false, 6},
35+
};
36+
37+
static_assert(HasUniqueCrcs(available_inputs), "CRC16 values are not unique");
38+
939
bool YardForceInputDriver::OnInputConfigValue(lwjson_stream_parser_t *jsp, const char *key, lwjson_stream_type_t type,
1040
Input &input) {
11-
// TODO: We probably want to take strings here and map them to the ID.
12-
if (strcmp(key, "button") == 0) {
13-
input.yardforce.type = Input::Type::BUTTON;
14-
return JsonGetNumber(jsp, type, input.yardforce.button.id);
15-
} else if (strcmp(key, "hall") == 0) {
16-
input.yardforce.type = Input::Type::HALL;
17-
return JsonGetNumber(jsp, type, input.yardforce.hall.bit);
41+
if (strcmp(key, "id") == 0) {
42+
JsonExpectType(STRING);
43+
const auto *id = jsp->data.str.buff;
44+
if (auto *params = LookupByName(id, available_inputs)) {
45+
input.yardforce.button = params->button;
46+
input.yardforce.id_or_bit = params->id_or_bit;
47+
return true;
48+
}
49+
ULOG_ERROR("Unknown ID \"%s\"", id);
50+
return false;
1851
}
1952
ULOG_ERROR("Unknown attribute \"%s\"", key);
2053
return false;

src/drivers/ui/YardForceCoverUI/yard_force_cover_ui_driver.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ void YardForceCoverUIDriver::ProcessPacket() {
208208
} else if (encode_decode_buf_[0] == Get_Button && size == sizeof(struct msg_event_button)) {
209209
msg_event_button *msg = (struct msg_event_button *)encode_decode_buf_;
210210
for (auto &input : input_driver_.Inputs()) {
211-
if (input.yardforce.type == Input::Type::BUTTON && input.yardforce.button.id == msg->button_id) {
211+
if (input.yardforce.button && input.yardforce.id_or_bit == msg->button_id) {
212212
const bool long_press = msg->press_duration >= 1;
213213
input.InjectPress(long_press);
214214
break;
@@ -217,8 +217,8 @@ void YardForceCoverUIDriver::ProcessPacket() {
217217
} else if (encode_decode_buf_[0] == Get_Emergency && size == sizeof(struct msg_event_emergency)) {
218218
msg_event_emergency *msg = (struct msg_event_emergency *)encode_decode_buf_;
219219
for (auto &input : input_driver_.Inputs()) {
220-
if (input.yardforce.type == Input::Type::HALL) {
221-
input.Update(IS_BIT_SET(msg->state, input.yardforce.hall.bit));
220+
if (!input.yardforce.button) {
221+
input.Update(IS_BIT_SET(msg->state, input.yardforce.id_or_bit));
222222
}
223223
}
224224
} /* else if (encode_decode_buf_[0] == Get_Rain && size == sizeof(struct msg_event_rain)) {

0 commit comments

Comments
 (0)