Skip to content

Commit 2021024

Browse files
committed
use homemade queue to avoid problems with stdlib
1 parent 957db19 commit 2021024

File tree

4 files changed

+96
-27
lines changed

4 files changed

+96
-27
lines changed

cpp/arduino/AvrMath.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,18 @@
1111
#define radians(deg) ((deg)*DEG_TO_RAD)
1212
#define degrees(rad) ((rad)*RAD_TO_DEG)
1313

14+
#ifdef abs
15+
#undef abs
16+
#endif
17+
#define abs(x) ((x)>0?(x):-(x))
18+
19+
#ifdef max
20+
#undef max
21+
#endif
22+
#define max(a,b) ((a)>(b)?(a):(b))
23+
24+
#ifdef min
25+
#undef min
26+
#endif
27+
#define min(a,b) ((a)<(b)?(a):(b))
1428

cpp/arduino/Forced.h

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

cpp/arduino/Godmode.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
#include <avr/io.h>
44
#include "WString.h"
55

6-
#include <queue>
7-
#include "Forced.h"
6+
#include "queue.h"
87

98
// random
109
void randomSeed(unsigned long seed);
@@ -22,12 +21,12 @@ unsigned long micros();
2221
template <typename T>
2322
class PinHistory {
2423
private:
25-
std::queue<T> qIn;
26-
std::queue<T> qOut;
24+
queue<T> qIn;
25+
queue<T> qOut;
2726

2827
unsigned long mLastRead = 0;
2928

30-
void clear(std::queue<T>* q) {
29+
void clear(queue<T>* q) {
3130
while (!q->empty()) q->pop();
3231
}
3332

@@ -53,9 +52,9 @@ class PinHistory {
5352
}
5453

5554
// this sets the value of the pin authoritatively
56-
// so if there was a queue dump it.
55+
// so if there was a queue, dump it.
5756
// the actual "set" operation doesn't happen until the next read
58-
T &operator=(T const i) {
57+
const T &operator=(const T& i) {
5958
qOut.push(i);
6059
return qOut.back();
6160
}
@@ -96,7 +95,7 @@ class PinHistory {
9695
// copy elements to an array, up to a given length
9796
// return the number of elements moved
9897
int toArray(T* arr, unsigned int length) {
99-
std::queue<T> q2(qOut);
98+
queue<T> q2(qOut);
10099

101100
int ret = 0;
102101
for (int i = 0; i < length && q2.size(); ++i) {
@@ -110,7 +109,7 @@ class PinHistory {
110109
// see if the array matches the elements in the queue
111110
bool hasElements(T* arr, unsigned int length) {
112111
int i;
113-
std::queue<T> q2(qOut);
112+
queue<T> q2(qOut);
114113
for (i = 0; i < length && q2.size(); ++i) {
115114
if (q2.front() != arr[i]) return false;
116115
q2.pop();
@@ -123,7 +122,7 @@ class PinHistory {
123122
String toAscii(unsigned int offset, bool bigEndian) {
124123
String ret = "";
125124

126-
std::queue<T> q2(qOut);
125+
queue<T> q2(qOut);
127126

128127
while (offset) {
129128
q2.pop();

cpp/arduino/queue.h

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#pragma once
2+
3+
template <typename T>
4+
class queue {
5+
private:
6+
struct Node {
7+
T data;
8+
Node* next;
9+
};
10+
11+
Node* mFront;
12+
Node* mBack;
13+
unsigned long mSize;
14+
T mNil;
15+
16+
void init() {
17+
mFront = mBack = NULL;
18+
mSize = 0;
19+
}
20+
21+
public:
22+
queue() { init(); }
23+
24+
queue(const queue<T>& q) {
25+
init();
26+
for (Node* n = q.mFront; n; n = n->next) push(n->data);
27+
}
28+
29+
inline unsigned long size() const { return mSize; }
30+
31+
inline bool empty() const { return 0 == mSize; }
32+
33+
const T& front() const { return empty() ? mNil : mFront->data; }
34+
35+
const T& back() const { return empty() ? mNil : mBack->data; }
36+
37+
bool push(const T& v)
38+
{
39+
Node *n = new Node;
40+
if (n == NULL) return false;
41+
42+
n->data = v;
43+
n->next = NULL;
44+
45+
if (mFront == NULL)
46+
{
47+
mFront = mBack = n;
48+
} else {
49+
mBack->next = n;
50+
mBack = n;
51+
}
52+
53+
++mSize;
54+
return true;
55+
}
56+
57+
void pop() {
58+
if (empty()) return;
59+
if (mFront == mBack) {
60+
mFront = mBack = NULL;
61+
} else {
62+
Node* n = mFront;
63+
mFront = mFront->next;
64+
delete n;
65+
}
66+
67+
--mSize;
68+
}
69+
70+
void clear() { while (!empty()) pop(); }
71+
72+
~queue() { clear(); }
73+
};

0 commit comments

Comments
 (0)