-
Notifications
You must be signed in to change notification settings - Fork 1
/
DCCPacketQueue.cpp
202 lines (176 loc) · 5.07 KB
/
DCCPacketQueue.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "DCCPacketQueue.h"
DCCPacketQueue::DCCPacketQueue(void) : read_pos(0), write_pos(0), size(10), written(0)
{
return;
}
void DCCPacketQueue::setup(byte length)
{
size = length;
queue = (DCCPacket *)malloc(sizeof(DCCPacket) *size);
for(int i = 0; i<size; ++i)
{
queue[i] = DCCPacket();
}
}
bool DCCPacketQueue::insertPacket(DCCPacket *packet)
{
// Serial.print("Enqueueing a packet of kind: ");
// Serial.println(packet->getKind(), DEC);
//First: Overwrite any packet with the same address and kind; if no such packet THEN hitup the packet at write_pos
byte i = read_pos;
while(i != (read_pos+written)%(size) )//(size+1) ) //size+1 so we can check the last slot, too…
{
if( (queue[i].getAddress() == packet->getAddress()) && (queue[i].getKind() == packet->getKind()) && (packet->getKind() != basic_accessory_packet_kind) && (packet->getKind() != ops_mode_programming_kind))
{
//ATTENTION: accessory packet does have a 2. Adr inside the packet - just add them all, also ops_mode_programming_kind!
// Serial.print("Overwriting existing packet at index ");
// Serial.println(i, DEC);
memcpy(&queue[i],packet,sizeof(DCCPacket));
//do not increment written or modify write_pos
return true;
}
i = (i+1)%size;
}
//else, tack it on to the end
if(!isFull())
{
//else, just write it at the end of the queue.
memcpy(&queue[write_pos],packet,sizeof(DCCPacket));
// Serial.print("Write packet to index ");
// Serial.println(write_pos, DEC);
write_pos = (write_pos + 1) % size;
++written;
return true;
}
// Serial.println("Queue is full!");
return false;
}
// void DCCPacketQueue::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("");
// }
// }
bool DCCPacketQueue::readPacket(DCCPacket *packet)
{
if(!isEmpty())
{
// Serial.print("Reading a packet from index: ");
// Serial.println(read_pos, DEC);
memcpy(packet,&queue[read_pos],sizeof(DCCPacket));
read_pos = (read_pos + 1) % size;
--written;
return true;
}
return false;
}
bool DCCPacketQueue::forget(uint16_t address /*, uint8_t address_kind*/)
{
bool found = false;
for(int i = 0; i < size; ++i)
{
if( (queue[i].getAddress() == address) /*&& (queue[i].getAddressKind() == address_kind)*/ )
{
found = true;
queue[i] = DCCPacket(); //revert to default value
}
}
--written;
return found;
}
void DCCPacketQueue::clear(uint8_t packet_kind)
{
// read_pos = 0;
// write_pos = 0;
// written = 0;
for(int i = 0; i<size; ++i)
{
if ((queue[i].getKind() == packet_kind) || (packet_kind == 0x00)) {
queue[i] = DCCPacket();
--written;
}
}
}
/*****************************/
DCCRepeatQueue::DCCRepeatQueue(void) : DCCPacketQueue()
{
}
bool DCCRepeatQueue::insertPacket(DCCPacket *packet)
{
if(packet->getRepeat())
{
return(DCCPacketQueue::insertPacket(packet));
}
return false;
}
bool DCCRepeatQueue::readPacket(DCCPacket *packet)
{
if(!isEmpty())
{
memcpy(packet,&queue[read_pos],sizeof(DCCPacket));
read_pos = (read_pos + 1) % size;
--written;
if(packet->getRepeat()) //the packet needs to be sent out at least one more time
{
//***************************************************************************
//Add repeat for special packets!!
// if (packet->getKind() != speed_packet_kind && packet->getKind() != function_packet_1_kind && packet->getKind() != function_packet_2_kind && packet->getKind() != function_packet_3_kind)
packet->setRepeat(packet->getRepeat()-1);
insertPacket(packet);
}
return true;
}
return false;
}
/**************/
DCCEmergencyQueue::DCCEmergencyQueue(void) : DCCPacketQueue()
{
}
/* Goes through each packet in the queue, repeats it getRepeat() times, and discards it */
bool DCCEmergencyQueue::readPacket(DCCPacket *packet)
{
if(!isEmpty()) //anything in the queue?
{
queue[read_pos].setRepeat(queue[read_pos].getRepeat()-1); //decrement the current packet's repeat count
if(queue[read_pos].getRepeat()) //if the topmost packet needs repeating
{
memcpy(packet,&queue[read_pos],sizeof(DCCPacket));
return true;
}
else //the topmost packet is ready to be discarded; use the DCCPacketQueue mechanism
{
return(DCCPacketQueue::readPacket(packet));
}
}
return false;
}
/**********DCCTemporalQueue*******************/
//for speed and function repeat (repeat never stops!)
DCCTemporalQueue::DCCTemporalQueue(void) : DCCPacketQueue()
{
}
bool DCCTemporalQueue::insertPacket(DCCPacket *packet)
{
return(DCCPacketQueue::insertPacket(packet));
}
bool DCCTemporalQueue::readPacket(DCCPacket *packet)
{
if (!isEmpty())
{
memcpy(packet, &queue[read_pos], sizeof(DCCPacket));
read_pos = (read_pos + 1) % size;
--written;
insertPacket(packet);
return true;
}
return false;
}