-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMessages.cpp
More file actions
173 lines (135 loc) · 2.93 KB
/
Messages.cpp
File metadata and controls
173 lines (135 loc) · 2.93 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
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
//
//
//
#include "Print.h";
#include "Messages.h"
template <typename T> Print &operator << (Print &s, T n) { s.print(n); return s; }
extern word FreeMem(void);
TMessage::TMessage()
{
Message = msg_Empty;
LoParam = HiParam = 0;
}
TMessage::TMessage(byte msg, word loparam, word hiparam)
{
Message = msg;
LoParam = loparam;
HiParam = hiparam;
}
TMessage::TMessage(byte msg): TMessage(msg,0,0){}
#ifdef DDEBUG
void TMessage::PrintTo(Print &Stream)
{
Stream << Message << " " << LoParam << " " << HiParam << '\n';
}
#endif // DDEBUG
bool TMessage::operator==(TMessage msg)
{
return ((Message == msg.Message) AND (LoParam == msg.LoParam) AND (HiParam == msg.HiParam));
}
bool TMessage::operator==(PMessage msg)
{
return ((Message == msg->Message) AND (LoParam == msg->LoParam) AND (HiParam == msg->HiParam));
}
bool TMessage::operator==(byte msg)
{
return Message == msg;
}
void TMessageList::DeleteFirst(void)
{
byte _sreg = SREG;
cli();
for (byte i = 1; i < flength; i++) Items[i - 1] = Items[i];
Items[flength - 1] = NULL;
if (fcount>0) fcount--;
SREG = _sreg;
}
bool TMessageList::FindMessage(PMessage msg)
{
if (msg == NULL) return false;
for (byte i = 0; i < fcount; i++)
{
if ((Items[i] != NULL) AND (*msg == *Items[i])) return true;
}
return false;
}
bool TMessageList::Availiable()
{
return (fcount > 0);
}
TMessage TMessageList::GetMessage()
{
TMessage msg;
PMessage pmsg = Items[0];
if (pmsg != NULL)
{
msg = *pmsg;
free(pmsg);
DeleteFirst();
}
// else Serial << "pmsg = NULL\n";
return msg;
}
TMessageList::TMessageList() : TMessageList(16){}
TMessageList::TMessageList(byte len)
{
flength = len;
Items = new PMessage[flength];
for (byte i = 0; i < len; i++) Items[i] = NULL;
fcount = 0;
}
TMessageList::~TMessageList()
{
while (fcount) GetMessage();
delete[] Items;
}
#ifdef DDEBUG
void TMessageList::PrintOut(Print &stream)
{
for (byte i = 0; i < length; i++)
{
stream << "Address: " << word(Items[i]);
if (Items[i] == NULL) stream << '\n';
else stream << ' ' << Items[i]->Message << '\t' << Items[i]->LoParam << '\t' << Items[i]->HiParam << '\n';
}
}
#endif
bool TMessageList::Add(PMessage msg)
{
if (msg == NULL) return false;
if ((fcount<flength) AND (NOT FindMessage(msg)))
{
Items[fcount++] = msg;
return true;
}
else
{
free(msg);
return false;
}
}
bool TMessageList::Add(byte msg, word lo, word hi)
{
return Add(new TMessage(msg, lo, hi));
}
bool TMessageList::Add(byte msg)
{
return Add(new TMessage(msg,0,0));
}
bool TMessageList::AddEmpty(void)
{
return Add(new TMessage());
}
bool TMessageList::Paint(void)
{
return Add(msg_Paint);
}
bool TMessageList::Error(int errornum)
{
return Add(msg_Error, errornum, 0);
}
byte TMessageList::Count()
{
return fcount;
}
TMessageList *MessageList;