-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathQueueDuplicates.ino
92 lines (73 loc) · 1.81 KB
/
QueueDuplicates.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
QueueDuplicates
q_PeekIdx implementation can be tested instead of q_peekPrevious by commenting USE_PEEK_PREVIOUS
This example code is in the public domain.
created 3 November 2019
modified 04 November 2020
by SMFSW
*/
#include <cppQueue.h>
#define OVERWRITE true
#define USE_PEEK_PREVIOUS // Comment this line to check the whole queue instead of just previous record
#define NB_RECS 7
typedef struct strRec {
uint16_t entry1;
uint16_t entry2;
} Rec;
Rec tab[14] = {
{ 0x1234, 0x3456 },
{ 0x1234, 0x3456 },
{ 0x5678, 0x7890 },
{ 0x5678, 0x7890 },
{ 0x90AB, 0xABCD },
{ 0x90AB, 0xABCD },
{ 0xCDEF, 0xEFDC },
{ 0xCDEF, 0xEFDC },
{ 0xDCBA, 0xBA09 },
{ 0xDCBA, 0xBA09 },
{ 0x0987, 0x8765 },
{ 0x0987, 0x8765 },
{ 0x6543, 0x2112 },
{ 0x6543, 0x2112 }
};
cppQueue q(sizeof(Rec), NB_RECS, FIFO, OVERWRITE); // Instantiate queue
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(115200);
}
// the loop function runs over and over again forever
void loop() {
unsigned int i;
for (i = 0 ; i < sizeof(tab)/sizeof(Rec) ; i++)
{
Rec rec = tab[i % (sizeof(tab)/sizeof(Rec))];
#ifdef USE_PEEK_PREVIOUS // Check only previous record
Rec chk = {0xffff,0xffff};
q.peekPrevious(&chk);
if (memcmp(&rec, &chk, sizeof(Rec))) { q.push(&rec); }
#else // Check the whole queue
bool duplicate = false;
for (int j = (int) q_getCount(&q) - 1 ; j >= 0 ; j--)
{
Rec chk = {0xffff,0xffff};
q.peekIdx(&chk, j);
if (!memcmp(&rec, &chk, sizeof(Rec)))
{
duplicate = true;
break;
}
}
if (!duplicate) { q.push(&rec); }
#endif
}
for (i = 0 ; i < NB_RECS ; i++)
{
Rec rec = {0xffff,0xffff};
q.pop(&rec);
Serial.print(" ");
Serial.print(rec.entry1, HEX);
Serial.print(" ");
Serial.println(rec.entry2, HEX);
}
while(1);
}