REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
mblock_int.cpp
Go to the documentation of this file.
1// Copyright (c) 2024 anabrid GmbH
2// Contact: https://www.anabrid.com/licensing/
3// SPDX-License-Identifier: MIT OR GPL-2.0-or-later
4
5#include "pb_decode.h"
6#include "pb_encode.h"
7
8#include <proto/main.pb.h>
9
10#include <algorithm>
11#include <bitset>
12
13#include <block/mblock.h>
14#include <utils/logging.h>
15
16#include <carrier/carrier.h>
17#include <carrier/cluster.h>
18#include <entity/visitor.h>
19#include <io/io.h>
20#include <mode/mode.h>
21
22extern int abs_clamp(float in, int min, int max);
23
24metadata::eui_t blocks::MIntBlock::get_entity_eui() const {
25 if (hardware)
26 return hardware->get_entity_eui();
27 return {};
28}
29
30blocks::MIntBlock::MIntBlock(SLOT slot, MIntBlockHAL *hardware)
31 : blocks::MBlock{slot, hardware}, hardware(hardware), ic_values{}, time_factors{} {
32 reset_ic_values();
33 reset_time_factors();
34}
35
36bool blocks::MIntBlock::init() {
37 return FunctionBlock::init() && hardware->init();
38}
39
40const std::array<float, 8> &blocks::MIntBlock::get_ic_values() const { return ic_values; }
41
42float blocks::MIntBlock::get_ic_value(uint8_t idx) const {
43 if (idx >= ic_values.size())
44 return 0.0f;
45 return ic_values[idx];
46}
47
48bool blocks::MIntBlock::set_ic_values(const std::array<float, 8> &ic_values_) {
49 for (auto idx = 0u; idx < ic_values_.size(); idx++)
50 if (!set_ic_value(idx, ic_values_[idx]))
51 return false;
52 return true;
53}
54
55bool blocks::MIntBlock::set_ic_values(float value) {
56 for (auto idx = 0u; idx < ic_values.size(); idx++)
57 if (!set_ic_value(idx, value))
58 return false;
59 return true;
60}
61
62bool blocks::MIntBlock::set_ic_value(uint8_t idx, float value) {
63 if (idx >= ic_values.size())
64 return false;
65 if (value > 1.0f or value < -1.0f)
66 return false;
67 ic_values[idx] = value;
68 return true;
69}
70
71void blocks::MIntBlock::reset_ic_values() { std::fill(ic_values.begin(), ic_values.end(), 0.0f); }
72
73const std::array<unsigned int, 8> &blocks::MIntBlock::get_time_factors() const {
74 return time_factors;
75}
76
77unsigned int blocks::MIntBlock::get_time_factor(uint8_t idx) const {
78 if (idx >= time_factors.size())
79 return 0;
80 return time_factors[idx];
81}
82
83bool blocks::MIntBlock::set_time_factors(const std::array<unsigned int, 8> &time_factors_) {
84 for (auto idx = 0u; idx < time_factors_.size(); idx++)
85 if (!set_time_factor(idx, time_factors_[idx]))
86 return false;
87 return true;
88}
89
90bool blocks::MIntBlock::set_time_factors(unsigned int k) {
91 for (auto idx = 0u; idx < time_factors.size(); idx++)
92 if (!set_time_factor(idx, k))
93 return false;
94 return true;
95}
96
97bool blocks::MIntBlock::set_time_factor(uint8_t int_idx, unsigned int k) {
98 if (!(k == 100 or k == 10000))
99 return false;
100 if (int_idx >= NUM_INTEGRATORS)
101 return false;
102 time_factors[int_idx] = k;
103 return true;
104}
105
106void blocks::MIntBlock::reset_time_factors() {
107 // Copying solves a strange linker issue "relocation against ... in read-only section `.text'"
108 auto default_ = DEFAULT_TIME_FACTOR;
109 std::fill(std::begin(time_factors), std::end(time_factors), default_);
110}
111
112bool blocks::MIntBlock::write_to_hardware() {
113 // Write IC values one channel at a time
114 for (decltype(ic_values.size()) i = 0; i < ic_values.size(); i++) {
115 if (!hardware->write_ic(i, ic_values[i])) {
116 LOG(ANABRID_PEDANTIC, __PRETTY_FUNCTION__);
117 return UnitResult::err("Error writing IC values to hardware.");
118 }
119 }
120 // Write time factor switches by converting to bitset
121 std::bitset<NUM_INTEGRATORS> time_factor_switches{};
122 for (auto idx = 0u; idx < time_factors.size(); idx++)
123 if (time_factors[idx] != DEFAULT_TIME_FACTOR)
124 time_factor_switches.set(idx);
125 if (!hardware->write_time_factor_switches(time_factor_switches))
126 return UnitResult::err("Error writing time factor switches.");
127
128 return UnitResult::ok();
129}
130
131void blocks::MIntBlock::reset(entities::ResetAction action) {
132 FunctionBlock::reset(action);
133
134 if (action.has(entities::ResetAction::CIRCUIT_RESET)) {
135 reset_ic_values();
136 reset_time_factors();
137 }
138
139 if (action.has(entities::ResetAction::OVERLOAD_RESET)) {
140 // !!ATTENTION!! This breaks a lot of things but is neccessary and the ONLY
141 // way to reset overloads on the MIntBlock.
142 mode::RealManualControl::enable();
143 mode::RealManualControl::to_ic();
144 }
145}
146
147ConfigResult blocks::MIntBlock::config(const pb_Item &item) {
148#ifdef ANABRID_DEBUG_ENTITY_CONFIG
149 Serial.println(__PRETTY_FUNCTION__);
150#endif
151
152 if (item.which_kind != pb_Item_itor_config_tag)
153 return ConfigResult::ok(false);
154
155 auto& itor_config = item.kind.itor_config;
156 for (size_t idx = 0; idx < itor_config.elements_count; idx++) {
157 auto& element = itor_config.elements[idx];
158 if (!set_ic_value(element.idx, element.ic)) {
159 return ConfigResult::err_fmt("MIntBlock: Integrator %d IC is illegal: %f", element.idx, element.ic);
160 }
161 if (!set_time_factor(element.idx, element.k)) {
162 return ConfigResult::err_fmt("MIntBlock: Integrator %d time factor k illegal: %d", element.idx, element.k);
163 }
164 }
165
166 return ConfigResult::ok(true);
167}
168
169void blocks::MIntBlock::extract(entities::ExtractVisitor &collector) {
170 Entity::extract(collector);
171
172 if (!collector.include_configuration()) return;
173 auto& item = collector.create(pb_Item_itor_config_tag);
174 auto& itor_config = item.kind.itor_config;
175
176 itor_config.elements_count = NUM_INTEGRATORS;
177 for (auto idx = 0; idx < NUM_INTEGRATORS; ++idx) {
178 auto& element = itor_config.elements[idx];
179 element.idx = idx;
180 element.ic = ic_values[idx];
181 element.k = time_factors[idx];
182 }
183}
int abs_clamp(float in, int min, int max)
Definition mblock.cpp:20