-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino-RF433-OOK-test.ino
More file actions
179 lines (150 loc) · 3.53 KB
/
Copy pathArduino-RF433-OOK-test.ino
File metadata and controls
179 lines (150 loc) · 3.53 KB
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
#define RF_RECEIVER_PIN 2
#define MAX_RING_SIZE 2048
#define MAX_FRAMES 16
#define MIN_PULSE_LENGTH 10
#define MIN_PULSE_NUM 16
#define MAX_PULSE_NUM 256
#define MIN_GAP_LENGTH 2000
#define MAX_GAP_LENGTH 10000
struct frame
{
int head;
int size;
};
static volatile unsigned long pulse_start;
static volatile bool pulse_sync = false;
static volatile uint16_t ring_data[MAX_RING_SIZE];
static volatile int ring_tail = 0;
static volatile int ring_size = MAX_RING_SIZE;
static volatile struct frame received_frames[MAX_FRAMES];
static volatile int received_num = 0;
static volatile struct frame* current_frame = NULL;
inline void receive_restart(bool sync)
{
ring_tail = current_frame->head;
current_frame->size = 0;
pulse_sync = sync;
}
inline void receive_finished(bool sync)
{
pulse_sync = sync;
ring_size -= current_frame->size;
received_num++;
if (received_num < MAX_FRAMES && ring_size >= MIN_PULSE_NUM * 2)
{
current_frame = received_frames + received_num;
current_frame->head = ring_tail;
current_frame->size = 0;
}
else
{
current_frame = NULL;
}
}
static void receive_interrupt_handler(void)
{
unsigned long now = micros();
unsigned long pulse_length = now - pulse_start;
bool pulse_level = digitalRead(RF_RECEIVER_PIN);
pulse_start = now;
if (!current_frame) return;
if (pulse_length < MIN_PULSE_LENGTH)
{
if (current_frame->size >= MIN_PULSE_NUM * 2)
{
receive_finished(false);
}
else
{
receive_restart(false);
}
}
else
{
if (!pulse_sync)
{
if (pulse_length >= MIN_GAP_LENGTH && pulse_level)
{
pulse_sync = true;
}
}
else
{
ring_data[ring_tail]= pulse_length;
ring_tail = (ring_tail < MAX_RING_SIZE - 1) ? ring_tail + 1 : 0;
current_frame->size++;
if (pulse_length >= MAX_GAP_LENGTH && pulse_level)
{
if (current_frame->size >= MIN_PULSE_NUM * 2)
{
receive_finished(true);
}
else
{
receive_restart(true);
}
}
else if (current_frame->size >= MAX_PULSE_NUM * 2)
{
receive_finished(false);
}
else if (current_frame->size >= ring_size)
{
receive_finished(false);
}
}
}
}
void setup(void)
{
pulse_start = micros();
received_frames[0].head = 0;
received_frames[0].size = 0;
current_frame = received_frames;
Serial.begin(115200);
pinMode(RF_RECEIVER_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(RF_RECEIVER_PIN), receive_interrupt_handler, CHANGE);
}
static void show_pulses(int n, int head)
{
Serial.println(";pulse data");
Serial.println(";version 1");
Serial.println(";timescale 1us");
n /= 2;
Serial.print(";ook ");
Serial.print(n);
Serial.println(" pulses");
Serial.println(";freq1 433849088");
for (; n > 0; n--)
{
Serial.print(ring_data[head]);
head= (head < MAX_RING_SIZE - 1) ? head + 1 : 0;
Serial.print(' ');
Serial.println(ring_data[head]);
head= (head < MAX_RING_SIZE - 1) ? head + 1 : 0;
}
Serial.println(";end");
}
void loop()
{
if (received_num > 0)
{
show_pulses(received_frames[0].size, received_frames[0].head);
noInterrupts();
ring_size += received_frames[0].size;
memcpy(received_frames, received_frames + 1, sizeof(received_frames) - sizeof(received_frames[0]));
received_num--;
if (!current_frame)
{
current_frame = received_frames + received_num;
current_frame->head = ring_tail;
current_frame->size = 0;
pulse_sync = false;
}
else
{
current_frame--;
}
interrupts();
}
}