Skip to content

Commit 9ff18fd

Browse files
committed
Move parsing of bit sequences into NexaCommand
1 parent cf93c6f commit 9ff18fd

File tree

2 files changed

+2
-104
lines changed

2 files changed

+2
-104
lines changed

libraries/nexa_node

Submodule nexa_node updated 1 file

nexa_decoder/nexa_decoder.ino

Lines changed: 1 addition & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -57,108 +57,6 @@ void setup()
5757
Serial.println(F("nexa_decoder ready:"));
5858
}
5959

60-
/*
61-
* Add the given bit into the given byte array at the given bit index.
62-
*
63-
* The bit index is LSB, so index 0 corresponds to the LSB of
64-
* dst[dst_len - 1], index 7 corresponds to the MSB of dst[dst_len - 1],
65-
* index 8 corresponds to the LSB of dst[dst_len - 2], and so on.
66-
*/
67-
void add_bit(byte * dst, size_t dst_len, size_t bit_idx, int bit_val)
68-
{
69-
// assert(bit_idx < 8 * dst_len);
70-
size_t byte_idx = dst_len - (1 + bit_idx / 8);
71-
byte bit_mask = 1 << (bit_idx % 8);
72-
if (bit_val)
73-
dst[byte_idx] |= bit_mask;
74-
else
75-
dst[byte_idx] &= ~bit_mask;
76-
}
77-
78-
/*
79-
* Initialize the given old-style NexaCommand from the 12 bits at buf.
80-
*
81-
* The command bits are of the form: DDDDDDDD011S
82-
*/
83-
void parse_12bit_cmd(NexaCommand & cmd, const char buf[12])
84-
{
85-
cmd.version = NexaCommand::NEXA_12BIT;
86-
cmd.device[0] = 0;
87-
cmd.device[1] = 0;
88-
for (size_t i = 0; i < 8; ++i)
89-
add_bit(cmd.device + 2, 1, i, buf[i] == '1');
90-
cmd.channel = 0;
91-
cmd.group = 0;
92-
cmd.state = buf[11] == '1';
93-
}
94-
95-
/*
96-
* Initialize the given new-style NexaCommandfrom the 32 bits at buf.
97-
*
98-
* The command bits are of the form: DDDDDDDDDDDDDDDDDDDDDDDD10GSCCCC
99-
*/
100-
void parse_32bit_cmd(NexaCommand & cmd, const char buf[32])
101-
{
102-
size_t i;
103-
cmd.version = NexaCommand::NEXA_32BIT;
104-
for (i = 0; i < 24; ++i)
105-
add_bit(cmd.device, ARRAY_LENGTH(cmd.device), i,
106-
buf[i] == '1');
107-
cmd.channel = (buf[28] == '1' ? B1000 : 0) |
108-
(buf[29] == '1' ? B100 : 0) |
109-
(buf[30] == '1' ? B10 : 0) |
110-
(buf[31] == '1' ? B1 : 0);
111-
cmd.group = buf[26] == '1';
112-
cmd.state = buf[27] == '1';
113-
}
114-
115-
/*
116-
* Parse data from ring buffer, and generate NexaCommands.
117-
*
118-
* The function will consume (some, but not necessarily all) available
119-
* data in the ring buffer.
120-
*
121-
* This function will not generate a NexaCommand every time it's called,
122-
* but when it does, it will return true, and store the NexaCommand into
123-
* the given object.
124-
*/
125-
bool decode_bits(NexaCommand & cmd, RingBuffer<char> & rx_bits)
126-
{
127-
static NexaCommand::Version version = NexaCommand::NEXA_INVAL;
128-
static char buf[32]; // Long enough for the longest command
129-
static size_t buf_pos = 0;
130-
static size_t expect = 0;
131-
while (!rx_bits.r_empty()) {
132-
char b = rx_bits.r_pop();
133-
if (b == 'A' || b == 'B') {
134-
buf_pos = 0;
135-
if (b == 'A') {
136-
version = NexaCommand::NEXA_32BIT;
137-
expect = 32;
138-
}
139-
else {
140-
version = NexaCommand::NEXA_12BIT;
141-
expect = 12;
142-
}
143-
}
144-
else if ((b == '0' || b == '1') && buf_pos < expect)
145-
buf[buf_pos++] = b;
146-
147-
if (expect && buf_pos == expect) { // all bits present
148-
if (version == NexaCommand::NEXA_12BIT)
149-
parse_12bit_cmd(cmd, buf);
150-
else if (version == NexaCommand::NEXA_32BIT)
151-
parse_32bit_cmd(cmd, buf);
152-
153-
expect = 0;
154-
buf_pos = 0;
155-
version = NexaCommand::NEXA_INVAL;
156-
return true;
157-
}
158-
}
159-
return false;
160-
}
161-
16260
void loop()
16361
{
16462
pulse_parser(rf_port.rx_get_pulse());
@@ -169,7 +67,7 @@ void loop()
16967
Serial.write((const byte *) rx_bits.r_wrapped_buf(),
17068
rx_bits.r_wrapped_buf_len());
17169
#endif
172-
if (decode_bits(cmd, rx_bits)) {
70+
if (NexaCommand::from_bit_buffer(cmd, rx_bits)) {
17371
#if DEBUG
17472
Serial.println();
17573
#endif

0 commit comments

Comments
 (0)