forked from gioblu/PJON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReceiver.ino
111 lines (102 loc) · 3.35 KB
/
Receiver.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
#define PJON_INCLUDE_PORT
#include <PJONSoftwareBitBang.h>
int busy;
bool debug = true;
int fail;
float mistakes;
float test;
// Bus id definition
uint8_t bus_id[] = {0, 0, 0, 1};
// PJON object
PJONSoftwareBitBang bus(bus_id, 44);
void receiver_function(uint8_t *payload, uint16_t length, const PJON_Packet_Info &packet_info) {
/* Make use of the payload before sending something, the buffer where payload points to is
overwritten when a new message is dispatched */
if(debug) {
Serial.print("Header: ");
Serial.print(packet_info.header, BIN);
// If packet formatted for a shared medium
if(packet_info.header & PJON_MODE_BIT) {
Serial.print(" Receiver bus id: ");
Serial.print(packet_info.rx.bus_id[0]);
Serial.print(packet_info.rx.bus_id[1]);
Serial.print(packet_info.rx.bus_id[2]);
Serial.print(packet_info.rx.bus_id[3]);
Serial.print(" Receiver id: ");
Serial.print(packet_info.rx.id);
// If sender info is included
if(packet_info.header & PJON_TX_INFO_BIT) {
Serial.print(" Sender bus id: ");
Serial.print(packet_info.tx.bus_id[0]);
Serial.print(packet_info.tx.bus_id[1]);
Serial.print(packet_info.tx.bus_id[2]);
Serial.print(packet_info.tx.bus_id[3]);
}
}
// If sender device id is included
if(packet_info.header & PJON_TX_INFO_BIT) {
Serial.print(" Sender id: ");
Serial.print(packet_info.tx.id);
}
// Payload Length
Serial.print(" Length: ");
Serial.print(length);
// If port id is included
if(packet_info.header & PJON_PORT_BIT) {
Serial.print(" Port bit: ");
Serial.print(packet_info.port);
}
}
Serial.println();
};
void setup() {
/* Include a custom port, only packet including port 8001
are received others are filtered out. */
bus.include_port(8001);
bus.strategy.set_pin(12);
bus.set_receiver(receiver_function);
bus.begin();
Serial.begin(115200);
};
void loop() {
Serial.print("Starting 1 second communication speed test on port ");
Serial.println(bus.port);
long time = millis();
unsigned int response = 0;
while(millis() - time < 1000) {
response = bus.receive();
if(response == PJON_ACK) test++;
if(response == PJON_NAK) mistakes++;
if(response == PJON_BUSY) busy++;
if(response == PJON_FAIL) fail++;
}
Serial.print("Packet Overhead: ");
Serial.print(bus.packet_overhead(bus.last_packet_info.header) + 1);
Serial.print("B - Total: ");
Serial.print((unsigned int)((bus.packet_overhead(bus.last_packet_info.header) + 1) * test));
Serial.println("B");
Serial.print("Bandwidth: ");
Serial.print(test * (20 + bus.packet_overhead(bus.last_packet_info.header) + 1));
Serial.println("B/s");
Serial.print("Data throughput: ");
Serial.print(test * 20);
Serial.println("B/s");
Serial.print("Packets sent: ");
Serial.println(test);
Serial.print("Mistakes (error found with CRC): ");
Serial.println(mistakes);
Serial.print("Fail (no data found): ");
Serial.println(fail);
Serial.print("Busy (Channel is busy or affected by interference): ");
Serial.println(busy);
Serial.print("Accuracy: ");
Serial.print(100 - (100 / (test / mistakes)));
Serial.println(" %");
Serial.println(" --------------------- ");
// Avoid Serial interference during test flushing
Serial.flush();
busy = 0;
fail = 0;
mistakes = 0;
test = 0;
};