Dawn Framework 1.0
Universal data acquisition framework for embedded systems
threshold.hxx
1// dawn/include/dawn/prog/threshold.hxx
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5
6#pragma once
7
8#include <algorithm>
9#include <cstdint>
10#include <cstring>
11#include <inttypes.h>
12#include <map>
13#include <vector>
14
15#include "dawn/io/ddata.hxx"
16#include "dawn/io/common.hxx"
17#include "dawn/porting/config.hxx"
18#include "dawn/prog/common.hxx"
19#include "dawn/prog/process.hxx"
20
21namespace dawn
22{
24
25class CProgThresholdBase : public CProgProcess
26{
27public:
28 enum
29 {
30 PROG_THRESHOLD_CFG_MODE = 2,
31 PROG_THRESHOLD_CFG_LOW = 3,
32 PROG_THRESHOLD_CFG_HIGH = 4,
33 };
34
35 enum
36 {
37 MODE_ABOVE = 0,
38 MODE_BELOW = 1,
39 MODE_HYSTERESIS = 2,
40 MODE_WINDOW = 3,
41 };
42
43 explicit CProgThresholdBase(CDescObject &desc)
44 : CProgProcess(desc)
45 , mode(MODE_ABOVE)
46 , low(0)
47 , high(0)
48 {
49 }
50
51 int trigger(uint8_t cmd) override
52 {
53 if (cmd == CMD_RESET)
54 {
55 state.clear();
56 }
57
58 return CProgProcess::trigger(cmd);
59 }
60
61protected:
64 size_t &offset) override
65 {
67 uint32_t val;
68
69 (void)desc;
70 (void)offset;
71
72 if (item->cfgid.s.size != 1)
73 {
74 DAWNERR("threshold: invalid config size %u\n", item->cfgid.s.size);
75 return -EINVAL;
76 }
77
78 raw = reinterpret_cast<const SObjectCfg::ObjectCfgData_t *>(item->data);
79 val = SObjectCfg::cfgToU32(raw[0]);
80
81 switch (item->cfgid.s.id)
82 {
83 case PROG_THRESHOLD_CFG_MODE:
84 {
85 if (val > MODE_WINDOW)
86 {
87 DAWNERR("threshold: invalid mode %" PRIu32 "\n", val);
88 return -EINVAL;
89 }
90
91 mode = val;
92 break;
93 }
94
95 case PROG_THRESHOLD_CFG_LOW:
96 {
97 low = raw[0];
98 break;
99 }
100
101 case PROG_THRESHOLD_CFG_HIGH:
102 {
103 high = raw[0];
104 break;
105 }
106
107 default:
108 {
109 return -ENOTSUP;
110 }
111 }
112
113 return OK;
114 }
115
116 void handle(CIOCommon *output,
117 io_ddata_t *data,
118 io_ddata_t *ioData,
119 io_ddata_t *outputData,
120 bool &initsample) override
121 {
122 (void)initsample;
123
124 if (!validateOutputDtype(data->getDtype(), outputData->getDtype()))
125 {
126 DAWNERR("threshold: invalid output dtype %d for input dtype %d\n",
127 outputData->getDtype(),
128 data->getDtype());
129 return;
130 }
131
132 switch (data->getDtype())
133 {
134#ifdef CONFIG_DAWN_DTYPE_INT8
136 {
137 handleTyped<int8_t>(output,
138 data,
139 ioData,
140 outputData,
141 static_cast<int8_t>(low),
142 static_cast<int8_t>(high));
143 break;
144 }
145#endif
146
147#ifdef CONFIG_DAWN_DTYPE_UINT8
149 {
150 handleTyped<uint8_t>(output,
151 data,
152 ioData,
153 outputData,
154 static_cast<uint8_t>(low),
155 static_cast<uint8_t>(high));
156 break;
157 }
158#endif
159
160#ifdef CONFIG_DAWN_DTYPE_INT16
162 {
163 handleTyped<int16_t>(output,
164 data,
165 ioData,
166 outputData,
167 static_cast<int16_t>(low),
168 static_cast<int16_t>(high));
169 break;
170 }
171#endif
172
173#ifdef CONFIG_DAWN_DTYPE_UINT16
175 {
176 handleTyped<uint16_t>(output,
177 data,
178 ioData,
179 outputData,
180 static_cast<uint16_t>(low),
181 static_cast<uint16_t>(high));
182 break;
183 }
184#endif
185
186#ifdef CONFIG_DAWN_DTYPE_INT32
188 {
189 handleTyped<int32_t>(output,
190 data,
191 ioData,
192 outputData,
195 break;
196 }
197#endif
198
199#ifdef CONFIG_DAWN_DTYPE_UINT32
201 {
202 handleTyped<uint32_t>(output,
203 data,
204 ioData,
205 outputData,
208 break;
209 }
210#endif
211
212#ifdef CONFIG_DAWN_DTYPE_INT64
214 {
215 handleTyped<int64_t>(output,
216 data,
217 ioData,
218 outputData,
219 static_cast<int64_t>(low),
220 static_cast<int64_t>(high));
221 break;
222 }
223#endif
224
225#ifdef CONFIG_DAWN_DTYPE_UINT64
227 {
228 handleTyped<uint64_t>(output,
229 data,
230 ioData,
231 outputData,
232 static_cast<uint64_t>(low),
233 static_cast<uint64_t>(high));
234 break;
235 }
236#endif
237
238#ifdef CONFIG_DAWN_DTYPE_FLOAT
240 {
241 handleTyped<float>(
242 output, data, ioData, outputData, SObjectCfg::cfgToF(low), SObjectCfg::cfgToF(high));
243 break;
244 }
245#endif
246
247#ifdef CONFIG_DAWN_DTYPE_DOUBLE
249 {
250 handleTyped<double>(output,
251 data,
252 ioData,
253 outputData,
254 static_cast<double>(SObjectCfg::cfgToF(low)),
255 static_cast<double>(SObjectCfg::cfgToF(high)));
256 break;
257 }
258#endif
259
260 default:
261 {
262 DAWNERR("threshold: unsupported dtype %d\n", data->getDtype());
263 break;
264 }
265 }
266 }
267
268 // Every sample of a batched source is evaluated
269
270 bool isBatchAware() const override
271 {
272 return true;
273 }
274
275 virtual bool validateOutputDtype(uint8_t inputDtype, uint8_t outputDtype) const = 0;
276 virtual int emitOutput(CIOCommon *output,
277 uint8_t inputDtype,
278 size_t items,
279 io_ddata_t *ioData,
280 io_ddata_t *outputData) = 0;
281
282private:
283 struct SState
284 {
285 size_t items = 0;
286 std::vector<uint8_t> last;
287 };
288
289 template<typename T>
290 bool evaluate(T x, T lowT, T highT, uint8_t prev) const
291 {
292 switch (mode)
293 {
294 case MODE_ABOVE:
295 return x >= highT;
296
297 case MODE_BELOW:
298 return x <= lowT;
299
300 case MODE_WINDOW:
301 return x >= lowT && x <= highT;
302
303 case MODE_HYSTERESIS:
304 if (x >= highT)
305 {
306 return true;
307 }
308
309 if (x <= lowT)
310 {
311 return false;
312 }
313
314 return prev != 0;
315
316 default:
317 return false;
318 }
319 }
320
321 template<typename T>
322 void handleTyped(CIOCommon *output,
323 io_ddata_t *data,
324 io_ddata_t *ioData,
325 io_ddata_t *outputData,
326 T lowT,
327 T highT)
328 {
329 const size_t batches = std::min(data->getBatch(), ioData->getBatch());
330 int ret;
331
332 const SObjectId::ObjectId outputId = output->getIdV();
333 SState &st = state[outputId];
334 const size_t items = ioData->getItems();
335
336 if (st.items != items || st.last.size() != items)
337 {
338 st.items = items;
339 st.last.assign(items, 0);
340 }
341
342 // Samples are evaluated in order so hysteresis carries across the batch
343
344 this->alerts.resize(batches * items);
345
346 for (size_t b = 0; b < batches; b++)
347 {
348 std::memcpy(ioData->getDataPtr(b), data->getDataPtr(b), ioData->getDataSize());
349
350 for (size_t i = 0; i < items; i++)
351 {
352 const T x = ioData->get<T>(i, b);
353
354 st.last[i] = evaluate(x, lowT, highT, st.last[i]) ? 1 : 0;
355 this->alerts[b * items + i] = st.last[i];
356 }
357 }
358
359 ret = emitOutput(output, data->getDtype(), items, ioData, outputData);
360 if (ret != OK)
361 {
362 DAWNERR("threshold: emit failed %d\n", ret);
363 }
364 }
365
366protected:
367 uint32_t mode;
370 std::vector<uint8_t> alerts;
371
373
374 size_t firstBatch(size_t items, io_ddata_t *outputData) const
375 {
376 return alerts.size() / items - outputData->getBatch();
377 }
378
379private:
380 std::map<SObjectId::ObjectId, SState> state;
381};
382
386
387class CProgThreshold : public CProgThresholdBase
388{
389public:
390 explicit CProgThreshold(CDescObject &desc)
391 : CProgThresholdBase(desc)
392 {
393 }
394
395#ifdef CONFIG_DAWN_OBJECT_HAS_NAME
396 const char *getClassNameStr() const override
397 {
398 return "threshold";
399 }
400#endif
401
402 constexpr static SObjectId::ObjectId objectId(uint16_t inst)
403 {
404 return SObjectId::objectId(
406 }
407
408 constexpr static SObjectCfg::ObjectCfgId cfgId(bool rw, uint16_t size, uint8_t id)
409 {
413 rw,
414 size,
415 id);
416 }
417
418 constexpr static SObjectCfg::ObjectCfgId cfgIdIOBind(uint16_t size = 2)
419 {
420 return CProgThreshold::cfgId(false, size, PROG_STATS_CFG_IOBIND);
421 }
422
423 constexpr static SObjectCfg::ObjectCfgId cfgIdMode()
424 {
425 return CProgThreshold::cfgId(false, 1, PROG_THRESHOLD_CFG_MODE);
426 }
427
428 constexpr static SObjectCfg::ObjectCfgId cfgIdLow()
429 {
430 return CProgThreshold::cfgId(false, 1, PROG_THRESHOLD_CFG_LOW);
431 }
432
433 constexpr static SObjectCfg::ObjectCfgId cfgIdHigh()
434 {
435 return CProgThreshold::cfgId(false, 1, PROG_THRESHOLD_CFG_HIGH);
436 }
437
438protected:
439 bool validateOutputDtype(uint8_t inputDtype, uint8_t outputDtype) const override
440 {
441 (void)inputDtype;
442 return outputDtype == SObjectId::DTYPE_BOOL;
443 }
444
445 int emitOutput(CIOCommon *output,
446 uint8_t inputDtype,
447 size_t items,
448 io_ddata_t *ioData,
449 io_ddata_t *outputData) override
450 {
451 const size_t first = firstBatch(items, outputData);
452
453 (void)inputDtype;
454 (void)ioData;
455
456 for (size_t b = 0; b < outputData->getBatch(); b++)
457 {
458 for (size_t i = 0; i < items; i++)
459 {
460 outputData->get<uint8_t>(i, b) = this->alerts[(first + b) * items + i];
461 }
462 }
463
464 return output->setData(*outputData);
465 }
466};
467
471
472class CProgThresholdValue : public CProgThresholdBase
473{
474public:
475 explicit CProgThresholdValue(CDescObject &desc)
476 : CProgThresholdBase(desc)
477 {
478 }
479
480#ifdef CONFIG_DAWN_OBJECT_HAS_NAME
481 const char *getClassNameStr() const override
482 {
483 return "thresholdvalue";
484 }
485#endif
486
487 constexpr static SObjectId::ObjectId objectId(uint16_t inst)
488 {
492 0,
493 inst);
494 }
495
496 constexpr static SObjectCfg::ObjectCfgId cfgId(bool rw, uint16_t size, uint8_t id)
497 {
501 rw,
502 size,
503 id);
504 }
505
506 constexpr static SObjectCfg::ObjectCfgId cfgIdIOBind(uint16_t size = 2)
507 {
508 return CProgThresholdValue::cfgId(false, size, PROG_STATS_CFG_IOBIND);
509 }
510
511 constexpr static SObjectCfg::ObjectCfgId cfgIdMode()
512 {
513 return CProgThresholdValue::cfgId(false, 1, PROG_THRESHOLD_CFG_MODE);
514 }
515
516 constexpr static SObjectCfg::ObjectCfgId cfgIdLow()
517 {
518 return CProgThresholdValue::cfgId(false, 1, PROG_THRESHOLD_CFG_LOW);
519 }
520
521 constexpr static SObjectCfg::ObjectCfgId cfgIdHigh()
522 {
523 return CProgThresholdValue::cfgId(false, 1, PROG_THRESHOLD_CFG_HIGH);
524 }
525
526protected:
527 bool validateOutputDtype(uint8_t inputDtype, uint8_t outputDtype) const override
528 {
529 return outputDtype == inputDtype;
530 }
531
532 int emitOutput(CIOCommon *output,
533 uint8_t inputDtype,
534 size_t items,
535 io_ddata_t *ioData,
536 io_ddata_t *outputData) override
537 {
538 switch (inputDtype)
539 {
540#ifdef CONFIG_DAWN_DTYPE_INT8
542 {
543 return emitOutputTyped<int8_t>(output, items, ioData, outputData);
544 }
545#endif
546
547#ifdef CONFIG_DAWN_DTYPE_UINT8
549 {
550 return emitOutputTyped<uint8_t>(output, items, ioData, outputData);
551 }
552#endif
553
554#ifdef CONFIG_DAWN_DTYPE_INT16
556 {
557 return emitOutputTyped<int16_t>(output, items, ioData, outputData);
558 }
559#endif
560
561#ifdef CONFIG_DAWN_DTYPE_UINT16
563 {
564 return emitOutputTyped<uint16_t>(output, items, ioData, outputData);
565 }
566#endif
567
568#ifdef CONFIG_DAWN_DTYPE_INT32
570 {
571 return emitOutputTyped<int32_t>(output, items, ioData, outputData);
572 }
573#endif
574
575#ifdef CONFIG_DAWN_DTYPE_UINT32
577 {
578 return emitOutputTyped<uint32_t>(output, items, ioData, outputData);
579 }
580#endif
581
582#ifdef CONFIG_DAWN_DTYPE_INT64
584 {
585 return emitOutputTyped<int64_t>(output, items, ioData, outputData);
586 }
587#endif
588
589#ifdef CONFIG_DAWN_DTYPE_UINT64
591 {
592 return emitOutputTyped<uint64_t>(output, items, ioData, outputData);
593 }
594#endif
595
596#ifdef CONFIG_DAWN_DTYPE_FLOAT
598 {
599 return emitOutputTyped<float>(output, items, ioData, outputData);
600 }
601#endif
602
603#ifdef CONFIG_DAWN_DTYPE_DOUBLE
605 {
606 return emitOutputTyped<double>(output, items, ioData, outputData);
607 }
608#endif
609
610 default:
611 {
612 return -ENOTSUP;
613 }
614 }
615 }
616
617private:
618 template<typename T>
619 int emitOutputTyped(CIOCommon *output, size_t items, io_ddata_t *ioData, io_ddata_t *outputData)
620 {
621 const size_t first = firstBatch(items, outputData);
622
623 for (size_t b = 0; b < outputData->getBatch(); b++)
624 {
625 for (size_t i = 0; i < items; i++)
626 {
627 const size_t src = first + b;
628
629 if (this->alerts[src * items + i] != 0)
630 {
631 outputData->get<T>(i, b) = ioData->get<T>(i, src);
632 }
633 else
634 {
635 outputData->get<T>(i, b) = static_cast<T>(0);
636 }
637 }
638 }
639
640 return output->setData(*outputData);
641 }
642};
643} // Namespace dawn
Descriptor wrapper for individual object configuration.
Base class for all I/O objects.
Definition common.hxx:30
int setData(IODataCmn &data, size_t offset=0)
Set data for I/O (public interface with stats tracking).
Definition common.hxx:421
@ CMD_RESET
Reset object internal state.
Definition object.hxx:42
@ PROG_CLASS_THRESHOLD_VALUE
Threshold comparator returning gated source value.
Definition common.hxx:100
@ PROG_CLASS_THRESHOLD
Threshold and hysteresis comparator.
Definition common.hxx:96
CProgProcess(CDescObject &desc)
Construct a a Program from descriptor.
Definition process.hxx:53
int trigger(uint8_t cmd)
Execute a trigger command.
Definition process.cxx:323
@ PROG_STATS_CFG_IOBIND
I/O binding configuration.
Definition process.hxx:35
int trigger(uint8_t cmd)
Execute a trigger command.
Definition threshold.hxx:51
bool isBatchAware() const
Whether handle() consumes every batch of a batched source.
int configureExtraCfgItem(const CDescObject &desc, const SObjectCfg::SObjectCfgItem *item, size_t &offset)
Parse program-specific config item (optional extension hook).
Definition threshold.hxx:62
size_t firstBatch(size_t items, io_ddata_t *outputData) const
First alert batch for an output holding fewer batches.
std::vector< uint8_t > alerts
Alert per sample, items per batch.
void handle(CIOCommon *output, io_ddata_t *data, io_ddata_t *ioData, io_ddata_t *outputData, bool &initsample)
Process incoming sample.
static ObjectCfgId objectCfg(uint8_t type, uint16_t cls, uint8_t dtype, bool rw, uint16_t size, uint8_t id)
Construct 32-bit ConfigID from component fields.
uint32_t ObjectCfgId
ConfigID type - single 32-bit value.
Definition objectcfg.hxx:60
uint32_t ObjectCfgData_t
Configuration data element - single 32-bit word.
Definition objectcfg.hxx:69
static float cfgToF(ObjectCfgData_t x)
Convert ObjectCfgData_t to float.
static uint32_t cfgToU32(ObjectCfgData_t x)
Convert ObjectCfgData_t to uint32_t.
static int32_t cfgtoi32(ObjectCfgData_t x)
Convert ObjectCfgData_t to int32_t.
Out-of-tree user-extension hooks for Dawn.
Definition bindable.hxx:13
Single configuration item within object.
ObjectCfgData_t data[]
Configuration data array (flexible, size from cfgid.s.size).
UObjectCfgId cfgid
Configuration ID header (type, class, id, size, rw, dtype).
@ OBJTYPE_PROG
Program/algorithm object type.
Definition objectid.hxx:202
@ DTYPE_FLOAT
IEEE 754 single-precision floating point (32-bit).
Definition objectid.hxx:112
@ DTYPE_INT32
Signed 32-bit integer (-2147483648 to 2147483647).
Definition objectid.hxx:92
@ DTYPE_UINT8
Unsigned 8-bit integer (0 to 255).
Definition objectid.hxx:80
@ DTYPE_INT16
Signed 16-bit integer (-32768 to 32767).
Definition objectid.hxx:84
@ DTYPE_INT8
Signed 8-bit integer (-128 to 127).
Definition objectid.hxx:76
@ DTYPE_ANY
Wildcard data type (matches any actual type).
Definition objectid.hxx:68
@ DTYPE_UINT64
Unsigned 64-bit integer.
Definition objectid.hxx:104
@ DTYPE_UINT16
Unsigned 16-bit integer (0 to 65535).
Definition objectid.hxx:88
@ DTYPE_DOUBLE
IEEE 754 double-precision floating point (64-bit).
Definition objectid.hxx:120
@ DTYPE_INT64
Signed 64-bit integer.
Definition objectid.hxx:100
@ DTYPE_UINT32
Unsigned 32-bit integer (0 to 4294967295).
Definition objectid.hxx:96
@ DTYPE_BOOL
Boolean data type (stored in 32-bit container).
Definition objectid.hxx:72
uint32_t ObjectId
ObjectID type - single 32-bit value.
Definition objectid.hxx:44
static ObjectId objectId(uint8_t type, uint16_t cls, uint8_t dtype, uint8_t flags, uint16_t priv)
Construct 32-bit ObjectID from component fields.
Definition objectid.hxx:290
Heap-allocated dynamic I/O data buffer.
Definition ddata.hxx:21
uint8_t getDtype() const
Get data type enum for introspection.
Definition ddata.hxx:217
T & get(size_t index, size_t batch=0)
Get data element by index and batch (type-safe).
Definition ddata.hxx:93
size_t getBatch()
Get number of batches.
Definition ddata.hxx:135
uint32_t id
Configuration identifier (bits 0-4, max 31).
Definition objectcfg.hxx:94
struct dawn::SObjectCfg::UObjectCfgId::@334307326077356333363326153167141302007100111136 s
Bit-field structure for named member access.
uint32_t size
Configuration data size in 32-bit words (bits 5-14, max 1023).