diff --git a/keywords.txt b/keywords.txt index 4117a78..4750cb6 100644 --- a/keywords.txt +++ b/keywords.txt @@ -3,8 +3,8 @@ Controller KEYWORD1 Device KEYWORD1 VoltageSensor KEYWORD1 WheelSpeedSensor KEYWORD1 +Peer KEYWORD1 pulseTriggered KEYWORD2 -equivalent KEYWORD2 returnFloat KEYWORD2 returnDouble KEYWORD2 LEFT_FRONT_WHEEL_SPEED_SENSOR LITERAL1 diff --git a/src/ArrayList.h b/src/ArrayList.h index 6fdc294..473e7c2 100644 --- a/src/ArrayList.h +++ b/src/ArrayList.h @@ -39,46 +39,64 @@ class ArrayList { ~ArrayList() { delete[] _array; } int size() { return _size; } E const *toArray() { return _array; } - void set(int index, E element) { _array[index] = element; } + void set(int index, const E &element) { _array[index] = element; } E get(int index) { return _array[index]; } void clear() { delete[] _array; _array = new E[0]; _size = _capacity = 0; } - void add(E element) { + void add(const E &element) { ensureCapacityInternal(_size + 1); _array[_size++] = element; } ArrayList add(const ArrayList &other) { ArrayList result = copy(); - result.add(other._array, other._size); + result += other; return result; } - bool contains(E element) { return indexOf(element) >= 0; } - int indexOfInRange(E element, int start, int stop) { + bool contains(const E &element) { return indexOf(element) >= 0; } + int indexOfInRange(const E &element, int start, int stop) { for (int i = start; i < stop; i++) if (_array[i] == element) return i; return -1; } - int indexOf(E element) { return indexOfInRange(element, 0, _size); } - int lastIndexOfInRange(E element, int start, int stop) { + int indexOf(const E &element) { return indexOfInRange(element, 0, _size); } + int lastIndexOfInRange(const E &element, int start, int stop) { for (int i = stop - 1; i >= start; i--) if (_array[i] == element) return i; return -1; } - int lastIndexOf(E element) { return lastIndexOfInRange(element, 0, _size); } - ArrayList copy() { return ArrayList(this); } + int lastIndexOf(const E &element) { return lastIndexOfInRange(element, 0, _size); } + ArrayList copy() { return ArrayList(reinterpret_cast(this)); } + ArrayList operator+(const E &element) { + ArrayList result = copy(); + result += element; + return result; + } ArrayList operator+(const ArrayList &other) { return add(other); } + ArrayList &operator+=(const E &element) { + add(element); + return *this; + } + ArrayList &operator+=(const ArrayList &other) { + add(other._array, other._size); + return *this; + } ArrayList &operator=(const ArrayList &other) { if (this != &other) { ensureCapacityInternal(other._capacity); - memcpy(_array, other._array, _size * sizeof(E)); + if (other._size > 0) + memcpy(_array, other._array, _size * sizeof(E)); } return *this; } + E *begin() { return _array; } + E *end() { return _array + _size; } + const E *begin() const { return _array; } + const E *end() const { return _array + _size; } }; diff --git a/src/Controller.h b/src/Controller.h index b76b214..905bb78 100644 --- a/src/Controller.h +++ b/src/Controller.h @@ -9,7 +9,7 @@ class Controller : public Device { protected: ArrayList _device_tags; ArrayList> _devices; - void _attachDevice(String &tag, Device device) { + void _attachDevice(const String &tag, Device device) { _device_tags.add(tag); _devices.add(device); device.tag(tag); @@ -18,15 +18,13 @@ class Controller : public Device { public: Controller() : Device() {} int level() { return this->_parentTags.size(); } - void device(const String &tag, Device device) { _attachDevice(tag, device); } + const ArrayList> &devices() { return _devices; } + void device(const String &tag, const Device &device) { _attachDevice(tag, device); } Device device(const String &tag) { return _devices.get(_device_tags.indexOf(tag)); } - virtual void initialize(const ArrayList &parentTags) { + void initialize(const ArrayList &parentTags) override { Device::initialize(parentTags); - for (Device d: _devices) { - ArrayList deviceParentTags = this->_parentTags.copy(); - deviceParentTags.add(this->_tag); - d.initialize(deviceParentTags); - } + for (Device d: _devices) + d.initialize(this->_parentTags + this->_tag); } }; diff --git a/src/Device.h b/src/Device.h index f195dc5..ec47040 100644 --- a/src/Device.h +++ b/src/Device.h @@ -8,19 +8,21 @@ template class Device { protected: String _tag = ""; - ArrayList _parentTags = ArrayList(); + ArrayList _parentTags = ArrayList(0); ArrayList _pins; public: explicit Device(const ArrayList &pins) : _pins(pins) {} + Device() : Device(ArrayList(0)) {} ~Device() = default; void tag(const String &tag) { _tag = tag; } String tag() { return _tag; } - void parentTags(const ArrayList &parentTags) { _parentTags = parentTags; } const ArrayList &parentTags() { return _parentTags; } virtual void initialize(const ArrayList &parentTags) { _parentTags = parentTags; } - virtual T read() = 0; + void initializeAsRoot() { initialize(ArrayList(0)); } + virtual T read() { return T(); } virtual void write(T payload) {} + virtual void update(T data) {} virtual void close() {} }; diff --git a/src/LEADS.h b/src/LEADS.h index ab7a810..53b2951 100644 --- a/src/LEADS.h +++ b/src/LEADS.h @@ -1,5 +1,6 @@ #include "Controller.h" #include "Device.h" +#include "Peer.h" #include "PredefinedTags.h" #include "Utils.h" #include "VoltageSensor.h" diff --git a/src/Peer.cpp b/src/Peer.cpp new file mode 100644 index 0000000..dd3743a --- /dev/null +++ b/src/Peer.cpp @@ -0,0 +1,29 @@ +#include "Peer.h" + + +Peer::Peer(unsigned int baudRate, String separator, String remainder) : + Controller(), _baudRate(baudRate), _separator(separator), _remainder(remainder) {} +void Peer::initialize(const ArrayList &parentTags) { + Controller::initialize(parentTags); + Serial.begin(_baudRate); +} +String Peer::read() { + char c = Serial.read(); + if (c > 0) { + _remainder += c; + if (!_remainder.endsWith(_separator)) + return ""; + String result = _remainder.substring(0, _remainder.length() - _separator.length()); + _remainder = ""; + return result; + } + return ""; +} +void Peer::write(String payload) { Serial.print(payload + _separator); } +void Peer::refresh() { + String msg = read(); + if (msg == "") + return; + for (Device d: _devices) + d.update(msg); +} diff --git a/src/Peer.h b/src/Peer.h new file mode 100644 index 0000000..8a0554b --- /dev/null +++ b/src/Peer.h @@ -0,0 +1,22 @@ +#ifndef PEER_H +#define PEER_H + + +#include "Controller.h" + +// although we try to keep a consistent format, the C++ version does not support multithreading +class Peer : public Controller { +protected: + unsigned int _baudRate; + String _separator, _remainder; + +public: + explicit Peer(unsigned int baudRate = 9600, String separator = ";", String remainder = ""); + void initialize(const ArrayList &parentTags) override; + String read() override; + void write(String payload) override; + void refresh(); +}; + + +#endif // PEER_H diff --git a/src/Utils.cpp b/src/Utils.cpp index b814633..f558f4e 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -2,18 +2,6 @@ bool pulseTriggered(int pin) { return digitalRead(pin) == LOW; } -bool equivalent(float a, float b, float epsilon) { return abs(a - b) <= epsilon * max(abs(a), abs(b)); } +void returnFloat(Peer peer, const String &tag, float n) { peer.write(tag + ":" + n); } -bool equivalent(long a, long b, float epsilon) { return labs(a - b) <= epsilon * max(labs(a), labs(b)); } - -void returnFloat(const String &tag, float n) { - Serial.print(tag + ":"); - Serial.print(n); - Serial.print(";"); -} - -void returnDouble(const String &tag, double n) { - Serial.print(tag + ":"); - Serial.print(n); - Serial.print(";"); -} +void returnDouble(Peer peer, const String &tag, double n) { peer.write(tag + ":" + n); } diff --git a/src/Utils.h b/src/Utils.h index 6f718d4..826439f 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -3,16 +3,13 @@ #include "Arduino.h" +#include "Peer.h" bool pulseTriggered(int pin); -bool equivalent(long a, long b, float epsilon); +void returnFloat(Peer peer, const String &tag, float n); -bool equivalent(float a, float b, float epsilon); - -void returnFloat(const String &tag, float n); - -void returnDouble(const String &tag, double n); +void returnDouble(Peer peer, const String &tag, double n); #endif // UTILS_H