REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
hybrid_controller.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
14#include "mode/mode.h"
15
16#include <Arduino.h>
17#include <cstring>
18#include <list>
19
20#include <build/distributor.h>
21#include <daq/daq.h>
22#include <lucidac/front_panel_signaling.h>
23#include <lucidac/lucidac.h>
24#include <mode/mode.h>
25#include <net/auth.h>
26#include <net/settings.h>
27#include <plugin/static.h>
28#include <protocol/handler.h>
29#include <protocol/jsonl_logging.h>
30#include <protocol/jsonl_server.h>
31#include <protocol/protocol.h>
32#include <protocol/registry.h>
33#include <utils/crash_report.h>
34#include <utils/hashflash.h>
35#include <utils/logging.h>
36#include <web/server.h>
37
38// #include "handlers/loader_flasher.h"
39
40auto &carrier_ = platform::LUCIDAC::get();
41auto &netconf = net::StartupConfig::get();
43
44/*void setup_remote_log() {
45 IPAddress remote{192,168,68,96};
46 static EthernetClient client;
47 client.connect(remote, 1234);
48 msg::Log::get().sinks.add(&client);
49}*/
50
51FLASHMEM void setup() {
52 // Initialize serial communication
53 Serial.begin(0);
54 // while (!Serial && millis() < 4000) {
55 // Wait for Serial, but not forever
56 //}
57
58 msg::Log::get().sinks.add_Serial();
59 msg::Log::get().sinks.add(&msg::StartupLog::get());
60
61 bus::init();
62 net::register_settings();
63
64 LOG_ALWAYS(dist::ident());
65 LOGV("Flash image (%d bytes) sha256 sum: %s\n", loader::flashimage::len(),
66 loader::flashimage::sha256sum().to_string().c_str());
67
69
70 // Initialize carrier board
71 // TODO, _ERROR_OUT_ shall not be used, see #116
72 LOG(ANABRID_DEBUG_INIT, "Initializing carrier board...");
73 if (!carrier_.init()) {
74 LOG_ERROR("Error initializing carrier board.");
75 leds::indicate_error();
76 }
77
78 // all LEDs on: Both visual self-test and showing that now ethernet search takes place.
79 leds::set(0xFF);
80
81 LOG(ANABRID_DEBUG_INIT, "Starting up Ethernet...");
82 int net_error = netconf.begin_ip();
83 if (!net_error && netconf.enable_mdns) {
84 LOG_ALWAYS("mdns");
85 netconf.begin_mdns();
86 }
87 if (!net_error && netconf.enable_jsonl)
88 msg::JsonlServer::get().begin();
89 if (!net_error && netconf.enable_webserver)
90 web::LucidacWebServer::get().begin();
91
92 // setup_remote_log();
93 // LOG_ALWAYS("Have set up remote log");
94
95 // Initialize things related to runs
96 // TODO, _ERROR_OUT_ shall not be used, see #116
97
98 msg::handlers::Registry::get().init(carrier_); // registers all commonly known messages
99
100 auto &reg = msg::handlers::Registry::get();
101 reg.init(carrier_); // registers all commonly known messages
102
103 // LOG("msg::handlers::DynamicRegistry set up with handlers")
104 // msg::handlers::DynamicRegistry::dump();
105
106 msg::JsonLinesProtocol::get().init(4096); // Envelope size
107
108 // Initialize data acquisition before it's used later
109 daq::init();
110 if (!daq::calibrate(carrier_)) {
111 LOG_ALWAYS("Warning: Error during DAQ calibration. Machine will continue with reduced accuracy.");
112 }
113
114 // Reset overload line
115 carrier_.reset(entities::ResetAction::OVERLOAD_RESET);
116
117 // Set computer into IC mode at startup
118 mode::RealManualControl::to_ic();
119
120 // Run calibration
121 LOG(ANABRID_DEBUG_INIT, "Executing self-calibration, this may take a few moments...");
122 if (!carrier_.calibrate_m_blocks()) {
123 // Right now, self-calibration is still not well tested and the error handling
124 // suggest it fails despite it most likely does not. Therefore, do not make
125 // the output so harsh.
126 //
127 // LOG_ERROR("Error during self-calibration. Machine will continue with reduced accuracy.");
128 // leds::indicate_error();
129 //
130 // instead, do this:
131
132 LOG_ALWAYS("Info: Startup self-calibration apparently did not succeed. The calibration scheme is by "
133 "default not used in this build anyway.");
134
135 // For a quick fix, reset machine here because calibration routines exit
136 // early which *do* result in a machine in an unusable state.
137 carrier_.reset(entities::ResetAction::CIRCUIT_RESET | entities::ResetAction::OVERLOAD_RESET);
138 (void)carrier_.write_to_hardware();
139 }
140
141 // Done.
142 LOG(ANABRID_DEBUG_INIT, "Initialization done.");
143
144 static_plugin::setup();
145
146 // visual effect to show that booting has been done
147 leds::ease_out();
148}
149
150FLASHMEM void loop() {
151 if (netconf.enable_jsonl)
152 msg::JsonlServer::get().loop();
153
154 static net::auth::AuthentificationContext admin_context{net::auth::UserPasswordAuthentification::admin};
155 msg::JsonLinesProtocol::get().process_serial_input(admin_context);
156
157 if (netconf.enable_webserver)
158 web::LucidacWebServer::get().loop();
159
160 msg::JsonLinesProtocol::get().process_out_of_band_handlers(carrier_);
161
162 static_plugin::loop();
163}
bool network_working
FLASHMEM void setup()
auto & carrier_
auto & netconf
FLASHMEM void loop()
void check_and_log_crash()
Logs out what has been captured by the Teensy CrashReport tooling, cf.