|
5 | 5 | // pins with history.
|
6 | 6 | template <typename T>
|
7 | 7 | class PinHistory {
|
8 |
| -private: |
9 |
| - queue<T> qIn; |
10 |
| - queue<T> qOut; |
11 |
| - |
12 |
| - unsigned long mLastRead = 0; |
13 |
| - |
14 |
| - void clear(queue<T>* q) { |
15 |
| - while (!q->empty()) q->pop(); |
16 |
| - } |
17 |
| - |
18 |
| - void clear() { |
19 |
| - clear(&qOut); |
20 |
| - clear(&qIn); |
21 |
| - } |
22 |
| - |
23 |
| -public: |
24 |
| - PinHistory() {} |
25 |
| - |
26 |
| - void reset(T val) { |
27 |
| - clear(); |
28 |
| - qOut.push(val); |
29 |
| - } |
30 |
| - |
31 |
| - unsigned int historySize() { return qOut.size(); } |
32 |
| - |
33 |
| - // This returns the "value" of the pin in a raw sense |
34 |
| - operator T() const { |
35 |
| - if (mLastRead == 0 && qIn.size()) return qIn.front(); |
36 |
| - return qOut.back(); |
37 |
| - } |
38 |
| - |
39 |
| - // this sets the value of the pin authoritatively |
40 |
| - // so if there was a queue, dump it. |
41 |
| - // the actual "set" operation doesn't happen until the next read |
42 |
| - const T &operator=(const T& i) { |
43 |
| - qOut.push(i); |
44 |
| - return qOut.back(); |
45 |
| - } |
46 |
| - |
47 |
| - // This returns the "value" of the pin according to the queued values |
48 |
| - // if now is the same as the last read time, return the same val and exit |
49 |
| - // if there is input, advance it to the output. |
50 |
| - // then take the latest output. |
51 |
| - T retrieve() { |
52 |
| - if (qIn.size()) { |
53 |
| - qOut.push(qIn.front()); |
54 |
| - qIn.pop(); |
| 8 | + private: |
| 9 | + queue<T> qIn; |
| 10 | + queue<T> qOut; |
| 11 | + |
| 12 | + unsigned long mLastRead = 0; |
| 13 | + |
| 14 | + void clear() { |
| 15 | + qOut.clear(); |
| 16 | + qIn.clear(); |
55 | 17 | }
|
56 |
| - return qOut.back(); |
57 |
| - } |
58 |
| - |
59 |
| - // enqueue a set of elements |
60 |
| - void fromArray(T* arr, unsigned int length) { |
61 |
| - for (int i = 0; i < length; ++i) qIn.push(arr[i]); |
62 |
| - } |
63 |
| - |
64 |
| - // enqueue ascii bits |
65 |
| - void fromAscii(String input, bool bigEndian) { |
66 |
| - String ret = ""; |
67 |
| - |
68 |
| - // 8 chars at a time, form up |
69 |
| - for (int j = 0; j < input.length(); ++j) { |
70 |
| - for (int i = 0; i < 8; ++i) { |
71 |
| - int shift = bigEndian ? 7 - i : i; |
72 |
| - unsigned char mask = (0x01 << shift); |
73 |
| - qIn.push(mask & input[j]); |
74 |
| - } |
| 18 | + |
| 19 | + public: |
| 20 | + PinHistory() {} |
| 21 | + |
| 22 | + void reset(T val) { |
| 23 | + clear(); |
| 24 | + qOut.push(val); |
75 | 25 | }
|
76 |
| - } |
77 |
| - |
78 |
| - // copy elements to an array, up to a given length |
79 |
| - // return the number of elements moved |
80 |
| - int toArray(T* arr, unsigned int length) { |
81 |
| - queue<T> q2(qOut); |
82 |
| - |
83 |
| - int ret = 0; |
84 |
| - for (int i = 0; i < length && q2.size(); ++i) { |
85 |
| - arr[i] = q2.front(); |
86 |
| - q2.pop(); |
87 |
| - ++ret; |
| 26 | + |
| 27 | + unsigned int historySize() { return qOut.size(); } |
| 28 | + |
| 29 | + // This returns the "value" of the pin in a raw sense |
| 30 | + operator T() const { |
| 31 | + if (mLastRead == 0 && qIn.size()) return qIn.front(); |
| 32 | + return qOut.back(); |
88 | 33 | }
|
89 |
| - return ret; |
90 |
| - } |
91 |
| - |
92 |
| - // see if the array matches the elements in the queue |
93 |
| - bool hasElements(T* arr, unsigned int length) { |
94 |
| - int i; |
95 |
| - queue<T> q2(qOut); |
96 |
| - for (i = 0; i < length && q2.size(); ++i) { |
97 |
| - if (q2.front() != arr[i]) return false; |
98 |
| - q2.pop(); |
| 34 | + |
| 35 | + // this sets the value of the pin authoritatively |
| 36 | + // so if there was a queue, dump it. |
| 37 | + // the actual "set" operation doesn't happen until the next read |
| 38 | + const T &operator=(const T& i) { |
| 39 | + qOut.push(i); |
| 40 | + return qOut.back(); |
99 | 41 | }
|
100 |
| - return i == length; |
101 |
| - } |
102 | 42 |
|
103 |
| - // convert the pin history to a string as if it was Serial comms |
104 |
| - // start from offset, consider endianness |
105 |
| - String toAscii(unsigned int offset, bool bigEndian) { |
106 |
| - String ret = ""; |
| 43 | + // This returns the "value" of the pin according to the queued values |
| 44 | + // if now is the same as the last read time, return the same val and exit |
| 45 | + // if there is input, advance it to the output. |
| 46 | + // then take the latest output. |
| 47 | + T retrieve() { |
| 48 | + if (qIn.size()) { |
| 49 | + qOut.push(qIn.front()); |
| 50 | + qIn.pop(); |
| 51 | + } |
| 52 | + return qOut.back(); |
| 53 | + } |
107 | 54 |
|
108 |
| - queue<T> q2(qOut); |
| 55 | + // enqueue a set of elements |
| 56 | + void fromArray(T* arr, unsigned int length) { |
| 57 | + for (int i = 0; i < length; ++i) qIn.push(arr[i]); |
| 58 | + } |
109 | 59 |
|
110 |
| - while (offset) { |
111 |
| - q2.pop(); |
112 |
| - --offset; |
| 60 | + // enqueue ascii bits |
| 61 | + void fromAscii(String input, bool bigEndian) { |
| 62 | + String ret = ""; |
| 63 | + |
| 64 | + // 8 chars at a time, form up |
| 65 | + for (int j = 0; j < input.length(); ++j) { |
| 66 | + for (int i = 0; i < 8; ++i) { |
| 67 | + int shift = bigEndian ? 7 - i : i; |
| 68 | + unsigned char mask = (0x01 << shift); |
| 69 | + qIn.push(mask & input[j]); |
| 70 | + } |
| 71 | + } |
113 | 72 | }
|
114 | 73 |
|
115 |
| - if (offset) return ret; |
| 74 | + // copy elements to an array, up to a given length |
| 75 | + // return the number of elements moved |
| 76 | + int toArray(T* arr, unsigned int length) { |
| 77 | + queue<T> q2(qOut); |
116 | 78 |
|
117 |
| - // 8 chars at a time, form up |
118 |
| - while (q2.size() >= 8) { |
119 |
| - unsigned char acc = 0x00; |
120 |
| - for (int i = 0; i < 8; ++i) { |
121 |
| - int shift = bigEndian ? 7 - i : i; |
122 |
| - T val = q2.front(); |
123 |
| - unsigned char bit = val ? 0x1 : 0x0; |
124 |
| - acc |= (bit << shift); |
| 79 | + int ret = 0; |
| 80 | + for (int i = 0; i < length && q2.size(); ++i) { |
| 81 | + arr[i] = q2.front(); |
125 | 82 | q2.pop();
|
| 83 | + ++ret; |
126 | 84 | }
|
127 |
| - ret.append(String((char)acc)); |
| 85 | + return ret; |
128 | 86 | }
|
129 | 87 |
|
130 |
| - return ret; |
131 |
| - } |
| 88 | + // see if the array matches the elements in the queue |
| 89 | + bool hasElements(T* arr, unsigned int length) { |
| 90 | + int i; |
| 91 | + queue<T> q2(qOut); |
| 92 | + for (i = 0; i < length && q2.size(); ++i) { |
| 93 | + if (q2.front() != arr[i]) return false; |
| 94 | + q2.pop(); |
| 95 | + } |
| 96 | + return i == length; |
| 97 | + } |
| 98 | + |
| 99 | + // convert the pin history to a string as if it was Serial comms |
| 100 | + // start from offset, consider endianness |
| 101 | + String toAscii(unsigned int offset, bool bigEndian) { |
| 102 | + String ret = ""; |
| 103 | + |
| 104 | + queue<T> q2(qOut); |
| 105 | + |
| 106 | + while (offset) { |
| 107 | + q2.pop(); |
| 108 | + --offset; |
| 109 | + } |
| 110 | + |
| 111 | + if (offset) return ret; |
| 112 | + |
| 113 | + // 8 chars at a time, form up |
| 114 | + while (q2.size() >= 8) { |
| 115 | + unsigned char acc = 0x00; |
| 116 | + for (int i = 0; i < 8; ++i) { |
| 117 | + int shift = bigEndian ? 7 - i : i; |
| 118 | + T val = q2.front(); |
| 119 | + unsigned char bit = val ? 0x1 : 0x0; |
| 120 | + acc |= (bit << shift); |
| 121 | + q2.pop(); |
| 122 | + } |
| 123 | + ret.append(String((char)acc)); |
| 124 | + } |
| 125 | + |
| 126 | + return ret; |
| 127 | + } |
132 | 128 | };
|
133 | 129 |
|
0 commit comments