Skip to content

Commit 2b88dc8

Browse files
committed
Integrate virtual input into all_inputs_ vector
1 parent 25b3efd commit 2b88dc8

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

src/services/input_service/input_service.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ bool InputService::OnRegisterInputConfigsChanged(const void* data, size_t length
2828
}
2929
num_active_lift_ = 0;
3030

31+
// Add virtual inputs.
32+
lift_input_ = &all_inputs_.emplace_back();
33+
lift_input_->idx = Input::VIRTUAL;
34+
lift_input_->emergency_reason = EmergencyReason::LIFT | EmergencyReason::LATCH;
35+
lift_input_->emergency_delay_ms = 10;
36+
3137
input_config_json_data_t json_data;
3238
json_data.callback = etl::make_delegate<InputService, &InputService::InputConfigsJsonCallback>(*this);
3339
return ProcessJson(source, json_data);
@@ -65,7 +71,7 @@ bool InputService::InputConfigsJsonCallback(lwjson_stream_parser_t* jsp, lwjson_
6571
JsonExpectTypeOrEnd(OBJECT);
6672
if (type == LWJSON_STREAM_TYPE_OBJECT) {
6773
if (all_inputs_.full()) {
68-
ULOG_ERROR("Too many inputs (max. %d)", all_inputs_.max_size());
74+
ULOG_ERROR("Too many inputs (max. %d)", all_inputs_.max_size() - NUM_VIRTUAL_INPUTS);
6975
return false;
7076
}
7177
data->current_input = &all_inputs_.emplace_back();
@@ -166,7 +172,7 @@ uint32_t InputService::OnLoop(uint32_t, uint32_t) {
166172
void InputService::SendStatus() {
167173
uint64_t active_inputs_mask = 0;
168174
for (auto& input : all_inputs_) {
169-
if (input.IsActive()) {
175+
if (input.IsActive() && input.idx != input.VIRTUAL) {
170176
active_inputs_mask |= 1 << input.idx;
171177
}
172178
}
@@ -184,7 +190,7 @@ void InputService::OnInputChanged(Input& input, const bool active, const uint32_
184190

185191
if ((input.emergency_reason & EmergencyReason::TILT) != 0) {
186192
uint8_t lift_active = active ? ++num_active_lift_ : --num_active_lift_;
187-
lift_input_.Update(lift_active >= 2);
193+
lift_input_->Update(lift_active >= 2);
188194
}
189195

190196
// TODO: This will be called in the middle of the driver's update loop.
@@ -219,12 +225,5 @@ etl::pair<uint16_t, uint32_t> InputService::GetEmergencyReasons(uint32_t now) {
219225
reasons |= input.emergency_reason;
220226
}
221227
}
222-
223-
if (lift_input_.IsActive()) {
224-
if (TimeoutReached(lift_input_.ActiveDuration(now), lift_input_.emergency_delay_ms * 1'000, block_time)) {
225-
reasons |= lift_input_.emergency_reason;
226-
}
227-
}
228-
229228
return {reasons, block_time};
230229
}

src/services/input_service/input_service.hpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ struct input_config_json_data_t;
1818
class InputService : public InputServiceBase {
1919
public:
2020
explicit InputService(uint16_t service_id) : InputServiceBase(service_id, wa, sizeof(wa)) {
21-
lift_input_.idx = Input::VIRTUAL;
22-
lift_input_.emergency_reason = EmergencyReason::LIFT | EmergencyReason::LATCH;
23-
lift_input_.emergency_delay_ms = 10;
2421
}
2522

2623
void RegisterInputDriver(const char* id, InputDriver* driver) {
@@ -41,10 +38,11 @@ class InputService : public InputServiceBase {
4138
etl::flat_map<etl::string<10>, InputDriver*, 3> drivers_;
4239

4340
// Must not have more than 64 inputs due to the size of various bitmasks.
44-
etl::vector<Input, 30> all_inputs_;
41+
constexpr static uint8_t NUM_VIRTUAL_INPUTS = 1;
42+
etl::vector<Input, 30 + NUM_VIRTUAL_INPUTS> all_inputs_;
4543

4644
etl::atomic<uint8_t> num_active_lift_{0};
47-
Input lift_input_;
45+
Input* lift_input_ = nullptr;
4846

4947
bool OnRegisterInputConfigsChanged(const void* data, size_t length) override;
5048
bool InputConfigsJsonCallback(lwjson_stream_parser_t* jsp, lwjson_stream_type_t type, void* data);

0 commit comments

Comments
 (0)