-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharduinousb.cpp
More file actions
283 lines (242 loc) · 9.26 KB
/
Copy patharduinousb.cpp
File metadata and controls
283 lines (242 loc) · 9.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
____ _ _
/ ___| ___ _ _ _ __ ___ ___ _ __ | |_ _ __ _(_)_ __
\___ \ / _ \| | | | '__/ __/ _ \ | '_ \| | | | |/ _` | | '_ \
___) | (_) | |_| | | | (_| __/ | |_) | | |_| | (_| | | | | |
|____/ \___/ \__,_|_| \___\___| | .__/|_|\__,_|\__, |_|_| |_|
|_| |___/
# A Template for UsbsourcePlugin, a Source Plugin
# Generated by the command: plugin -t source -d ArduinoIO_plugin USBSource
# Hostname: Fram-IV
# Current working directory: /Users/p4010/Develop/MADS_plugins
# Creation date: 2026-09-02T21:59:15.981+0200
# NOTICE: MADS Version 2.4.3
*/
// Mandatory included headers
#include <source.hpp>
#include <sink.hpp>
#include <nlohmann/json.hpp>
#include <pugg/Kernel.h>
#include <map>
#include <vector>
#include <stdio.h>
// other includes as needed here
#include <arduino_driver/Device.h>
#include <arduino_driver/Enumerator.h>
#include <arduino_driver/Protocol.h>
#include "usb_driver.hpp"
// Load the namespaces
using namespace std;
using namespace ArduinoDriver;
using json = nlohmann::json;
// Plugin class. This shall be the only part that needs to be modified,
// implementing the actual functionality
class UsbsourcePlugin : public Source<json>, public USBDriver {
public:
// Don't forget to inherit the constructors of the parent class
// If you really need to implement your own constructor, make sure to call
// the parent constructor. Normally, use set_params() to set the parameters
// instead of the constructor.
using Source::Source; // inherit constructors
// Typically, no need to change this.
// On multi-driver plugins, the kind() is used to select the settings section
// in the agent's configuration file.
string kind() override { return "usbsource"; }
// Implement the actual functionality here
// Return types:
// return_type::success: the result is published
// return_type::retry: go to next loop, nothing is published
// return_type::warning: content of _error is added to result befor publishing
// return_type::error: the result only has a copy of _error
// return_type::critical: execution stops
return_type get_output(json &out, vector<unsigned char> *blob = nullptr) override {
if (_init_error) return return_type::critical;
out.clear();
try {
for (const auto &[pin, mode] : _pin_modes) {
if (mode == PinMode::Input || mode == PinMode::InputPullup || mode == PinMode::InputPulldown) {
out["digital"][to_string(pin)] = _dev->digital_read(pin);
} else if (mode == PinMode::AnalogIn) {
out["analog"][to_string(pin)] = _dev->analog_read_volts(pin);
}
}
} catch (const exception &e) {
_error = e.what();
return return_type::error;
}
if (!_agent_id.empty()) out["agent_id"] = _agent_id;
return return_type::success;
}
void set_params(const json ¶ms) override {
Source::set_params(params);
_params["serial"] = "";
_params["pin_modes"] = json::object();
_params["pin_modes"]["1"] = "PULLDOWN";
_params.merge_patch(params);
try {
open(_params["serial"].get<string>());
read_pin_modes(_params["pin_modes"], _allowed_pin_modes);
apply_pin_modes();
} catch (const exception &e) {
_error = e.what();
cerr << e.what() << endl;
_init_error = true;
}
}
// Implement this method if you want to provide additional information
map<string, string> info() override {
// return a map of strings with additional information about the plugin
// it is used to print the information about the plugin when it is loaded
// by the agent
return {
{"pin modes", _params["pin_modes"].dump()}
};
};
private:
// Define the fields that are used to store internal resources
const vector<string> _allowed_pin_modes{"INPUT", "PULLUP", "PULLDOWN", "ANALOG"};
};
class UsbsinkPlugin : public Sink<json>, public USBDriver {
public:
// Don't forget to inherit the constructors of the parent class
// If you really need to implement your own constructor, make sure to call
// the parent constructor. Normally, use set_params() to set the parameters
// instead of the constructor.
using Sink::Sink; // inherit constructors
// Typically, no need to change this.
// On multi-driver plugins, the kind() is used to select the settings section
// in the agent's configuration file.
string kind() override { return "usbsink"; }
// Implement the actual functionality here
// Return types:
// return_type::success: processing is valid
// return_type::retry: skip processing go to next loop
// return_type::warning: content of _error is tracked with register_event
// return_type::error: _error is traced, skip process
// return_type::critical: execution stops
return_type load_data(json const &input, string topic = "", vector<unsigned char> const *blob = nullptr) override {
if (_init_error) return return_type::critical;
if (!input.is_object()) {
_error = "Input must be a JSON object";
return return_type::error;
}
try {
if (input.contains("digital")) {
for (const auto &[pin_str, value] : input["digital"].items()) {
if (!all_of(pin_str.begin(), pin_str.end(), ::isdigit)) {
_error = "Pin must be a number: " + pin_str;
return return_type::error;
}
uint8_t pin = stoi(pin_str);
if (_pin_modes.find(pin) == _pin_modes.end() || _pin_modes[pin] != PinMode::Output) {
_error = "Pin not configured as OUTPUT: " + pin_str;
return return_type::error;
}
_dev->digital_write(pin, value.get<bool>());
}
}
if (input.contains("pwm")) {
for (const auto &[pin_str, value] : input["pwm"].items()) {
if (!all_of(pin_str.begin(), pin_str.end(), ::isdigit)) {
_error = "Pin must be a number: " + pin_str;
return return_type::error;
}
uint8_t pin = stoi(pin_str);
if (_pin_modes.find(pin) == _pin_modes.end() || _pin_modes[pin] != PinMode::Pwm) {
_error = "Pin not configured as PWM: " + pin_str;
return return_type::error;
}
_dev->pwm_write_fraction(pin, value.get<double>());
}
}
if (input.contains("dac")) {
for (const auto &[pin_str, value] : input["dac"].items()) {
if (!all_of(pin_str.begin(), pin_str.end(), ::isdigit)) {
_error = "Pin must be a number: " + pin_str;
return return_type::error;
}
uint8_t pin = stoi(pin_str);
if (_pin_modes.find(pin) == _pin_modes.end() || _pin_modes[pin] != PinMode::Dac) {
_error = "Pin not configured as DAC: " + pin_str;
return return_type::error;
}
_dev->dac_write_volts(pin, value.get<double>());
}
}
} catch (const exception &e) {
_error = e.what();
return return_type::error;
}
return return_type::success;
}
void set_params(const json ¶ms) override {
// Call the parent class method to set the common parameters
// (e.g. agent_id, etc.)
Sink::set_params(params);
// provide sensible defaults for the parameters
_params["serial"] = "";
_params["pin_modes"] = json::object();
_params.merge_patch(params);
try {
open(_params["serial"].get<string>());
read_pin_modes(_params["pin_modes"], _allowed_pin_modes);
apply_pin_modes();
} catch (const exception &e) {
_error = e.what();
cerr << e.what() << endl;
_init_error = true;
}
}
// Implement this method if you want to provide additional information
map<string, string> info() override {
// return a map of strings with additional information about the plugin
// it is used to print the information about the plugin when it is loaded
// by the agent
return {
{"pin modes", _params["pin_modes"].dump()}
};
};
private:
// Define the fields that are used to store internal resources
const vector<string> _allowed_pin_modes{"OUTPUT", "PWM", "DAC"};
};
/*
____ _ _ _ _
| _ \| |_ _ __ _(_)_ __ __| |_ __(_)_ _____ _ __
| |_) | | | | |/ _` | | '_ \ / _` | '__| \ \ / / _ \ '__|
| __/| | |_| | (_| | | | | | | (_| | | | |\ V / __/ |
|_| |_|\__,_|\__, |_|_| |_| \__,_|_| |_| \_/ \___|_|
|___/
Enable the class as plugin
*/
MADS_REGISTER_PLUGINS(UsbsourcePlugin, UsbsinkPlugin)
/*
_
_ __ ___ __ _(_)_ __
| '_ ` _ \ / _` | | '_ \
| | | | | | (_| | | | | |
|_| |_| |_|\__,_|_|_| |_|
For testing purposes, when directly executing the plugin
*/
int main(int argc, char const *argv[]) {
json output, params;
try {
cout << "Listing devices..." << endl;
cout << USBDriver::list_devices() << endl;
} catch (const Error &e) {
cerr << "Error: " << e.what() << endl;
}
UsbsourcePlugin plugin{};
// Set example values to params
params["pin_modes"]["1"] = "PULLDOWN";
params["pin_modes"]["2"] = "PULLDOWN";
params["pin_modes"]["3"] = "PULLUP";
params["pin_modes"]["15"] = "ANALOG";
// Set the parameters
plugin.set_params(params);
// Process data
plugin.get_output(output);
// Produce output
cout << "Output: " << output << endl;
return 0;
}