-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrf_zeus_rx.cpp
179 lines (154 loc) · 4.38 KB
/
rf_zeus_rx.cpp
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
/*
* Class to receive and decode signals from wireless sensors of an old
* security system.
*
* See the README.md for more details.
*
* Copyright 2017 David M Kent.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "rf_zeus_rx.h"
#include "rf_zeus_rx_internal.h"
#include "src/ZeusRfDecode.h"
void RF_ZEUS_RX::init(unsigned int input_pin) {
_input_pin = input_pin;
pinMode(_input_pin, INPUT);
attachInterrupt(digitalPinToInterrupt(_input_pin), transition, CHANGE);
}
void RF_ZEUS_RX::set_buffer(byte* buffer) {
this->_buffer = buffer;
count = 0;
}
unsigned int RF_ZEUS_RX::get_byte_count() {
return count;
}
/*
* The ISR used to flag we've had a transition on the input.
*/
void RF_ZEUS_RX::handle_interrupt() {
new_state = digitalRead(_input_pin);
if (new_state != is_high) {
found_transition = true;
is_high = new_state;
}
}
/*
* Method to block until we change state on input.
*
* Note the yield statement; the ESP8266 has a watchdog timer
* that will reboot the system if we block too long. The yield
* allows the system to go and do other things, such as maintain
* wifi.
*/
inline void RF_ZEUS_RX::_block_until_transition() {
found_transition = false;
while (!found_transition) {
yield();
}
}
/*
* Get pair of transitions and store in passed pointers.
*/
void RF_ZEUS_RX::get_transition_pair(unsigned long* pos_start, unsigned long* pos_end,
unsigned long* width_high, unsigned long* width_low){
unsigned long middle;
(*pos_start) = micros();
_block_until_transition();
middle = micros();
(*width_high) = middle - (*pos_start);
_block_until_transition();
(*pos_end) = micros();
(*width_low) = (*pos_end) - middle;
}
/*
* Advance position until we hit a HIGH pulse.
*/
void RF_ZEUS_RX::advance_to_high_pulse(){
if (!is_high) {
_block_until_transition();
}
}
/*
* Handle recieve of sync bit
*/
void RF_ZEUS_RX::handle_sync_bit(unsigned long pos) {
return;
}
/*
* Handle a completed decoded data byte
*/
void RF_ZEUS_RX::handle_data_byte(unsigned long byte_start, unsigned long byte_end, byte data) {
_buffer[count++] = data;
}
/*
* The following are c-style functions wrapping a singleton RF_ZEUS_RX instance.
*
* This is needed to get interrupt and callback functions behaving.
* Full use of c++ STL std::function objects would get around this...
* No other simple way to pass around member functions (with pointer to
* actual instance) without this kind of construct.
*/
// The singleton itself.
RF_ZEUS_RX radio;
/*
* ISR - proxies to handle_interrupt.
*/
void transition() {
radio.handle_interrupt();
}
void _single_advance_to_high() {
radio.advance_to_high_pulse();
}
void _single_get_pair(unsigned long* pos_start, unsigned long* pos_end,
unsigned long* width_high, unsigned long* width_low) {
radio.get_transition_pair(pos_start, pos_end, width_high, width_low);
}
void _single_mark_byte(unsigned long byte_start, unsigned long byte_end, byte data) {
radio.handle_data_byte(byte_start, byte_end, data);
}
void _single_mark_sync(unsigned long pos){
radio.handle_sync_bit(pos);
}
/*
* Finally, the public API. Three functions from rf_zeus_rx.h, used by sketch.
*/
/*
* Initialise the reciever.
*/
void init_radio(unsigned int input_pin){
radio.init(input_pin);
}
/*
* Block until we detect data preamble completion.
*/
unsigned long wait_for_data(){
return block_until_data(
&_single_advance_to_high,
&_single_get_pair,
&_single_mark_sync
);
}
/*
* Receive, decode and write data to buffer.
*/
void get_data(unsigned int* nbytes, byte* data){
radio.set_buffer(data);
receive_and_process_data(
0,
&_single_get_pair,
&_single_mark_byte
);
(*nbytes) = radio.get_byte_count();
}