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