Skip to content

Commit f64f4e3

Browse files
authored
Introduce Modern C++ to the Arduino Framework (#1)
* Integrated with PlatformIO. (#320) * Integrated with PlatformIO. (#320) * Refactored ArrayList.h. (#320) * Refactored ArrayList.h. (#320) * Added ArrayList test. (#320) * Refactored ArrayList.h. (#320) * Merged Controller.cpp and Controller.h. (#320) * Replaced all arrays with `ArrayList`s. (#320) * Added `returnDouble()`. (#320) * Code reformatted. (#320) * Bug fixed. (#320) * Code reformatted. (#320) * Bug fixed: All calls of function 'ensureCapacityInternal' are unreachable. (#320) * Created clang-tidy.yml. (#320)
1 parent cae1666 commit f64f4e3

File tree

16 files changed

+166
-98
lines changed

16 files changed

+166
-98
lines changed

.github/workflows/clang-tidy.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
on:
2+
pull_request:
3+
types:
4+
- opened
5+
- review_requested
6+
7+
permissions:
8+
contents: read
9+
10+
jobs:
11+
deploy:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- uses: actions/checkout@v4
16+
- uses: cpp-linter/cpp-linter-action@v2
17+
id: linter
18+
env:
19+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
20+
with:
21+
style: 'file' # Use .clang-format config file
22+
tidy-checks: '' # Use .clang-tidy config file
23+
# only 'update' a single comment in a pull request's thread.
24+
thread-comments: ${{ github.event_name == 'pull_request' && 'update' }}
25+
- name: Fail fast?!
26+
if: steps.linter.outputs.checks-failed > 0
27+
run: exit 1

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.idea
2+
.pio
23
cmake-build-debug
4+
main.cpp

CMakeLists.txt

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,2 @@
11
cmake_minimum_required(VERSION 3.28)
2-
project(LEADS_Arduino)
3-
4-
set(CMAKE_CXX_STANDARD 14)
5-
6-
include_directories(src)
7-
8-
add_executable(LEADS_Arduino
9-
src/ArrayList.h
10-
src/Controller.cpp
11-
src/Controller.h
12-
src/Device.h
13-
src/LEADS.h
14-
src/PredefinedTags.h
15-
src/Utils.cpp
16-
src/Utils.h
17-
src/VoltageSensor.cpp
18-
src/VoltageSensor.h
19-
src/WheelSpeedSensor.cpp
20-
src/WheelSpeedSensor.h)
2+
project(LEADS-Arduino C CXX)

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ WheelSpeedSensor KEYWORD1
66
pulseTriggered KEYWORD2
77
equivalent KEYWORD2
88
returnFloat KEYWORD2
9+
returnDouble KEYWORD2
910
LEFT_FRONT_WHEEL_SPEED_SENSOR LITERAL1
1011
RIGHT_FRONT_WHEEL_SPEED_SENSOR LITERAL1
1112
CENTER_REAR_WHEEL_SPEED_SENSOR LITERAL1

platformio.ini

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter
4+
; Upload options: custom upload port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
; Advanced options: extra scripting
7+
;
8+
; Please visit documentation for the other options and examples
9+
; https://docs.platformio.org/page/projectconf.html
10+
11+
[env:micro]
12+
platform = atmelavr
13+
board = nanoatmega328new
14+
framework = arduino

src/ArrayList.h

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,55 @@
66

77
template<typename E>
88
class ArrayList {
9-
private:
10-
int _size;
9+
protected:
10+
size_t _size, _capacity;
1111
E *_array;
1212
void grow(size_t minCapacity) {
13-
int oldCapacity = _array.length;
14-
int newCapacity = oldCapacity + (oldCapacity >> 1);
13+
size_t newCapacity = _capacity + (_capacity >> 1);
1514
if (newCapacity < minCapacity)
1615
newCapacity = minCapacity;
17-
E newArray = new E[newCapacity];
18-
copy(begin(_array), end(_array), begin(newArray));
16+
E *newArray = new E[newCapacity];
17+
memmove(newArray, _array, _size * sizeof(E));
18+
delete[] _array;
1919
_array = newArray;
20+
_capacity = newCapacity;
2021
}
2122
void ensureExplicitCapacity(size_t minCapacity) {
22-
if (minCapacity > _array.length)
23+
if (minCapacity > _capacity)
2324
grow(minCapacity);
2425
}
25-
void ensureCapacityInternal(size_t minCapacity) {
26-
if (_array.length == 0)
27-
minCapacity = max(10, minCapacity);
28-
ensureExplicitCapacity(minCapacity);
26+
void ensureCapacityInternal(size_t minCapacity) { ensureExplicitCapacity(max(10, minCapacity)); }
27+
void add(E *elements, size_t size) {
28+
ensureCapacityInternal(_size + size);
29+
memcpy(_array + _size, elements, size * sizeof(E));
2930
}
3031

3132
public:
32-
ArrayList(size_t initialCapacity = 10) : _size(0), _array(new E[initialCapacity]) {}
33-
ArrayList(E *const initialArray) : _size(0), _array(initialArray) {}
33+
explicit ArrayList(size_t initialCapacity = 10) :
34+
_size(0), _capacity(initialCapacity), _array(new E[initialCapacity]) {}
35+
ArrayList(E const *const initialArray, size_t size) : _size(0), _capacity(size), _array(new E[size]) {
36+
memcpy(_array, initialArray, size * sizeof(E));
37+
}
38+
ArrayList(const ArrayList<E> &other) : ArrayList(other._array, other._size) {}
3439
~ArrayList() { delete[] _array; }
3540
int size() { return _size; }
36-
E *const &toArray() { return _array; }
41+
E const *toArray() { return _array; }
42+
void set(int index, E element) { _array[index] = element; }
3743
E get(int index) { return _array[index]; }
44+
void clear() {
45+
delete[] _array;
46+
_array = new E[0];
47+
_size = _capacity = 0;
48+
}
3849
void add(E element) {
3950
ensureCapacityInternal(_size + 1);
4051
_array[_size++] = element;
4152
}
42-
bool insert(int index, E element);
53+
ArrayList<E> add(const ArrayList<E> &other) {
54+
ArrayList<E> result = copy();
55+
result.add(other._array, other._size);
56+
return result;
57+
}
4358
bool contains(E element) { return indexOf(element) >= 0; }
4459
int indexOfInRange(E element, int start, int stop) {
4560
for (int i = start; i < stop; i++)
@@ -55,6 +70,15 @@ class ArrayList {
5570
return -1;
5671
}
5772
int lastIndexOf(E element) { return lastIndexOfInRange(element, 0, _size); }
73+
ArrayList<E> copy() { return ArrayList<E>(this); }
74+
ArrayList<E> operator+(const ArrayList<E> &other) { return add(other); }
75+
ArrayList<E> &operator=(const ArrayList<E> &other) {
76+
if (this != &other) {
77+
ensureCapacityInternal(other._capacity);
78+
memcpy(_array, other._array, _size * sizeof(E));
79+
}
80+
return *this;
81+
}
5882
};
5983

6084

src/Controller.cpp

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/Controller.h

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,30 @@
44

55
#include "Device.h"
66

7-
template<typename T>
7+
template<typename T, typename E>
88
class Controller : public Device<T> {
99
protected:
10-
ArrayList<Device<String>> _devices;
11-
void _attachDevice(String tag, Device<String> device);
10+
ArrayList<String> _device_tags;
11+
ArrayList<Device<E>> _devices;
12+
void _attachDevice(String &tag, Device<E> device) {
13+
_device_tags.add(tag);
14+
_devices.add(device);
15+
device.tag(tag);
16+
}
1217

1318
public:
14-
Controller();
15-
int level();
16-
void device(String tag, Device<String> device);
17-
Device<String> device(String tag);
19+
Controller() : Device<T>() {}
20+
int level() { return this->_parentTags.size(); }
21+
void device(const String &tag, Device<E> device) { _attachDevice(tag, device); }
22+
Device<E> device(const String &tag) { return _devices.get(_device_tags.indexOf(tag)); }
23+
virtual void initialize(const ArrayList<String> &parentTags) {
24+
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+
}
30+
}
1831
};
1932

2033

src/Device.h

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,20 @@ template<typename T>
88
class Device {
99
protected:
1010
String _tag = "";
11-
ArrayList<String> _parentTags();
12-
int *const _pins;
11+
ArrayList<String> _parentTags = ArrayList<String>();
12+
ArrayList<int> _pins;
1313

1414
public:
15-
Device(int *const pins) : _pins(pins) {}
16-
~Device() { delete[] _pins; }
17-
void tag(String tag) { _tag = tag; }
15+
explicit Device(const ArrayList<int> &pins) : _pins(pins) {}
16+
~Device() = default;
17+
void tag(const String &tag) { _tag = tag; }
1818
String tag() { return _tag; }
19-
void parentTags(ArrayList<String> parentTags) { _parentTags = parentTags; }
20-
ArrayList<String> parentTags() { return _parentTags; }
21-
void pinsCheck(int requiredNum) {
22-
if (sizeof(_pins) != requiredNum)
23-
throw value_error(format("This device only takes in {} pins", requiredNum));
24-
}
25-
void initialize(String *parentTags);
26-
T read();
27-
void write(T payload);
28-
void close();
19+
void parentTags(const ArrayList<String> &parentTags) { _parentTags = parentTags; }
20+
const ArrayList<String> &parentTags() { return _parentTags; }
21+
virtual void initialize(const ArrayList<String> &parentTags) { _parentTags = parentTags; }
22+
virtual T read() = 0;
23+
virtual void write(T payload) {}
24+
virtual void close() {}
2925
};
3026

3127

src/Utils.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

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

5-
bool equivalent(float a, float b, float epsilon) { return fabs(a - b) <= epsilon * max(fabs(a), fabs(b)); }
5+
bool equivalent(float a, float b, float epsilon) { return abs(a - b) <= epsilon * max(abs(a), abs(b)); }
66

7-
bool equivalent(long a, long b, float epsilon) { return fabs(a - b) <= epsilon * max(fabs(a), fabs(b)); }
7+
bool equivalent(long a, long b, float epsilon) { return labs(a - b) <= epsilon * max(labs(a), labs(b)); }
88

9-
void returnFloat(String header, float n) {
10-
Serial.print(header + ":");
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 + ":");
1117
Serial.print(n);
1218
Serial.print(";");
1319
}

0 commit comments

Comments
 (0)