1 |
// File: RF.c |
---|
2 |
//------------------------------------------------------------------------------ |
---|
3 |
// Author: Giovanni de Sanctis |
---|
4 |
// Email: info@lateral-technologies.com |
---|
5 |
//------------------------------------------------------------------------------ |
---|
6 |
// Date: Sep 2019 |
---|
7 |
// RF remote decoder |
---|
8 |
//------------------------------------------------------------------------------ |
---|
9 |
|
---|
10 |
|
---|
11 |
|
---|
12 |
#include "RF.h" |
---|
13 |
#include "mcc_generated_files/tmr1.h" |
---|
14 |
|
---|
15 |
#define CODE_LEN 24u //Number of bits in the code (including start stop etc.; we don't care) |
---|
16 |
#define BUT_MASK (0x000000FFul) |
---|
17 |
#define CODE_MASK (0xFFFFFE00ul) |
---|
18 |
#define CODE1 (0x00430000ul) //RF code1 to match (433) |
---|
19 |
#define CODE2 (0x006AAA00ul) //RF code2 to match (315) |
---|
20 |
#define CODE3 (0x00011200ul) //RF code3 to match (new 315) |
---|
21 |
|
---|
22 |
#define T_BASE_NS 500u //T1 Clock in ns |
---|
23 |
#define T_START_MIN (8000000u/T_BASE_NS) //Min long space before code (8ms) |
---|
24 |
|
---|
25 |
#define T_MARK0_MIN ((0.8* 333333u)/T_BASE_NS) //Min 0 mark |
---|
26 |
#define T_MARK0_MAX ((1.8* 333333u)/T_BASE_NS) //Max 0 mark |
---|
27 |
|
---|
28 |
#define T_MARK1_MIN ((0.8* 1000000u)/T_BASE_NS) //Min 0 mark |
---|
29 |
#define T_MARK1_MAX ((1.8*1000000u)/T_BASE_NS) //Max 0 mark |
---|
30 |
|
---|
31 |
#define T_MARK_TOL 80000u/T_BASE_NS //Tolerance +/- 0.08ms |
---|
32 |
|
---|
33 |
#define N_LONG_PRESS 10 //# code matches for long press |
---|
34 |
|
---|
35 |
#define RF_A1 0x50 |
---|
36 |
#define RF_B1 0x48 |
---|
37 |
#define RF_C1 0x44 |
---|
38 |
#define RF_D1 0x42 |
---|
39 |
|
---|
40 |
#define RF_A2 0x18 |
---|
41 |
#define RF_B2 0x80 |
---|
42 |
#define RF_C2 0x06 |
---|
43 |
#define RF_D2 0x60 |
---|
44 |
|
---|
45 |
#define RF_A3 0x81 |
---|
46 |
#define RF_B3 0x82 |
---|
47 |
#define RF_C3 0x84 |
---|
48 |
#define RF_D3 0x88 |
---|
49 |
|
---|
50 |
|
---|
51 |
uint8_t bitPtr; //Number of bits received |
---|
52 |
uint8_t matches; //Number of code matches (for button press length) |
---|
53 |
uint8_t attempts; //Number of restarts before code detected |
---|
54 |
uint8_t waitingRelease; |
---|
55 |
uint32_t code; //Code being read |
---|
56 |
uint32_t lastCode1; //Last code read |
---|
57 |
uint32_t lastCode2; //2nd last code read |
---|
58 |
uint16_t tMark0min,tMark0max,tMark1min,tMark1max; //Current durations for 1 and 0 codes |
---|
59 |
|
---|
60 |
|
---|
61 |
void intHandler() |
---|
62 |
{ |
---|
63 |
uint16_t t; |
---|
64 |
t = TMR1_ReadTimer(); |
---|
65 |
|
---|
66 |
if (bitPtr==0) |
---|
67 |
{ |
---|
68 |
TMR1G_Polarity(0); |
---|
69 |
if (t>T_START_MIN) |
---|
70 |
{ |
---|
71 |
code = 0; |
---|
72 |
attempts++; |
---|
73 |
bitPtr++; |
---|
74 |
TMR1G_Polarity(1); //Looking for marks now |
---|
75 |
} |
---|
76 |
} |
---|
77 |
else |
---|
78 |
{ |
---|
79 |
if (bitPtr==1) |
---|
80 |
{ |
---|
81 |
if ((t>T_MARK1_MIN) && (t<T_MARK1_MAX)) |
---|
82 |
{ |
---|
83 |
tMark0min = t/3-T_MARK_TOL; |
---|
84 |
tMark0max = t/3+T_MARK_TOL; |
---|
85 |
tMark1min = t-(3*T_MARK_TOL); |
---|
86 |
tMark1max = t+(3*T_MARK_TOL); |
---|
87 |
} |
---|
88 |
else if ((t>T_MARK0_MIN) && (t<T_MARK0_MAX)) |
---|
89 |
{ |
---|
90 |
tMark0min = t-T_MARK_TOL; |
---|
91 |
tMark0max = t+T_MARK_TOL; |
---|
92 |
tMark1min = 3*t-(3*T_MARK_TOL); |
---|
93 |
tMark1max = 3*t+(3*T_MARK_TOL); |
---|
94 |
|
---|
95 |
} |
---|
96 |
else bitPtr=0; |
---|
97 |
} |
---|
98 |
|
---|
99 |
if (bitPtr<CODE_LEN) |
---|
100 |
{ |
---|
101 |
if ( ((t>tMark0min) && (t<tMark0max) ) || ((t>tMark1min)&&(t<tMark1max)) ) |
---|
102 |
{ |
---|
103 |
bitPtr++; |
---|
104 |
code<<=1; |
---|
105 |
if (t>tMark1min) |
---|
106 |
{ |
---|
107 |
code++; |
---|
108 |
} |
---|
109 |
} |
---|
110 |
else |
---|
111 |
{ |
---|
112 |
bitPtr=0; //If pulse too long or too short reset |
---|
113 |
} |
---|
114 |
} |
---|
115 |
else |
---|
116 |
{ |
---|
117 |
bitPtr = 0; |
---|
118 |
attempts = 0; |
---|
119 |
if (lastCode1) lastCode2 = lastCode1; |
---|
120 |
lastCode1 = code; |
---|
121 |
if (lastCode1 == lastCode2) matches++; |
---|
122 |
} |
---|
123 |
} |
---|
124 |
|
---|
125 |
TMR1_WriteTimer(0x0000); |
---|
126 |
TMR1_StartSinglePulseAcquisition(); |
---|
127 |
|
---|
128 |
} |
---|
129 |
|
---|
130 |
void initRF() |
---|
131 |
{ |
---|
132 |
bitPtr=0; |
---|
133 |
TMR1G_Polarity(0); |
---|
134 |
TMR1G_SetInterruptHandler(intHandler); |
---|
135 |
TMR1_StartSinglePulseAcquisition(); |
---|
136 |
matches = 0; |
---|
137 |
code = 0; |
---|
138 |
lastCode1 = 0; |
---|
139 |
lastCode2 = 0; |
---|
140 |
//waitingRelease = 0; |
---|
141 |
attempts = 0; |
---|
142 |
} |
---|
143 |
|
---|
144 |
uint8_t rfGetNewCode() |
---|
145 |
{ |
---|
146 |
uint8_t res; |
---|
147 |
uint32_t maskedCode = lastCode2 & CODE_MASK; |
---|
148 |
if (lastCode2 == 0) return 0; |
---|
149 |
if ((maskedCode!=CODE1)&&(maskedCode!=CODE2)&&(maskedCode!=CODE3)) return 0; |
---|
150 |
if (matches<2) return 0; |
---|
151 |
|
---|
152 |
res=lastCode1 & BUT_MASK; |
---|
153 |
|
---|
154 |
switch (res) |
---|
155 |
{ |
---|
156 |
case RF_A1: |
---|
157 |
case RF_A2: |
---|
158 |
case RF_A3: |
---|
159 |
res = RF_A; |
---|
160 |
break; |
---|
161 |
|
---|
162 |
case RF_B1: |
---|
163 |
case RF_B2: |
---|
164 |
case RF_B3: |
---|
165 |
res = RF_B; |
---|
166 |
break; |
---|
167 |
|
---|
168 |
case RF_C1: |
---|
169 |
case RF_C2: |
---|
170 |
case RF_C3: |
---|
171 |
res = RF_C; |
---|
172 |
break; |
---|
173 |
|
---|
174 |
case RF_D1: |
---|
175 |
case RF_D2: |
---|
176 |
case RF_D3: |
---|
177 |
res = RF_D; |
---|
178 |
break; |
---|
179 |
} |
---|
180 |
|
---|
181 |
if (matches>N_LONG_PRESS) //Passed long press time |
---|
182 |
{ |
---|
183 |
if (waitingRelease == 0) //..for the first time |
---|
184 |
{ |
---|
185 |
waitingRelease = 1; |
---|
186 |
return res|RF_LONG_MASK; |
---|
187 |
} |
---|
188 |
else res = 0; |
---|
189 |
} |
---|
190 |
|
---|
191 |
if (attempts < 2) return 0; //Button still pressed |
---|
192 |
else |
---|
193 |
{ |
---|
194 |
lastCode1=lastCode2=code=0; |
---|
195 |
matches = 0; |
---|
196 |
waitingRelease = 0; |
---|
197 |
} |
---|
198 |
|
---|
199 |
return res; |
---|
200 |
} |
---|
201 |
|
---|
202 |
uint8_t isRFreceiving() |
---|
203 |
{ |
---|
204 |
return (matches>0 && attempts<1); |
---|
205 |
} |
---|
206 |
|
---|