Skip to content

Commit 70e206f

Browse files
authored
Supports Full-duplex Serial Communication within a Single Arduino Thread (#2)
* Matched Python APIs. (#319) * Matched Python APIs. (#319) * Matched Python APIs: `Device` is not abstract. (#319) * Improved `ArrayList`. (#319) * Added default methods for involvement of `ArrayList`. (#319) * Improved `ArrayList`. (#319) * Renamed `initializeTop()` to `initializeAsRoot()`. (#319) * Code reformatted. (#319) * Matched Python APIs: removed parent tags setter. (#319) * Removed unused utils. (#319) * Added `Peer`. (#319) * Updated keywords.txt. (#319)
1 parent f64f4e3 commit 70e206f

File tree

9 files changed

+97
-42
lines changed

9 files changed

+97
-42
lines changed

keywords.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ Controller KEYWORD1
33
Device KEYWORD1
44
VoltageSensor KEYWORD1
55
WheelSpeedSensor KEYWORD1
6+
Peer KEYWORD1
67
pulseTriggered KEYWORD2
7-
equivalent KEYWORD2
88
returnFloat KEYWORD2
99
returnDouble KEYWORD2
1010
LEFT_FRONT_WHEEL_SPEED_SENSOR LITERAL1

src/ArrayList.h

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,46 +39,64 @@ class ArrayList {
3939
~ArrayList() { delete[] _array; }
4040
int size() { return _size; }
4141
E const *toArray() { return _array; }
42-
void set(int index, E element) { _array[index] = element; }
42+
void set(int index, const E &element) { _array[index] = element; }
4343
E get(int index) { return _array[index]; }
4444
void clear() {
4545
delete[] _array;
4646
_array = new E[0];
4747
_size = _capacity = 0;
4848
}
49-
void add(E element) {
49+
void add(const E &element) {
5050
ensureCapacityInternal(_size + 1);
5151
_array[_size++] = element;
5252
}
5353
ArrayList<E> add(const ArrayList<E> &other) {
5454
ArrayList<E> result = copy();
55-
result.add(other._array, other._size);
55+
result += other;
5656
return result;
5757
}
58-
bool contains(E element) { return indexOf(element) >= 0; }
59-
int indexOfInRange(E element, int start, int stop) {
58+
bool contains(const E &element) { return indexOf(element) >= 0; }
59+
int indexOfInRange(const E &element, int start, int stop) {
6060
for (int i = start; i < stop; i++)
6161
if (_array[i] == element)
6262
return i;
6363
return -1;
6464
}
65-
int indexOf(E element) { return indexOfInRange(element, 0, _size); }
66-
int lastIndexOfInRange(E element, int start, int stop) {
65+
int indexOf(const E &element) { return indexOfInRange(element, 0, _size); }
66+
int lastIndexOfInRange(const E &element, int start, int stop) {
6767
for (int i = stop - 1; i >= start; i--)
6868
if (_array[i] == element)
6969
return i;
7070
return -1;
7171
}
72-
int lastIndexOf(E element) { return lastIndexOfInRange(element, 0, _size); }
73-
ArrayList<E> copy() { return ArrayList<E>(this); }
72+
int lastIndexOf(const E &element) { return lastIndexOfInRange(element, 0, _size); }
73+
ArrayList<E> copy() { return ArrayList<E>(reinterpret_cast<size_t>(this)); }
74+
ArrayList<E> operator+(const E &element) {
75+
ArrayList<E> result = copy();
76+
result += element;
77+
return result;
78+
}
7479
ArrayList<E> operator+(const ArrayList<E> &other) { return add(other); }
80+
ArrayList<E> &operator+=(const E &element) {
81+
add(element);
82+
return *this;
83+
}
84+
ArrayList<E> &operator+=(const ArrayList<E> &other) {
85+
add(other._array, other._size);
86+
return *this;
87+
}
7588
ArrayList<E> &operator=(const ArrayList<E> &other) {
7689
if (this != &other) {
7790
ensureCapacityInternal(other._capacity);
78-
memcpy(_array, other._array, _size * sizeof(E));
91+
if (other._size > 0)
92+
memcpy(_array, other._array, _size * sizeof(E));
7993
}
8094
return *this;
8195
}
96+
E *begin() { return _array; }
97+
E *end() { return _array + _size; }
98+
const E *begin() const { return _array; }
99+
const E *end() const { return _array + _size; }
82100
};
83101

84102

src/Controller.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Controller : public Device<T> {
99
protected:
1010
ArrayList<String> _device_tags;
1111
ArrayList<Device<E>> _devices;
12-
void _attachDevice(String &tag, Device<E> device) {
12+
void _attachDevice(const String &tag, Device<E> device) {
1313
_device_tags.add(tag);
1414
_devices.add(device);
1515
device.tag(tag);
@@ -18,15 +18,13 @@ class Controller : public Device<T> {
1818
public:
1919
Controller() : Device<T>() {}
2020
int level() { return this->_parentTags.size(); }
21-
void device(const String &tag, Device<E> device) { _attachDevice(tag, device); }
21+
const ArrayList<Device<E>> &devices() { return _devices; }
22+
void device(const String &tag, const Device<E> &device) { _attachDevice(tag, device); }
2223
Device<E> device(const String &tag) { return _devices.get(_device_tags.indexOf(tag)); }
23-
virtual void initialize(const ArrayList<String> &parentTags) {
24+
void initialize(const ArrayList<String> &parentTags) override {
2425
Device<E>::initialize(parentTags);
25-
for (Device<E> d: _devices) {
26-
ArrayList<String> deviceParentTags = this->_parentTags.copy();
27-
deviceParentTags.add(this->_tag);
28-
d.initialize(deviceParentTags);
29-
}
26+
for (Device<E> d: _devices)
27+
d.initialize(this->_parentTags + this->_tag);
3028
}
3129
};
3230

src/Device.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@ template<typename T>
88
class Device {
99
protected:
1010
String _tag = "";
11-
ArrayList<String> _parentTags = ArrayList<String>();
11+
ArrayList<String> _parentTags = ArrayList<String>(0);
1212
ArrayList<int> _pins;
1313

1414
public:
1515
explicit Device(const ArrayList<int> &pins) : _pins(pins) {}
16+
Device() : Device(ArrayList<int>(0)) {}
1617
~Device() = default;
1718
void tag(const String &tag) { _tag = tag; }
1819
String tag() { return _tag; }
19-
void parentTags(const ArrayList<String> &parentTags) { _parentTags = parentTags; }
2020
const ArrayList<String> &parentTags() { return _parentTags; }
2121
virtual void initialize(const ArrayList<String> &parentTags) { _parentTags = parentTags; }
22-
virtual T read() = 0;
22+
void initializeAsRoot() { initialize(ArrayList<String>(0)); }
23+
virtual T read() { return T(); }
2324
virtual void write(T payload) {}
25+
virtual void update(T data) {}
2426
virtual void close() {}
2527
};
2628

src/LEADS.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Controller.h"
22
#include "Device.h"
3+
#include "Peer.h"
34
#include "PredefinedTags.h"
45
#include "Utils.h"
56
#include "VoltageSensor.h"

src/Peer.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "Peer.h"
2+
3+
4+
Peer::Peer(unsigned int baudRate, String separator, String remainder) :
5+
Controller<String, String>(), _baudRate(baudRate), _separator(separator), _remainder(remainder) {}
6+
void Peer::initialize(const ArrayList<String> &parentTags) {
7+
Controller<String, String>::initialize(parentTags);
8+
Serial.begin(_baudRate);
9+
}
10+
String Peer::read() {
11+
char c = Serial.read();
12+
if (c > 0) {
13+
_remainder += c;
14+
if (!_remainder.endsWith(_separator))
15+
return "";
16+
String result = _remainder.substring(0, _remainder.length() - _separator.length());
17+
_remainder = "";
18+
return result;
19+
}
20+
return "";
21+
}
22+
void Peer::write(String payload) { Serial.print(payload + _separator); }
23+
void Peer::refresh() {
24+
String msg = read();
25+
if (msg == "")
26+
return;
27+
for (Device<String> d: _devices)
28+
d.update(msg);
29+
}

src/Peer.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef PEER_H
2+
#define PEER_H
3+
4+
5+
#include "Controller.h"
6+
7+
// although we try to keep a consistent format, the C++ version does not support multithreading
8+
class Peer : public Controller<String, String> {
9+
protected:
10+
unsigned int _baudRate;
11+
String _separator, _remainder;
12+
13+
public:
14+
explicit Peer(unsigned int baudRate = 9600, String separator = ";", String remainder = "");
15+
void initialize(const ArrayList<String> &parentTags) override;
16+
String read() override;
17+
void write(String payload) override;
18+
void refresh();
19+
};
20+
21+
22+
#endif // PEER_H

src/Utils.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,6 @@
22

33
bool pulseTriggered(int pin) { return digitalRead(pin) == LOW; }
44

5-
bool equivalent(float a, float b, float epsilon) { return abs(a - b) <= epsilon * max(abs(a), abs(b)); }
5+
void returnFloat(Peer peer, const String &tag, float n) { peer.write(tag + ":" + n); }
66

7-
bool equivalent(long a, long b, float epsilon) { return labs(a - b) <= epsilon * max(labs(a), labs(b)); }
8-
9-
void returnFloat(const String &tag, float n) {
10-
Serial.print(tag + ":");
11-
Serial.print(n);
12-
Serial.print(";");
13-
}
14-
15-
void returnDouble(const String &tag, double n) {
16-
Serial.print(tag + ":");
17-
Serial.print(n);
18-
Serial.print(";");
19-
}
7+
void returnDouble(Peer peer, const String &tag, double n) { peer.write(tag + ":" + n); }

src/Utils.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33

44

55
#include "Arduino.h"
6+
#include "Peer.h"
67

78
bool pulseTriggered(int pin);
89

9-
bool equivalent(long a, long b, float epsilon);
10+
void returnFloat(Peer peer, const String &tag, float n);
1011

11-
bool equivalent(float a, float b, float epsilon);
12-
13-
void returnFloat(const String &tag, float n);
14-
15-
void returnDouble(const String &tag, double n);
12+
void returnDouble(Peer peer, const String &tag, double n);
1613

1714

1815
#endif // UTILS_H

0 commit comments

Comments
 (0)