-
Notifications
You must be signed in to change notification settings - Fork 2
/
receiver.c
executable file
·134 lines (106 loc) · 3.73 KB
/
receiver.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
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
#define F_CPU 12000000L
#include <inttypes.h>
#include "radio/packet.h"
#include "radio/radio.h"
#include <util/delay.h>
#include <avr/io.h>
#include "usart/ad_usart.h"
#include "misc/led.h"
char output[128];
volatile uint8_t rxflag = 0;
radiopacket_t packet;
uint8_t station_addr[5] = { 0xE4, 0xE4, 0xE4, 0xE4, 0xE4 };
// setup function is called once at the program's start
void setup()
{
// start the serial output module at 100000 bps
//Serial.begin(100000);
init_usart(BRATE_576);
// initialize the radio, including the SPI module
Radio_Init();
init_leds();
//snprintf(output, sizeof(output), "Status(setup): %x \n\r",status);
//put_str(output,0);
// configure the receive settings for radio pipe 0
Radio_Configure_Rx(RADIO_PIPE_0, station_addr, ENABLE);
// configure radio transciever settings.
Radio_Configure(RADIO_2MBPS, RADIO_HIGHEST_POWER);
// print a message to UART to indicate that the program has started up
snprintf(output, sizeof(output), "STATION START\n\r");
put_str(output,0);
}
// loop function is called over and over while the system runs.
void loop()
{
// if a packet hasn't been received, do nothing (see radio_rxhandler below).
if (!rxflag) return;
// if a packet has been received, get it.
if (Radio_Receive(&packet) != RADIO_RX_MORE_PACKETS)
{
// if there are no more packets on the radio, clear the receive flag;
// otherwise, we want to handle the next packet on the next loop iteration.
rxflag = 0;
//set_led(1,TOGGLE);
_delay_ms(175);
//set_led(1,TOGGLE);
}
// This station is only expecting to receive MESSAGE packets (see packet.h).
if (packet.type != MESSAGE)
//if (0)
{
snprintf(output, sizeof(output), "Error: wrong packet type (type %d).\n\r", packet.type);
put_str(output,1);
return;
}
// Set the transmit address to the one specified in the received message packet.
Radio_Set_Tx_Addr(packet.payload.message.address);
// Print out the message, along with the message ID and sender address.
snprintf(output, sizeof(output), "Message ID %d from 0x%.2X%.2X%.2X%.2X%.2X: '%s'\n\r",
packet.payload.message.messageid,
packet.payload.message.address[0],
packet.payload.message.address[1],
packet.payload.message.address[2],
packet.payload.message.address[3],
packet.payload.message.address[4],
packet.payload.message.messagecontent);
put_str(output,0);
//snprintf(output, sizeof(output), "Packet size: %d.\n\r", sizeof(packet));
//put_str(output,0);
// Reply to the sender by sending an ACK packet, reusing the packet data structure.
packet.type = ACK;
//packet.type = 3;
//snprintf(output, sizeof(output), "ACK: %d , MESSAGE: %d \n\r",ACK,MESSAGE);
//put_str(output,0);
//snprintf(output, sizeof(output), "timestamp: %x \n\r",packet.timestamp);
//put_str(output,0);
if (Radio_Transmit(&packet, RADIO_WAIT_FOR_TX) == RADIO_TX_MAX_RT)
{
// If the max retries was reached, the packet was not acknowledged.
// This usually occurs if the receiver was not configured correctly or
// if the sender didn't copy its address into the radio packet properly.
snprintf(output, sizeof(output), "Could not reply to sender.\n\r");
put_str(output,0);
}
else
{
// the transmission was completed successfully
snprintf(output, sizeof(output), "Replied to sender.\n\r");
put_str(output,0);
//set_led(0,TOGGLE);
//_delay_ms(100);
//set_led(0,TOGGLE);
}
// the LED should flash for a little over 50 ms.
//digitalWrite(13, LOW); turn off led
}
void radio_rxhandler(uint8_t pipe_number)
{
rxflag = 1;
}
// Arduino's default main function (included here for clarity)
int main()
{
setup();
for (;;) loop();
return 0;
}