-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradio.c
76 lines (59 loc) · 1.32 KB
/
radio.c
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
#include <stdio.h>
#include <stdlib.h>
#include "radio.h"
#include "RFM12B.h"
#include "usart.h"
#include "tools.h"
void initializeRadio(uint8_t nr) {
node_id = nr;
Initialize(nr, NETWORKID);
}
void sendRadio(char payload, uint8_t timesToResend) {
// We want an ack
uint8_t requestACK = 1;
//sendString("Transmitting...\r\n");
char payloadArray[1];
payloadArray[0] = payload;
// Send to node_id 1 or 2 (the opposite)
Send(((node_id % 2) + 1), payloadArray, 1, requestACK);
//sendString("Transmitted...\r\n");
if (requestACK) {
//sendString(" Waiting for ACK...");
if (waitForAck()) {
//sendString("Ack OK!");
} else {
if (timesToResend > 0) {
//sendString("No ack - resending...");
sendRadio(payload, timesToResend - 1);
} else {
//sendString("No ack - resending cancelled...");
}
}
}
}
char waitForAck() {
uint64_t waitCounter = 4000;
while(waitCounter > 0) {
if (ACKReceived((node_id % 2) + 1)) {
return 1;
}
waitCounter--;
}
return 0;
}
char receiveRadio() {
if (ReceiveComplete()) {
if (CRCPass()) {
//uint8_t sender_node_id = GetSender();
if (ACKRequested()) {
SendACK();
//sendString(" - ACK sent");
}
return (char) Data[0];
} else {
//sendString("BAD-CRC");
return '0';
}
}
return '0';
}