forked from MERG-DEV/CANCMDDC-Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFIFO.h
More file actions
119 lines (107 loc) · 3.59 KB
/
FIFO.h
File metadata and controls
119 lines (107 loc) · 3.59 KB
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
/**
* Copyright (c) 2017 Ian Morgan
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3 of the License
*/
// FIFO buffer for incoming CBus messages.
//
// Class: CBusMessageBufferClass
//
// Methods:
// CBusMessageBufferClass() Class Constructor
// int addMessage (messageRecordType newMessage)
// messageRecordType getMessage ()
//
#define BUFFERSIZE 128
typedef struct {
long unsigned int canId; // 32 bits
unsigned char rxBuf[8];
byte len; // unsigned 8-bit number
}messageRecordType;
class CBusMessageBufferClass
{
// declare local variables as volatile because they will be updated by interrupts
// so the compiler optimiser should not assume copies of the data in registers will
// still be valid at a later time
volatile struct {
messageRecordType messageRecord;
} messageBuffer[BUFFERSIZE];
volatile int writePointer;
volatile int readPointer;
public:
// Constructor - initializes the member variables and state
CBusMessageBufferClass()
{
writePointer = 0;
readPointer = 0;
messageBuffer[writePointer].messageRecord.canId = 0;
messageBuffer[writePointer].messageRecord.len = 0;
}
int addMessage (messageRecordType newMessage)
{
// this function is uded by the interface interrupt routine to add a new
// CBus message to the FIFO buffer. If the buffer is full, the oldest
// entry will be overwritten, and the function returns status '1'.
int statusReturn = 0;
// add the message to the buffer
messageBuffer[writePointer].messageRecord.canId = newMessage.canId;
messageBuffer[writePointer].messageRecord.len = newMessage.len;
for(int i = 0; i<newMessage.len; i++)
{
messageBuffer[writePointer].messageRecord.rxBuf[i] = newMessage.rxBuf[i];
}
// point to the next buffer location, ready for the next message to come in
writePointer++;
if (writePointer == BUFFERSIZE)
{
writePointer = 0;
}
// Check if the buffer has been over-filled
if (writePointer == readPointer)
{
#if DEBUG
Serial.println("FIFO Buffer is full");
#endif
// oldest message has been overwritten, get ready to return next oldest message
readPointer++;
if (readPointer == BUFFERSIZE)
{
readPointer = 0;
}
statusReturn = 1;
}
return statusReturn; // Returns 1 if buffer overflow occurred, otherwise returns 0
}
// ------------------------------------------------------------------------
messageRecordType getMessage ()
{
// This function is used to return the next unread CBus message from the
// FIFO buffer, if there is one.
messageRecordType nextMessage;
if (writePointer == readPointer)
{
// No new message to be removed from buffer
nextMessage.canId = 0xFFFFFFFF; // invalid;
nextMessage.len = 0;
return nextMessage;
}
else
{
// There is an unread message, so return it to the calling program
nextMessage.canId = messageBuffer[readPointer].messageRecord.canId;
nextMessage.len = messageBuffer[readPointer].messageRecord.len;
for(int i = 0; i<nextMessage.len; i++)
{
nextMessage.rxBuf[i] = messageBuffer[readPointer].messageRecord.rxBuf[i];
}
// point to the next message location, ready for the next call
readPointer++;
if (readPointer == BUFFERSIZE)
{
readPointer = 0;
}
return nextMessage;
}
}
};