REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
mblock.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 "block/mblock.h"
6#include "block/mblock_int.h"
7#include "block/mblock_mul.h"
8#include "utils/logging.h"
9
10#include "carrier/cluster.h"
11
12#include <math.h>
13
14// This functions clamps an input value but keeps the sign of the input. min and max shall be positive
15// Created for the automatic calibration in m blocks. Currently not typed, as there is no real need for it.
16int abs_clamp(float in, int min, int max) {
17 int sign;
18
19 if (in < 0.0f)
20 sign = -1;
21 else
22 sign = 1;
23
24 if (abs(in) >= max)
25 return sign * max;
26 else if (abs(in) <= min)
27 return sign * min;
28 else
29 return in;
30}
31
32FLASHMEM blocks::MBlock::MBlock(SLOT slot, MBlockHAL *hardware)
33 : blocks::FunctionBlock{slot == SLOT::M0 ? "M0" : "M1", hardware}, slot(slot), hardware(hardware) {
34 classifier.class_enum = CLASS_;
35}
36
37FLASHMEM uint8_t blocks::MBlock::slot_to_global_io_index(uint8_t local) const {
38 switch (slot) {
39 case SLOT::M0:
40 return local;
41 case SLOT::M1:
42 return local + 8;
43 }
44 // This should never be reached
45 return local;
46}
47
48FLASHMEM
49blocks::MBlock *blocks::MBlock::from_entity_classifier(entities::EntityClassifier classifier,
50 const bus::addr_t block_address) {
51 if (!classifier or classifier.class_enum != entities::EntityClass::M_BLOCK)
52 return nullptr;
53
54 auto type = classifier.type_as<TYPES>();
55 switch (type) {
56 case TYPES::UNKNOWN:
57 // This is already checked by !classifier above
58 return nullptr;
59 case TYPES::M_MUL4_BLOCK:
60 return MMulBlock::from_entity_classifier(classifier, block_address);
61 case TYPES::M_INT8_BLOCK:
62 return MIntBlock::from_entity_classifier(classifier, block_address);
63 }
64 // Any unknown value results in a nullptr here.
65 // Adding default case to switch suppresses warnings about missing cases.
66 return nullptr;
67}
68
69FLASHMEM utils::status blocks::EmptyMBlock::write_to_hardware() { return utils::status::success(); }
70
71FLASHMEM utils::status blocks::EmptyMBlock::config_self_from_json(JsonObjectConst cfg) {
72 return utils::status::success();
73}
74
75FLASHMEM bool blocks::MBlockHAL::init() {
76 if (!entities::EntityHAL::init())
77 return false;
78 reset_overload_flags();
79 return true;
80}
81
82FLASHMEM void blocks::MBlock::overload_flags_to_json(JsonArray msg_out) const {
83 msg_out.clear();
84 if (!hardware)
85 return;
86 auto flags = hardware->read_overload_flags();
87 for (size_t i = 0; i < flags.size(); i++) {
88 msg_out.add((bool)flags[i]);
89 }
90}
int abs_clamp(float in, int min, int max)
Definition mblock.cpp:16