rtl433  UNKNOWN
RTL-433 utility
bitbuffer.h
Go to the documentation of this file.
1 
12 #ifndef INCLUDE_BITBUFFER_H_
13 #define INCLUDE_BITBUFFER_H_
14 
15 #include <stdint.h>
16 
17 #define BITBUF_COLS 256 // Number of bytes in a column
18 #define BITBUF_ROWS 25
19 #define BITBUF_MAX_PRINT_BITS 50 // Maximum number of bits to print (in addition to hex values)
20 
21 typedef uint8_t bitrow_t[BITBUF_COLS];
22 typedef bitrow_t bitarray_t[BITBUF_ROWS];
23 
25 typedef struct bitbuffer {
26  uint16_t num_rows; // Number of active rows
27  uint16_t bits_per_row[BITBUF_ROWS]; // Number of active bits per row
28  uint16_t syncs_before_row[BITBUF_ROWS]; // Number of sync pulses before row
29  bitarray_t bb; // The actual bits buffer
30 } bitbuffer_t;
31 
33 void bitbuffer_clear(bitbuffer_t *bits);
34 
36 void bitbuffer_add_bit(bitbuffer_t *bits, int bit);
37 
39 void bitbuffer_add_row(bitbuffer_t *bits);
40 
43 
46  unsigned pos, uint8_t *out, unsigned len);
47 
49 void bitbuffer_invert(bitbuffer_t *bits);
50 
54 
58 
60 void bitbuffer_print(const bitbuffer_t *bits);
61 
63 void bitbuffer_debug(const bitbuffer_t *bits);
64 
66 void bitrow_print(bitrow_t const bitrow, unsigned bit_len);
67 
69 void bitrow_debug(bitrow_t const bitrow, unsigned bit_len);
70 
72 void bitbuffer_parse(bitbuffer_t *bits, const char *code);
73 
79 unsigned bitbuffer_search(bitbuffer_t *bitbuffer, unsigned row, unsigned start,
80  const uint8_t *pattern, unsigned pattern_bits_len);
81 
87 unsigned bitbuffer_manchester_decode(bitbuffer_t *inbuf, unsigned row, unsigned start,
88  bitbuffer_t *outbuf, unsigned max);
89 
94 unsigned bitbuffer_differential_manchester_decode(bitbuffer_t *inbuf, unsigned row, unsigned start,
95  bitbuffer_t *outbuf, unsigned max);
96 
98 int compare_rows(bitbuffer_t *bits, unsigned row_a, unsigned row_b);
99 
100 unsigned count_repeats(bitbuffer_t *bits, unsigned row);
101 
104 int bitbuffer_find_repeated_row(bitbuffer_t *bits, unsigned min_repeats, unsigned min_bits);
105 
106 
108 static inline uint8_t bitrow_get_bit(const bitrow_t bitrow, unsigned bit_idx)
109 {
110  return bitrow[bit_idx >> 3] >> (7 - (bit_idx & 7)) & 1;
111 }
112 
114 static inline uint8_t bitrow_get_byte(const bitrow_t bitrow, unsigned bit_idx)
115 {
116  return ((bitrow[(bit_idx >> 3)] << (bit_idx & 7)) |
117  (bitrow[(bit_idx >> 3) + 1] >> (8 - (bit_idx & 7))));
118 }
119 
120 #endif /* INCLUDE_BITBUFFER_H_ */
void bitbuffer_nrzm_decode(bitbuffer_t *bits)
Non-Return-to-Zero Mark (NRZI) decode the bitbuffer.
Definition: bitbuffer.c:94
uint16_t syncs_before_row[BITBUF_ROWS]
Definition: bitbuffer.h:28
void bitbuffer_nrzs_decode(bitbuffer_t *bits)
Non-Return-to-Zero Space (NRZI) decode the bitbuffer.
Definition: bitbuffer.c:76
void bitbuffer_add_sync(bitbuffer_t *bits)
Increment sync counter, add new row if not empty.
Definition: bitbuffer.c:52
static int bit(const uint8_t *bytes, unsigned bit)
Definition: bitbuffer.c:142
unsigned short uint16_t
Definition: mongoose.h:271
void bitbuffer_invert(bitbuffer_t *bits)
Invert all bits in the bitbuffer (do not invert the empty bits).
Definition: bitbuffer.c:62
unsigned count_repeats(bitbuffer_t *bits, unsigned row)
Definition: bitbuffer.c:388
void bitbuffer_add_row(bitbuffer_t *bits)
Add a new row to the bitbuffer.
Definition: bitbuffer.c:39
void bitbuffer_parse(bitbuffer_t *bits, const char *code)
Parse a string into a bitbuffer.
Definition: bitbuffer.c:320
uint16_t bits_per_row[BITBUF_ROWS]
Definition: bitbuffer.h:27
struct bitbuffer bitbuffer_t
Bit buffer.
void bitbuffer_add_bit(bitbuffer_t *bits, int bit)
Add a single bit at the end of the bitbuffer (MSB first).
Definition: bitbuffer.c:24
uint16_t num_rows
Definition: bitbuffer.h:26
unsigned bitbuffer_search(bitbuffer_t *bitbuffer, unsigned row, unsigned start, const uint8_t *pattern, unsigned pattern_bits_len)
Search the specified row of the bitbuffer, starting from bit &#39;start&#39;, for the pattern provided...
Definition: bitbuffer.c:147
uint8_t bitrow_t[BITBUF_COLS]
Definition: bitbuffer.h:21
static uint8_t bitrow_get_byte(const bitrow_t bitrow, unsigned bit_idx)
Return a single byte from a bitrow at bit_idx position (which may be unaligned).
Definition: bitbuffer.h:114
unsigned bitbuffer_manchester_decode(bitbuffer_t *inbuf, unsigned row, unsigned start, bitbuffer_t *outbuf, unsigned max)
Manchester decoding from one bitbuffer into another, starting at the specified row and start bit...
Definition: bitbuffer.c:173
Bit buffer.
Definition: bitbuffer.h:25
int bitbuffer_find_repeated_row(bitbuffer_t *bits, unsigned min_repeats, unsigned min_bits)
Find a repeated row that has a minimum count of bits.
Definition: bitbuffer.c:399
void bitrow_print(bitrow_t const bitrow, unsigned bit_len)
Print the content of a bit row (byte buffer).
Definition: bitbuffer.c:310
void bitbuffer_clear(bitbuffer_t *bits)
Clear the content of the bitbuffer.
Definition: bitbuffer.c:17
unsigned char uint8_t
Definition: mongoose.h:267
int compare_rows(bitbuffer_t *bits, unsigned row_a, unsigned row_b)
Function to compare bitbuffer rows and count repetitions.
Definition: bitbuffer.c:381
void bitrow_debug(bitrow_t const bitrow, unsigned bit_len)
Debug the content of a bit row (byte buffer).
Definition: bitbuffer.c:315
static uint8_t bitrow_get_bit(const bitrow_t bitrow, unsigned bit_idx)
Return a single bit from a bitrow at bit_idx position.
Definition: bitbuffer.h:108
void bitbuffer_print(const bitbuffer_t *bits)
Print the content of the bitbuffer.
Definition: bitbuffer.c:300
unsigned bitbuffer_differential_manchester_decode(bitbuffer_t *inbuf, unsigned row, unsigned start, bitbuffer_t *outbuf, unsigned max)
Differential Manchester decoding from one bitbuffer into another, starting at the specified row and s...
Definition: bitbuffer.c:198
void bitbuffer_debug(const bitbuffer_t *bits)
Debug the content of the bitbuffer.
Definition: bitbuffer.c:305
bitrow_t bitarray_t[BITBUF_ROWS]
Definition: bitbuffer.h:22
void bitbuffer_extract_bytes(bitbuffer_t *bitbuffer, unsigned row, unsigned pos, uint8_t *out, unsigned len)
Extract (potentially unaligned) bytes from the bit buffer. Len is bits.
Definition: bitbuffer.c:112
bitarray_t bb
Definition: bitbuffer.h:29