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"}, 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
70
74
75FLASHMEM bool blocks::MBlockHAL::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}
utils::status config_self_from_json(JsonObjectConst cfg) override
Deserialize a new configuration for this entity from a JsonObject.
Definition mblock.cpp:71
utils::status write_to_hardware() override
returns true in case of success
Definition mblock.cpp:69
virtual bool init()
Definition base.h:15
A function block represents one module in a cluster, such as an M-Block, C-Block, I-Block or U-Block.
Definition base.h:29
bool init() override
Definition mblock.cpp:75
A REDAC Math block (M-Block) is represented by this class.
Definition mblock.h:43
static MBlock * from_entity_classifier(entities::EntityClassifier classifier, bus::addr_t block_address)
Definition mblock.cpp:49
void overload_flags_to_json(JsonArray msg_out) const
Definition mblock.cpp:82
MBlock(SLOT slot, MBlockHAL *hardware)
Definition mblock.cpp:32
static constexpr auto CLASS_
Definition mblock.h:46
uint8_t slot_to_global_io_index(uint8_t local) const
Definition mblock.cpp:37
static MIntBlock * from_entity_classifier(entities::EntityClassifier classifier, bus::addr_t block_address)
static MMulBlock * from_entity_classifier(entities::EntityClassifier classifier, bus::addr_t block_address)
EntityClassifier classifier
Definition base.h:217
A recoverable error, inspired from https://abseil.io/docs/cpp/guides/status and https://github....
Definition error.h:35
static status success()
Syntactic sugar for success.
Definition error.h:104
int abs_clamp(float in, int min, int max)
Definition mblock.cpp:16
Definition base.h:10
uint16_t addr_t
Definition bus.h:27