-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwireless.h
More file actions
205 lines (173 loc) · 8.19 KB
/
wireless.h
File metadata and controls
205 lines (173 loc) · 8.19 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
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
202
203
204
205
/*
Copyright (C) 2010 Fabian Schmitthenner
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, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "Dispatcher.h"
#include "MessageTypes.h"
class MyXBee
{
public:
static const unsigned int baudRate = 38400;
enum SpecialCharacters
{
#ifdef REALLY_WORLD
START_BYTE = 200,
ESCAPE = 201
#else
START_BYTE = 's',
ESCAPE = 'e'
#endif
};
enum SpecialPackageTypes
{
ERROR = 254,
CONNECTION_INTERRUPTED = 255
};
enum ErrorMessages
{
NO_START_BYTE = 1,
START_BYTE_INSIDE_MESSAGE = 2,
MESSAGE_NOT_REGISTERED = 3,
ERROR_TO_LONG_MESSAGE = 4
};
const static unsigned int MAX_TIME_BETWEEN_TWO_REQUESTS = 2000; // 2 seconds
const static uint8_t MAX_MESSAGE_LENGTH = 50;
MyXBee ();
void readPackages();
bool isConnected ();
template< const char type >
void writePackage ()
{
static_assert (Message::MessageData< type >::ParamCount == 0, "wrong param count");
Serial.write (START_BYTE);
writeEscaped (1);
writeEscaped (type);
}
template< const char type >
void writePackage (typename Message::MessageData< type >::Param1 param1)
{
static_assert (Message::MessageData< type >::ParamCount == 1, "wrong param count");
Serial.write (START_BYTE);
writeEscaped (1+sizeof (typename Message::MessageData< type >::Param1));
writeEscaped (type);
for (uint8_t* it = reinterpret_cast< uint8_t * > (¶m1); it != reinterpret_cast< uint8_t * > (¶m1 + 1); ++it)
writeEscaped (*it);
}
template< const char type >
void writePackage (typename Message::MessageData< type >::Param1 param1, typename Message::MessageData< type >::Param2 param2)
{
static_assert (Message::MessageData< type >::ParamCount == 2, "wrong param count");
Serial.write (START_BYTE);
writeEscaped (1+sizeof (typename Message::MessageData< type >::Param1)+sizeof (typename Message::MessageData< type >::Param2));
writeEscaped (type);
for (uint8_t* it = reinterpret_cast< uint8_t * > (¶m1); it != reinterpret_cast< uint8_t * > (¶m1 + 1); ++it)
writeEscaped (*it);
for (uint8_t* it = reinterpret_cast< uint8_t * > (¶m2); it != reinterpret_cast< uint8_t * > (¶m2 + 1); ++it)
writeEscaped (*it);
}
template< const char type >
void writePackage (typename Message::MessageData< type >::Param1 param1, typename Message::MessageData< type >::Param2 param2, typename Message::MessageData< type >::Param3 param3)
{
static_assert (Message::MessageData< type >::ParamCount == 3, "wrong param count");
Serial.write (START_BYTE);
writeEscaped (1+sizeof (typename Message::MessageData< type >::Param1) + sizeof (typename Message::MessageData< type >::Param2) + sizeof (typename Message::MessageData< type >::Param3));
writeEscaped (type);
for (uint8_t* it = reinterpret_cast< uint8_t * > (¶m1); it != reinterpret_cast< uint8_t * > (¶m1 + 1); ++it)
writeEscaped (*it);
for (uint8_t* it = reinterpret_cast< uint8_t * > (¶m2); it != reinterpret_cast< uint8_t * > (¶m2 + 1); ++it)
writeEscaped (*it);
for (uint8_t* it = reinterpret_cast< uint8_t * > (¶m3); it != reinterpret_cast< uint8_t * > (¶m3 + 1); ++it)
writeEscaped (*it);
}
template< const char type >
void writePackage (typename Message::MessageData< type >::Param1 param1, typename Message::MessageData< type >::Param2 param2, typename Message::MessageData< type >::Param3 param3, typename Message::MessageData< type >::Param4 param4)
{
static_assert (Message::MessageData< type >::ParamCount == 4, "wrong param count");
Serial.write (START_BYTE);
writeEscaped (1+sizeof (typename Message::MessageData< type >::Param1) + sizeof (typename Message::MessageData< type >::Param2) + sizeof (typename Message::MessageData< type >::Param3) + sizeof (typename Message::MessageData< type >::Param4));
writeEscaped (type);
for (uint8_t* it = reinterpret_cast< uint8_t * > (¶m1); it != reinterpret_cast< uint8_t * > (¶m1 + 1); ++it)
writeEscaped (*it);
for (uint8_t* it = reinterpret_cast< uint8_t * > (¶m2); it != reinterpret_cast< uint8_t * > (¶m2 + 1); ++it)
writeEscaped (*it);
for (uint8_t* it = reinterpret_cast< uint8_t * > (¶m3); it != reinterpret_cast< uint8_t * > (¶m3 + 1); ++it)
writeEscaped (*it);
for (uint8_t* it = reinterpret_cast< uint8_t * > (¶m4); it != reinterpret_cast< uint8_t * > (¶m4 + 1); ++it)
writeEscaped (*it);
}
template< const char type >
void writePackage (typename Message::MessageData< type >::Param1 param1, typename Message::MessageData< type >::Param2 param2, typename Message::MessageData< type >::Param3 param3, typename Message::MessageData< type >::Param4 param4, typename Message::MessageData< type >::Param5 param5)
{
static_assert (Message::MessageData< type >::ParamCount == 5, "wrong param count");
Serial.write (START_BYTE);
writeEscaped (1+sizeof (typename Message::MessageData< type >::Param1) + sizeof (typename Message::MessageData< type >::Param2) + sizeof (typename Message::MessageData< type >::Param3) + sizeof (typename Message::MessageData< type >::Param4) + sizeof (typename Message::MessageData< type >::Param5));
writeEscaped (type);
for (uint8_t* it = reinterpret_cast< uint8_t * > (¶m1); it != reinterpret_cast< uint8_t * > (¶m1 + 1); ++it)
writeEscaped (*it);
for (uint8_t* it = reinterpret_cast< uint8_t * > (¶m2); it != reinterpret_cast< uint8_t * > (¶m2 + 1); ++it)
writeEscaped (*it);
for (uint8_t* it = reinterpret_cast< uint8_t * > (¶m3); it != reinterpret_cast< uint8_t * > (¶m3 + 1); ++it)
writeEscaped (*it);
for (uint8_t* it = reinterpret_cast< uint8_t * > (¶m4); it != reinterpret_cast< uint8_t * > (¶m4 + 1); ++it)
writeEscaped (*it);
for (uint8_t* it = reinterpret_cast< uint8_t * > (¶m5); it != reinterpret_cast< uint8_t * > (¶m5 + 1); ++it)
writeEscaped (*it);
}
long getReadCount () { return read_count; }
typedef void (*ReadPackage) (const void *data, uint8_t length);
void _registerMethod (uint8_t type, ReadPackage package);
template< const char type >
void registerMethod (void (*p) (const Message::MessageData< type > *data, uint8_t length))
{
_registerMethod (type, reinterpret_cast< ReadPackage > (p));
}
private:
void readData (const uint8_t *data, uint8_t length);
void writeEscaped(uint8_t arg1);
void error(uint8_t arg1)
{
uint8_t data[2];
data[0] = ERROR;
data[1] = arg1;
return readData (data, 2);
}
void connectionInterrupted ()
{
static const uint8_t connectionInterrupted[2] = { CONNECTION_INTERRUPTED, false };
readData (connectionInterrupted, 2);
}
void connectionRestored()
{
static const uint8_t connectionRestored[2] = { CONNECTION_INTERRUPTED, true };
readData (connectionRestored, 2);
}
private:
uint8_t package[MAX_MESSAGE_LENGTH + 5];
long unsigned int alredyRead;
long unsigned int lastPackageRead;
bool _isConnected;
bool in_escape_mode;
long read_count;
struct RegisterPair
{
RegisterPair () : type (0), delegate (0) { }
uint8_t type;
ReadPackage delegate;
};
RegisterPair registrants[16];
int registrantsCount;
};
namespace Message{
MESSAGE_DATA_1 (MyXBee::CONNECTION_INTERRUPTED, bool, connected);
MESSAGE_DATA_1 (MyXBee::ERROR, uint8_t, errorNumber);
};