Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 110.47 KB | None | 0 0
  1. // The sketch is auto-generated with XOD (https://xod.io).
  2. //
  3. // You can compile and upload it to an Arduino-compatible board with
  4. // Arduino IDE.
  5. //
  6. // Rough code overview:
  7. //
  8. // - Configuration section
  9. // - STL shim
  10. // - Immutable list classes and functions
  11. // - XOD runtime environment
  12. // - Native node implementation
  13. // - Program graph definition
  14. //
  15. // Search for comments fenced with '====' and '----' to navigate through
  16. // the major code blocks.
  17.  
  18. #include <Arduino.h>
  19. #include <inttypes.h>
  20.  
  21.  
  22. /*=============================================================================
  23. *
  24. *
  25. * Configuration
  26. *
  27. *
  28. =============================================================================*/
  29.  
  30. // Uncomment to turn on debug of the program
  31. //#define XOD_DEBUG
  32.  
  33. // Uncomment to trace the program runtime in the Serial Monitor
  34. //#define XOD_DEBUG_ENABLE_TRACE
  35.  
  36.  
  37. // Uncomment to make possible simulation of the program
  38. //#define XOD_SIMULATION
  39.  
  40. #ifdef XOD_SIMULATION
  41. #include <WasmSerial.h>
  42. #define XOD_DEBUG_SERIAL WasmSerial
  43. #else
  44. #define XOD_DEBUG_SERIAL DEBUG_SERIAL
  45. #endif
  46.  
  47. /*=============================================================================
  48. *
  49. *
  50. * STL shim. Provides implementation for vital std::* constructs
  51. *
  52. *
  53. =============================================================================*/
  54.  
  55. namespace xod {
  56. namespace std {
  57.  
  58. template< class T > struct remove_reference {typedef T type;};
  59. template< class T > struct remove_reference<T&> {typedef T type;};
  60. template< class T > struct remove_reference<T&&> {typedef T type;};
  61.  
  62. template <class T>
  63. typename remove_reference<T>::type&& move(T&& a) {
  64. return static_cast<typename remove_reference<T>::type&&>(a);
  65. }
  66.  
  67. } // namespace std
  68. } // namespace xod
  69.  
  70. /*=============================================================================
  71. *
  72. *
  73. * Basic XOD types
  74. *
  75. *
  76. =============================================================================*/
  77. namespace xod {
  78. #if __SIZEOF_FLOAT__ == 4
  79. typedef float Number;
  80. #else
  81. typedef double Number;
  82. #endif
  83. typedef bool Logic;
  84. typedef unsigned long TimeMs;
  85. typedef uint8_t ErrorFlags;
  86.  
  87. struct Pulse {
  88. Pulse() {}
  89. Pulse(bool) {}
  90. Pulse(int) {}
  91. };
  92.  
  93. struct XColor {
  94. uint8_t r, g, b;
  95. };
  96.  
  97. } // namespace xod
  98.  
  99. /*=============================================================================
  100. *
  101. *
  102. * XOD-specific list/array implementations
  103. *
  104. *
  105. =============================================================================*/
  106.  
  107. #ifndef XOD_LIST_H
  108. #define XOD_LIST_H
  109.  
  110. namespace xod {
  111. namespace detail {
  112.  
  113. /*
  114. * Cursors are used internaly by iterators and list views. They are not exposed
  115. * directly to a list consumer.
  116. *
  117. * The base `Cursor` is an interface which provides the bare minimum of methods
  118. * to facilitate a single iteration pass.
  119. */
  120. template<typename T> class Cursor {
  121. public:
  122. virtual ~Cursor() { }
  123. virtual bool isValid() const = 0;
  124. virtual bool value(T* out) const = 0;
  125. virtual void next() = 0;
  126. };
  127.  
  128. template<typename T> class NilCursor : public Cursor<T> {
  129. public:
  130. virtual bool isValid() const { return false; }
  131. virtual bool value(T*) const { return false; }
  132. virtual void next() { }
  133. };
  134.  
  135. } // namespace detail
  136.  
  137. /*
  138. * Iterator is an object used to iterate a list once.
  139. *
  140. * Users create new iterators by calling `someList.iterate()`.
  141. * Iterators are created on stack and are supposed to have a
  142. * short live, e.g. for a duration of `for` loop or node’s
  143. * `evaluate` function. Iterators can’t be copied.
  144. *
  145. * Implemented as a pimpl pattern wrapper over the cursor.
  146. * Once created for a cursor, an iterator owns that cursor
  147. * and will delete the cursor object once destroyed itself.
  148. */
  149. template<typename T>
  150. class Iterator {
  151. public:
  152. static Iterator<T> nil() {
  153. return Iterator<T>(new detail::NilCursor<T>());
  154. }
  155.  
  156. Iterator(detail::Cursor<T>* cursor)
  157. : _cursor(cursor)
  158. { }
  159.  
  160. ~Iterator() {
  161. if (_cursor)
  162. delete _cursor;
  163. }
  164.  
  165. Iterator(const Iterator& that) = delete;
  166. Iterator& operator=(const Iterator& that) = delete;
  167.  
  168. Iterator(Iterator&& it)
  169. : _cursor(it._cursor)
  170. {
  171. it._cursor = nullptr;
  172. }
  173.  
  174. Iterator& operator=(Iterator&& it) {
  175. auto tmp = it._cursor;
  176. it._cursor = _cursor;
  177. _cursor = tmp;
  178. return *this;
  179. }
  180.  
  181. operator bool() const { return _cursor->isValid(); }
  182.  
  183. bool value(T* out) const {
  184. return _cursor->value(out);
  185. }
  186.  
  187. T operator*() const {
  188. T out;
  189. _cursor->value(&out);
  190. return out;
  191. }
  192.  
  193. Iterator& operator++() {
  194. _cursor->next();
  195. return *this;
  196. }
  197.  
  198. private:
  199. detail::Cursor<T>* _cursor;
  200. };
  201.  
  202. /*
  203. * An interface for a list view. A particular list view provides a new
  204. * kind of iteration over existing data. This way we can use list slices,
  205. * list concatenations, list rotations, etc without introducing new data
  206. * buffers. We just change the way already existing data is iterated.
  207. *
  208. * ListView is not exposed to a list user directly, it is used internally
  209. * by the List class. However, deriving a new ListView is necessary if you
  210. * make a new list/string processing node.
  211. */
  212. template<typename T> class ListView {
  213. public:
  214. virtual Iterator<T> iterate() const = 0;
  215. };
  216.  
  217. /*
  218. * The list as it seen by data consumers. Have a single method `iterate`
  219. * to create a new iterator.
  220. *
  221. * Implemented as pimpl pattern wrapper over a list view. Takes pointer
  222. * to a list view in constructor and expects the view will be alive for
  223. * the whole life time of the list.
  224. */
  225. template<typename T> class List {
  226. public:
  227. constexpr List()
  228. : _view(nullptr)
  229. { }
  230.  
  231. List(const ListView<T>* view)
  232. : _view(view)
  233. { }
  234.  
  235. Iterator<T> iterate() const {
  236. return _view ? _view->iterate() : Iterator<T>::nil();
  237. }
  238.  
  239. // pre 0.15.0 backward compatibility
  240. List* operator->() __attribute__ ((deprecated)) { return this; }
  241. const List* operator->() const __attribute__ ((deprecated)) { return this; }
  242.  
  243. private:
  244. const ListView<T>* _view;
  245. };
  246.  
  247. /*
  248. * A list view over an old good plain C array.
  249. *
  250. * Expects the array will be alive for the whole life time of the
  251. * view.
  252. */
  253. template<typename T> class PlainListView : public ListView<T> {
  254. public:
  255. class Cursor : public detail::Cursor<T> {
  256. public:
  257. Cursor(const PlainListView* owner)
  258. : _owner(owner)
  259. , _idx(0)
  260. { }
  261.  
  262. bool isValid() const override {
  263. return _idx < _owner->_len;
  264. }
  265.  
  266. bool value(T* out) const override {
  267. if (!isValid())
  268. return false;
  269. *out = _owner->_data[_idx];
  270. return true;
  271. }
  272.  
  273. void next() override { ++_idx; }
  274.  
  275. private:
  276. const PlainListView* _owner;
  277. size_t _idx;
  278. };
  279.  
  280. public:
  281. PlainListView(const T* data, size_t len)
  282. : _data(data)
  283. , _len(len)
  284. { }
  285.  
  286. virtual Iterator<T> iterate() const override {
  287. return Iterator<T>(new Cursor(this));
  288. }
  289.  
  290. private:
  291. friend class Cursor;
  292. const T* _data;
  293. size_t _len;
  294. };
  295.  
  296. /*
  297. * A list view over a null-terminated C-String.
  298. *
  299. * Expects the char buffer will be alive for the whole life time of the view.
  300. * You can use string literals as a buffer, since they are persistent for
  301. * the program execution time.
  302. */
  303. class CStringView : public ListView<char> {
  304. public:
  305. class Cursor : public detail::Cursor<char> {
  306. public:
  307. Cursor(const char* str)
  308. : _ptr(str)
  309. { }
  310.  
  311. bool isValid() const override {
  312. return (bool)*_ptr;
  313. }
  314.  
  315. bool value(char* out) const override {
  316. *out = *_ptr;
  317. return (bool)*_ptr;
  318. }
  319.  
  320. void next() override { ++_ptr; }
  321.  
  322. private:
  323. const char* _ptr;
  324. };
  325.  
  326. public:
  327. CStringView(const char* str = nullptr)
  328. : _str(str)
  329. { }
  330.  
  331. CStringView& operator=(const CStringView& rhs) {
  332. _str = rhs._str;
  333. return *this;
  334. }
  335.  
  336. virtual Iterator<char> iterate() const override {
  337. return _str ? Iterator<char>(new Cursor(_str)) : Iterator<char>::nil();
  338. }
  339.  
  340. private:
  341. friend class Cursor;
  342. const char* _str;
  343. };
  344.  
  345. /*
  346. * A list view over two other lists (Left and Right) which first iterates the
  347. * left one, and when exhausted, iterates the right one.
  348. *
  349. * Expects both Left and Right to be alive for the whole view life time.
  350. */
  351. template<typename T> class ConcatListView : public ListView<T> {
  352. public:
  353. class Cursor : public detail::Cursor<T> {
  354. public:
  355. Cursor(Iterator<T>&& left, Iterator<T>&& right)
  356. : _left(std::move(left))
  357. , _right(std::move(right))
  358. { }
  359.  
  360. bool isValid() const override {
  361. return _left || _right;
  362. }
  363.  
  364. bool value(T* out) const override {
  365. return _left.value(out) || _right.value(out);
  366. }
  367.  
  368. void next() override {
  369. _left ? ++_left : ++_right;
  370. }
  371.  
  372. private:
  373. Iterator<T> _left;
  374. Iterator<T> _right;
  375. };
  376.  
  377. public:
  378. ConcatListView() { }
  379.  
  380. ConcatListView(List<T> left, List<T> right)
  381. : _left(left)
  382. , _right(right)
  383. { }
  384.  
  385. ConcatListView& operator=(const ConcatListView& rhs) {
  386. _left = rhs._left;
  387. _right = rhs._right;
  388. return *this;
  389. }
  390.  
  391. virtual Iterator<T> iterate() const override {
  392. return Iterator<T>(new Cursor(_left.iterate(), _right.iterate()));
  393. }
  394.  
  395. private:
  396. friend class Cursor;
  397. List<T> _left;
  398. List<T> _right;
  399. };
  400.  
  401. //----------------------------------------------------------------------------
  402. // Text string helpers
  403. //----------------------------------------------------------------------------
  404.  
  405. using XString = List<char>;
  406.  
  407. /*
  408. * List and list view in a single pack. An utility used to define constant
  409. * string literals in XOD.
  410. */
  411. class XStringCString : public XString {
  412. public:
  413. XStringCString(const char* str)
  414. : XString(&_view)
  415. , _view(str)
  416. { }
  417.  
  418. private:
  419. CStringView _view;
  420. };
  421.  
  422. } // namespace xod
  423.  
  424. #endif
  425.  
  426. /*=============================================================================
  427. *
  428. *
  429. * Functions to work with memory
  430. *
  431. *
  432. =============================================================================*/
  433.  
  434. // Define the placement new operator for cores that do not provide their own.
  435. // Note, this definition takes precedence over the existing one (if any). We found no C++ way
  436. // to use the existing implementation _and_ this implementation if not yet defined.
  437. template<typename T>
  438. void* operator new(size_t, T* ptr) noexcept {
  439. return ptr;
  440. }
  441.  
  442. /*=============================================================================
  443. *
  444. *
  445. * UART Classes, that wraps Serials
  446. *
  447. *
  448. =============================================================================*/
  449.  
  450. class HardwareSerial;
  451. class SoftwareSerial;
  452.  
  453. namespace xod {
  454.  
  455. class Uart {
  456. private:
  457. long _baud;
  458.  
  459. protected:
  460. bool _started = false;
  461.  
  462. public:
  463. Uart(long baud) {
  464. _baud = baud;
  465. }
  466.  
  467. virtual void begin() = 0;
  468.  
  469. virtual void end() = 0;
  470.  
  471. virtual void flush() = 0;
  472.  
  473. virtual bool available() = 0;
  474.  
  475. virtual bool writeByte(uint8_t) = 0;
  476.  
  477. virtual bool readByte(uint8_t*) = 0;
  478.  
  479. virtual SoftwareSerial* toSoftwareSerial() {
  480. return nullptr;
  481. }
  482.  
  483. virtual HardwareSerial* toHardwareSerial() {
  484. return nullptr;
  485. }
  486.  
  487. void changeBaudRate(long baud) {
  488. _baud = baud;
  489. if (_started) {
  490. end();
  491. begin();
  492. }
  493. }
  494.  
  495. long getBaudRate() const {
  496. return _baud;
  497. }
  498.  
  499. Stream* toStream() {
  500. Stream* stream = (Stream*) toHardwareSerial();
  501. if (stream) return stream;
  502. return (Stream*) toSoftwareSerial();
  503. }
  504. };
  505.  
  506. class HardwareUart : public Uart {
  507. private:
  508. HardwareSerial* _serial;
  509.  
  510. public:
  511. HardwareUart(HardwareSerial& hserial, uint32_t baud = 115200) : Uart(baud) {
  512. _serial = &hserial;
  513. }
  514.  
  515. void begin();
  516. void end();
  517. void flush();
  518.  
  519. bool available() {
  520. return (bool) _serial->available();
  521. }
  522.  
  523. bool writeByte(uint8_t byte) {
  524. return (bool) _serial->write(byte);
  525. }
  526.  
  527. bool readByte(uint8_t* out) {
  528. int data = _serial->read();
  529. if (data == -1) return false;
  530. *out = data;
  531. return true;
  532. }
  533.  
  534. HardwareSerial* toHardwareSerial() {
  535. return _serial;
  536. }
  537. };
  538.  
  539. void HardwareUart::begin() {
  540. _started = true;
  541. _serial->begin(getBaudRate());
  542. };
  543. void HardwareUart::end() {
  544. _started = false;
  545. _serial->end();
  546. };
  547. void HardwareUart::flush() {
  548. _serial->flush();
  549. };
  550.  
  551. } // namespace xod
  552.  
  553. /*=============================================================================
  554. *
  555. *
  556. * Basic algorithms for XOD lists
  557. *
  558. *
  559. =============================================================================*/
  560.  
  561. #ifndef XOD_LIST_FUNCS_H
  562. #define XOD_LIST_FUNCS_H
  563.  
  564.  
  565.  
  566. namespace xod {
  567.  
  568. /*
  569. * Folds a list from left. Also known as "reduce".
  570. */
  571. template<typename T, typename TR>
  572. TR foldl(List<T> xs, TR (*func)(TR, T), TR acc) {
  573. for (auto it = xs.iterate(); it; ++it)
  574. acc = func(acc, *it);
  575. return acc;
  576. }
  577.  
  578. template<typename T> size_t lengthReducer(size_t len, T) {
  579. return len + 1;
  580. }
  581.  
  582. /*
  583. * Computes length of a list.
  584. */
  585. template<typename T> size_t length(List<T> xs) {
  586. return foldl(xs, lengthReducer<T>, (size_t)0);
  587. }
  588.  
  589. template<typename T> T* dumpReducer(T* buff, T x) {
  590. *buff = x;
  591. return buff + 1;
  592. }
  593.  
  594. /*
  595. * Copies a list content into a memory buffer.
  596. *
  597. * It is expected that `outBuff` has enough size to fit all the data.
  598. */
  599. template<typename T> size_t dump(List<T> xs, T* outBuff) {
  600. T* buffEnd = foldl(xs, dumpReducer, outBuff);
  601. return buffEnd - outBuff;
  602. }
  603.  
  604. /*
  605. * Compares two lists.
  606. */
  607. template<typename T> bool equal(List<T> lhs, List<T> rhs) {
  608. auto lhsIt = lhs.iterate();
  609. auto rhsIt = rhs.iterate();
  610.  
  611. for (; lhsIt && rhsIt; ++lhsIt, ++rhsIt) {
  612. if (*lhsIt != *rhsIt) return false;
  613. }
  614.  
  615. return !lhsIt && !rhsIt;
  616. }
  617.  
  618. template<typename T> bool operator == (List<T> lhs, List<T> rhs) {
  619. return equal(lhs, rhs);
  620. }
  621.  
  622. } // namespace xod
  623.  
  624. #endif
  625.  
  626. /*=============================================================================
  627. *
  628. *
  629. * Format Numbers
  630. *
  631. *
  632. =============================================================================*/
  633.  
  634. /**
  635. * Provide `formatNumber` cross-platform number to string converter function.
  636. *
  637. * Taken from here:
  638. * https://github.com/client9/stringencoders/blob/master/src/modp_numtoa.c
  639. * Original function name: `modp_dtoa2`.
  640. *
  641. * Modified:
  642. * - `isnan` instead of tricky comparing and return "NaN"
  643. * - handle Infinity values and return "Inf" or "-Inf"
  644. * - return `OVF` and `-OVF` for numbers bigger than max possible, instead of using `sprintf`
  645. * - use `Number` instead of double
  646. * - if negative number rounds to zero, return just "0" instead of "-0"
  647. *
  648. * This is a replacement of `dtostrf`.
  649. */
  650.  
  651. #ifndef XOD_FORMAT_NUMBER_H
  652. #define XOD_FORMAT_NUMBER_H
  653.  
  654. namespace xod {
  655.  
  656. /**
  657. * Powers of 10
  658. * 10^0 to 10^9
  659. */
  660. static const Number powers_of_10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000,
  661. 10000000, 100000000, 1000000000 };
  662.  
  663. static void strreverse(char* begin, char* end) {
  664. char aux;
  665. while (end > begin)
  666. aux = *end, *end-- = *begin, *begin++ = aux;
  667. };
  668.  
  669. size_t formatNumber(Number value, int prec, char* str) {
  670. if (isnan(value)) {
  671. strcpy(str, "NaN");
  672. return (size_t)3;
  673. }
  674.  
  675. if (isinf(value)) {
  676. bool isNegative = value < 0;
  677. strcpy(str, isNegative ? "-Inf" : "Inf");
  678. return (size_t)isNegative ? 4 : 3;
  679. }
  680.  
  681. /* if input is larger than thres_max return "OVF" */
  682. const Number thres_max = (Number)(0x7FFFFFFF);
  683.  
  684. Number diff = 0.0;
  685. char* wstr = str;
  686.  
  687. if (prec < 0) {
  688. prec = 0;
  689. } else if (prec > 9) {
  690. /* precision of >= 10 can lead to overflow errors */
  691. prec = 9;
  692. }
  693.  
  694. /* we'll work in positive values and deal with the
  695. negative sign issue later */
  696. int neg = 0;
  697. if (value < 0) {
  698. neg = 1;
  699. value = -value;
  700. }
  701.  
  702. uint32_t whole = (uint32_t)value;
  703. Number tmp = (value - whole) * powers_of_10[prec];
  704. uint32_t frac = (uint32_t)(tmp);
  705. diff = tmp - frac;
  706.  
  707. if (diff > 0.5) {
  708. ++frac;
  709. /* handle rollover, e.g. case 0.99 with prec 1 is 1.0 */
  710. if (frac >= powers_of_10[prec]) {
  711. frac = 0;
  712. ++whole;
  713. }
  714. } else if (diff == 0.5 && prec > 0 && (frac & 1)) {
  715. /* if halfway, round up if odd, OR
  716. if last digit is 0. That last part is strange */
  717. ++frac;
  718. if (frac >= powers_of_10[prec]) {
  719. frac = 0;
  720. ++whole;
  721. }
  722. } else if (diff == 0.5 && prec == 0 && (whole & 1)) {
  723. ++frac;
  724. if (frac >= powers_of_10[prec]) {
  725. frac = 0;
  726. ++whole;
  727. }
  728. }
  729.  
  730. if (value > thres_max) {
  731. if (neg) {
  732. strcpy(str, "-OVF");
  733. return (size_t)4;
  734. }
  735. strcpy(str, "OVF");
  736. return (size_t)3;
  737. }
  738.  
  739. int has_decimal = 0;
  740. int count = prec;
  741. bool notzero = frac > 0;
  742.  
  743. while (count > 0) {
  744. --count;
  745. *wstr++ = (char)(48 + (frac % 10));
  746. frac /= 10;
  747. has_decimal = 1;
  748. }
  749.  
  750. if (frac > 0) {
  751. ++whole;
  752. }
  753.  
  754. /* add decimal */
  755. if (has_decimal) {
  756. *wstr++ = '.';
  757. }
  758.  
  759. notzero = notzero || whole > 0;
  760.  
  761. /* do whole part
  762. * Take care of sign conversion
  763. * Number is reversed.
  764. */
  765. do
  766. *wstr++ = (char)(48 + (whole % 10));
  767. while (whole /= 10);
  768.  
  769. if (neg && notzero) {
  770. *wstr++ = '-';
  771. }
  772. *wstr = '\0';
  773. strreverse(str, wstr - 1);
  774. return (size_t)(wstr - str);
  775. }
  776.  
  777. } // namespace xod
  778. #endif
  779.  
  780.  
  781. /*=============================================================================
  782. *
  783. *
  784. * Runtime
  785. *
  786. *
  787. =============================================================================*/
  788.  
  789. //----------------------------------------------------------------------------
  790. // Debug routines
  791. //----------------------------------------------------------------------------
  792. // #ifndef DEBUG_SERIAL
  793. #if defined(XOD_DEBUG) && !defined(DEBUG_SERIAL)
  794. # define DEBUG_SERIAL Serial
  795. #endif
  796.  
  797. #if defined(XOD_DEBUG) && defined(XOD_DEBUG_ENABLE_TRACE)
  798. # define XOD_TRACE(x) { DEBUG_SERIAL.print(x); DEBUG_SERIAL.flush(); }
  799. # define XOD_TRACE_LN(x) { DEBUG_SERIAL.println(x); DEBUG_SERIAL.flush(); }
  800. # define XOD_TRACE_F(x) XOD_TRACE(F(x))
  801. # define XOD_TRACE_FLN(x) XOD_TRACE_LN(F(x))
  802. #else
  803. # define XOD_TRACE(x)
  804. # define XOD_TRACE_LN(x)
  805. # define XOD_TRACE_F(x)
  806. # define XOD_TRACE_FLN(x)
  807. #endif
  808.  
  809. //----------------------------------------------------------------------------
  810. // PGM space utilities
  811. //----------------------------------------------------------------------------
  812. #define pgm_read_nodeid(address) (pgm_read_word(address))
  813.  
  814. /*
  815. * Workaround for bugs:
  816. * https://github.com/arduino/ArduinoCore-sam/pull/43
  817. * https://github.com/arduino/ArduinoCore-samd/pull/253
  818. * Remove after the PRs merge
  819. */
  820. #if !defined(ARDUINO_ARCH_AVR) && defined(pgm_read_ptr)
  821. # undef pgm_read_ptr
  822. # define pgm_read_ptr(addr) (*(const void **)(addr))
  823. #endif
  824.  
  825. namespace xod {
  826. //----------------------------------------------------------------------------
  827. // Global variables
  828. //----------------------------------------------------------------------------
  829.  
  830. TimeMs g_transactionTime;
  831. bool g_isSettingUp;
  832. bool g_isEarlyDeferPass;
  833.  
  834. //----------------------------------------------------------------------------
  835. // Metaprogramming utilities
  836. //----------------------------------------------------------------------------
  837.  
  838. template<typename T> struct always_false {
  839. enum { value = 0 };
  840. };
  841.  
  842. //----------------------------------------------------------------------------
  843. // Forward declarations
  844. //----------------------------------------------------------------------------
  845.  
  846. TimeMs transactionTime();
  847. void runTransaction();
  848.  
  849. //----------------------------------------------------------------------------
  850. // Engine (private API)
  851. //----------------------------------------------------------------------------
  852.  
  853. namespace detail {
  854.  
  855. template<typename NodeT>
  856. bool isTimedOut(const NodeT* node) {
  857. TimeMs t = node->timeoutAt;
  858. // TODO: deal with uint32 overflow
  859. return t && t < transactionTime();
  860. }
  861.  
  862. template<typename NodeT>
  863. void clearTimeout(NodeT* node) {
  864. node->timeoutAt = 0;
  865. }
  866.  
  867. template<typename NodeT>
  868. void clearStaleTimeout(NodeT* node) {
  869. if (isTimedOut(node))
  870. clearTimeout(node);
  871. }
  872.  
  873. void printErrorToDebugSerial(uint16_t nodeId, ErrorFlags errorFlags) {
  874. #if defined(XOD_DEBUG) || defined(XOD_SIMULATION)
  875. XOD_DEBUG_SERIAL.print(F("+XOD_ERR:"));
  876. XOD_DEBUG_SERIAL.print(g_transactionTime);
  877. XOD_DEBUG_SERIAL.print(':');
  878. XOD_DEBUG_SERIAL.print(nodeId);
  879. XOD_DEBUG_SERIAL.print(':');
  880. XOD_DEBUG_SERIAL.print(errorFlags, DEC);
  881. XOD_DEBUG_SERIAL.print('\r');
  882. XOD_DEBUG_SERIAL.print('\n');
  883. #endif
  884. }
  885.  
  886. } // namespace detail
  887.  
  888. //----------------------------------------------------------------------------
  889. // Public API (can be used by native nodes’ `evaluate` functions)
  890. //----------------------------------------------------------------------------
  891.  
  892. TimeMs transactionTime() {
  893. return g_transactionTime;
  894. }
  895.  
  896. bool isSettingUp() {
  897. return g_isSettingUp;
  898. }
  899.  
  900. bool isEarlyDeferPass() {
  901. return g_isEarlyDeferPass;
  902. }
  903.  
  904. template<typename ContextT>
  905. void setTimeout(ContextT* ctx, TimeMs timeout) {
  906. ctx->_node->timeoutAt = transactionTime() + timeout;
  907. }
  908.  
  909. template<typename ContextT>
  910. void clearTimeout(ContextT* ctx) {
  911. detail::clearTimeout(ctx->_node);
  912. }
  913.  
  914. template<typename ContextT>
  915. bool isTimedOut(const ContextT* ctx) {
  916. return detail::isTimedOut(ctx->_node);
  917. }
  918.  
  919. bool isValidDigitalPort(uint8_t port) {
  920. #if defined(__AVR__) && defined(NUM_DIGITAL_PINS)
  921. return port < NUM_DIGITAL_PINS;
  922. #else
  923. return true;
  924. #endif
  925. }
  926.  
  927. bool isValidAnalogPort(uint8_t port) {
  928. #if defined(__AVR__) && defined(NUM_ANALOG_INPUTS)
  929. return port >= A0 && port < A0 + NUM_ANALOG_INPUTS;
  930. #else
  931. return true;
  932. #endif
  933. }
  934.  
  935. } // namespace xod
  936.  
  937. //----------------------------------------------------------------------------
  938. // Entry point
  939. //----------------------------------------------------------------------------
  940. void setup() {
  941. // FIXME: looks like there is a rounding bug. Waiting for 100ms fights it
  942. delay(100);
  943.  
  944. #if defined(XOD_DEBUG) || defined(XOD_SIMULATION)
  945. XOD_DEBUG_SERIAL.begin(115200);
  946. XOD_DEBUG_SERIAL.setTimeout(10);
  947. #endif
  948. XOD_TRACE_FLN("\n\nProgram started");
  949.  
  950. xod::g_isSettingUp = true;
  951. xod::runTransaction();
  952. xod::g_isSettingUp = false;
  953. }
  954.  
  955. void loop() {
  956. xod::runTransaction();
  957. }
  958.  
  959. /*=============================================================================
  960. *
  961. *
  962. * Native node implementations
  963. *
  964. *
  965. =============================================================================*/
  966.  
  967. namespace xod {
  968.  
  969. //-----------------------------------------------------------------------------
  970. // xod-dev/servo/servo-device implementation
  971. //-----------------------------------------------------------------------------
  972. namespace xod_dev__servo__servo_device {
  973.  
  974. //#pragma XOD error_raise enable
  975.  
  976. // --- Enter global namespace ---
  977. }}
  978. #include <Servo.h>
  979.  
  980. namespace xod {
  981. namespace xod_dev__servo__servo_device {
  982. // --- Back to local namespace ---
  983. /*
  984. A wrapper around the stock Servo object because we need to keep some details
  985. which the original object hides in private fields. This over-protection leads
  986. to increased RAM usage to duplicate the data. A pull request to the original
  987. library asking to add field read methods would be nice.
  988. */
  989. class XServo : public Servo {
  990. protected:
  991. // Here are the duplicates
  992. uint8_t port;
  993. int pulseMin;
  994. int pulseMax;
  995.  
  996. public:
  997. // Set pulse duration according the given `value` and set pulseMin, pulseMax
  998. // The value is clipped to the [0; 1] range
  999. void write01(Number value) {
  1000. ensureAttached();
  1001. int pseudoAngle = constrain((int)(value * 180), 0, 180);
  1002. this->write(pseudoAngle);
  1003. }
  1004.  
  1005. // Performs Servo::attach with the parameters set previously
  1006. void ensureAttached() {
  1007. if (this->attached())
  1008. return;
  1009.  
  1010. this->attach(port, pulseMin, pulseMax);
  1011. }
  1012.  
  1013. Number read01() {
  1014. int us = this->readMicroseconds();
  1015. return (Number)(us - pulseMin) / (Number)(pulseMax - pulseMin);
  1016. }
  1017.  
  1018. void reattach(uint8_t port, int pulseMin, int pulseMax) {
  1019. this->port = port;
  1020. this->pulseMin = pulseMin;
  1021. this->pulseMax = pulseMax;
  1022. if (this->attached())
  1023. this->attach(port, pulseMin, pulseMax);
  1024. }
  1025. };
  1026.  
  1027. using State = XServo;
  1028. using Type = XServo*;
  1029.  
  1030. union NodeErrors {
  1031. struct {
  1032. bool output_DEV : 1;
  1033. };
  1034.  
  1035. ErrorFlags flags;
  1036. };
  1037.  
  1038. struct Node {
  1039. NodeErrors errors;
  1040. xod_dev__servo__servo_device::Type output_DEV;
  1041. State state;
  1042. };
  1043.  
  1044. struct input_PORT { };
  1045. struct input_Pmin { };
  1046. struct input_Pmax { };
  1047. struct output_DEV { };
  1048.  
  1049. template<typename PinT> struct ValueType { using T = void; };
  1050. template<> struct ValueType<input_PORT> { using T = uint8_t; };
  1051. template<> struct ValueType<input_Pmin> { using T = Number; };
  1052. template<> struct ValueType<input_Pmax> { using T = Number; };
  1053. template<> struct ValueType<output_DEV> { using T = xod_dev__servo__servo_device::Type; };
  1054.  
  1055. struct ContextObject {
  1056. Node* _node;
  1057.  
  1058. uint8_t _input_PORT;
  1059. Number _input_Pmin;
  1060. Number _input_Pmax;
  1061.  
  1062. bool _isOutputDirty_DEV : 1;
  1063. };
  1064.  
  1065. using Context = ContextObject*;
  1066.  
  1067. template<typename PinT> typename ValueType<PinT>::T getValue(Context ctx) {
  1068. static_assert(always_false<PinT>::value,
  1069. "Invalid pin descriptor. Expected one of:" \
  1070. " input_PORT input_Pmin input_Pmax" \
  1071. " output_DEV");
  1072. }
  1073.  
  1074. template<> uint8_t getValue<input_PORT>(Context ctx) {
  1075. return ctx->_input_PORT;
  1076. }
  1077. template<> Number getValue<input_Pmin>(Context ctx) {
  1078. return ctx->_input_Pmin;
  1079. }
  1080. template<> Number getValue<input_Pmax>(Context ctx) {
  1081. return ctx->_input_Pmax;
  1082. }
  1083. template<> xod_dev__servo__servo_device::Type getValue<output_DEV>(Context ctx) {
  1084. return ctx->_node->output_DEV;
  1085. }
  1086.  
  1087. template<typename InputT> bool isInputDirty(Context ctx) {
  1088. static_assert(always_false<InputT>::value,
  1089. "Invalid input descriptor. Expected one of:" \
  1090. "");
  1091. return false;
  1092. }
  1093.  
  1094. template<typename OutputT> void emitValue(Context ctx, typename ValueType<OutputT>::T val) {
  1095. static_assert(always_false<OutputT>::value,
  1096. "Invalid output descriptor. Expected one of:" \
  1097. " output_DEV");
  1098. }
  1099.  
  1100. template<> void emitValue<output_DEV>(Context ctx, xod_dev__servo__servo_device::Type val) {
  1101. ctx->_node->output_DEV = val;
  1102. ctx->_isOutputDirty_DEV = true;
  1103. ctx->_node->errors.output_DEV = false;
  1104. }
  1105.  
  1106. State* getState(Context ctx) {
  1107. return &ctx->_node->state;
  1108. }
  1109.  
  1110. template<typename OutputT> void raiseError(Context ctx) {
  1111. static_assert(always_false<OutputT>::value,
  1112. "Invalid output descriptor. Expected one of:" \
  1113. " output_DEV");
  1114. }
  1115.  
  1116. template<> void raiseError<output_DEV>(Context ctx) {
  1117. ctx->_node->errors.output_DEV = true;
  1118. ctx->_isOutputDirty_DEV = true;
  1119. }
  1120.  
  1121. void raiseError(Context ctx) {
  1122. ctx->_node->errors.output_DEV = true;
  1123. ctx->_isOutputDirty_DEV = true;
  1124. }
  1125.  
  1126. void evaluate(Context ctx) {
  1127. State* servo = getState(ctx);
  1128.  
  1129. auto port = getValue<input_PORT>(ctx);
  1130. if (!isValidDigitalPort(port)) {
  1131. raiseError(ctx);
  1132. return;
  1133. }
  1134.  
  1135. servo->reattach(
  1136. port,
  1137. getValue<input_Pmin>(ctx),
  1138. getValue<input_Pmax>(ctx)
  1139. );
  1140.  
  1141. emitValue<output_DEV>(ctx, servo);
  1142. }
  1143.  
  1144. } // namespace xod_dev__servo__servo_device
  1145.  
  1146. //-----------------------------------------------------------------------------
  1147. // xod/core/continuously implementation
  1148. //-----------------------------------------------------------------------------
  1149. namespace xod__core__continuously {
  1150.  
  1151. struct State {
  1152. };
  1153.  
  1154. struct Node {
  1155. TimeMs timeoutAt;
  1156. State state;
  1157. };
  1158.  
  1159. struct output_TICK { };
  1160.  
  1161. template<typename PinT> struct ValueType { using T = void; };
  1162. template<> struct ValueType<output_TICK> { using T = Pulse; };
  1163.  
  1164. struct ContextObject {
  1165. Node* _node;
  1166.  
  1167. bool _isOutputDirty_TICK : 1;
  1168. };
  1169.  
  1170. using Context = ContextObject*;
  1171.  
  1172. template<typename PinT> typename ValueType<PinT>::T getValue(Context ctx) {
  1173. static_assert(always_false<PinT>::value,
  1174. "Invalid pin descriptor. Expected one of:" \
  1175. "" \
  1176. " output_TICK");
  1177. }
  1178.  
  1179. template<> Pulse getValue<output_TICK>(Context ctx) {
  1180. return Pulse();
  1181. }
  1182.  
  1183. template<typename InputT> bool isInputDirty(Context ctx) {
  1184. static_assert(always_false<InputT>::value,
  1185. "Invalid input descriptor. Expected one of:" \
  1186. "");
  1187. return false;
  1188. }
  1189.  
  1190. template<typename OutputT> void emitValue(Context ctx, typename ValueType<OutputT>::T val) {
  1191. static_assert(always_false<OutputT>::value,
  1192. "Invalid output descriptor. Expected one of:" \
  1193. " output_TICK");
  1194. }
  1195.  
  1196. template<> void emitValue<output_TICK>(Context ctx, Pulse val) {
  1197. ctx->_isOutputDirty_TICK = true;
  1198. }
  1199.  
  1200. State* getState(Context ctx) {
  1201. return &ctx->_node->state;
  1202. }
  1203.  
  1204. void evaluate(Context ctx) {
  1205. emitValue<output_TICK>(ctx, 1);
  1206. setTimeout(ctx, 0);
  1207. }
  1208.  
  1209. } // namespace xod__core__continuously
  1210.  
  1211. //-----------------------------------------------------------------------------
  1212. // xod/core/boot implementation
  1213. //-----------------------------------------------------------------------------
  1214. namespace xod__core__boot {
  1215.  
  1216. struct State {
  1217. };
  1218.  
  1219. struct Node {
  1220. State state;
  1221. };
  1222.  
  1223. struct output_BOOT { };
  1224.  
  1225. template<typename PinT> struct ValueType { using T = void; };
  1226. template<> struct ValueType<output_BOOT> { using T = Pulse; };
  1227.  
  1228. struct ContextObject {
  1229. Node* _node;
  1230.  
  1231. bool _isOutputDirty_BOOT : 1;
  1232. };
  1233.  
  1234. using Context = ContextObject*;
  1235.  
  1236. template<typename PinT> typename ValueType<PinT>::T getValue(Context ctx) {
  1237. static_assert(always_false<PinT>::value,
  1238. "Invalid pin descriptor. Expected one of:" \
  1239. "" \
  1240. " output_BOOT");
  1241. }
  1242.  
  1243. template<> Pulse getValue<output_BOOT>(Context ctx) {
  1244. return Pulse();
  1245. }
  1246.  
  1247. template<typename InputT> bool isInputDirty(Context ctx) {
  1248. static_assert(always_false<InputT>::value,
  1249. "Invalid input descriptor. Expected one of:" \
  1250. "");
  1251. return false;
  1252. }
  1253.  
  1254. template<typename OutputT> void emitValue(Context ctx, typename ValueType<OutputT>::T val) {
  1255. static_assert(always_false<OutputT>::value,
  1256. "Invalid output descriptor. Expected one of:" \
  1257. " output_BOOT");
  1258. }
  1259.  
  1260. template<> void emitValue<output_BOOT>(Context ctx, Pulse val) {
  1261. ctx->_isOutputDirty_BOOT = true;
  1262. }
  1263.  
  1264. State* getState(Context ctx) {
  1265. return &ctx->_node->state;
  1266. }
  1267.  
  1268. void evaluate(Context ctx) {
  1269. emitValue<output_BOOT>(ctx, 1);
  1270. }
  1271.  
  1272. } // namespace xod__core__boot
  1273.  
  1274. //-----------------------------------------------------------------------------
  1275. // xod/core/pulse-on-change(number) implementation
  1276. //-----------------------------------------------------------------------------
  1277. namespace xod__core__pulse_on_change__number {
  1278.  
  1279. struct State {
  1280. Number sample = NAN;
  1281. };
  1282.  
  1283. struct Node {
  1284. State state;
  1285. };
  1286.  
  1287. struct input_IN { };
  1288. struct output_OUT { };
  1289.  
  1290. template<typename PinT> struct ValueType { using T = void; };
  1291. template<> struct ValueType<input_IN> { using T = Number; };
  1292. template<> struct ValueType<output_OUT> { using T = Pulse; };
  1293.  
  1294. struct ContextObject {
  1295. Node* _node;
  1296.  
  1297. Number _input_IN;
  1298.  
  1299. bool _isOutputDirty_OUT : 1;
  1300. };
  1301.  
  1302. using Context = ContextObject*;
  1303.  
  1304. template<typename PinT> typename ValueType<PinT>::T getValue(Context ctx) {
  1305. static_assert(always_false<PinT>::value,
  1306. "Invalid pin descriptor. Expected one of:" \
  1307. " input_IN" \
  1308. " output_OUT");
  1309. }
  1310.  
  1311. template<> Number getValue<input_IN>(Context ctx) {
  1312. return ctx->_input_IN;
  1313. }
  1314. template<> Pulse getValue<output_OUT>(Context ctx) {
  1315. return Pulse();
  1316. }
  1317.  
  1318. template<typename InputT> bool isInputDirty(Context ctx) {
  1319. static_assert(always_false<InputT>::value,
  1320. "Invalid input descriptor. Expected one of:" \
  1321. "");
  1322. return false;
  1323. }
  1324.  
  1325. template<typename OutputT> void emitValue(Context ctx, typename ValueType<OutputT>::T val) {
  1326. static_assert(always_false<OutputT>::value,
  1327. "Invalid output descriptor. Expected one of:" \
  1328. " output_OUT");
  1329. }
  1330.  
  1331. template<> void emitValue<output_OUT>(Context ctx, Pulse val) {
  1332. ctx->_isOutputDirty_OUT = true;
  1333. }
  1334.  
  1335. State* getState(Context ctx) {
  1336. return &ctx->_node->state;
  1337. }
  1338.  
  1339. void evaluate(Context ctx) {
  1340. State* state = getState(ctx);
  1341. auto newValue = getValue<input_IN>(ctx);
  1342.  
  1343. if (!isSettingUp() && newValue != state->sample)
  1344. emitValue<output_OUT>(ctx, 1);
  1345.  
  1346. state->sample = newValue;
  1347. }
  1348.  
  1349. } // namespace xod__core__pulse_on_change__number
  1350.  
  1351. //-----------------------------------------------------------------------------
  1352. // xod/core/cast-to-pulse(boolean) implementation
  1353. //-----------------------------------------------------------------------------
  1354. namespace xod__core__cast_to_pulse__boolean {
  1355.  
  1356. struct State {
  1357. bool state = false;
  1358. };
  1359.  
  1360. struct Node {
  1361. State state;
  1362. };
  1363.  
  1364. struct input_IN { };
  1365. struct output_OUT { };
  1366.  
  1367. template<typename PinT> struct ValueType { using T = void; };
  1368. template<> struct ValueType<input_IN> { using T = Logic; };
  1369. template<> struct ValueType<output_OUT> { using T = Pulse; };
  1370.  
  1371. struct ContextObject {
  1372. Node* _node;
  1373.  
  1374. Logic _input_IN;
  1375.  
  1376. bool _isOutputDirty_OUT : 1;
  1377. };
  1378.  
  1379. using Context = ContextObject*;
  1380.  
  1381. template<typename PinT> typename ValueType<PinT>::T getValue(Context ctx) {
  1382. static_assert(always_false<PinT>::value,
  1383. "Invalid pin descriptor. Expected one of:" \
  1384. " input_IN" \
  1385. " output_OUT");
  1386. }
  1387.  
  1388. template<> Logic getValue<input_IN>(Context ctx) {
  1389. return ctx->_input_IN;
  1390. }
  1391. template<> Pulse getValue<output_OUT>(Context ctx) {
  1392. return Pulse();
  1393. }
  1394.  
  1395. template<typename InputT> bool isInputDirty(Context ctx) {
  1396. static_assert(always_false<InputT>::value,
  1397. "Invalid input descriptor. Expected one of:" \
  1398. "");
  1399. return false;
  1400. }
  1401.  
  1402. template<typename OutputT> void emitValue(Context ctx, typename ValueType<OutputT>::T val) {
  1403. static_assert(always_false<OutputT>::value,
  1404. "Invalid output descriptor. Expected one of:" \
  1405. " output_OUT");
  1406. }
  1407.  
  1408. template<> void emitValue<output_OUT>(Context ctx, Pulse val) {
  1409. ctx->_isOutputDirty_OUT = true;
  1410. }
  1411.  
  1412. State* getState(Context ctx) {
  1413. return &ctx->_node->state;
  1414. }
  1415.  
  1416. void evaluate(Context ctx) {
  1417. State* state = getState(ctx);
  1418. auto newValue = getValue<input_IN>(ctx);
  1419.  
  1420. if (newValue == true && state->state == false)
  1421. emitValue<output_OUT>(ctx, 1);
  1422.  
  1423. state->state = newValue;
  1424. }
  1425.  
  1426. } // namespace xod__core__cast_to_pulse__boolean
  1427.  
  1428. //-----------------------------------------------------------------------------
  1429. // xod/gpio/digital-read-pullup implementation
  1430. //-----------------------------------------------------------------------------
  1431. namespace xod__gpio__digital_read_pullup {
  1432.  
  1433. //#pragma XOD evaluate_on_pin disable
  1434. //#pragma XOD evaluate_on_pin enable input_UPD
  1435. //#pragma XOD error_raise enable
  1436.  
  1437. struct State {
  1438. };
  1439.  
  1440. union NodeErrors {
  1441. struct {
  1442. bool output_SIG : 1;
  1443. bool output_DONE : 1;
  1444. };
  1445.  
  1446. ErrorFlags flags;
  1447. };
  1448.  
  1449. struct Node {
  1450. NodeErrors errors;
  1451. Logic output_SIG;
  1452. State state;
  1453. };
  1454.  
  1455. struct input_PORT { };
  1456. struct input_UPD { };
  1457. struct output_SIG { };
  1458. struct output_DONE { };
  1459.  
  1460. template<typename PinT> struct ValueType { using T = void; };
  1461. template<> struct ValueType<input_PORT> { using T = uint8_t; };
  1462. template<> struct ValueType<input_UPD> { using T = Pulse; };
  1463. template<> struct ValueType<output_SIG> { using T = Logic; };
  1464. template<> struct ValueType<output_DONE> { using T = Pulse; };
  1465.  
  1466. struct ContextObject {
  1467. Node* _node;
  1468.  
  1469. uint8_t _input_PORT;
  1470.  
  1471. bool _isInputDirty_UPD;
  1472.  
  1473. bool _isOutputDirty_SIG : 1;
  1474. bool _isOutputDirty_DONE : 1;
  1475. };
  1476.  
  1477. using Context = ContextObject*;
  1478.  
  1479. template<typename PinT> typename ValueType<PinT>::T getValue(Context ctx) {
  1480. static_assert(always_false<PinT>::value,
  1481. "Invalid pin descriptor. Expected one of:" \
  1482. " input_PORT input_UPD" \
  1483. " output_SIG output_DONE");
  1484. }
  1485.  
  1486. template<> uint8_t getValue<input_PORT>(Context ctx) {
  1487. return ctx->_input_PORT;
  1488. }
  1489. template<> Pulse getValue<input_UPD>(Context ctx) {
  1490. return Pulse();
  1491. }
  1492. template<> Logic getValue<output_SIG>(Context ctx) {
  1493. return ctx->_node->output_SIG;
  1494. }
  1495. template<> Pulse getValue<output_DONE>(Context ctx) {
  1496. return Pulse();
  1497. }
  1498.  
  1499. template<typename InputT> bool isInputDirty(Context ctx) {
  1500. static_assert(always_false<InputT>::value,
  1501. "Invalid input descriptor. Expected one of:" \
  1502. " input_UPD");
  1503. return false;
  1504. }
  1505.  
  1506. template<> bool isInputDirty<input_UPD>(Context ctx) {
  1507. return ctx->_isInputDirty_UPD;
  1508. }
  1509.  
  1510. template<typename OutputT> void emitValue(Context ctx, typename ValueType<OutputT>::T val) {
  1511. static_assert(always_false<OutputT>::value,
  1512. "Invalid output descriptor. Expected one of:" \
  1513. " output_SIG output_DONE");
  1514. }
  1515.  
  1516. template<> void emitValue<output_SIG>(Context ctx, Logic val) {
  1517. ctx->_node->output_SIG = val;
  1518. ctx->_isOutputDirty_SIG = true;
  1519. ctx->_node->errors.output_SIG = false;
  1520. }
  1521. template<> void emitValue<output_DONE>(Context ctx, Pulse val) {
  1522. ctx->_isOutputDirty_DONE = true;
  1523. ctx->_node->errors.output_DONE = false;
  1524. }
  1525.  
  1526. State* getState(Context ctx) {
  1527. return &ctx->_node->state;
  1528. }
  1529.  
  1530. template<typename OutputT> void raiseError(Context ctx) {
  1531. static_assert(always_false<OutputT>::value,
  1532. "Invalid output descriptor. Expected one of:" \
  1533. " output_SIG output_DONE");
  1534. }
  1535.  
  1536. template<> void raiseError<output_SIG>(Context ctx) {
  1537. ctx->_node->errors.output_SIG = true;
  1538. ctx->_isOutputDirty_SIG = true;
  1539. }
  1540. template<> void raiseError<output_DONE>(Context ctx) {
  1541. ctx->_node->errors.output_DONE = true;
  1542. ctx->_isOutputDirty_DONE = true;
  1543. }
  1544.  
  1545. void raiseError(Context ctx) {
  1546. ctx->_node->errors.output_SIG = true;
  1547. ctx->_isOutputDirty_SIG = true;
  1548. ctx->_node->errors.output_DONE = true;
  1549. ctx->_isOutputDirty_DONE = true;
  1550. }
  1551.  
  1552. void evaluate(Context ctx) {
  1553. if (!isInputDirty<input_UPD>(ctx))
  1554. return;
  1555.  
  1556. const uint8_t port = getValue<input_PORT>(ctx);
  1557. if (!isValidDigitalPort(port)) {
  1558. raiseError(ctx);
  1559. return;
  1560. }
  1561.  
  1562. ::pinMode(port, INPUT_PULLUP);
  1563. emitValue<output_SIG>(ctx, ::digitalRead(port));
  1564. emitValue<output_DONE>(ctx, 1);
  1565. }
  1566.  
  1567. } // namespace xod__gpio__digital_read_pullup
  1568.  
  1569. //-----------------------------------------------------------------------------
  1570. // xod/gpio/analog-read implementation
  1571. //-----------------------------------------------------------------------------
  1572. namespace xod__gpio__analog_read {
  1573.  
  1574. //#pragma XOD evaluate_on_pin disable
  1575. //#pragma XOD evaluate_on_pin enable input_UPD
  1576. //#pragma XOD error_raise enable
  1577.  
  1578. struct State {
  1579. };
  1580.  
  1581. union NodeErrors {
  1582. struct {
  1583. bool output_VAL : 1;
  1584. bool output_DONE : 1;
  1585. };
  1586.  
  1587. ErrorFlags flags;
  1588. };
  1589.  
  1590. struct Node {
  1591. NodeErrors errors;
  1592. Number output_VAL;
  1593. State state;
  1594. };
  1595.  
  1596. struct input_PORT { };
  1597. struct input_UPD { };
  1598. struct output_VAL { };
  1599. struct output_DONE { };
  1600.  
  1601. template<typename PinT> struct ValueType { using T = void; };
  1602. template<> struct ValueType<input_PORT> { using T = uint8_t; };
  1603. template<> struct ValueType<input_UPD> { using T = Pulse; };
  1604. template<> struct ValueType<output_VAL> { using T = Number; };
  1605. template<> struct ValueType<output_DONE> { using T = Pulse; };
  1606.  
  1607. struct ContextObject {
  1608. Node* _node;
  1609.  
  1610. uint8_t _input_PORT;
  1611.  
  1612. bool _isInputDirty_UPD;
  1613.  
  1614. bool _isOutputDirty_VAL : 1;
  1615. bool _isOutputDirty_DONE : 1;
  1616. };
  1617.  
  1618. using Context = ContextObject*;
  1619.  
  1620. template<typename PinT> typename ValueType<PinT>::T getValue(Context ctx) {
  1621. static_assert(always_false<PinT>::value,
  1622. "Invalid pin descriptor. Expected one of:" \
  1623. " input_PORT input_UPD" \
  1624. " output_VAL output_DONE");
  1625. }
  1626.  
  1627. template<> uint8_t getValue<input_PORT>(Context ctx) {
  1628. return ctx->_input_PORT;
  1629. }
  1630. template<> Pulse getValue<input_UPD>(Context ctx) {
  1631. return Pulse();
  1632. }
  1633. template<> Number getValue<output_VAL>(Context ctx) {
  1634. return ctx->_node->output_VAL;
  1635. }
  1636. template<> Pulse getValue<output_DONE>(Context ctx) {
  1637. return Pulse();
  1638. }
  1639.  
  1640. template<typename InputT> bool isInputDirty(Context ctx) {
  1641. static_assert(always_false<InputT>::value,
  1642. "Invalid input descriptor. Expected one of:" \
  1643. " input_UPD");
  1644. return false;
  1645. }
  1646.  
  1647. template<> bool isInputDirty<input_UPD>(Context ctx) {
  1648. return ctx->_isInputDirty_UPD;
  1649. }
  1650.  
  1651. template<typename OutputT> void emitValue(Context ctx, typename ValueType<OutputT>::T val) {
  1652. static_assert(always_false<OutputT>::value,
  1653. "Invalid output descriptor. Expected one of:" \
  1654. " output_VAL output_DONE");
  1655. }
  1656.  
  1657. template<> void emitValue<output_VAL>(Context ctx, Number val) {
  1658. ctx->_node->output_VAL = val;
  1659. ctx->_isOutputDirty_VAL = true;
  1660. ctx->_node->errors.output_VAL = false;
  1661. }
  1662. template<> void emitValue<output_DONE>(Context ctx, Pulse val) {
  1663. ctx->_isOutputDirty_DONE = true;
  1664. ctx->_node->errors.output_DONE = false;
  1665. }
  1666.  
  1667. State* getState(Context ctx) {
  1668. return &ctx->_node->state;
  1669. }
  1670.  
  1671. template<typename OutputT> void raiseError(Context ctx) {
  1672. static_assert(always_false<OutputT>::value,
  1673. "Invalid output descriptor. Expected one of:" \
  1674. " output_VAL output_DONE");
  1675. }
  1676.  
  1677. template<> void raiseError<output_VAL>(Context ctx) {
  1678. ctx->_node->errors.output_VAL = true;
  1679. ctx->_isOutputDirty_VAL = true;
  1680. }
  1681. template<> void raiseError<output_DONE>(Context ctx) {
  1682. ctx->_node->errors.output_DONE = true;
  1683. ctx->_isOutputDirty_DONE = true;
  1684. }
  1685.  
  1686. void raiseError(Context ctx) {
  1687. ctx->_node->errors.output_VAL = true;
  1688. ctx->_isOutputDirty_VAL = true;
  1689. ctx->_node->errors.output_DONE = true;
  1690. ctx->_isOutputDirty_DONE = true;
  1691. }
  1692.  
  1693. void evaluate(Context ctx) {
  1694. if (!isInputDirty<input_UPD>(ctx))
  1695. return;
  1696.  
  1697. const uint8_t port = getValue<input_PORT>(ctx);
  1698.  
  1699. if (!isValidAnalogPort(port)) {
  1700. raiseError(ctx);
  1701. return;
  1702. }
  1703.  
  1704. ::pinMode(port, INPUT);
  1705. emitValue<output_VAL>(ctx, ::analogRead(port) / 1023.);
  1706. emitValue<output_DONE>(ctx, 1);
  1707. }
  1708.  
  1709. } // namespace xod__gpio__analog_read
  1710.  
  1711. //-----------------------------------------------------------------------------
  1712. // xod/core/any implementation
  1713. //-----------------------------------------------------------------------------
  1714. namespace xod__core__any {
  1715.  
  1716. struct State {
  1717. };
  1718.  
  1719. struct Node {
  1720. State state;
  1721. };
  1722.  
  1723. struct input_IN1 { };
  1724. struct input_IN2 { };
  1725. struct output_OUT { };
  1726.  
  1727. template<typename PinT> struct ValueType { using T = void; };
  1728. template<> struct ValueType<input_IN1> { using T = Pulse; };
  1729. template<> struct ValueType<input_IN2> { using T = Pulse; };
  1730. template<> struct ValueType<output_OUT> { using T = Pulse; };
  1731.  
  1732. struct ContextObject {
  1733. Node* _node;
  1734.  
  1735. bool _isInputDirty_IN1;
  1736. bool _isInputDirty_IN2;
  1737.  
  1738. bool _isOutputDirty_OUT : 1;
  1739. };
  1740.  
  1741. using Context = ContextObject*;
  1742.  
  1743. template<typename PinT> typename ValueType<PinT>::T getValue(Context ctx) {
  1744. static_assert(always_false<PinT>::value,
  1745. "Invalid pin descriptor. Expected one of:" \
  1746. " input_IN1 input_IN2" \
  1747. " output_OUT");
  1748. }
  1749.  
  1750. template<> Pulse getValue<input_IN1>(Context ctx) {
  1751. return Pulse();
  1752. }
  1753. template<> Pulse getValue<input_IN2>(Context ctx) {
  1754. return Pulse();
  1755. }
  1756. template<> Pulse getValue<output_OUT>(Context ctx) {
  1757. return Pulse();
  1758. }
  1759.  
  1760. template<typename InputT> bool isInputDirty(Context ctx) {
  1761. static_assert(always_false<InputT>::value,
  1762. "Invalid input descriptor. Expected one of:" \
  1763. " input_IN1 input_IN2");
  1764. return false;
  1765. }
  1766.  
  1767. template<> bool isInputDirty<input_IN1>(Context ctx) {
  1768. return ctx->_isInputDirty_IN1;
  1769. }
  1770. template<> bool isInputDirty<input_IN2>(Context ctx) {
  1771. return ctx->_isInputDirty_IN2;
  1772. }
  1773.  
  1774. template<typename OutputT> void emitValue(Context ctx, typename ValueType<OutputT>::T val) {
  1775. static_assert(always_false<OutputT>::value,
  1776. "Invalid output descriptor. Expected one of:" \
  1777. " output_OUT");
  1778. }
  1779.  
  1780. template<> void emitValue<output_OUT>(Context ctx, Pulse val) {
  1781. ctx->_isOutputDirty_OUT = true;
  1782. }
  1783.  
  1784. State* getState(Context ctx) {
  1785. return &ctx->_node->state;
  1786. }
  1787.  
  1788. void evaluate(Context ctx) {
  1789. bool p1 = isInputDirty<input_IN1>(ctx);
  1790. bool p2 = isInputDirty<input_IN2>(ctx);
  1791. if (p1 || p2)
  1792. emitValue<output_OUT>(ctx, true);
  1793. }
  1794.  
  1795. } // namespace xod__core__any
  1796.  
  1797. //-----------------------------------------------------------------------------
  1798. // xod/core/not implementation
  1799. //-----------------------------------------------------------------------------
  1800. namespace xod__core__not {
  1801.  
  1802. //#pragma XOD dirtieness disable
  1803.  
  1804. struct State {
  1805. };
  1806.  
  1807. struct Node {
  1808. Logic output_OUT;
  1809. State state;
  1810. };
  1811.  
  1812. struct input_IN { };
  1813. struct output_OUT { };
  1814.  
  1815. template<typename PinT> struct ValueType { using T = void; };
  1816. template<> struct ValueType<input_IN> { using T = Logic; };
  1817. template<> struct ValueType<output_OUT> { using T = Logic; };
  1818.  
  1819. struct ContextObject {
  1820. Node* _node;
  1821.  
  1822. Logic _input_IN;
  1823.  
  1824. };
  1825.  
  1826. using Context = ContextObject*;
  1827.  
  1828. template<typename PinT> typename ValueType<PinT>::T getValue(Context ctx) {
  1829. static_assert(always_false<PinT>::value,
  1830. "Invalid pin descriptor. Expected one of:" \
  1831. " input_IN" \
  1832. " output_OUT");
  1833. }
  1834.  
  1835. template<> Logic getValue<input_IN>(Context ctx) {
  1836. return ctx->_input_IN;
  1837. }
  1838. template<> Logic getValue<output_OUT>(Context ctx) {
  1839. return ctx->_node->output_OUT;
  1840. }
  1841.  
  1842. template<typename InputT> bool isInputDirty(Context ctx) {
  1843. static_assert(always_false<InputT>::value,
  1844. "Invalid input descriptor. Expected one of:" \
  1845. "");
  1846. return false;
  1847. }
  1848.  
  1849. template<typename OutputT> void emitValue(Context ctx, typename ValueType<OutputT>::T val) {
  1850. static_assert(always_false<OutputT>::value,
  1851. "Invalid output descriptor. Expected one of:" \
  1852. " output_OUT");
  1853. }
  1854.  
  1855. template<> void emitValue<output_OUT>(Context ctx, Logic val) {
  1856. ctx->_node->output_OUT = val;
  1857. }
  1858.  
  1859. State* getState(Context ctx) {
  1860. return &ctx->_node->state;
  1861. }
  1862.  
  1863. void evaluate(Context ctx) {
  1864. auto x = getValue<input_IN>(ctx);
  1865. emitValue<output_OUT>(ctx, !x);
  1866. }
  1867.  
  1868. } // namespace xod__core__not
  1869.  
  1870. //-----------------------------------------------------------------------------
  1871. // xod/math/map implementation
  1872. //-----------------------------------------------------------------------------
  1873. namespace xod__math__map {
  1874.  
  1875. //#pragma XOD dirtieness disable
  1876.  
  1877. struct State {
  1878. };
  1879.  
  1880. struct Node {
  1881. Number output_OUT;
  1882. State state;
  1883. };
  1884.  
  1885. struct input_X { };
  1886. struct input_Smin { };
  1887. struct input_Smax { };
  1888. struct input_Tmin { };
  1889. struct input_Tmax { };
  1890. struct output_OUT { };
  1891.  
  1892. template<typename PinT> struct ValueType { using T = void; };
  1893. template<> struct ValueType<input_X> { using T = Number; };
  1894. template<> struct ValueType<input_Smin> { using T = Number; };
  1895. template<> struct ValueType<input_Smax> { using T = Number; };
  1896. template<> struct ValueType<input_Tmin> { using T = Number; };
  1897. template<> struct ValueType<input_Tmax> { using T = Number; };
  1898. template<> struct ValueType<output_OUT> { using T = Number; };
  1899.  
  1900. struct ContextObject {
  1901. Node* _node;
  1902.  
  1903. Number _input_X;
  1904. Number _input_Smin;
  1905. Number _input_Smax;
  1906. Number _input_Tmin;
  1907. Number _input_Tmax;
  1908.  
  1909. };
  1910.  
  1911. using Context = ContextObject*;
  1912.  
  1913. template<typename PinT> typename ValueType<PinT>::T getValue(Context ctx) {
  1914. static_assert(always_false<PinT>::value,
  1915. "Invalid pin descriptor. Expected one of:" \
  1916. " input_X input_Smin input_Smax input_Tmin input_Tmax" \
  1917. " output_OUT");
  1918. }
  1919.  
  1920. template<> Number getValue<input_X>(Context ctx) {
  1921. return ctx->_input_X;
  1922. }
  1923. template<> Number getValue<input_Smin>(Context ctx) {
  1924. return ctx->_input_Smin;
  1925. }
  1926. template<> Number getValue<input_Smax>(Context ctx) {
  1927. return ctx->_input_Smax;
  1928. }
  1929. template<> Number getValue<input_Tmin>(Context ctx) {
  1930. return ctx->_input_Tmin;
  1931. }
  1932. template<> Number getValue<input_Tmax>(Context ctx) {
  1933. return ctx->_input_Tmax;
  1934. }
  1935. template<> Number getValue<output_OUT>(Context ctx) {
  1936. return ctx->_node->output_OUT;
  1937. }
  1938.  
  1939. template<typename InputT> bool isInputDirty(Context ctx) {
  1940. static_assert(always_false<InputT>::value,
  1941. "Invalid input descriptor. Expected one of:" \
  1942. "");
  1943. return false;
  1944. }
  1945.  
  1946. template<typename OutputT> void emitValue(Context ctx, typename ValueType<OutputT>::T val) {
  1947. static_assert(always_false<OutputT>::value,
  1948. "Invalid output descriptor. Expected one of:" \
  1949. " output_OUT");
  1950. }
  1951.  
  1952. template<> void emitValue<output_OUT>(Context ctx, Number val) {
  1953. ctx->_node->output_OUT = val;
  1954. }
  1955.  
  1956. State* getState(Context ctx) {
  1957. return &ctx->_node->state;
  1958. }
  1959.  
  1960. void evaluate(Context ctx) {
  1961. auto x = getValue<input_X>(ctx);
  1962. auto sMin = getValue<input_Smin>(ctx);
  1963. auto sMax = getValue<input_Smax>(ctx);
  1964. auto tMin = getValue<input_Tmin>(ctx);
  1965. auto tMax = getValue<input_Tmax>(ctx);
  1966. auto k = (x - sMin) / (sMax - sMin);
  1967. auto xm = isnan(x) ? x : tMin + k * (tMax - tMin);
  1968. emitValue<output_OUT>(ctx, xm);
  1969. }
  1970.  
  1971. } // namespace xod__math__map
  1972.  
  1973. //-----------------------------------------------------------------------------
  1974. // xod/core/debounce(boolean) implementation
  1975. //-----------------------------------------------------------------------------
  1976. namespace xod__core__debounce__boolean {
  1977.  
  1978. struct State {
  1979. bool state = false;
  1980. };
  1981.  
  1982. struct Node {
  1983. TimeMs timeoutAt;
  1984. Logic output_OUT;
  1985. State state;
  1986. };
  1987.  
  1988. struct input_ST { };
  1989. struct input_Ts { };
  1990. struct output_OUT { };
  1991.  
  1992. template<typename PinT> struct ValueType { using T = void; };
  1993. template<> struct ValueType<input_ST> { using T = Logic; };
  1994. template<> struct ValueType<input_Ts> { using T = Number; };
  1995. template<> struct ValueType<output_OUT> { using T = Logic; };
  1996.  
  1997. struct ContextObject {
  1998. Node* _node;
  1999.  
  2000. Logic _input_ST;
  2001. Number _input_Ts;
  2002.  
  2003. bool _isOutputDirty_OUT : 1;
  2004. };
  2005.  
  2006. using Context = ContextObject*;
  2007.  
  2008. template<typename PinT> typename ValueType<PinT>::T getValue(Context ctx) {
  2009. static_assert(always_false<PinT>::value,
  2010. "Invalid pin descriptor. Expected one of:" \
  2011. " input_ST input_Ts" \
  2012. " output_OUT");
  2013. }
  2014.  
  2015. template<> Logic getValue<input_ST>(Context ctx) {
  2016. return ctx->_input_ST;
  2017. }
  2018. template<> Number getValue<input_Ts>(Context ctx) {
  2019. return ctx->_input_Ts;
  2020. }
  2021. template<> Logic getValue<output_OUT>(Context ctx) {
  2022. return ctx->_node->output_OUT;
  2023. }
  2024.  
  2025. template<typename InputT> bool isInputDirty(Context ctx) {
  2026. static_assert(always_false<InputT>::value,
  2027. "Invalid input descriptor. Expected one of:" \
  2028. "");
  2029. return false;
  2030. }
  2031.  
  2032. template<typename OutputT> void emitValue(Context ctx, typename ValueType<OutputT>::T val) {
  2033. static_assert(always_false<OutputT>::value,
  2034. "Invalid output descriptor. Expected one of:" \
  2035. " output_OUT");
  2036. }
  2037.  
  2038. template<> void emitValue<output_OUT>(Context ctx, Logic val) {
  2039. ctx->_node->output_OUT = val;
  2040. ctx->_isOutputDirty_OUT = true;
  2041. }
  2042.  
  2043. State* getState(Context ctx) {
  2044. return &ctx->_node->state;
  2045. }
  2046.  
  2047. void evaluate(Context ctx) {
  2048. State* state = getState(ctx);
  2049. bool x = getValue<input_ST>(ctx);
  2050.  
  2051. if (x != state->state) {
  2052. state->state = x;
  2053. TimeMs dt = getValue<input_Ts>(ctx) * 1000;
  2054. setTimeout(ctx, dt);
  2055. }
  2056.  
  2057. if (isTimedOut(ctx)) {
  2058. emitValue<output_OUT>(ctx, x);
  2059. }
  2060. }
  2061.  
  2062. } // namespace xod__core__debounce__boolean
  2063.  
  2064. //-----------------------------------------------------------------------------
  2065. // xod/core/greater implementation
  2066. //-----------------------------------------------------------------------------
  2067. namespace xod__core__greater {
  2068.  
  2069. //#pragma XOD dirtieness disable
  2070.  
  2071. struct State {
  2072. };
  2073.  
  2074. struct Node {
  2075. Logic output_OUT;
  2076. State state;
  2077. };
  2078.  
  2079. struct input_IN1 { };
  2080. struct input_IN2 { };
  2081. struct output_OUT { };
  2082.  
  2083. template<typename PinT> struct ValueType { using T = void; };
  2084. template<> struct ValueType<input_IN1> { using T = Number; };
  2085. template<> struct ValueType<input_IN2> { using T = Number; };
  2086. template<> struct ValueType<output_OUT> { using T = Logic; };
  2087.  
  2088. struct ContextObject {
  2089. Node* _node;
  2090.  
  2091. Number _input_IN1;
  2092. Number _input_IN2;
  2093.  
  2094. };
  2095.  
  2096. using Context = ContextObject*;
  2097.  
  2098. template<typename PinT> typename ValueType<PinT>::T getValue(Context ctx) {
  2099. static_assert(always_false<PinT>::value,
  2100. "Invalid pin descriptor. Expected one of:" \
  2101. " input_IN1 input_IN2" \
  2102. " output_OUT");
  2103. }
  2104.  
  2105. template<> Number getValue<input_IN1>(Context ctx) {
  2106. return ctx->_input_IN1;
  2107. }
  2108. template<> Number getValue<input_IN2>(Context ctx) {
  2109. return ctx->_input_IN2;
  2110. }
  2111. template<> Logic getValue<output_OUT>(Context ctx) {
  2112. return ctx->_node->output_OUT;
  2113. }
  2114.  
  2115. template<typename InputT> bool isInputDirty(Context ctx) {
  2116. static_assert(always_false<InputT>::value,
  2117. "Invalid input descriptor. Expected one of:" \
  2118. "");
  2119. return false;
  2120. }
  2121.  
  2122. template<typename OutputT> void emitValue(Context ctx, typename ValueType<OutputT>::T val) {
  2123. static_assert(always_false<OutputT>::value,
  2124. "Invalid output descriptor. Expected one of:" \
  2125. " output_OUT");
  2126. }
  2127.  
  2128. template<> void emitValue<output_OUT>(Context ctx, Logic val) {
  2129. ctx->_node->output_OUT = val;
  2130. }
  2131.  
  2132. State* getState(Context ctx) {
  2133. return &ctx->_node->state;
  2134. }
  2135.  
  2136. void evaluate(Context ctx) {
  2137. auto lhs = getValue<input_IN1>(ctx);
  2138. auto rhs = getValue<input_IN2>(ctx);
  2139. emitValue<output_OUT>(ctx, lhs > rhs);
  2140. }
  2141.  
  2142. } // namespace xod__core__greater
  2143.  
  2144. //-----------------------------------------------------------------------------
  2145. // xod/core/pulse-on-change(boolean) implementation
  2146. //-----------------------------------------------------------------------------
  2147. namespace xod__core__pulse_on_change__boolean {
  2148.  
  2149. struct State {
  2150. bool sample = false;
  2151. };
  2152.  
  2153. struct Node {
  2154. State state;
  2155. };
  2156.  
  2157. struct input_IN { };
  2158. struct output_OUT { };
  2159.  
  2160. template<typename PinT> struct ValueType { using T = void; };
  2161. template<> struct ValueType<input_IN> { using T = Logic; };
  2162. template<> struct ValueType<output_OUT> { using T = Pulse; };
  2163.  
  2164. struct ContextObject {
  2165. Node* _node;
  2166.  
  2167. Logic _input_IN;
  2168.  
  2169. bool _isOutputDirty_OUT : 1;
  2170. };
  2171.  
  2172. using Context = ContextObject*;
  2173.  
  2174. template<typename PinT> typename ValueType<PinT>::T getValue(Context ctx) {
  2175. static_assert(always_false<PinT>::value,
  2176. "Invalid pin descriptor. Expected one of:" \
  2177. " input_IN" \
  2178. " output_OUT");
  2179. }
  2180.  
  2181. template<> Logic getValue<input_IN>(Context ctx) {
  2182. return ctx->_input_IN;
  2183. }
  2184. template<> Pulse getValue<output_OUT>(Context ctx) {
  2185. return Pulse();
  2186. }
  2187.  
  2188. template<typename InputT> bool isInputDirty(Context ctx) {
  2189. static_assert(always_false<InputT>::value,
  2190. "Invalid input descriptor. Expected one of:" \
  2191. "");
  2192. return false;
  2193. }
  2194.  
  2195. template<typename OutputT> void emitValue(Context ctx, typename ValueType<OutputT>::T val) {
  2196. static_assert(always_false<OutputT>::value,
  2197. "Invalid output descriptor. Expected one of:" \
  2198. " output_OUT");
  2199. }
  2200.  
  2201. template<> void emitValue<output_OUT>(Context ctx, Pulse val) {
  2202. ctx->_isOutputDirty_OUT = true;
  2203. }
  2204.  
  2205. State* getState(Context ctx) {
  2206. return &ctx->_node->state;
  2207. }
  2208.  
  2209. void evaluate(Context ctx) {
  2210. State* state = getState(ctx);
  2211. int8_t newValue = (int8_t) getValue<input_IN>(ctx);
  2212.  
  2213. if (!isSettingUp() && newValue != state->sample)
  2214. emitValue<output_OUT>(ctx, 1);
  2215.  
  2216. state->sample = newValue;
  2217. }
  2218.  
  2219. } // namespace xod__core__pulse_on_change__boolean
  2220.  
  2221. //-----------------------------------------------------------------------------
  2222. // xod/core/gate(pulse) implementation
  2223. //-----------------------------------------------------------------------------
  2224. namespace xod__core__gate__pulse {
  2225.  
  2226. struct State {
  2227. };
  2228.  
  2229. struct Node {
  2230. State state;
  2231. };
  2232.  
  2233. struct input_IN { };
  2234. struct input_EN { };
  2235. struct output_OUT { };
  2236.  
  2237. template<typename PinT> struct ValueType { using T = void; };
  2238. template<> struct ValueType<input_IN> { using T = Pulse; };
  2239. template<> struct ValueType<input_EN> { using T = Logic; };
  2240. template<> struct ValueType<output_OUT> { using T = Pulse; };
  2241.  
  2242. struct ContextObject {
  2243. Node* _node;
  2244.  
  2245. Logic _input_EN;
  2246.  
  2247. bool _isInputDirty_IN;
  2248.  
  2249. bool _isOutputDirty_OUT : 1;
  2250. };
  2251.  
  2252. using Context = ContextObject*;
  2253.  
  2254. template<typename PinT> typename ValueType<PinT>::T getValue(Context ctx) {
  2255. static_assert(always_false<PinT>::value,
  2256. "Invalid pin descriptor. Expected one of:" \
  2257. " input_IN input_EN" \
  2258. " output_OUT");
  2259. }
  2260.  
  2261. template<> Pulse getValue<input_IN>(Context ctx) {
  2262. return Pulse();
  2263. }
  2264. template<> Logic getValue<input_EN>(Context ctx) {
  2265. return ctx->_input_EN;
  2266. }
  2267. template<> Pulse getValue<output_OUT>(Context ctx) {
  2268. return Pulse();
  2269. }
  2270.  
  2271. template<typename InputT> bool isInputDirty(Context ctx) {
  2272. static_assert(always_false<InputT>::value,
  2273. "Invalid input descriptor. Expected one of:" \
  2274. " input_IN");
  2275. return false;
  2276. }
  2277.  
  2278. template<> bool isInputDirty<input_IN>(Context ctx) {
  2279. return ctx->_isInputDirty_IN;
  2280. }
  2281.  
  2282. template<typename OutputT> void emitValue(Context ctx, typename ValueType<OutputT>::T val) {
  2283. static_assert(always_false<OutputT>::value,
  2284. "Invalid output descriptor. Expected one of:" \
  2285. " output_OUT");
  2286. }
  2287.  
  2288. template<> void emitValue<output_OUT>(Context ctx, Pulse val) {
  2289. ctx->_isOutputDirty_OUT = true;
  2290. }
  2291.  
  2292. State* getState(Context ctx) {
  2293. return &ctx->_node->state;
  2294. }
  2295.  
  2296. void evaluate(Context ctx) {
  2297. if (getValue<input_EN>(ctx) && isInputDirty<input_IN>(ctx))
  2298. emitValue<output_OUT>(ctx, true);
  2299. }
  2300.  
  2301. } // namespace xod__core__gate__pulse
  2302.  
  2303. //-----------------------------------------------------------------------------
  2304. // xod-dev/servo/rotate implementation
  2305. //-----------------------------------------------------------------------------
  2306. namespace xod_dev__servo__rotate {
  2307.  
  2308. //#pragma XOD evaluate_on_pin disable
  2309. //#pragma XOD evaluate_on_pin enable input_DO
  2310.  
  2311. struct State { };
  2312.  
  2313. struct Node {
  2314. xod_dev__servo__servo_device::Type output_DEVU0027;
  2315. State state;
  2316. };
  2317.  
  2318. struct input_VAL { };
  2319. struct input_DEV { };
  2320. struct input_DO { };
  2321. struct output_ACK { };
  2322. struct output_DEVU0027 { };
  2323.  
  2324. template<typename PinT> struct ValueType { using T = void; };
  2325. template<> struct ValueType<input_VAL> { using T = Number; };
  2326. template<> struct ValueType<input_DEV> { using T = xod_dev__servo__servo_device::Type; };
  2327. template<> struct ValueType<input_DO> { using T = Pulse; };
  2328. template<> struct ValueType<output_ACK> { using T = Pulse; };
  2329. template<> struct ValueType<output_DEVU0027> { using T = xod_dev__servo__servo_device::Type; };
  2330.  
  2331. struct ContextObject {
  2332. Node* _node;
  2333.  
  2334. Number _input_VAL;
  2335. xod_dev__servo__servo_device::Type _input_DEV;
  2336.  
  2337. bool _isInputDirty_DO;
  2338.  
  2339. bool _isOutputDirty_ACK : 1;
  2340. bool _isOutputDirty_DEVU0027 : 1;
  2341. };
  2342.  
  2343. using Context = ContextObject*;
  2344.  
  2345. template<typename PinT> typename ValueType<PinT>::T getValue(Context ctx) {
  2346. static_assert(always_false<PinT>::value,
  2347. "Invalid pin descriptor. Expected one of:" \
  2348. " input_VAL input_DEV input_DO" \
  2349. " output_ACK output_DEVU0027");
  2350. }
  2351.  
  2352. template<> Number getValue<input_VAL>(Context ctx) {
  2353. return ctx->_input_VAL;
  2354. }
  2355. template<> xod_dev__servo__servo_device::Type getValue<input_DEV>(Context ctx) {
  2356. return ctx->_input_DEV;
  2357. }
  2358. template<> Pulse getValue<input_DO>(Context ctx) {
  2359. return Pulse();
  2360. }
  2361. template<> Pulse getValue<output_ACK>(Context ctx) {
  2362. return Pulse();
  2363. }
  2364. template<> xod_dev__servo__servo_device::Type getValue<output_DEVU0027>(Context ctx) {
  2365. return ctx->_node->output_DEVU0027;
  2366. }
  2367.  
  2368. template<typename InputT> bool isInputDirty(Context ctx) {
  2369. static_assert(always_false<InputT>::value,
  2370. "Invalid input descriptor. Expected one of:" \
  2371. " input_DO");
  2372. return false;
  2373. }
  2374.  
  2375. template<> bool isInputDirty<input_DO>(Context ctx) {
  2376. return ctx->_isInputDirty_DO;
  2377. }
  2378.  
  2379. template<typename OutputT> void emitValue(Context ctx, typename ValueType<OutputT>::T val) {
  2380. static_assert(always_false<OutputT>::value,
  2381. "Invalid output descriptor. Expected one of:" \
  2382. " output_ACK output_DEVU0027");
  2383. }
  2384.  
  2385. template<> void emitValue<output_ACK>(Context ctx, Pulse val) {
  2386. ctx->_isOutputDirty_ACK = true;
  2387. }
  2388. template<> void emitValue<output_DEVU0027>(Context ctx, xod_dev__servo__servo_device::Type val) {
  2389. ctx->_node->output_DEVU0027 = val;
  2390. ctx->_isOutputDirty_DEVU0027 = true;
  2391. }
  2392.  
  2393. State* getState(Context ctx) {
  2394. return &ctx->_node->state;
  2395. }
  2396.  
  2397. void evaluate(Context ctx) {
  2398. auto xservo = getValue<input_DEV>(ctx);
  2399.  
  2400. if (isSettingUp()) {
  2401. // Short-circuit DEV and DEV'
  2402. emitValue<output_DEVU0027>(ctx, xservo);
  2403. }
  2404.  
  2405. if (!isInputDirty<input_DO>(ctx))
  2406. return;
  2407.  
  2408. auto angle = getValue<input_VAL>(ctx);
  2409. xservo->write01(angle);
  2410. emitValue<output_ACK>(ctx, 1);
  2411. }
  2412.  
  2413. } // namespace xod_dev__servo__rotate
  2414.  
  2415. //-----------------------------------------------------------------------------
  2416. // xod/gpio/digital-write implementation
  2417. //-----------------------------------------------------------------------------
  2418. namespace xod__gpio__digital_write {
  2419.  
  2420. //#pragma XOD evaluate_on_pin disable
  2421. //#pragma XOD evaluate_on_pin enable input_UPD
  2422. //#pragma XOD error_raise enable
  2423.  
  2424. struct State {
  2425. };
  2426.  
  2427. union NodeErrors {
  2428. struct {
  2429. bool output_DONE : 1;
  2430. };
  2431.  
  2432. ErrorFlags flags;
  2433. };
  2434.  
  2435. struct Node {
  2436. NodeErrors errors;
  2437. State state;
  2438. };
  2439.  
  2440. struct input_PORT { };
  2441. struct input_SIG { };
  2442. struct input_UPD { };
  2443. struct output_DONE { };
  2444.  
  2445. template<typename PinT> struct ValueType { using T = void; };
  2446. template<> struct ValueType<input_PORT> { using T = uint8_t; };
  2447. template<> struct ValueType<input_SIG> { using T = Logic; };
  2448. template<> struct ValueType<input_UPD> { using T = Pulse; };
  2449. template<> struct ValueType<output_DONE> { using T = Pulse; };
  2450.  
  2451. struct ContextObject {
  2452. Node* _node;
  2453.  
  2454. uint8_t _input_PORT;
  2455. Logic _input_SIG;
  2456.  
  2457. bool _isInputDirty_UPD;
  2458.  
  2459. bool _isOutputDirty_DONE : 1;
  2460. };
  2461.  
  2462. using Context = ContextObject*;
  2463.  
  2464. template<typename PinT> typename ValueType<PinT>::T getValue(Context ctx) {
  2465. static_assert(always_false<PinT>::value,
  2466. "Invalid pin descriptor. Expected one of:" \
  2467. " input_PORT input_SIG input_UPD" \
  2468. " output_DONE");
  2469. }
  2470.  
  2471. template<> uint8_t getValue<input_PORT>(Context ctx) {
  2472. return ctx->_input_PORT;
  2473. }
  2474. template<> Logic getValue<input_SIG>(Context ctx) {
  2475. return ctx->_input_SIG;
  2476. }
  2477. template<> Pulse getValue<input_UPD>(Context ctx) {
  2478. return Pulse();
  2479. }
  2480. template<> Pulse getValue<output_DONE>(Context ctx) {
  2481. return Pulse();
  2482. }
  2483.  
  2484. template<typename InputT> bool isInputDirty(Context ctx) {
  2485. static_assert(always_false<InputT>::value,
  2486. "Invalid input descriptor. Expected one of:" \
  2487. " input_UPD");
  2488. return false;
  2489. }
  2490.  
  2491. template<> bool isInputDirty<input_UPD>(Context ctx) {
  2492. return ctx->_isInputDirty_UPD;
  2493. }
  2494.  
  2495. template<typename OutputT> void emitValue(Context ctx, typename ValueType<OutputT>::T val) {
  2496. static_assert(always_false<OutputT>::value,
  2497. "Invalid output descriptor. Expected one of:" \
  2498. " output_DONE");
  2499. }
  2500.  
  2501. template<> void emitValue<output_DONE>(Context ctx, Pulse val) {
  2502. ctx->_isOutputDirty_DONE = true;
  2503. ctx->_node->errors.output_DONE = false;
  2504. }
  2505.  
  2506. State* getState(Context ctx) {
  2507. return &ctx->_node->state;
  2508. }
  2509.  
  2510. template<typename OutputT> void raiseError(Context ctx) {
  2511. static_assert(always_false<OutputT>::value,
  2512. "Invalid output descriptor. Expected one of:" \
  2513. " output_DONE");
  2514. }
  2515.  
  2516. template<> void raiseError<output_DONE>(Context ctx) {
  2517. ctx->_node->errors.output_DONE = true;
  2518. ctx->_isOutputDirty_DONE = true;
  2519. }
  2520.  
  2521. void raiseError(Context ctx) {
  2522. ctx->_node->errors.output_DONE = true;
  2523. ctx->_isOutputDirty_DONE = true;
  2524. }
  2525.  
  2526. void evaluate(Context ctx) {
  2527. if (!isInputDirty<input_UPD>(ctx))
  2528. return;
  2529.  
  2530. const uint8_t port = getValue<input_PORT>(ctx);
  2531. if (!isValidDigitalPort(port)) {
  2532. raiseError<output_DONE>(ctx);
  2533. return;
  2534. }
  2535.  
  2536. ::pinMode(port, OUTPUT);
  2537. const bool val = getValue<input_SIG>(ctx);
  2538. ::digitalWrite(port, val);
  2539. emitValue<output_DONE>(ctx, 1);
  2540. }
  2541.  
  2542. } // namespace xod__gpio__digital_write
  2543.  
  2544. } // namespace xod
  2545.  
  2546.  
  2547. /*=============================================================================
  2548. *
  2549. *
  2550. * Main loop components
  2551. *
  2552. *
  2553. =============================================================================*/
  2554.  
  2555. namespace xod {
  2556.  
  2557. // Define/allocate persistent storages (state, timeout, output data) for all nodes
  2558. #pragma GCC diagnostic push
  2559. #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
  2560.  
  2561. constexpr Number node_0_output_VAL = 0.25;
  2562.  
  2563. constexpr Number node_1_output_VAL = 36;
  2564.  
  2565. constexpr Number node_2_output_VAL = 0.02;
  2566.  
  2567. constexpr uint8_t node_3_output_VAL = 52;
  2568.  
  2569. constexpr Logic node_4_output_VAL = true;
  2570.  
  2571. constexpr uint8_t node_5_output_VAL = 32;
  2572.  
  2573. constexpr Logic node_6_output_VAL = true;
  2574.  
  2575. constexpr uint8_t node_7_output_VAL = 0;
  2576.  
  2577. constexpr Number node_8_output_VAL = 0.15;
  2578.  
  2579. constexpr Number node_9_output_VAL = 0.35;
  2580.  
  2581. constexpr Number node_10_output_VAL = 25;
  2582.  
  2583. constexpr Number node_11_output_VAL = 125;
  2584.  
  2585. constexpr uint8_t node_12_output_VAL = A0;
  2586.  
  2587. constexpr uint8_t node_13_output_VAL = A5;
  2588.  
  2589. constexpr Number node_14_output_VAL = 544;
  2590.  
  2591. constexpr Number node_15_output_VAL = 2400;
  2592.  
  2593. constexpr Logic node_16_output_VAL = true;
  2594.  
  2595. constexpr Logic node_17_output_VAL = true;
  2596.  
  2597. constexpr xod_dev__servo__servo_device::Type node_21_output_DEV = { /* xod-dev/servo/servo-device */ };
  2598.  
  2599. constexpr Logic node_24_output_SIG = false;
  2600.  
  2601. constexpr Number node_25_output_VAL = 0;
  2602.  
  2603. constexpr Logic node_27_output_OUT = false;
  2604.  
  2605. constexpr Number node_28_output_OUT = 0;
  2606.  
  2607. constexpr Logic node_29_output_OUT = false;
  2608.  
  2609. constexpr Logic node_30_output_OUT = false;
  2610.  
  2611. constexpr xod_dev__servo__servo_device::Type node_42_output_DEVU0027 = { /* xod-dev/servo/servo-device */ };
  2612.  
  2613. #pragma GCC diagnostic pop
  2614.  
  2615. struct TransactionState {
  2616. bool node_18_isNodeDirty : 1;
  2617. bool node_18_isOutputDirty_TICK : 1;
  2618. bool node_19_isNodeDirty : 1;
  2619. bool node_19_isOutputDirty_BOOT : 1;
  2620. bool node_20_isNodeDirty : 1;
  2621. bool node_20_isOutputDirty_OUT : 1;
  2622. bool node_21_isNodeDirty : 1;
  2623. bool node_22_isNodeDirty : 1;
  2624. bool node_22_isOutputDirty_OUT : 1;
  2625. bool node_23_isNodeDirty : 1;
  2626. bool node_23_isOutputDirty_OUT : 1;
  2627. bool node_24_isNodeDirty : 1;
  2628. bool node_24_isOutputDirty_SIG : 1;
  2629. bool node_25_isNodeDirty : 1;
  2630. bool node_25_isOutputDirty_VAL : 1;
  2631. bool node_26_isNodeDirty : 1;
  2632. bool node_26_isOutputDirty_OUT : 1;
  2633. bool node_27_isNodeDirty : 1;
  2634. bool node_27_isOutputDirty_OUT : 1;
  2635. bool node_27_hasUpstreamError : 1;
  2636. bool node_28_isNodeDirty : 1;
  2637. bool node_28_isOutputDirty_OUT : 1;
  2638. bool node_28_hasUpstreamError : 1;
  2639. bool node_29_isNodeDirty : 1;
  2640. bool node_29_isOutputDirty_OUT : 1;
  2641. bool node_29_hasUpstreamError : 1;
  2642. bool node_30_isNodeDirty : 1;
  2643. bool node_30_isOutputDirty_OUT : 1;
  2644. bool node_30_hasUpstreamError : 1;
  2645. bool node_31_isNodeDirty : 1;
  2646. bool node_31_isOutputDirty_OUT : 1;
  2647. bool node_31_hasUpstreamError : 1;
  2648. bool node_32_isNodeDirty : 1;
  2649. bool node_32_isOutputDirty_OUT : 1;
  2650. bool node_32_hasUpstreamError : 1;
  2651. bool node_33_isNodeDirty : 1;
  2652. bool node_33_isOutputDirty_OUT : 1;
  2653. bool node_33_hasUpstreamError : 1;
  2654. bool node_34_isNodeDirty : 1;
  2655. bool node_34_isOutputDirty_OUT : 1;
  2656. bool node_34_hasUpstreamError : 1;
  2657. bool node_35_isNodeDirty : 1;
  2658. bool node_35_isOutputDirty_OUT : 1;
  2659. bool node_35_hasUpstreamError : 1;
  2660. bool node_36_isNodeDirty : 1;
  2661. bool node_36_isOutputDirty_OUT : 1;
  2662. bool node_36_hasUpstreamError : 1;
  2663. bool node_37_isNodeDirty : 1;
  2664. bool node_37_isOutputDirty_OUT : 1;
  2665. bool node_37_hasUpstreamError : 1;
  2666. bool node_38_isNodeDirty : 1;
  2667. bool node_38_isOutputDirty_OUT : 1;
  2668. bool node_38_hasUpstreamError : 1;
  2669. bool node_39_isNodeDirty : 1;
  2670. bool node_39_isOutputDirty_OUT : 1;
  2671. bool node_39_hasUpstreamError : 1;
  2672. bool node_40_isNodeDirty : 1;
  2673. bool node_40_isOutputDirty_OUT : 1;
  2674. bool node_40_hasUpstreamError : 1;
  2675. bool node_41_isNodeDirty : 1;
  2676. bool node_41_isOutputDirty_OUT : 1;
  2677. bool node_41_hasUpstreamError : 1;
  2678. bool node_42_isNodeDirty : 1;
  2679. bool node_42_hasUpstreamError : 1;
  2680. bool node_43_isNodeDirty : 1;
  2681. bool node_43_hasUpstreamError : 1;
  2682. bool node_44_isNodeDirty : 1;
  2683. bool node_44_hasUpstreamError : 1;
  2684. TransactionState() {
  2685. node_18_isNodeDirty = true;
  2686. node_18_isOutputDirty_TICK = false;
  2687. node_19_isNodeDirty = true;
  2688. node_19_isOutputDirty_BOOT = false;
  2689. node_20_isNodeDirty = true;
  2690. node_20_isOutputDirty_OUT = false;
  2691. node_21_isNodeDirty = true;
  2692. node_22_isNodeDirty = true;
  2693. node_22_isOutputDirty_OUT = false;
  2694. node_23_isNodeDirty = true;
  2695. node_23_isOutputDirty_OUT = false;
  2696. node_24_isNodeDirty = true;
  2697. node_24_isOutputDirty_SIG = true;
  2698. node_25_isNodeDirty = true;
  2699. node_25_isOutputDirty_VAL = true;
  2700. node_26_isNodeDirty = true;
  2701. node_26_isOutputDirty_OUT = false;
  2702. node_27_isNodeDirty = true;
  2703. node_28_isNodeDirty = true;
  2704. node_29_isNodeDirty = true;
  2705. node_29_isOutputDirty_OUT = true;
  2706. node_30_isNodeDirty = true;
  2707. node_31_isNodeDirty = true;
  2708. node_31_isOutputDirty_OUT = false;
  2709. node_32_isNodeDirty = true;
  2710. node_32_isOutputDirty_OUT = false;
  2711. node_33_isNodeDirty = true;
  2712. node_33_isOutputDirty_OUT = false;
  2713. node_34_isNodeDirty = true;
  2714. node_34_isOutputDirty_OUT = false;
  2715. node_35_isNodeDirty = true;
  2716. node_35_isOutputDirty_OUT = false;
  2717. node_36_isNodeDirty = true;
  2718. node_36_isOutputDirty_OUT = false;
  2719. node_37_isNodeDirty = true;
  2720. node_37_isOutputDirty_OUT = false;
  2721. node_38_isNodeDirty = true;
  2722. node_38_isOutputDirty_OUT = false;
  2723. node_39_isNodeDirty = true;
  2724. node_39_isOutputDirty_OUT = false;
  2725. node_40_isNodeDirty = true;
  2726. node_40_isOutputDirty_OUT = false;
  2727. node_41_isNodeDirty = true;
  2728. node_41_isOutputDirty_OUT = false;
  2729. node_42_isNodeDirty = true;
  2730. node_43_isNodeDirty = true;
  2731. node_44_isNodeDirty = true;
  2732. }
  2733. };
  2734.  
  2735. TransactionState g_transaction;
  2736.  
  2737. xod__core__continuously::Node node_18 = {
  2738. 0, // timeoutAt
  2739. xod__core__continuously::State() // state default
  2740. };
  2741. xod__core__boot::Node node_19 = {
  2742. xod__core__boot::State() // state default
  2743. };
  2744. xod__core__pulse_on_change__number::Node node_20 = {
  2745. xod__core__pulse_on_change__number::State() // state default
  2746. };
  2747. xod_dev__servo__servo_device::Node node_21 = {
  2748. false, // DEV has no errors on start
  2749. node_21_output_DEV, // output DEV default
  2750. xod_dev__servo__servo_device::State() // state default
  2751. };
  2752. xod__core__cast_to_pulse__boolean::Node node_22 = {
  2753. xod__core__cast_to_pulse__boolean::State() // state default
  2754. };
  2755. xod__core__cast_to_pulse__boolean::Node node_23 = {
  2756. xod__core__cast_to_pulse__boolean::State() // state default
  2757. };
  2758. xod__gpio__digital_read_pullup::Node node_24 = {
  2759. false, // SIG has no errors on start
  2760. false, // DONE has no errors on start
  2761. node_24_output_SIG, // output SIG default
  2762. xod__gpio__digital_read_pullup::State() // state default
  2763. };
  2764. xod__gpio__analog_read::Node node_25 = {
  2765. false, // VAL has no errors on start
  2766. false, // DONE has no errors on start
  2767. node_25_output_VAL, // output VAL default
  2768. xod__gpio__analog_read::State() // state default
  2769. };
  2770. xod__core__any::Node node_26 = {
  2771. xod__core__any::State() // state default
  2772. };
  2773. xod__core__not::Node node_27 = {
  2774. node_27_output_OUT, // output OUT default
  2775. xod__core__not::State() // state default
  2776. };
  2777. xod__math__map::Node node_28 = {
  2778. node_28_output_OUT, // output OUT default
  2779. xod__math__map::State() // state default
  2780. };
  2781. xod__core__debounce__boolean::Node node_29 = {
  2782. 0, // timeoutAt
  2783. node_29_output_OUT, // output OUT default
  2784. xod__core__debounce__boolean::State() // state default
  2785. };
  2786. xod__core__greater::Node node_30 = {
  2787. node_30_output_OUT, // output OUT default
  2788. xod__core__greater::State() // state default
  2789. };
  2790. xod__core__pulse_on_change__boolean::Node node_31 = {
  2791. xod__core__pulse_on_change__boolean::State() // state default
  2792. };
  2793. xod__core__pulse_on_change__boolean::Node node_32 = {
  2794. xod__core__pulse_on_change__boolean::State() // state default
  2795. };
  2796. xod__core__cast_to_pulse__boolean::Node node_33 = {
  2797. xod__core__cast_to_pulse__boolean::State() // state default
  2798. };
  2799. xod__core__any::Node node_34 = {
  2800. xod__core__any::State() // state default
  2801. };
  2802. xod__core__any::Node node_35 = {
  2803. xod__core__any::State() // state default
  2804. };
  2805. xod__core__any::Node node_36 = {
  2806. xod__core__any::State() // state default
  2807. };
  2808. xod__core__any::Node node_37 = {
  2809. xod__core__any::State() // state default
  2810. };
  2811. xod__core__any::Node node_38 = {
  2812. xod__core__any::State() // state default
  2813. };
  2814. xod__core__gate__pulse::Node node_39 = {
  2815. xod__core__gate__pulse::State() // state default
  2816. };
  2817. xod__core__gate__pulse::Node node_40 = {
  2818. xod__core__gate__pulse::State() // state default
  2819. };
  2820. xod__core__gate__pulse::Node node_41 = {
  2821. xod__core__gate__pulse::State() // state default
  2822. };
  2823. xod_dev__servo__rotate::Node node_42 = {
  2824. node_42_output_DEVU0027, // output DEVU0027 default
  2825. xod_dev__servo__rotate::State() // state default
  2826. };
  2827. xod__gpio__digital_write::Node node_43 = {
  2828. false, // DONE has no errors on start
  2829. xod__gpio__digital_write::State() // state default
  2830. };
  2831. xod__gpio__digital_write::Node node_44 = {
  2832. false, // DONE has no errors on start
  2833. xod__gpio__digital_write::State() // state default
  2834. };
  2835.  
  2836. #if defined(XOD_DEBUG) || defined(XOD_SIMULATION)
  2837. namespace detail {
  2838. void handleTweaks() {
  2839. if (XOD_DEBUG_SERIAL.available() > 0 && XOD_DEBUG_SERIAL.find("+XOD:", 5)) {
  2840. int tweakedNodeId = XOD_DEBUG_SERIAL.parseInt();
  2841.  
  2842. switch (tweakedNodeId) {
  2843. }
  2844.  
  2845. XOD_DEBUG_SERIAL.find('\n');
  2846. }
  2847. }
  2848. } // namespace detail
  2849. #endif
  2850.  
  2851. void handleDefers() {
  2852. }
  2853.  
  2854. void runTransaction() {
  2855. g_transactionTime = millis();
  2856.  
  2857. XOD_TRACE_F("Transaction started, t=");
  2858. XOD_TRACE_LN(g_transactionTime);
  2859.  
  2860. #if defined(XOD_DEBUG) || defined(XOD_SIMULATION)
  2861. detail::handleTweaks();
  2862. #endif
  2863.  
  2864. // Check for timeouts
  2865. g_transaction.node_18_isNodeDirty |= detail::isTimedOut(&node_18);
  2866. g_transaction.node_29_isNodeDirty |= detail::isTimedOut(&node_29);
  2867.  
  2868. // defer-* nodes are always at the very bottom of the graph, so no one will
  2869. // recieve values emitted by them. We must evaluate them before everybody
  2870. // else to give them a chance to emit values.
  2871. //
  2872. // If trigerred, keep only output dirty, not the node itself, so it will
  2873. // evaluate on the regular pass only if it receives a new value again.
  2874. if (!isSettingUp()) {
  2875. g_isEarlyDeferPass = true;
  2876. handleDefers();
  2877. g_isEarlyDeferPass = false;
  2878. }
  2879.  
  2880. // Evaluate all dirty nodes
  2881. { // xod__core__continuously #18
  2882. if (g_transaction.node_18_isNodeDirty) {
  2883. XOD_TRACE_F("Eval node #");
  2884. XOD_TRACE_LN(18);
  2885.  
  2886. xod__core__continuously::ContextObject ctxObj;
  2887. ctxObj._node = &node_18;
  2888.  
  2889. // copy data from upstream nodes into context
  2890.  
  2891. // initialize temporary output dirtyness state in the context,
  2892. // where it can be modified from `raiseError` and `emitValue`
  2893. ctxObj._isOutputDirty_TICK = false;
  2894.  
  2895. xod__core__continuously::evaluate(&ctxObj);
  2896.  
  2897. // transfer possibly modified dirtiness state from context to g_transaction
  2898. g_transaction.node_18_isOutputDirty_TICK = ctxObj._isOutputDirty_TICK;
  2899.  
  2900. // mark downstream nodes dirty
  2901. g_transaction.node_24_isNodeDirty |= g_transaction.node_18_isOutputDirty_TICK;
  2902. g_transaction.node_25_isNodeDirty |= g_transaction.node_18_isOutputDirty_TICK;
  2903. }
  2904.  
  2905. }
  2906. { // xod__core__boot #19
  2907. if (g_transaction.node_19_isNodeDirty) {
  2908. XOD_TRACE_F("Eval node #");
  2909. XOD_TRACE_LN(19);
  2910.  
  2911. xod__core__boot::ContextObject ctxObj;
  2912. ctxObj._node = &node_19;
  2913.  
  2914. // copy data from upstream nodes into context
  2915.  
  2916. // initialize temporary output dirtyness state in the context,
  2917. // where it can be modified from `raiseError` and `emitValue`
  2918. ctxObj._isOutputDirty_BOOT = false;
  2919.  
  2920. xod__core__boot::evaluate(&ctxObj);
  2921.  
  2922. // transfer possibly modified dirtiness state from context to g_transaction
  2923. g_transaction.node_19_isOutputDirty_BOOT = ctxObj._isOutputDirty_BOOT;
  2924.  
  2925. // mark downstream nodes dirty
  2926. g_transaction.node_26_isNodeDirty |= g_transaction.node_19_isOutputDirty_BOOT;
  2927. g_transaction.node_34_isNodeDirty |= g_transaction.node_19_isOutputDirty_BOOT;
  2928. g_transaction.node_35_isNodeDirty |= g_transaction.node_19_isOutputDirty_BOOT;
  2929. }
  2930.  
  2931. }
  2932. { // xod__core__pulse_on_change__number #20
  2933. if (g_transaction.node_20_isNodeDirty) {
  2934. XOD_TRACE_F("Eval node #");
  2935. XOD_TRACE_LN(20);
  2936.  
  2937. xod__core__pulse_on_change__number::ContextObject ctxObj;
  2938. ctxObj._node = &node_20;
  2939.  
  2940. // copy data from upstream nodes into context
  2941. ctxObj._input_IN = node_0_output_VAL;
  2942.  
  2943. // initialize temporary output dirtyness state in the context,
  2944. // where it can be modified from `raiseError` and `emitValue`
  2945. ctxObj._isOutputDirty_OUT = false;
  2946.  
  2947. xod__core__pulse_on_change__number::evaluate(&ctxObj);
  2948.  
  2949. // transfer possibly modified dirtiness state from context to g_transaction
  2950. g_transaction.node_20_isOutputDirty_OUT = ctxObj._isOutputDirty_OUT;
  2951.  
  2952. // mark downstream nodes dirty
  2953. g_transaction.node_26_isNodeDirty |= g_transaction.node_20_isOutputDirty_OUT;
  2954. }
  2955.  
  2956. }
  2957. { // xod_dev__servo__servo_device #21
  2958. if (g_transaction.node_21_isNodeDirty) {
  2959. XOD_TRACE_F("Eval node #");
  2960. XOD_TRACE_LN(21);
  2961.  
  2962. xod_dev__servo__servo_device::ContextObject ctxObj;
  2963. ctxObj._node = &node_21;
  2964.  
  2965. // copy data from upstream nodes into context
  2966. ctxObj._input_PORT = node_13_output_VAL;
  2967. ctxObj._input_Pmin = node_14_output_VAL;
  2968. ctxObj._input_Pmax = node_15_output_VAL;
  2969.  
  2970. // initialize temporary output dirtyness state in the context,
  2971. // where it can be modified from `raiseError` and `emitValue`
  2972. ctxObj._isOutputDirty_DEV = false;
  2973.  
  2974. xod_dev__servo__servo_device::NodeErrors previousErrors = node_21.errors;
  2975.  
  2976. xod_dev__servo__servo_device::evaluate(&ctxObj);
  2977.  
  2978. // transfer possibly modified dirtiness state from context to g_transaction
  2979.  
  2980. if (previousErrors.flags != node_21.errors.flags) {
  2981. detail::printErrorToDebugSerial(21, node_21.errors.flags);
  2982.  
  2983. // if an error was just raised or cleared from an output,
  2984. // mark nearest downstream error catchers as dirty
  2985. if (node_21.errors.output_DEV != previousErrors.output_DEV) {
  2986. }
  2987.  
  2988. // if a pulse output was cleared from error, mark downstream nodes as dirty
  2989. // (no matter if a pulse was emitted or not)
  2990. }
  2991.  
  2992. // mark downstream nodes dirty
  2993. }
  2994.  
  2995. // propagate errors hold by the node outputs
  2996. if (node_21.errors.flags) {
  2997. if (node_21.errors.output_DEV) {
  2998. g_transaction.node_42_hasUpstreamError = true;
  2999. }
  3000. }
  3001. }
  3002. { // xod__core__cast_to_pulse__boolean #22
  3003. if (g_transaction.node_22_isNodeDirty) {
  3004. XOD_TRACE_F("Eval node #");
  3005. XOD_TRACE_LN(22);
  3006.  
  3007. xod__core__cast_to_pulse__boolean::ContextObject ctxObj;
  3008. ctxObj._node = &node_22;
  3009.  
  3010. // copy data from upstream nodes into context
  3011. ctxObj._input_IN = node_16_output_VAL;
  3012.  
  3013. // initialize temporary output dirtyness state in the context,
  3014. // where it can be modified from `raiseError` and `emitValue`
  3015. ctxObj._isOutputDirty_OUT = false;
  3016.  
  3017. xod__core__cast_to_pulse__boolean::evaluate(&ctxObj);
  3018.  
  3019. // transfer possibly modified dirtiness state from context to g_transaction
  3020. g_transaction.node_22_isOutputDirty_OUT = ctxObj._isOutputDirty_OUT;
  3021.  
  3022. // mark downstream nodes dirty
  3023. g_transaction.node_37_isNodeDirty |= g_transaction.node_22_isOutputDirty_OUT;
  3024. }
  3025.  
  3026. }
  3027. { // xod__core__cast_to_pulse__boolean #23
  3028. if (g_transaction.node_23_isNodeDirty) {
  3029. XOD_TRACE_F("Eval node #");
  3030. XOD_TRACE_LN(23);
  3031.  
  3032. xod__core__cast_to_pulse__boolean::ContextObject ctxObj;
  3033. ctxObj._node = &node_23;
  3034.  
  3035. // copy data from upstream nodes into context
  3036. ctxObj._input_IN = node_17_output_VAL;
  3037.  
  3038. // initialize temporary output dirtyness state in the context,
  3039. // where it can be modified from `raiseError` and `emitValue`
  3040. ctxObj._isOutputDirty_OUT = false;
  3041.  
  3042. xod__core__cast_to_pulse__boolean::evaluate(&ctxObj);
  3043.  
  3044. // transfer possibly modified dirtiness state from context to g_transaction
  3045. g_transaction.node_23_isOutputDirty_OUT = ctxObj._isOutputDirty_OUT;
  3046.  
  3047. // mark downstream nodes dirty
  3048. g_transaction.node_38_isNodeDirty |= g_transaction.node_23_isOutputDirty_OUT;
  3049. }
  3050.  
  3051. }
  3052. { // xod__gpio__digital_read_pullup #24
  3053. if (g_transaction.node_24_isNodeDirty) {
  3054. XOD_TRACE_F("Eval node #");
  3055. XOD_TRACE_LN(24);
  3056.  
  3057. xod__gpio__digital_read_pullup::ContextObject ctxObj;
  3058. ctxObj._node = &node_24;
  3059.  
  3060. // copy data from upstream nodes into context
  3061. ctxObj._input_PORT = node_3_output_VAL;
  3062.  
  3063. ctxObj._isInputDirty_UPD = g_transaction.node_18_isOutputDirty_TICK;
  3064.  
  3065. // initialize temporary output dirtyness state in the context,
  3066. // where it can be modified from `raiseError` and `emitValue`
  3067. ctxObj._isOutputDirty_SIG = false;
  3068. ctxObj._isOutputDirty_DONE = false;
  3069.  
  3070. xod__gpio__digital_read_pullup::NodeErrors previousErrors = node_24.errors;
  3071.  
  3072. node_24.errors.output_DONE = false;
  3073.  
  3074. xod__gpio__digital_read_pullup::evaluate(&ctxObj);
  3075.  
  3076. // transfer possibly modified dirtiness state from context to g_transaction
  3077. g_transaction.node_24_isOutputDirty_SIG = ctxObj._isOutputDirty_SIG;
  3078.  
  3079. if (previousErrors.flags != node_24.errors.flags) {
  3080. detail::printErrorToDebugSerial(24, node_24.errors.flags);
  3081.  
  3082. // if an error was just raised or cleared from an output,
  3083. // mark nearest downstream error catchers as dirty
  3084. if (node_24.errors.output_SIG != previousErrors.output_SIG) {
  3085. }
  3086. if (node_24.errors.output_DONE != previousErrors.output_DONE) {
  3087. }
  3088.  
  3089. // if a pulse output was cleared from error, mark downstream nodes as dirty
  3090. // (no matter if a pulse was emitted or not)
  3091. if (previousErrors.output_DONE && !node_24.errors.output_DONE) {
  3092. }
  3093. }
  3094.  
  3095. // mark downstream nodes dirty
  3096. g_transaction.node_27_isNodeDirty |= g_transaction.node_24_isOutputDirty_SIG;
  3097. }
  3098.  
  3099. // propagate errors hold by the node outputs
  3100. if (node_24.errors.flags) {
  3101. if (node_24.errors.output_SIG) {
  3102. g_transaction.node_27_hasUpstreamError = true;
  3103. }
  3104. if (node_24.errors.output_DONE) {
  3105. }
  3106. }
  3107. }
  3108. { // xod__gpio__analog_read #25
  3109. if (g_transaction.node_25_isNodeDirty) {
  3110. XOD_TRACE_F("Eval node #");
  3111. XOD_TRACE_LN(25);
  3112.  
  3113. xod__gpio__analog_read::ContextObject ctxObj;
  3114. ctxObj._node = &node_25;
  3115.  
  3116. // copy data from upstream nodes into context
  3117. ctxObj._input_PORT = node_12_output_VAL;
  3118.  
  3119. ctxObj._isInputDirty_UPD = g_transaction.node_18_isOutputDirty_TICK;
  3120.  
  3121. // initialize temporary output dirtyness state in the context,
  3122. // where it can be modified from `raiseError` and `emitValue`
  3123. ctxObj._isOutputDirty_VAL = false;
  3124. ctxObj._isOutputDirty_DONE = false;
  3125.  
  3126. xod__gpio__analog_read::NodeErrors previousErrors = node_25.errors;
  3127.  
  3128. node_25.errors.output_DONE = false;
  3129.  
  3130. xod__gpio__analog_read::evaluate(&ctxObj);
  3131.  
  3132. // transfer possibly modified dirtiness state from context to g_transaction
  3133. g_transaction.node_25_isOutputDirty_VAL = ctxObj._isOutputDirty_VAL;
  3134.  
  3135. if (previousErrors.flags != node_25.errors.flags) {
  3136. detail::printErrorToDebugSerial(25, node_25.errors.flags);
  3137.  
  3138. // if an error was just raised or cleared from an output,
  3139. // mark nearest downstream error catchers as dirty
  3140. if (node_25.errors.output_VAL != previousErrors.output_VAL) {
  3141. }
  3142. if (node_25.errors.output_DONE != previousErrors.output_DONE) {
  3143. }
  3144.  
  3145. // if a pulse output was cleared from error, mark downstream nodes as dirty
  3146. // (no matter if a pulse was emitted or not)
  3147. if (previousErrors.output_DONE && !node_25.errors.output_DONE) {
  3148. }
  3149. }
  3150.  
  3151. // mark downstream nodes dirty
  3152. g_transaction.node_28_isNodeDirty |= g_transaction.node_25_isOutputDirty_VAL;
  3153. }
  3154.  
  3155. // propagate errors hold by the node outputs
  3156. if (node_25.errors.flags) {
  3157. if (node_25.errors.output_VAL) {
  3158. g_transaction.node_28_hasUpstreamError = true;
  3159. }
  3160. if (node_25.errors.output_DONE) {
  3161. }
  3162. }
  3163. }
  3164. { // xod__core__any #26
  3165. if (g_transaction.node_26_isNodeDirty) {
  3166. XOD_TRACE_F("Eval node #");
  3167. XOD_TRACE_LN(26);
  3168.  
  3169. xod__core__any::ContextObject ctxObj;
  3170. ctxObj._node = &node_26;
  3171.  
  3172. // copy data from upstream nodes into context
  3173.  
  3174. ctxObj._isInputDirty_IN1 = g_transaction.node_20_isOutputDirty_OUT;
  3175. ctxObj._isInputDirty_IN2 = g_transaction.node_19_isOutputDirty_BOOT;
  3176.  
  3177. // initialize temporary output dirtyness state in the context,
  3178. // where it can be modified from `raiseError` and `emitValue`
  3179. ctxObj._isOutputDirty_OUT = false;
  3180.  
  3181. xod__core__any::evaluate(&ctxObj);
  3182.  
  3183. // transfer possibly modified dirtiness state from context to g_transaction
  3184. g_transaction.node_26_isOutputDirty_OUT = ctxObj._isOutputDirty_OUT;
  3185.  
  3186. // mark downstream nodes dirty
  3187. g_transaction.node_36_isNodeDirty |= g_transaction.node_26_isOutputDirty_OUT;
  3188. }
  3189.  
  3190. }
  3191. { // xod__core__not #27
  3192.  
  3193. if (g_transaction.node_27_hasUpstreamError) {
  3194. g_transaction.node_29_hasUpstreamError = true;
  3195. } else if (g_transaction.node_27_isNodeDirty) {
  3196. XOD_TRACE_F("Eval node #");
  3197. XOD_TRACE_LN(27);
  3198.  
  3199. xod__core__not::ContextObject ctxObj;
  3200. ctxObj._node = &node_27;
  3201.  
  3202. // copy data from upstream nodes into context
  3203. ctxObj._input_IN = node_24.output_SIG;
  3204.  
  3205. // initialize temporary output dirtyness state in the context,
  3206. // where it can be modified from `raiseError` and `emitValue`
  3207.  
  3208. xod__core__not::evaluate(&ctxObj);
  3209.  
  3210. // transfer possibly modified dirtiness state from context to g_transaction
  3211.  
  3212. // mark downstream nodes dirty
  3213. g_transaction.node_29_isNodeDirty = true;
  3214. }
  3215.  
  3216. }
  3217. { // xod__math__map #28
  3218.  
  3219. if (g_transaction.node_28_hasUpstreamError) {
  3220. g_transaction.node_30_hasUpstreamError = true;
  3221. } else if (g_transaction.node_28_isNodeDirty) {
  3222. XOD_TRACE_F("Eval node #");
  3223. XOD_TRACE_LN(28);
  3224.  
  3225. xod__math__map::ContextObject ctxObj;
  3226. ctxObj._node = &node_28;
  3227.  
  3228. // copy data from upstream nodes into context
  3229. ctxObj._input_X = node_25.output_VAL;
  3230. ctxObj._input_Smin = node_8_output_VAL;
  3231. ctxObj._input_Smax = node_9_output_VAL;
  3232. ctxObj._input_Tmin = node_10_output_VAL;
  3233. ctxObj._input_Tmax = node_11_output_VAL;
  3234.  
  3235. // initialize temporary output dirtyness state in the context,
  3236. // where it can be modified from `raiseError` and `emitValue`
  3237.  
  3238. xod__math__map::evaluate(&ctxObj);
  3239.  
  3240. // transfer possibly modified dirtiness state from context to g_transaction
  3241.  
  3242. // mark downstream nodes dirty
  3243. g_transaction.node_30_isNodeDirty = true;
  3244. }
  3245.  
  3246. }
  3247. { // xod__core__debounce__boolean #29
  3248.  
  3249. if (g_transaction.node_29_hasUpstreamError) {
  3250. g_transaction.node_31_hasUpstreamError = true;
  3251. g_transaction.node_43_hasUpstreamError = true;
  3252. } else if (g_transaction.node_29_isNodeDirty) {
  3253. XOD_TRACE_F("Eval node #");
  3254. XOD_TRACE_LN(29);
  3255.  
  3256. xod__core__debounce__boolean::ContextObject ctxObj;
  3257. ctxObj._node = &node_29;
  3258.  
  3259. // copy data from upstream nodes into context
  3260. ctxObj._input_ST = node_27.output_OUT;
  3261. ctxObj._input_Ts = node_2_output_VAL;
  3262.  
  3263. // initialize temporary output dirtyness state in the context,
  3264. // where it can be modified from `raiseError` and `emitValue`
  3265. ctxObj._isOutputDirty_OUT = false;
  3266.  
  3267. xod__core__debounce__boolean::evaluate(&ctxObj);
  3268.  
  3269. // transfer possibly modified dirtiness state from context to g_transaction
  3270. g_transaction.node_29_isOutputDirty_OUT = ctxObj._isOutputDirty_OUT;
  3271.  
  3272. // mark downstream nodes dirty
  3273. g_transaction.node_31_isNodeDirty |= g_transaction.node_29_isOutputDirty_OUT;
  3274. }
  3275.  
  3276. }
  3277. { // xod__core__greater #30
  3278.  
  3279. if (g_transaction.node_30_hasUpstreamError) {
  3280. g_transaction.node_32_hasUpstreamError = true;
  3281. g_transaction.node_33_hasUpstreamError = true;
  3282. g_transaction.node_39_hasUpstreamError = true;
  3283. g_transaction.node_44_hasUpstreamError = true;
  3284. } else if (g_transaction.node_30_isNodeDirty) {
  3285. XOD_TRACE_F("Eval node #");
  3286. XOD_TRACE_LN(30);
  3287.  
  3288. xod__core__greater::ContextObject ctxObj;
  3289. ctxObj._node = &node_30;
  3290.  
  3291. // copy data from upstream nodes into context
  3292. ctxObj._input_IN1 = node_28.output_OUT;
  3293. ctxObj._input_IN2 = node_1_output_VAL;
  3294.  
  3295. // initialize temporary output dirtyness state in the context,
  3296. // where it can be modified from `raiseError` and `emitValue`
  3297.  
  3298. xod__core__greater::evaluate(&ctxObj);
  3299.  
  3300. // transfer possibly modified dirtiness state from context to g_transaction
  3301.  
  3302. // mark downstream nodes dirty
  3303. g_transaction.node_32_isNodeDirty = true;
  3304. g_transaction.node_33_isNodeDirty = true;
  3305. g_transaction.node_39_isNodeDirty = true;
  3306. }
  3307.  
  3308. }
  3309. { // xod__core__pulse_on_change__boolean #31
  3310.  
  3311. if (g_transaction.node_31_hasUpstreamError) {
  3312. g_transaction.node_34_hasUpstreamError = true;
  3313. } else if (g_transaction.node_31_isNodeDirty) {
  3314. XOD_TRACE_F("Eval node #");
  3315. XOD_TRACE_LN(31);
  3316.  
  3317. xod__core__pulse_on_change__boolean::ContextObject ctxObj;
  3318. ctxObj._node = &node_31;
  3319.  
  3320. // copy data from upstream nodes into context
  3321. ctxObj._input_IN = node_29.output_OUT;
  3322.  
  3323. // initialize temporary output dirtyness state in the context,
  3324. // where it can be modified from `raiseError` and `emitValue`
  3325. ctxObj._isOutputDirty_OUT = false;
  3326.  
  3327. xod__core__pulse_on_change__boolean::evaluate(&ctxObj);
  3328.  
  3329. // transfer possibly modified dirtiness state from context to g_transaction
  3330. g_transaction.node_31_isOutputDirty_OUT = ctxObj._isOutputDirty_OUT;
  3331.  
  3332. // mark downstream nodes dirty
  3333. g_transaction.node_34_isNodeDirty |= g_transaction.node_31_isOutputDirty_OUT;
  3334. }
  3335.  
  3336. }
  3337. { // xod__core__pulse_on_change__boolean #32
  3338.  
  3339. if (g_transaction.node_32_hasUpstreamError) {
  3340. g_transaction.node_35_hasUpstreamError = true;
  3341. } else if (g_transaction.node_32_isNodeDirty) {
  3342. XOD_TRACE_F("Eval node #");
  3343. XOD_TRACE_LN(32);
  3344.  
  3345. xod__core__pulse_on_change__boolean::ContextObject ctxObj;
  3346. ctxObj._node = &node_32;
  3347.  
  3348. // copy data from upstream nodes into context
  3349. ctxObj._input_IN = node_30.output_OUT;
  3350.  
  3351. // initialize temporary output dirtyness state in the context,
  3352. // where it can be modified from `raiseError` and `emitValue`
  3353. ctxObj._isOutputDirty_OUT = false;
  3354.  
  3355. xod__core__pulse_on_change__boolean::evaluate(&ctxObj);
  3356.  
  3357. // transfer possibly modified dirtiness state from context to g_transaction
  3358. g_transaction.node_32_isOutputDirty_OUT = ctxObj._isOutputDirty_OUT;
  3359.  
  3360. // mark downstream nodes dirty
  3361. g_transaction.node_35_isNodeDirty |= g_transaction.node_32_isOutputDirty_OUT;
  3362. }
  3363.  
  3364. }
  3365. { // xod__core__cast_to_pulse__boolean #33
  3366.  
  3367. if (g_transaction.node_33_hasUpstreamError) {
  3368. g_transaction.node_36_hasUpstreamError = true;
  3369. } else if (g_transaction.node_33_isNodeDirty) {
  3370. XOD_TRACE_F("Eval node #");
  3371. XOD_TRACE_LN(33);
  3372.  
  3373. xod__core__cast_to_pulse__boolean::ContextObject ctxObj;
  3374. ctxObj._node = &node_33;
  3375.  
  3376. // copy data from upstream nodes into context
  3377. ctxObj._input_IN = node_30.output_OUT;
  3378.  
  3379. // initialize temporary output dirtyness state in the context,
  3380. // where it can be modified from `raiseError` and `emitValue`
  3381. ctxObj._isOutputDirty_OUT = false;
  3382.  
  3383. xod__core__cast_to_pulse__boolean::evaluate(&ctxObj);
  3384.  
  3385. // transfer possibly modified dirtiness state from context to g_transaction
  3386. g_transaction.node_33_isOutputDirty_OUT = ctxObj._isOutputDirty_OUT;
  3387.  
  3388. // mark downstream nodes dirty
  3389. g_transaction.node_36_isNodeDirty |= g_transaction.node_33_isOutputDirty_OUT;
  3390. }
  3391.  
  3392. }
  3393. { // xod__core__any #34
  3394.  
  3395. if (g_transaction.node_34_hasUpstreamError) {
  3396. g_transaction.node_37_hasUpstreamError = true;
  3397. } else if (g_transaction.node_34_isNodeDirty) {
  3398. XOD_TRACE_F("Eval node #");
  3399. XOD_TRACE_LN(34);
  3400.  
  3401. xod__core__any::ContextObject ctxObj;
  3402. ctxObj._node = &node_34;
  3403.  
  3404. // copy data from upstream nodes into context
  3405.  
  3406. ctxObj._isInputDirty_IN1 = g_transaction.node_31_isOutputDirty_OUT;
  3407. ctxObj._isInputDirty_IN2 = g_transaction.node_19_isOutputDirty_BOOT;
  3408.  
  3409. // initialize temporary output dirtyness state in the context,
  3410. // where it can be modified from `raiseError` and `emitValue`
  3411. ctxObj._isOutputDirty_OUT = false;
  3412.  
  3413. xod__core__any::evaluate(&ctxObj);
  3414.  
  3415. // transfer possibly modified dirtiness state from context to g_transaction
  3416. g_transaction.node_34_isOutputDirty_OUT = ctxObj._isOutputDirty_OUT;
  3417.  
  3418. // mark downstream nodes dirty
  3419. g_transaction.node_37_isNodeDirty |= g_transaction.node_34_isOutputDirty_OUT;
  3420. }
  3421.  
  3422. }
  3423. { // xod__core__any #35
  3424.  
  3425. if (g_transaction.node_35_hasUpstreamError) {
  3426. g_transaction.node_38_hasUpstreamError = true;
  3427. } else if (g_transaction.node_35_isNodeDirty) {
  3428. XOD_TRACE_F("Eval node #");
  3429. XOD_TRACE_LN(35);
  3430.  
  3431. xod__core__any::ContextObject ctxObj;
  3432. ctxObj._node = &node_35;
  3433.  
  3434. // copy data from upstream nodes into context
  3435.  
  3436. ctxObj._isInputDirty_IN1 = g_transaction.node_32_isOutputDirty_OUT;
  3437. ctxObj._isInputDirty_IN2 = g_transaction.node_19_isOutputDirty_BOOT;
  3438.  
  3439. // initialize temporary output dirtyness state in the context,
  3440. // where it can be modified from `raiseError` and `emitValue`
  3441. ctxObj._isOutputDirty_OUT = false;
  3442.  
  3443. xod__core__any::evaluate(&ctxObj);
  3444.  
  3445. // transfer possibly modified dirtiness state from context to g_transaction
  3446. g_transaction.node_35_isOutputDirty_OUT = ctxObj._isOutputDirty_OUT;
  3447.  
  3448. // mark downstream nodes dirty
  3449. g_transaction.node_38_isNodeDirty |= g_transaction.node_35_isOutputDirty_OUT;
  3450. }
  3451.  
  3452. }
  3453. { // xod__core__any #36
  3454.  
  3455. if (g_transaction.node_36_hasUpstreamError) {
  3456. g_transaction.node_39_hasUpstreamError = true;
  3457. } else if (g_transaction.node_36_isNodeDirty) {
  3458. XOD_TRACE_F("Eval node #");
  3459. XOD_TRACE_LN(36);
  3460.  
  3461. xod__core__any::ContextObject ctxObj;
  3462. ctxObj._node = &node_36;
  3463.  
  3464. // copy data from upstream nodes into context
  3465.  
  3466. ctxObj._isInputDirty_IN1 = g_transaction.node_26_isOutputDirty_OUT;
  3467. ctxObj._isInputDirty_IN2 = g_transaction.node_33_isOutputDirty_OUT;
  3468.  
  3469. // initialize temporary output dirtyness state in the context,
  3470. // where it can be modified from `raiseError` and `emitValue`
  3471. ctxObj._isOutputDirty_OUT = false;
  3472.  
  3473. xod__core__any::evaluate(&ctxObj);
  3474.  
  3475. // transfer possibly modified dirtiness state from context to g_transaction
  3476. g_transaction.node_36_isOutputDirty_OUT = ctxObj._isOutputDirty_OUT;
  3477.  
  3478. // mark downstream nodes dirty
  3479. g_transaction.node_39_isNodeDirty |= g_transaction.node_36_isOutputDirty_OUT;
  3480. }
  3481.  
  3482. }
  3483. { // xod__core__any #37
  3484.  
  3485. if (g_transaction.node_37_hasUpstreamError) {
  3486. g_transaction.node_40_hasUpstreamError = true;
  3487. } else if (g_transaction.node_37_isNodeDirty) {
  3488. XOD_TRACE_F("Eval node #");
  3489. XOD_TRACE_LN(37);
  3490.  
  3491. xod__core__any::ContextObject ctxObj;
  3492. ctxObj._node = &node_37;
  3493.  
  3494. // copy data from upstream nodes into context
  3495.  
  3496. ctxObj._isInputDirty_IN1 = g_transaction.node_34_isOutputDirty_OUT;
  3497. ctxObj._isInputDirty_IN2 = g_transaction.node_22_isOutputDirty_OUT;
  3498.  
  3499. // initialize temporary output dirtyness state in the context,
  3500. // where it can be modified from `raiseError` and `emitValue`
  3501. ctxObj._isOutputDirty_OUT = false;
  3502.  
  3503. xod__core__any::evaluate(&ctxObj);
  3504.  
  3505. // transfer possibly modified dirtiness state from context to g_transaction
  3506. g_transaction.node_37_isOutputDirty_OUT = ctxObj._isOutputDirty_OUT;
  3507.  
  3508. // mark downstream nodes dirty
  3509. g_transaction.node_40_isNodeDirty |= g_transaction.node_37_isOutputDirty_OUT;
  3510. }
  3511.  
  3512. }
  3513. { // xod__core__any #38
  3514.  
  3515. if (g_transaction.node_38_hasUpstreamError) {
  3516. g_transaction.node_41_hasUpstreamError = true;
  3517. } else if (g_transaction.node_38_isNodeDirty) {
  3518. XOD_TRACE_F("Eval node #");
  3519. XOD_TRACE_LN(38);
  3520.  
  3521. xod__core__any::ContextObject ctxObj;
  3522. ctxObj._node = &node_38;
  3523.  
  3524. // copy data from upstream nodes into context
  3525.  
  3526. ctxObj._isInputDirty_IN1 = g_transaction.node_35_isOutputDirty_OUT;
  3527. ctxObj._isInputDirty_IN2 = g_transaction.node_23_isOutputDirty_OUT;
  3528.  
  3529. // initialize temporary output dirtyness state in the context,
  3530. // where it can be modified from `raiseError` and `emitValue`
  3531. ctxObj._isOutputDirty_OUT = false;
  3532.  
  3533. xod__core__any::evaluate(&ctxObj);
  3534.  
  3535. // transfer possibly modified dirtiness state from context to g_transaction
  3536. g_transaction.node_38_isOutputDirty_OUT = ctxObj._isOutputDirty_OUT;
  3537.  
  3538. // mark downstream nodes dirty
  3539. g_transaction.node_41_isNodeDirty |= g_transaction.node_38_isOutputDirty_OUT;
  3540. }
  3541.  
  3542. }
  3543. { // xod__core__gate__pulse #39
  3544.  
  3545. if (g_transaction.node_39_hasUpstreamError) {
  3546. g_transaction.node_42_hasUpstreamError = true;
  3547. } else if (g_transaction.node_39_isNodeDirty) {
  3548. XOD_TRACE_F("Eval node #");
  3549. XOD_TRACE_LN(39);
  3550.  
  3551. xod__core__gate__pulse::ContextObject ctxObj;
  3552. ctxObj._node = &node_39;
  3553.  
  3554. // copy data from upstream nodes into context
  3555. ctxObj._input_EN = node_30.output_OUT;
  3556.  
  3557. ctxObj._isInputDirty_IN = g_transaction.node_36_isOutputDirty_OUT;
  3558.  
  3559. // initialize temporary output dirtyness state in the context,
  3560. // where it can be modified from `raiseError` and `emitValue`
  3561. ctxObj._isOutputDirty_OUT = false;
  3562.  
  3563. xod__core__gate__pulse::evaluate(&ctxObj);
  3564.  
  3565. // transfer possibly modified dirtiness state from context to g_transaction
  3566. g_transaction.node_39_isOutputDirty_OUT = ctxObj._isOutputDirty_OUT;
  3567.  
  3568. // mark downstream nodes dirty
  3569. g_transaction.node_42_isNodeDirty |= g_transaction.node_39_isOutputDirty_OUT;
  3570. }
  3571.  
  3572. }
  3573. { // xod__core__gate__pulse #40
  3574.  
  3575. if (g_transaction.node_40_hasUpstreamError) {
  3576. g_transaction.node_43_hasUpstreamError = true;
  3577. } else if (g_transaction.node_40_isNodeDirty) {
  3578. XOD_TRACE_F("Eval node #");
  3579. XOD_TRACE_LN(40);
  3580.  
  3581. xod__core__gate__pulse::ContextObject ctxObj;
  3582. ctxObj._node = &node_40;
  3583.  
  3584. // copy data from upstream nodes into context
  3585. ctxObj._input_EN = node_4_output_VAL;
  3586.  
  3587. ctxObj._isInputDirty_IN = g_transaction.node_37_isOutputDirty_OUT;
  3588.  
  3589. // initialize temporary output dirtyness state in the context,
  3590. // where it can be modified from `raiseError` and `emitValue`
  3591. ctxObj._isOutputDirty_OUT = false;
  3592.  
  3593. xod__core__gate__pulse::evaluate(&ctxObj);
  3594.  
  3595. // transfer possibly modified dirtiness state from context to g_transaction
  3596. g_transaction.node_40_isOutputDirty_OUT = ctxObj._isOutputDirty_OUT;
  3597.  
  3598. // mark downstream nodes dirty
  3599. g_transaction.node_43_isNodeDirty |= g_transaction.node_40_isOutputDirty_OUT;
  3600. }
  3601.  
  3602. }
  3603. { // xod__core__gate__pulse #41
  3604.  
  3605. if (g_transaction.node_41_hasUpstreamError) {
  3606. g_transaction.node_44_hasUpstreamError = true;
  3607. } else if (g_transaction.node_41_isNodeDirty) {
  3608. XOD_TRACE_F("Eval node #");
  3609. XOD_TRACE_LN(41);
  3610.  
  3611. xod__core__gate__pulse::ContextObject ctxObj;
  3612. ctxObj._node = &node_41;
  3613.  
  3614. // copy data from upstream nodes into context
  3615. ctxObj._input_EN = node_6_output_VAL;
  3616.  
  3617. ctxObj._isInputDirty_IN = g_transaction.node_38_isOutputDirty_OUT;
  3618.  
  3619. // initialize temporary output dirtyness state in the context,
  3620. // where it can be modified from `raiseError` and `emitValue`
  3621. ctxObj._isOutputDirty_OUT = false;
  3622.  
  3623. xod__core__gate__pulse::evaluate(&ctxObj);
  3624.  
  3625. // transfer possibly modified dirtiness state from context to g_transaction
  3626. g_transaction.node_41_isOutputDirty_OUT = ctxObj._isOutputDirty_OUT;
  3627.  
  3628. // mark downstream nodes dirty
  3629. g_transaction.node_44_isNodeDirty |= g_transaction.node_41_isOutputDirty_OUT;
  3630. }
  3631.  
  3632. }
  3633. { // xod_dev__servo__rotate #42
  3634.  
  3635. if (g_transaction.node_42_hasUpstreamError) {
  3636. } else if (g_transaction.node_42_isNodeDirty) {
  3637. XOD_TRACE_F("Eval node #");
  3638. XOD_TRACE_LN(42);
  3639.  
  3640. xod_dev__servo__rotate::ContextObject ctxObj;
  3641. ctxObj._node = &node_42;
  3642.  
  3643. // copy data from upstream nodes into context
  3644. ctxObj._input_VAL = node_0_output_VAL;
  3645. ctxObj._input_DEV = node_21.output_DEV;
  3646.  
  3647. ctxObj._isInputDirty_DO = g_transaction.node_39_isOutputDirty_OUT;
  3648.  
  3649. // initialize temporary output dirtyness state in the context,
  3650. // where it can be modified from `raiseError` and `emitValue`
  3651. ctxObj._isOutputDirty_ACK = false;
  3652. ctxObj._isOutputDirty_DEVU0027 = false;
  3653.  
  3654. xod_dev__servo__rotate::evaluate(&ctxObj);
  3655.  
  3656. // transfer possibly modified dirtiness state from context to g_transaction
  3657.  
  3658. // mark downstream nodes dirty
  3659. }
  3660.  
  3661. }
  3662. { // xod__gpio__digital_write #43
  3663.  
  3664. if (g_transaction.node_43_hasUpstreamError) {
  3665. } else if (g_transaction.node_43_isNodeDirty) {
  3666. XOD_TRACE_F("Eval node #");
  3667. XOD_TRACE_LN(43);
  3668.  
  3669. xod__gpio__digital_write::ContextObject ctxObj;
  3670. ctxObj._node = &node_43;
  3671.  
  3672. // copy data from upstream nodes into context
  3673. ctxObj._input_PORT = node_5_output_VAL;
  3674. ctxObj._input_SIG = node_29.output_OUT;
  3675.  
  3676. ctxObj._isInputDirty_UPD = g_transaction.node_40_isOutputDirty_OUT;
  3677.  
  3678. // initialize temporary output dirtyness state in the context,
  3679. // where it can be modified from `raiseError` and `emitValue`
  3680. ctxObj._isOutputDirty_DONE = false;
  3681.  
  3682. xod__gpio__digital_write::NodeErrors previousErrors = node_43.errors;
  3683.  
  3684. node_43.errors.output_DONE = false;
  3685.  
  3686. xod__gpio__digital_write::evaluate(&ctxObj);
  3687.  
  3688. // transfer possibly modified dirtiness state from context to g_transaction
  3689.  
  3690. if (previousErrors.flags != node_43.errors.flags) {
  3691. detail::printErrorToDebugSerial(43, node_43.errors.flags);
  3692.  
  3693. // if an error was just raised or cleared from an output,
  3694. // mark nearest downstream error catchers as dirty
  3695. if (node_43.errors.output_DONE != previousErrors.output_DONE) {
  3696. }
  3697.  
  3698. // if a pulse output was cleared from error, mark downstream nodes as dirty
  3699. // (no matter if a pulse was emitted or not)
  3700. if (previousErrors.output_DONE && !node_43.errors.output_DONE) {
  3701. }
  3702. }
  3703.  
  3704. // mark downstream nodes dirty
  3705. }
  3706.  
  3707. // propagate errors hold by the node outputs
  3708. if (node_43.errors.flags) {
  3709. if (node_43.errors.output_DONE) {
  3710. }
  3711. }
  3712. }
  3713. { // xod__gpio__digital_write #44
  3714.  
  3715. if (g_transaction.node_44_hasUpstreamError) {
  3716. } else if (g_transaction.node_44_isNodeDirty) {
  3717. XOD_TRACE_F("Eval node #");
  3718. XOD_TRACE_LN(44);
  3719.  
  3720. xod__gpio__digital_write::ContextObject ctxObj;
  3721. ctxObj._node = &node_44;
  3722.  
  3723. // copy data from upstream nodes into context
  3724. ctxObj._input_PORT = node_7_output_VAL;
  3725. ctxObj._input_SIG = node_30.output_OUT;
  3726.  
  3727. ctxObj._isInputDirty_UPD = g_transaction.node_41_isOutputDirty_OUT;
  3728.  
  3729. // initialize temporary output dirtyness state in the context,
  3730. // where it can be modified from `raiseError` and `emitValue`
  3731. ctxObj._isOutputDirty_DONE = false;
  3732.  
  3733. xod__gpio__digital_write::NodeErrors previousErrors = node_44.errors;
  3734.  
  3735. node_44.errors.output_DONE = false;
  3736.  
  3737. xod__gpio__digital_write::evaluate(&ctxObj);
  3738.  
  3739. // transfer possibly modified dirtiness state from context to g_transaction
  3740.  
  3741. if (previousErrors.flags != node_44.errors.flags) {
  3742. detail::printErrorToDebugSerial(44, node_44.errors.flags);
  3743.  
  3744. // if an error was just raised or cleared from an output,
  3745. // mark nearest downstream error catchers as dirty
  3746. if (node_44.errors.output_DONE != previousErrors.output_DONE) {
  3747. }
  3748.  
  3749. // if a pulse output was cleared from error, mark downstream nodes as dirty
  3750. // (no matter if a pulse was emitted or not)
  3751. if (previousErrors.output_DONE && !node_44.errors.output_DONE) {
  3752. }
  3753. }
  3754.  
  3755. // mark downstream nodes dirty
  3756. }
  3757.  
  3758. // propagate errors hold by the node outputs
  3759. if (node_44.errors.flags) {
  3760. if (node_44.errors.output_DONE) {
  3761. }
  3762. }
  3763. }
  3764.  
  3765. // Clear dirtieness and timeouts for all nodes and pins
  3766. memset(&g_transaction, 0, sizeof(g_transaction));
  3767.  
  3768. detail::clearStaleTimeout(&node_18);
  3769. detail::clearStaleTimeout(&node_29);
  3770.  
  3771. XOD_TRACE_F("Transaction completed, t=");
  3772. XOD_TRACE_LN(millis());
  3773. }
  3774.  
  3775. } // namespace xod
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement