-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.cpp
executable file
·115 lines (100 loc) · 2.53 KB
/
history.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
#include "history.hpp"
#include <list>
#define MAX_HISTORY_SIZE 10U
#if defined SERIAL_DEBUG
extern Serial pc;
#endif
std::list<DisplayMessage_t> messageHistory;
void removeAllMessages( void )
{
messageHistory.clear();
}
bool removeMessage( const uint32_t p_id )
{
bool retVal = false;
for( std::list<DisplayMessage_t>::iterator i = messageHistory.begin();
i != messageHistory.end();
i++ )
{
if( i->id == p_id )
{
messageHistory.erase( i );
retVal = true;
break;
}
}
return retVal;
}
void addMessage( const DisplayMessage_t* const p_message )
{
/* If the message is already in the history, remove it */
removeMessage( p_message->id );
/* Add the message to the back of the history list */
messageHistory.push_back( *p_message );
/* Prevent the history from growing too long */
if( messageHistory.size() > MAX_HISTORY_SIZE )
{
messageHistory.pop_front();
}
#if defined SERIAL_DEBUG
pc.printf("Message history is size: %d\r\n",messageHistory.size());
#endif
}
DisplayMessage_t* getMessage( uint32_t p_id, Offset_t p_offset )
{
DisplayMessage_t* retVal = NULL;
if( p_id == UINT32_MAX )
{
if( messageHistory.size() > 0 )
{
retVal = &(*(messageHistory.begin()));
}
else
{
/* Nothing to do */
}
}
else
{
for( std::list<DisplayMessage_t>::iterator i = messageHistory.begin();
i != messageHistory.end();
i++ )
{
if( p_id == i->id )
{
switch( p_offset )
{
case OFFSET_NONE:
retVal = &(*i);
break;
case OFFSET_BEFORE:
if( i != messageHistory.begin() )
{
i--;
retVal = &(*i);
}
break;
case OFFSET_AFTER:
i++;
if( i != messageHistory.end() )
{
retVal = &(*i);
}
break;
}
break;
}
}
}
#if defined SERIAL_DEBUG
if( retVal != NULL )
{
pc.printf("Returning %d\r\n",retVal->id);
}
else
{
pc.printf("Returning NULL\r\n");
}
#endif
return retVal;
}