/
MinimalInterrupt.ino
140 lines (97 loc) · 3.56 KB
/
MinimalInterrupt.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
/*
* Copyright (c) 2014, Ivor Wanders
* MIT License, see the LICENSE.md file in the root folder.
*/
#include <SPI.h>
#include <plainRFM69.h>
// slave select pin.
#define SLAVE_SELECT_PIN 10
// connected to the reset pin of the RFM69.
#define RESET_PIN 23
// tie this pin down on the receiver.
#define SENDER_DETECT_PIN 15
// Pin DIO 2 on the RFM69 is attached to this digital pin.
// Pin should have interrupt capability.
#define DIO2_PIN 0
/*
This is a minimal example with the interrupt to call poll().
It transmits a 4 byte integer every 500 ms.
*/
plainRFM69 rfm = plainRFM69(SLAVE_SELECT_PIN);
void sender(){
uint32_t start_time = millis();
uint32_t counter = 0; // the counter which we are going to send.
while(true){
if (!rfm.canSend()){
continue; // sending is not possible, already sending.
}
if ((millis() - start_time) > 500){ // every 500 ms.
start_time = millis();
// be a little bit verbose.
Serial.print("Send:");Serial.println(counter);
// send the number of bytes equal to that set with setPacketLength.
// read those bytes from memory where counter starts.
rfm.send(&counter);
counter++; // increase the counter.
}
}
}
void receiver(){
uint32_t counter = 0; // to count the messages.
while(true){
while(rfm.available()){ // for all available messages:
uint32_t received_count = 0; // temporary for the new counter.
uint8_t len = rfm.read(&received_count); // read the packet into the new_counter.
// print verbose output.
Serial.print("Packet ("); Serial.print(len); Serial.print("): "); Serial.println(received_count);
if (counter+1 != received_count){
// if the increment is larger than one, we lost one or more packets.
Serial.println("Packetloss detected!");
}
// assign the received counter to our counter.
counter = received_count;
}
}
}
void interrupt_RFM(){
rfm.poll(); // in the interrupt, call the poll function.
}
void setup(){
Serial.begin(9600);
SPI.begin();
bareRFM69::reset(RESET_PIN); // sent the RFM69 a hard-reset.
rfm.setRecommended(); // set recommended paramters in RFM69.
rfm.setPacketType(false, false); // set the used packet type.
rfm.setBufferSize(2); // set the internal buffer size.
rfm.setPacketLength(4); // set the packet length.
rfm.setFrequency((uint32_t) 434*1000*1000); // set the frequency.
// baudrate is default, 4800 bps now.
rfm.receive();
// set it to receiving mode.
/*
setup up interrupts such that we don't have to call poll() in a loop.
*/
// tell the RFM to represent whether we are in automode on DIO 2.
rfm.setDioMapping1(RFM69_PACKET_DIO_2_AUTOMODE);
// set pinmode to input.
pinMode(DIO2_PIN, INPUT);
// Tell the SPI library we're going to use the SPI bus from an interrupt.
SPI.usingInterrupt(DIO2_PIN);
// hook our interrupt function to any edge.
attachInterrupt(DIO2_PIN, interrupt_RFM, CHANGE);
// start receiving.
rfm.receive();
pinMode(SENDER_DETECT_PIN, INPUT_PULLUP);
delay(5);
}
void loop(){
if (digitalRead(SENDER_DETECT_PIN) == LOW){
Serial.println("Going Receiver!");
receiver();
// this function never returns and contains an infinite loop.
} else {
Serial.println("Going sender!");
sender();
// idem.
}
}