-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzbee-handling.h
97 lines (80 loc) · 1.83 KB
/
zbee-handling.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
#include <XBee.h>
XBee xbee = XBee();
using namespace zb;
bool has_received_presync{false};
int presync_sending_round{0};
Rx16Response rx16 = Rx16Response();
void _send16(uint16_t target, uint8_t* payload, size_t size) {
Tx16Request tx=Tx16Request(target, payload, size);
xbee.send(tx);
}
void handle_zb_as_cd(uint16_t target) {
uint8_t payload[2] {1, 0};
_send16(target, payload, sizeof(payload));
}
void handle_zb_as_cd() {
if(presync_sending_round<=2) {
handle_zb_as_cd(0x0002);
} else {
handle_zb_as_cd(0x0003);
}
presync_sending_round++;
}
void handle_zb_as_ed() {
xbee.readPacket();
if(!xbee.getResponse().isAvailable()) {
//nothing to read
return;
}
if(!xbee.getResponse().getApiId() == RX_16_RESPONSE) {
//not something we expect, so dump it
return;
}
has_received_presync=true;
}
constexpr long zb_bump_thrs=250;
timer_ms time_since_last_bump_via_zb{zb_bump_thrs+1};
/*
enum data_type : char {
BUMP='B',
HEARTBEAT='H',
};*/
void _heartbeat(uint16_t target) {
uint8_t payload[1]={HEARTBEAT};
_send16(target, payload, sizeof(payload));
}
int fight_state= 0;
void handle_zb_as_mk() {
if(iterations_passed%64==0) {
_heartbeat(0x0001);
}
xbee.readPacket();
auto response=xbee.getResponse();
if(!response.isAvailable() || response.isError()) {
//nothing to read
return;
}
if(response.getApiId() != RX_16_RESPONSE) {
//not something we expect
return;
}
//at this point we have actual data
response.getRx16Response(rx16);
switch(rx16.getData()[0]) {
case BUMP: {
fight_state = 1;
//Serial.println("ghj");
} break;
case BUMP_N2M: {
fight_state = 2;
//Serial.println("jhg");
} break;
case TDD_BEGIN: {
scene = 3;
}
case HEARTBEAT: {
//do nothing
} break;
default: break;
}
}