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
40UnitResult 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 //default:
70 //return UnitResult("Unknown target state for SH Block");
71 }
72}
73
74ConfigResult blocks::SHBlock::config(const pb_Item &item) {
75 if (item.which_kind != pb_Item_shift_hold_config_tag)
76 return ConfigResult::err("expected shift hold config");
77
78 auto& sh_item = item.kind.shift_hold_config;
79
80 // FOR TESTING
81 switch (sh_item.state) {
82 case pb_ShiftHoldConfig_State_TRACK:
83 set_state(blocks::SHState::TRACK);
84 break;
85 case pb_ShiftHoldConfig_State_TRACK_AT_IC:
86 set_state(blocks::SHState::TRACK_AT_IC);
87 break;
88 case pb_ShiftHoldConfig_State_INJECT:
89 set_state(blocks::SHState::INJECT);
90 break;
91 case pb_ShiftHoldConfig_State_GAIN_ZERO_TO_SEVEN:
92 set_state(blocks::SHState::GAIN_ZERO_TO_SEVEN);
93 break;
94 case pb_ShiftHoldConfig_State_GAIN_EIGHT_TO_FIFTEEN:
95 set_state(blocks::SHState::GAIN_EIGHT_TO_FIFTEEN);
96 break;
97 case pb_ShiftHoldConfig_State_PASSTHROUGH:
98 set_state(blocks::SHState::PASSTHROUGH);
99 break;
100 default:
101 return ConfigResult::err("Unknown target state for SH Block");
102 }
103
104 return ConfigResult::ok(true);
105}
106} // namespace blocks
uint32_t
Definition flasher.cpp:195