-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDCCServiceModeQueue.cpp
67 lines (57 loc) · 1.58 KB
/
DCCServiceModeQueue.cpp
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
#include "DCCServiceModeQueue.h"
DCCServiceModeQueue::DCCServiceModeQueue(void) : _read_pos(0), _size(0)
{
return;
}
void DCCServiceModeQueue::setup(void)
{
// for(uint8_t i = 0; i<QUEUE_SIZE; ++i)
// {
// _queue[i].setup();
// }
}
bool DCCServiceModeQueue::insertPacket(DCCServiceModePacket &packet)
{
if(_size == QUEUE_SIZE) //queue is already full!
return false;
if(!packet.getRepeat()) //zero repeat set
return false;
memcpy(&_queue[_size],&packet,sizeof(DCCServiceModePacket));
++_size;
return true;
}
// void DCCServiceModeQueue::printQueue(void)
// {
// byte i, j;
// for(i = 0; i < size; ++i)
// {
// for(j = 0; j < (queue[i].size_repeat>>4); ++j)
// {
// Serial.print(queue[i].data[j],BIN);
// Serial.print(" ");
// }
// if(i == _read_pos) Serial.println(" r");
// else if(i == write_pos) Serial.println(" w");
// else Serial.println("");
// }
// }
/* Goes through each packet in the queue, repeats it getRepeat() times, and discards it */
bool DCCServiceModeQueue::readPacket(DCCServiceModePacket &packet)
{
if(isEmpty())
return false;
if(_queue[_read_pos].getRepeat()) //if the topmost packet needs repeating
{
_queue[_read_pos].setRepeat(_queue[_read_pos].getRepeat()-1); //decrement the current packet's repeat count
}
else //the topmost packet is ready to be discarded; use the DCCServiceModeQueue mechanism
{
++_read_pos;
}
if(!isEmpty()) //anything remaining in the queue?
{
memcpy(&packet,&_queue[_read_pos],sizeof(DCCServiceModePacket));
return true;
}
return false;
}