REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
shblock.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 anabrid GmbH
2// Contact: https://www.anabrid.com/licensing/
3//
4// SPDX-License-Identifier: MIT OR GPL-2.0-or-later
5
6#include <proto/main.pb.h>
7
8#include <block/shblock.h>
9#include <entity/visitor.h>
10
11namespace blocks {
12
13void SHBlockHAL::compensate_hardware_offsets(uint32_t track_time, uint32_t inject_time) {
14 // This is uncheckable + allowed on all SH blocks so we can ignore the return value
15 (void)set_state(SHState::TRACK);
16 delayMicroseconds(track_time);
17 (void)set_state(SHState::INJECT);
18 delayMicroseconds(inject_time);
19}
20
21metadata::eui_t blocks::SHBlock::get_entity_eui() const {
22 if (hardware)
23 return hardware->get_entity_eui();
24 return {};
25}
26
27blocks::SHBlock::SHBlock(SHBlockHAL *hardware) : FunctionBlock("SH", hardware), hardware(hardware) {
28 classifier.class_enum = CLASS_;
29}
30
31void blocks::SHBlock::set_state(SHState state_) { state = state_; }
32
33blocks::SHState blocks::SHBlock::get_state() const { return state; }
34
35void blocks::SHBlock::reset(entities::ResetAction action) {
36 if (action.has(entities::ResetAction::CIRCUIT_RESET))
37 state = SHState::INJECT;
38}
39
40bool blocks::SHBlock::write_to_hardware() { return hardware->set_state(state); }
41
42void blocks::SHBlock::compensate_hardware_offsets(uint32_t track_time, uint32_t inject_time) {
43 hardware->compensate_hardware_offsets(track_time, inject_time);
44 state = SHState::INJECT;
45}
46
47void SHBlock::extract(entities::ExtractVisitor &collector) {
48 Entity::extract(collector);
49 if (!collector.include_configuration()) return;
50 auto& item = collector.create(pb_Item_shift_hold_config_tag);
51 auto& sh_item = item.kind.shift_hold_config;
52
53 switch (state) {
54 case SHState::TRACK :
55 sh_item.state = pb_ShiftHoldConfig_State_TRACK;
56 break;
57 case SHState::TRACK_AT_IC:
58 sh_item.state = pb_ShiftHoldConfig_State_TRACK_AT_IC;
59 break;
60 case SHState::INJECT:
61 sh_item.state = pb_ShiftHoldConfig_State_INJECT;
62 break;
63 case SHState::GAIN_ZERO_TO_SEVEN:
64 sh_item.state = pb_ShiftHoldConfig_State_GAIN_ZERO_TO_SEVEN;
65 break;
66 case SHState::GAIN_EIGHT_TO_FIFTEEN:
67 sh_item.state = pb_ShiftHoldConfig_State_GAIN_EIGHT_TO_FIFTEEN;
68 break;
69 case SHState::PASSTHROUGH:
70 sh_item.state = pb_ShiftHoldConfig_State_PASSTHROUGH;
71 //default:
72 //return UnitResult("Unknown target state for SH Block");
73 }
74}
75
76ConfigResult blocks::SHBlock::config(const pb_Item &item) {
77 if (item.which_kind != pb_Item_shift_hold_config_tag)
78 return ConfigResult::err("expected shift hold config");
79
80 auto& sh_item = item.kind.shift_hold_config;
81
82 // FOR TESTING
83 switch (sh_item.state) {
84 case pb_ShiftHoldConfig_State_TRACK:
85 set_state(blocks::SHState::TRACK);
86 break;
87 case pb_ShiftHoldConfig_State_TRACK_AT_IC:
88 set_state(blocks::SHState::TRACK_AT_IC);
89 break;
90 case pb_ShiftHoldConfig_State_INJECT:
91 set_state(blocks::SHState::INJECT);
92 break;
93 case pb_ShiftHoldConfig_State_GAIN_ZERO_TO_SEVEN:
94 set_state(blocks::SHState::GAIN_ZERO_TO_SEVEN);
95 break;
96 case pb_ShiftHoldConfig_State_GAIN_EIGHT_TO_FIFTEEN:
97 set_state(blocks::SHState::GAIN_EIGHT_TO_FIFTEEN);
98 break;
99 case pb_ShiftHoldConfig_State_PASSTHROUGH:
100 set_state(blocks::SHState::PASSTHROUGH);
101 break;
102 default:
103 return ConfigResult::err("Unknown target state for SH Block");
104 }
105
106 return ConfigResult::ok(true);
107}
108} // namespace blocks