REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
user_plugin_static.cpp
Go to the documentation of this file.
1#include <Arduino.h>
2#include <plugin/static.h>
3#include <protocol/registry.h>
4#include <run/run.h>
5#include <utils/streaming_json.h>
6#include <lucidac/lucidac.h>
7#include <protocol/protocol.h>
8#include <protocol/protocol_oob.h>
9
10constexpr uint8_t bufsize = 300;
11uint16_t *results = nullptr;
12size_t cur_run = 0;
13run::Run *reprun = nullptr;
14
15void tear_down() {
16 delete reprun;
17 delete results;
18 cur_run = 0;
19}
20
21
22class MonteCarloHandler : public msg::handlers::MessageHandler {
23public:
24 int handle(JsonObjectConst msg_in, JsonObject &msg_out) override {
25 if(msg_in.containsKey("run_delete"))
26 tear_down();
27
28 if(msg_in.containsKey("run_config")) {
29 auto run_config_json = msg_in["run_config"].as<JsonObjectConst>();
30 auto run_config = run::RunConfig::from_json(run_config_json);
31 if(reprun)
32 tear_down();
33 reprun = new run::Run("monte_carlo", run_config, daq::DAQConfig::silence());
34 }
35
36 return success;
37 }
38};
39
40FLASHMEM void static_plugin::setup() {
41 msg::handlers::Registry::get().set("monte_carlo", new MonteCarloHandler(), net::auth::SecurityLevel::RequiresLogin);
42}
43
44FLASHMEM void static_plugin::loop() {
45 if(reprun) {
46 if(!results) {
47 results = new uint16_t[bufsize]; // because RAM2 has more of leftover
48 }
49
50 // carry out the next run, not streaming any data during run.
51 reprun->run(nullptr, nullptr, nullptr);
52 // sample exactly one channel at end of run.
53 results[cur_run++] = daq::sample_raw()[0];
54
55 if(cur_run >= bufsize) {
56 // stream out the results, reusing existing infrastructure.
57 client::StreamingRunDataNotificationHandler streamer{
58 platform::LUCIDAC::get(),
59 msg::JsonLinesProtocol::get().broadcast
60 };
61 streamer.handle(results, bufsize, 1, *reprun);
62 cur_run = 0;
63 }
64 }
65}
int handle(JsonObjectConst msg_in, JsonObject &msg_out) override
static constexpr int success
Definition flasher.cpp:275
void tear_down()
uint16_t * results
run::Run * reprun
size_t cur_run
constexpr uint8_t bufsize