39
39
* License: GNU GPL v2 or later
40
40
*/
41
41
42
+ #include < RF433Transceiver.h>
43
+ #include < NexaCommand.h>
44
+
42
45
#include < limits.h>
43
46
44
47
#define DEBUG 0
45
48
46
49
#define ARRAY_LENGTH (a ) ((sizeof (a)) / (sizeof (a)[0 ]))
47
50
48
51
// Adjust the following to match where the RF receiver is connected.
49
- #define RF_SETUP () bitClear(DDRC, 0 )
50
- #define RF_READ () bitRead(PINC, 0 )
52
+ RF433Transceiver rf_port (1 );
51
53
52
54
const size_t BUF_SIZE = 1280 ;
53
55
char buf[BUF_SIZE];
@@ -61,33 +63,18 @@ enum {
61
63
62
64
byte cur_bit = 0 ;
63
65
64
- enum nexa_cmd_version {
65
- NEXA_INVAL = 0 , // Unknown/invalid version
66
- NEXA_12BIT = 1 , // Old 12-bit command format: DDDDDDDD011S
67
- NEXA_32BIT = 2 // New 32-bit command format: D{24}10GSCCCC
68
- };
69
-
70
- struct nexa_cmd {
71
- enum nexa_cmd_version version; // Command version/format, One of enum nexa_cmd_version
72
- byte device[3 ]; // 24-bit (A) or 8-bit (B) device identifier
73
- byte channel; // 4-bit intra-device channel identifier (= 0 for B)
74
- bool group; // true iff the group bit is set (false for B)
75
- bool state; // ON - true, OFF - false
76
- };
77
-
78
66
void setup ()
79
67
{
80
- RF_SETUP ();
81
68
Serial.begin (115200 );
82
69
Serial.println (F (" nexa_decoder ready:" ));
83
70
}
84
71
85
72
/*
86
73
* Read the next pulse from the RF receiver and return it.
87
74
*
88
- * This will block until RF_READ() changes. At that point it will return
89
- * an int whose absolute value is the pulse length in µs, and the sign
90
- * is positive for a HIGH pulse and negative for a LOW pulse.
75
+ * This will block until the RF input bit changes. At that point it will
76
+ * return an int whose absolute value is the pulse length in µs, and the
77
+ * sign is positive for a HIGH pulse and negative for a LOW pulse.
91
78
*
92
79
* This function must be called more often than the shortest pulse to be
93
80
* detected.
@@ -100,13 +87,13 @@ void setup()
100
87
int next_pulse ()
101
88
{
102
89
static unsigned long start = 0 ;
103
- static int state = false ;
90
+ static bool state = false ;
104
91
105
- while (state == RF_READ ())
92
+ while (state == rf_port. rx_pin ())
106
93
; // spin until state changes
107
94
unsigned long now = micros ();
108
95
bool ret_state = state;
109
- state = RF_READ ();
96
+ state = rf_port. rx_pin ();
110
97
111
98
int ret;
112
99
if (ret_state)
@@ -167,40 +154,6 @@ int quantize_pulse(int p)
167
154
}
168
155
}
169
156
170
- /*
171
- * Return a 6-letter string containing the given 3 bytes in hex notation.
172
- */
173
- const char *three_bytes_in_hex (const byte s[3 ])
174
- {
175
- static char buf[7 ]; // 6 hex digits + NUL
176
- const char hex[] = " 0123456789ABCDEF" ;
177
- buf[0 ] = hex[s[0 ] >> 4 & B1111];
178
- buf[1 ] = hex[s[0 ] >> 0 & B1111];
179
- buf[2 ] = hex[s[1 ] >> 4 & B1111];
180
- buf[3 ] = hex[s[1 ] >> 0 & B1111];
181
- buf[4 ] = hex[s[2 ] >> 4 & B1111];
182
- buf[5 ] = hex[s[2 ] >> 0 & B1111];
183
- buf[6 ] = ' \0 ' ;
184
- return buf;
185
- }
186
-
187
- /*
188
- * Transmit the given code on the serial port.
189
- */
190
- void print_cmd (const struct nexa_cmd & cmd)
191
- {
192
- Serial.print (cmd.version , HEX);
193
- Serial.print (' :' );
194
- Serial.print (three_bytes_in_hex (cmd.device ));
195
- Serial.print (' :' );
196
- Serial.print (cmd.group ? ' 1' : ' 0' );
197
- Serial.print (' :' );
198
- Serial.print (cmd.channel , HEX);
199
- Serial.print (' :' );
200
- Serial.println (cmd.state ? ' 1' : ' 0' );
201
- Serial.flush ();
202
- }
203
-
204
157
/*
205
158
* Add the given bit into the given byte array at the given bit index.
206
159
*
@@ -220,13 +173,13 @@ void add_bit(byte * dst, size_t dst_len, size_t bit_idx, int bit_val)
220
173
}
221
174
222
175
/*
223
- * Initialize the given old-style 12-bit nexa_cmd from the 12 bits at buf.
176
+ * Initialize the given old-style NexaCommand from the 12 bits at buf.
224
177
*
225
178
* The command bits are of the form: DDDDDDDD011S
226
179
*/
227
- void parse_12bit_cmd (struct nexa_cmd & cmd, const char buf[12 ])
180
+ void parse_12bit_cmd (NexaCommand & cmd, const char buf[12 ])
228
181
{
229
- cmd.version = NEXA_12BIT;
182
+ cmd.version = NexaCommand:: NEXA_12BIT;
230
183
cmd.device [0 ] = 0 ;
231
184
cmd.device [1 ] = 0 ;
232
185
for (size_t i = 0 ; i < 8 ; ++i)
@@ -237,14 +190,14 @@ void parse_12bit_cmd(struct nexa_cmd & cmd, const char buf[12])
237
190
}
238
191
239
192
/*
240
- * Initialize the given new-style 32-bit nexa_cmd from the 32 bits at buf.
193
+ * Initialize the given new-style NexaCommandfrom the 32 bits at buf.
241
194
*
242
195
* The command bits are of the form: DDDDDDDDDDDDDDDDDDDDDDDD10GSCCCC
243
196
*/
244
- void parse_32bit_cmd (struct nexa_cmd & cmd, const char buf[32 ])
197
+ void parse_32bit_cmd (NexaCommand & cmd, const char buf[32 ])
245
198
{
246
199
size_t i;
247
- cmd.version = NEXA_32BIT;
200
+ cmd.version = NexaCommand:: NEXA_32BIT;
248
201
for (i = 0 ; i < 24 ; ++i)
249
202
add_bit (cmd.device , ARRAY_LENGTH (cmd.device ), i,
250
203
buf[i] == ' 1' );
@@ -257,12 +210,12 @@ void parse_32bit_cmd(struct nexa_cmd & cmd, const char buf[32])
257
210
}
258
211
259
212
/*
260
- * Parse data in buf[0..buf_pos] into nexa_cmds sent over the serial port.
213
+ * Parse data in buf[0..buf_pos] into NexaCommands sent over the serial port.
261
214
*/
262
215
void decode_buf ()
263
216
{
264
217
int state = -1 ; // Initial state - before SYNC
265
- enum nexa_cmd_version version = NEXA_INVAL;
218
+ NexaCommand::Version version = NexaCommand:: NEXA_INVAL;
266
219
for (size_t i = 0 ; i < buf_pos; i++) {
267
220
char b = buf[i];
268
221
if (state > 0 ) { // Expecting another data bit
@@ -273,32 +226,33 @@ void decode_buf()
273
226
}
274
227
275
228
if (state == 0 ) { // Finished reading data bits
276
- struct nexa_cmd cmd;
277
- if (version == NEXA_12BIT)
229
+ NexaCommand cmd;
230
+ if (version == NexaCommand:: NEXA_12BIT)
278
231
parse_12bit_cmd (cmd, buf + i + 1 - 12 );
279
- else if (version == NEXA_32BIT)
232
+ else if (version == NexaCommand:: NEXA_32BIT)
280
233
parse_32bit_cmd (cmd, buf + i + 1 - 32 );
281
- print_cmd (cmd);
234
+ cmd.print (Serial);
235
+ Serial.flush ();
282
236
283
237
state = -1 ; // Look for next SYNC
284
238
}
285
239
else if (state == -1 ) { // Looking for SYNC
286
240
if (b == ' A' ) { // SYNC for format A
287
- version = NEXA_32BIT;
241
+ version = NexaCommand:: NEXA_32BIT;
288
242
state = 32 ; // Expect 32 data bits
289
243
}
290
244
else if (b == ' B' ) { // SYNC for format B
291
- version = NEXA_12BIT;
245
+ version = NexaCommand:: NEXA_12BIT;
292
246
state = 12 ; // Expect 12 data bits
293
247
}
294
248
}
295
249
}
296
250
}
297
251
298
- void loop ( )
252
+ void handle_rf_pulse ( int pulse )
299
253
{
300
254
new_state = UNKNOWN;
301
- int p = quantize_pulse (next_pulse () ); // current pulse
255
+ int p = quantize_pulse (pulse ); // current pulse
302
256
switch (p) {
303
257
case -5 : // LOW: 8192µs <= pulse < 16384µs
304
258
new_state = SX1;
@@ -379,7 +333,7 @@ void loop()
379
333
buf[buf_pos] = ' \0 ' ;
380
334
Serial.println (buf);
381
335
Serial.flush ();
382
- #endif // DEBUG
336
+ #endif
383
337
// Reached end of valid data: Decode and print buffer.
384
338
// ...but only if it is longer than the shortest command
385
339
// (Format B: SYNC + 12 data bits)
@@ -389,3 +343,8 @@ void loop()
389
343
}
390
344
cur_state = new_state;
391
345
}
346
+
347
+ void loop ()
348
+ {
349
+ handle_rf_pulse (next_pulse ());
350
+ }
0 commit comments