-
Notifications
You must be signed in to change notification settings - Fork 0
/
RF24SensorNet.cpp
206 lines (185 loc) · 5.13 KB
/
RF24SensorNet.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
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
/*
RF24SensorNet
A library for sending standard sensor and actuator
messages between nodes connected using RF24Network.
--
Peter Hardy <[email protected]>
*/
#include "RF24Network.h"
#include "RF24SensorNet.h"
// Neat hack for reading Vcc compared to internal reference voltage.
// http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/
long readVcc() {
// Read 1.1V reference against AVcc
// set the reference to Vcc and the measurement to the internal 1.1V reference
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0);
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA,ADSC)); // measuring
uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t high = ADCH; // unlocks both
long result = (high<<8) | low;
result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
return result; // Vcc in millivolts
}
RF24SensorNet::RF24SensorNet(RF24Network& network): _network(network)
{
_infoReadHandler = NULL;
_infoWriteHandler = NULL;
_infoRcvHandler = NULL;
_powerReadHandler = NULL;
_powerRcvHandler = NULL;
_switchReadHandler = NULL;
_switchWriteHandler = NULL;
_switchRcvHandler = NULL;
_rgbReadHandler = NULL;
_rgbWriteHandler = NULL;
_rgbRcvHandler = NULL;
}
void RF24SensorNet::begin()
{
}
void RF24SensorNet::update(void)
{
_network.update();
while (_network.available()) {
RF24NetworkHeader header;
_network.peek(header);
switch (header.type % 64) {
case PKT_INFO:
_infoHandler(header);
break;
case PKT_POWER:
_powerHandler(header);
break;
case PKT_SWITCH:
_switchHandler(header);
break;
case PKT_RGB:
_rgbHandler(header);
break;
case PKT_TEMP:
_tempHandler(header);
break;
case PKT_HUMID:
_humidHandler(header);
break;
}
}
}
bool RF24SensorNet::_write(uint16_t toaddr, uint16_t type, const void* message)
{
uint8_t size = 0;
switch(type % 64) {
case PKT_INFO:
break;
case PKT_POWER:
size = sizeof(pkt_power_t);
break;
case PKT_SWITCH:
size = sizeof(pkt_switch_t);
break;
case PKT_RGB:
size = sizeof(pkt_rgb_t);
break;
case PKT_TEMP:
size = sizeof(pkt_temp_t);
break;
case PKT_HUMID:
size = sizeof(pkt_humid_t);
break;
default:
// No such type :-(
return false;
}
RF24NetworkHeader header(toaddr, type);
return _network.write(header, message, size);
}
void RF24SensorNet::_infoHandler(RF24NetworkHeader header)
{
}
void RF24SensorNet::_powerHandler(RF24NetworkHeader header)
{
if (_powerRcvHandler != NULL) {
pkt_power_t payload;
_network.read(header, &payload, sizeof(payload));
_powerRcvHandler(header.from_node, payload.battery, payload.solar,
payload.vcc, payload.vs);
}
}
void RF24SensorNet::_switchHandler(RF24NetworkHeader header)
{
pkt_switch_t payload;
_network.read(header, &payload, sizeof(payload));
if (header.type > 64) {
if (_switchWriteHandler != NULL) {
_switchWriteHandler(header.from_node, payload.id,
payload.state, payload.timer);
}
} else if (header.type > 32) {
if (_switchReadHandler != NULL) {
_switchReadHandler(header.from_node, payload.id);
}
} else {
if (_switchRcvHandler != NULL) {
_switchRcvHandler(header.from_node, payload.id,
payload.state, payload.timer);
}
}
}
void RF24SensorNet::_rgbHandler(RF24NetworkHeader header)
{
pkt_rgb_t payload;
_network.read(header, &payload, sizeof(payload));
if (header.type > 64) {
if (_rgbWriteHandler != NULL) {
_rgbWriteHandler(header.from_node, payload.id,
payload.rgb, payload.timer);
}
} else if (header.type > 32) {
if (_rgbReadHandler != NULL) {
_rgbReadHandler(header.from_node, payload.id);
}
} else {
if (_rgbRcvHandler != NULL) {
_rgbRcvHandler(header.from_node, payload.id,
payload.rgb, payload.timer);
}
}
}
void RF24SensorNet::_tempHandler(RF24NetworkHeader header)
{
pkt_temp_t payload;
_network.read(header, &payload, sizeof(payload));
if (header.type > 32) {
if (_tempReadHandler != NULL) {
_tempReadHandler(header.from_node, payload.id);
}
} else {
if (_tempRcvHandler != NULL) {
_tempRcvHandler(header.from_node, payload.id, payload.temp);
}
}
}
void RF24SensorNet::_humidHandler(RF24NetworkHeader header)
{
pkt_humid_t payload;
_network.read(header, &payload, sizeof(payload));
if (header.type > 32) {
if (_humidReadHandler != NULL) {
_humidReadHandler(header.from_node, payload.id);
}
} else {
if (_humidRcvHandler != NULL) {
_humidRcvHandler(header.from_node, payload.id, payload.humidity);
}
}
}