Skip to content

Commit 9b7b10a

Browse files
committed
Moving towards unifying nexa_decoder and nexa_encoder
Common parts are refactored into a new nexa_node library
1 parent 17428d6 commit 9b7b10a

File tree

4 files changed

+53
-437
lines changed

4 files changed

+53
-437
lines changed

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@
99
[submodule "DHT_sensor_library"]
1010
path = libraries/DHT_sensor_library
1111
url = git://github.com/adafruit/DHT-sensor-library.git
12+
13+
[submodule "nexa_node"]
14+
path = libraries/nexa_node
15+
url = git://github.com/jherland/nexa_node.git

libraries/nexa_node

Submodule nexa_node added at dde9685

nexa_decoder/nexa_decoder.ino

Lines changed: 33 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@
3939
* License: GNU GPL v2 or later
4040
*/
4141

42+
#include <RF433Transceiver.h>
43+
#include <NexaCommand.h>
44+
4245
#include <limits.h>
4346

4447
#define DEBUG 0
4548

4649
#define ARRAY_LENGTH(a) ((sizeof (a)) / (sizeof (a)[0]))
4750

4851
// 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);
5153

5254
const size_t BUF_SIZE = 1280;
5355
char buf[BUF_SIZE];
@@ -61,33 +63,18 @@ enum {
6163

6264
byte cur_bit = 0;
6365

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-
7866
void setup()
7967
{
80-
RF_SETUP();
8168
Serial.begin(115200);
8269
Serial.println(F("nexa_decoder ready:"));
8370
}
8471

8572
/*
8673
* Read the next pulse from the RF receiver and return it.
8774
*
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.
9178
*
9279
* This function must be called more often than the shortest pulse to be
9380
* detected.
@@ -100,13 +87,13 @@ void setup()
10087
int next_pulse()
10188
{
10289
static unsigned long start = 0;
103-
static int state = false;
90+
static bool state = false;
10491

105-
while (state == RF_READ())
92+
while (state == rf_port.rx_pin())
10693
; // spin until state changes
10794
unsigned long now = micros();
10895
bool ret_state = state;
109-
state = RF_READ();
96+
state = rf_port.rx_pin();
11097

11198
int ret;
11299
if (ret_state)
@@ -167,40 +154,6 @@ int quantize_pulse(int p)
167154
}
168155
}
169156

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-
204157
/*
205158
* Add the given bit into the given byte array at the given bit index.
206159
*
@@ -220,13 +173,13 @@ void add_bit(byte * dst, size_t dst_len, size_t bit_idx, int bit_val)
220173
}
221174

222175
/*
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.
224177
*
225178
* The command bits are of the form: DDDDDDDD011S
226179
*/
227-
void parse_12bit_cmd(struct nexa_cmd & cmd, const char buf[12])
180+
void parse_12bit_cmd(NexaCommand & cmd, const char buf[12])
228181
{
229-
cmd.version = NEXA_12BIT;
182+
cmd.version = NexaCommand::NEXA_12BIT;
230183
cmd.device[0] = 0;
231184
cmd.device[1] = 0;
232185
for (size_t i = 0; i < 8; ++i)
@@ -237,14 +190,14 @@ void parse_12bit_cmd(struct nexa_cmd & cmd, const char buf[12])
237190
}
238191

239192
/*
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.
241194
*
242195
* The command bits are of the form: DDDDDDDDDDDDDDDDDDDDDDDD10GSCCCC
243196
*/
244-
void parse_32bit_cmd(struct nexa_cmd & cmd, const char buf[32])
197+
void parse_32bit_cmd(NexaCommand & cmd, const char buf[32])
245198
{
246199
size_t i;
247-
cmd.version = NEXA_32BIT;
200+
cmd.version = NexaCommand::NEXA_32BIT;
248201
for (i = 0; i < 24; ++i)
249202
add_bit(cmd.device, ARRAY_LENGTH(cmd.device), i,
250203
buf[i] == '1');
@@ -257,12 +210,12 @@ void parse_32bit_cmd(struct nexa_cmd & cmd, const char buf[32])
257210
}
258211

259212
/*
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.
261214
*/
262215
void decode_buf()
263216
{
264217
int state = -1; // Initial state - before SYNC
265-
enum nexa_cmd_version version = NEXA_INVAL;
218+
NexaCommand::Version version = NexaCommand::NEXA_INVAL;
266219
for (size_t i = 0; i < buf_pos; i++) {
267220
char b = buf[i];
268221
if (state > 0) { // Expecting another data bit
@@ -273,32 +226,33 @@ void decode_buf()
273226
}
274227

275228
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)
278231
parse_12bit_cmd(cmd, buf + i + 1 - 12);
279-
else if (version == NEXA_32BIT)
232+
else if (version == NexaCommand::NEXA_32BIT)
280233
parse_32bit_cmd(cmd, buf + i + 1 - 32);
281-
print_cmd(cmd);
234+
cmd.print(Serial);
235+
Serial.flush();
282236

283237
state = -1; // Look for next SYNC
284238
}
285239
else if (state == -1) { // Looking for SYNC
286240
if (b == 'A') { // SYNC for format A
287-
version = NEXA_32BIT;
241+
version = NexaCommand::NEXA_32BIT;
288242
state = 32; // Expect 32 data bits
289243
}
290244
else if (b == 'B') { // SYNC for format B
291-
version = NEXA_12BIT;
245+
version = NexaCommand::NEXA_12BIT;
292246
state = 12; // Expect 12 data bits
293247
}
294248
}
295249
}
296250
}
297251

298-
void loop()
252+
void handle_rf_pulse(int pulse)
299253
{
300254
new_state = UNKNOWN;
301-
int p = quantize_pulse(next_pulse()); // current pulse
255+
int p = quantize_pulse(pulse); // current pulse
302256
switch (p) {
303257
case -5: // LOW: 8192µs <= pulse < 16384µs
304258
new_state = SX1;
@@ -379,7 +333,7 @@ void loop()
379333
buf[buf_pos] = '\0';
380334
Serial.println(buf);
381335
Serial.flush();
382-
#endif // DEBUG
336+
#endif
383337
// Reached end of valid data: Decode and print buffer.
384338
// ...but only if it is longer than the shortest command
385339
// (Format B: SYNC + 12 data bits)
@@ -389,3 +343,8 @@ void loop()
389343
}
390344
cur_state = new_state;
391345
}
346+
347+
void loop()
348+
{
349+
handle_rf_pulse(next_pulse());
350+
}

0 commit comments

Comments
 (0)