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
40utils::status 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 auto& cfg = collector.create(pb_Config_shift_hold_config_tag);
50 auto& sh_cfg = cfg.kind.shift_hold_config;
51
52 switch (state) {
53 case SHState::TRACK :
54 sh_cfg.state = pb_ShiftHoldConfig_State_TRACK;
55 break;
56 case SHState::TRACK_AT_IC:
57 sh_cfg.state = pb_ShiftHoldConfig_State_TRACK_AT_IC;
58 break;
59 case SHState::INJECT:
60 sh_cfg.state = pb_ShiftHoldConfig_State_INJECT;
61 break;
62 case SHState::GAIN_ZERO_TO_SEVEN:
63 sh_cfg.state = pb_ShiftHoldConfig_State_GAIN_ZERO_TO_SEVEN;
64 break;
65 case SHState::GAIN_EIGHT_TO_FIFTEEN:
66 sh_cfg.state = pb_ShiftHoldConfig_State_GAIN_EIGHT_TO_FIFTEEN;
67 break;
68 //default:
69 //return utils::status("Unknown target state for SH Block");
70 }
71}
72
73ConfigResult blocks::SHBlock::config(const pb_Config &cfg) {
74 if (cfg.which_kind != pb_Config_shift_hold_config_tag)
75 return ConfigResult::err("expected shift hold config");
76
77 auto& sh_cfg = cfg.kind.shift_hold_config;
78
79 // FOR TESTING
80 switch (sh_cfg.state) {
81 case pb_ShiftHoldConfig_State_TRACK:
82 set_state(blocks::SHState::TRACK);
83 break;
84 case pb_ShiftHoldConfig_State_TRACK_AT_IC:
85 set_state(blocks::SHState::TRACK_AT_IC);
86 break;
87 case pb_ShiftHoldConfig_State_INJECT:
88 set_state(blocks::SHState::INJECT);
89 break;
90 case pb_ShiftHoldConfig_State_GAIN_ZERO_TO_SEVEN:
91 set_state(blocks::SHState::GAIN_ZERO_TO_SEVEN);
92 break;
93 case pb_ShiftHoldConfig_State_GAIN_EIGHT_TO_FIFTEEN:
94 set_state(blocks::SHState::GAIN_EIGHT_TO_FIFTEEN);
95 break;
96 case pb_ShiftHoldConfig_State_PASSTHROUGH:
97 set_state(blocks::SHState::PASSTHROUGH);
98 break;
99 default:
100 return ConfigResult::err("Unknown target state for SH Block");
101 }
102
103 return ConfigResult::ok(true);
104}
105} // namespace blocks
uint32_t
Definition flasher.cpp:195