-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdva_pkt.ino
267 lines (240 loc) · 6.22 KB
/
dva_pkt.ino
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
264
265
266
267
/* dva_pkt file
* Defines the main
* functions and vars to
* send and receive data
*--------------------------*/
//Includes and defines
#ifndef _INCLUDED_WIRE
#include <VirtualWire.h>
#include <Wire.h>
#define _INCLUDED_WIRE
#endif
#include "DVA.h"
/* rf_setup function
* Used to setup the rf modules
* pins and variables
*---------------------------------
* returns: void
* ------------------------------*/
void rf_setup(){
vw_set_tx_pin(TX_PIN);
vw_set_rx_pin(RX_PIN);
vw_set_ptt_pin(TX_EN_PIN);
pinMode(LED_PIN, OUTPUT);
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000);
vw_rx_start();
}
/* rx_from_ble function
* Reads Serial (bluetooth) and searches
* for a valid packet.
*---------------------------------
* @pkt: packet structure used to
* store the results of the
* read process
*---------------------------------
* returns: true on successfull
* read or false if error
* ------------------------------*/
boolean rx_from_ble(Dva_pkt *pkt){
char currentValue;
boolean result = false;
pkt->initialized = false;
while(Serial2.available() > 0){
currentValue = Serial2.read();
if(currentValue == PKT_END){
break;
}
else{
if(!pkt->initialized){
pkt->data = String(currentValue);
pkt->len = 1;
pkt->initialized = true;
}
else{
pkt->len += 1;
pkt->data += currentValue;
}
}
}
//Packet must be something like "DvA IDIDID (packet_type) PAYLOAD #"; the smallest packet is the CHMODE packet
if(pkt->len >= PKT_CHMODE_LEN){
if(pkt->data.startsWith(PREAMBLE) && ((pkt->data.substring(PKT_ID_START_POS, PKT_ID_END_POS + 1)).equals(DVA_ID))){
pkt->type = pkt->data.charAt(PKT_TYPE_POS) - '0';
switch(pkt->type){
case BLE_CHMODE :
if(pkt->len == PKT_CHMODE_LEN){
if(pkt->data.charAt(10) == '1' || pkt->data.charAt(10) == '2'){
result = true;
}
else
result = false;
}
else
result = false;
break;
case BLE_UPDATELOCATION :
//Packet len must be x;
result = (pkt->len == PKT_GEO_LEN) ? true : false;
break;
default :
result = false;
break;
}
}
else{
result = false;
}
}
return result;
}
/* tx_to_ble function
* Sends data via Bluetoth
* -------------------------------
* @pkt: The packet we will send
* -------------------------------
* returns: void
* -----------------------------*/
void tx_to_ble(Dva_pkt *pkt){
Serial2.print(pkt->data.c_str());
}
/* tx_rf function
* Sends data via Bluetoth
* -------------------------------
* @pos: The dva structure we
* will read the geoposition
* information from
* @pkt: The packet we will send
* -------------------------------
* returns: void
* -----------------------------*/
void tx_rf(Dva *pos, int pkt_type){
Dva_pkt pkt;
pkt.type = 0;
switch(pkt_type){
case RF_SEND_LOCATION :
pkt.type = pkt_type;
pkt.len = 26;
pkt.data = PREAMBLE;
pkt.data += DVA_ID;
pkt.data += (char)(pkt_type + '0');
pkt.data += parseFloatToString(pos->latitude);
pkt.data += parseFloatToString(pos->longitude);
pkt.data += '#';
break;
}
if(pkt.type != 0){
Serial.println(pkt.data);
vw_send((uint8_t*)(pkt.data.c_str()), pkt.len);
vw_wait_tx();
}
}
/* rx_rf function
* Reads rf and searches
* for a valid packet.
*---------------------------------
* @pkt: packet structure used to
* store the results of the
* read process
*---------------------------------
* returns: true on successfull
* read or false if error
* ------------------------------*/
boolean rx_rf(Dva_pkt *pkt){
boolean foundEnd = false;
uint8_t message[VW_MAX_MESSAGE_LEN];
uint8_t messageLength = VW_MAX_MESSAGE_LEN;
if(vw_get_message(message, &messageLength)){
for(int i = 0; i < messageLength; i++){
if(foundEnd){
//We want to clear the buffer even if the end was found
continue;
}
else{
if(i == 0){
pkt->data = String((char)message[i]);
pkt->len = 1;
}
else if(message[i] == PKT_END){
foundEnd = true;
}
else{
pkt->data += (char)message[i];
pkt->len += 1;
}
}
}
}
else{
return false;
}
if(pkt->len == 26){
if(pkt->data.startsWith(PREAMBLE) && (pkt->data.charAt(PKT_TYPE_POS) - '0') == RF_SEND_LOCATION){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
/* pkt_uptade_geoposition function
* Modifies geoposition given
* a certain packet
*---------------------------------
* @pkt: packet structure used to
* read data
* @pos: geoposition structure;
* will store the new
* geoposition
*---------------------------------
* returns: void
* ------------------------------*/
void pkt_update_geoposition(Dva_pkt *pkt, Dva *pos){
pos->latitude = atof(pkt->data.substring(PKT_LATITUDE_START_POS, PKT_LATITUDE_END_POS +1).c_str());
pos->longitude = atof(pkt->data.substring(PKT_LONGITUDE_START_POS, PKT_LONGITUDE_END_POS + 1).c_str());
}
/* parseFloatToString function
* Returns a custom string of
* len 8
*---------------------------------
* @value: the float to parse
*---------------------------------
* returns: String value of
* the float
* ------------------------------*/
String parseFloatToString(float value){
String result = String((int) value);
float temp = value - (float)((int)value);
result += '.';
for(int i = result.length() + 1; i <= PKT_MAX_PRESITION; i++){
if(temp == 0.0){
result += '0';
}
else{
temp *= 10.0;
result += (char)((int) temp + '0');
temp = temp - (float)((int)temp);
}
}
return result;
}
/* get_pkt_info function
* Gets all the usefull information
* from a received packet
*---------------------------------
* @pkt: The packet to parse
*---------------------------------
* returns: A DVA struct with the
* valid values set
*---------------------------------*/
Dva get_pkt_info(Dva_pkt *pkt){
Dva newDva;
newDva.longitude = atof(pkt->data.substring(PKT_LONGITUDE_START_POS, PKT_LONGITUDE_END_POS +1).c_str());
newDva.latitude = atof(pkt->data.substring(PKT_LATITUDE_START_POS, PKT_LATITUDE_END_POS +1).c_str());
newDva.id = pkt->data.substring(PKT_ID_START_POS, PKT_ID_END_POS +1);
newDva.initialized = true;
return newDva;
}