forked from pa-pa/AskSinPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Device.h
263 lines (222 loc) · 5.57 KB
/
Device.h
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#ifndef __DEVICE_H__
#define __DEVICE_H__
#include "HMID.h"
#include "Channel.h"
#include "ChannelList.h"
#include "Message.h"
#include "Radio.h"
namespace as {
class Device {
HMID devid;
HMID master;
char serial[11];
uint8_t firmversion;
uint8_t model[2];
uint8_t subtype;
uint8_t devinfo[3];
CC1101* radio;
uint8_t msgcount;
protected:
Message msg;
public:
virtual ~Device () {}
void setRadio(CC1101& r) {
msgcount=0;
radio = &r;
}
CC1101& getRadio () {
return *radio;
}
void setFirmwareVersion (uint8_t v) {
firmversion = v;
}
void setModel (uint8_t m1, uint8_t m2) {
model[0] = m1;
model[1] = m2;
}
void setSubType (uint8_t st) {
subtype = st;
}
void setInfo (uint8_t i1, uint8_t i2, uint8_t i3) {
devinfo[0] = i1;
devinfo[1] = i2;
devinfo[2] = i3;
}
void setMasterID (const HMID& id) {
master = id;
}
const HMID& getMasterID () const {
return master;
}
void setDeviceID (const HMID& id) {
devid=id;
}
const HMID& getDeviceID () const {
return devid;
}
void setSerial (const char* ser) {
memcpy(serial,ser,10);
serial[10] = 0;
}
const char* getSerial () const {
return serial;
}
template <class ChannelType>
bool addPeer (ChannelType& ch,const Peer& p) {
ch.deletepeer(p);
uint8_t pidx = ch.findpeer();
if( pidx != 0xff ) {
ch.peer(pidx,p);
ch.getList3(pidx).single();
return true;
}
return false;
}
template <class ChannelType>
bool addPeer(ChannelType& ch,const Peer& p1, const Peer& p2) {
ch.deletepeer(p1);
ch.deletepeer(p2);
uint8_t pidx1 = ch.findpeer();
if( pidx1 != 0xff ) {
ch.peer(pidx1,p1);
uint8_t pidx2 = ch.findpeer();
if( pidx2 != 0xff ) {
ch.peer(pidx2,p2);
if( p1.odd() == true ) {
ch.getList3(pidx1).odd();
ch.getList3(pidx2).even();
}
else {
ch.getList3(pidx2).odd();
ch.getList3(pidx1).even();
}
return true;
}
else {
// free already stored data
ch.peer(pidx1,Peer());
}
}
return false;
}
void pollRadio () {
uint8_t num = getRadio().read(msg);
if( num > 0 ) {
process(msg);
}
}
uint8_t nextcount () {
return ++msgcount;
}
virtual void process(Message& msg);
bool isBoardcastMsg(Message msg) {
return msg.isPairSerial();
}
bool send(Message& msg,const HMID& to);
void sendAck (Message& msg) {
msg.ack().init();
send(msg,msg.from());
}
void sendNack (Message& msg) {
msg.nack().init();
send(msg,msg.from());
}
template <class ChannelType>
void sendAck (Message& msg,ChannelType& ch) {
msg.ackStatus().init(ch,radio->rssi());
send(msg,msg.from());
ch.changed(false);
}
void sendDeviceInfo () {
sendDeviceInfo(HMID::boardcast,nextcount());
}
void sendDeviceInfo (const HMID& to,uint8_t count) {
DeviceInfoMsg& pm = msg.deviceInfo();
pm.init(to,count);
pm.fill(firmversion,model,serial,subtype,devinfo);
send(msg,to);
}
template <class ChannelType>
void sendInfoActuatorStatus (const HMID& to,uint8_t count,ChannelType& ch) {
InfoActuatorStatusMsg& pm = msg.infoActuatorStatus();
pm.init(count,ch,radio->rssi());
send(msg,to);
ch.changed(false);
}
void sendInfoParamResponsePairs(HMID to,uint8_t count,const GenericList& list) {
// setup message for maximal size
// msg.initWithCount(0x0b-1+(8*2),0x10,Message::BIDI,0x02);
// msg.count(count);
InfoParamResponsePairsMsg& pm = msg.infoParamResponsePairs();
// setup message for maximal size
pm.init(count);
uint8_t current=0;
uint8_t* buf=pm.data();
for( int i=0; i<list.getSize(); ++i ) {
*buf++ = list.getRegister(i);
*buf++ = list.getByte(i);
current++;
if( current == 8 ) {
// reset to zero
current=0;
buf=pm.data();
if( send(msg,to) == false ) {
// exit loop in case of error
break;
}
}
}
*buf++ = 0;
*buf++ = 0;
current++;
pm.entries(current);
send(msg,to);
}
template <class ChannelType>
void sendInfoPeerList (HMID to,uint8_t count,const ChannelType& channel) {
InfoPeerListMsg& pm = msg.infoPeerList();
// setup message for maximal size
pm.init(count);
uint8_t current=0;
uint8_t* buf=pm.data();
for( uint8_t i=0; i<channel.peers(); ++i ) {
Peer p = channel.peer(i);
if( p.valid() == true ) {
memcpy(buf,&p,sizeof(Peer));
buf+=sizeof(Peer);
current++;
if( current == 4 ) {
// reset to zero
current=0;
buf=pm.data();
if( send(msg,to) == false ) {
// exit loop in case of error
break;
}
}
}
}
memset(buf,0,sizeof(Peer));
current++;
pm.entries(current);
send(msg,to);
}
void writeList (const GenericList& list,const uint8_t* data,uint8_t length) {
for( uint8_t i=0; i<length; i+=2, data+=2 ) {
list.writeRegister(*data,*(data+1));
}
}
bool waitForAck(Message& msg,uint8_t timeout) {
do {
if( radio->readAck(msg) == true ) {
return true;
}
delay(10); // wait 10ms
timeout--;
}
while( timeout > 0 );
return false;
}
};
}
#endif