REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
run.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#include "run/run.h"
7
8#include "utils/logging.h"
9#include <utility>
10#include "protocol/protocol_oob.h"
11
12uint64_t time2nanos(pb_Time time) {
13 uint64_t prefix;
14 switch (time.prefix) {
15 case pb_Prefix_NONE:
16 prefix = 1'000'000'000;
17 break;
18 case pb_Prefix_MILLI:
19 prefix = 1'000'000;
20 break;
21 case pb_Prefix_MICRO:
22 prefix = 1'000;
23 break;
24 default:
25 prefix = 1;
26 break;
27 }
28
29 return prefix * time.value;
30}
31
32run::RunConfig run::RunConfig::from_buf(const pb_RunConfig &config) {
33 run::RunConfig run;
34
35 run.ic_time = time2nanos(config.ic_time);
36 run.op_time = time2nanos(config.op_time);
37
38 // LOG_MEV("ic_time_ns = %lld, op_time_ns=%lld", run.ic_time, run.op_time);
39
40 // default values for the following keys are given
41 // in the RunConfig class definition.
42
43 if (config.has_halt_on_overload)
44 run.halt_on_overload = config.halt_on_overload;
45
46 if (config.has_repetitive)
47 run.streaming = config.streaming;
48
49 if (config.has_repetitive)
50 run.repetitive = config.repetitive;
51
52 if (config.has_write_run_state_changes)
53 run.write_run_state_changes = config.write_run_state_changes;
54
55 return run;
56}
57
58run::CalibrationConfig run::CalibrationConfig::from(const pb_CalibrationConfig &cfg) {
59 auto& carrier = carrier::Carrier::get();
60 bool is_leader = cfg.has_leader && std::string(cfg.leader.path) == carrier.get_entity_id();
61 return {cfg.enabled, is_leader};
62}
63
64
65run::Run::Run(
66 std::string_view id,
67 const RunConfig &config,
68 std::unique_ptr<RunStateChangeHandler> state_change_handler,
69 std::unique_ptr<RunDataHandler> data_handler,
70 const daq::DAQConfig &daq_config,
71 const mode::SyncConfig &sync_config,
72 const CalibrationConfig &calibration_config
73)
74 : id(id),
75 config(config),
76 daq_config(daq_config),
77 sync_config(sync_config),
78 calibration_config(calibration_config),
79 state_change_handler(std::move(state_change_handler)),
80 data_handler(std::move(data_handler)) {
81
82}
83
84
85run::Run run::Run::decode(
86 const pb_StartRunCommand& start_run_command,
87 std::unique_ptr<RunStateChangeHandler> state_change_handler,
88 std::unique_ptr<RunDataHandler> data_handler
89) {
90 auto run_config = RunConfig::from_buf(start_run_command.run_config);
91 auto daq_config = daq::DAQConfig::from_buf(start_run_command.daq_config);
92 auto sync_config = mode::SyncConfig::from_pb(start_run_command.sync_config);
93 auto calibration_config = CalibrationConfig::from(start_run_command.calibration_config);
94 return Run{
95 start_run_command.run.id,
96 run_config,
97 std::move(state_change_handler),
98 std::move(data_handler),
99 daq_config,
100 sync_config,
101 calibration_config
102 };
103}
104
105
106run::RunStateChange run::Run::to(run::RunState new_state, unsigned int t) {
107 auto old = state;
108 state = new_state;
109 return {t, old, state};
110}
Definition daq.h:14
uint64_t time2nanos(pb_Time time)
Definition run.cpp:12