Skip to content

Commit

Permalink
General decoder improved.
Browse files Browse the repository at this point in the history
  • Loading branch information
antirez committed Jan 27, 2023
1 parent 79ef4f5 commit 9352b0b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
34 changes: 28 additions & 6 deletions protocols/unknown.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static uint32_t find_pwm(uint8_t *bits, uint32_t numbytes, uint32_t numbits,
* Anyway at the end of the function we try to fix the sync. */
for (uint32_t off = 0; off < symlen; off++) {
uint32_t c = 0; // Number of contiguous symbols found.
uint32_t c1 = 0, c2 = 0; // Occurrences of first/second symbol.
*s1i = off; // Assume we start at one symbol boundaty.
*s2i = UINT32_MAX; // Second symbol first index still unknown.
uint32_t next = off;
Expand All @@ -74,7 +75,12 @@ static uint32_t find_pwm(uint8_t *bits, uint32_t numbytes, uint32_t numbits,
/* One or the other should match. */
if (match1 || match2) {
c++;
if (c > best_count) {
if (match1) c1++;
if (match2) c2++;
if (c > best_count &&
c1 >= best_count/5 && // Require enough presence of both
c2 >= best_count/5) // zero and one.
{
best_count = c;
best_idx1 = *s1i;
best_idx2 = *s2i;
Expand All @@ -83,6 +89,8 @@ static uint32_t find_pwm(uint8_t *bits, uint32_t numbytes, uint32_t numbits,
} else {
/* No match. Continue resetting the signal info. */
c = 0; // Start again to count contiguous symbols.
c1 = 0;
c2 = 0;
*s1i = next; // First symbol always at start.
*s2i = UINT32_MAX; // Second symbol unknown.
}
Expand Down Expand Up @@ -244,7 +252,8 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
{
linecode = LineCodeManchester;
start1 = tmp1;
msgbits = pwm3_bits*2;
msgbits = manchester_bits*2;
FURI_LOG_E(TAG, "MANCHESTER START: %lu", tmp1);
}

if (linecode == LineCodeNone) return false;
Expand All @@ -255,9 +264,13 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
uint32_t preamble_len = find_preamble(bits,numbytes,numbits,&tmp1);
uint32_t min_preamble_len = 10;
uint32_t max_preamble_distance = 32;
uint32_t preamble_start;
uint32_t preamble_start = 0;
bool preamble_found = false;

/* Note that because of the following checks, if the Manchester detector
* detected the preamble bits as data, we are ok with that, since it
* means that the synchronization is not designed to "break" the bits
* flow. In this case we ignore the preamble and return all as data. */
if (preamble_len >= min_preamble_len && // Not too short.
tmp1 < start1 && // Should be before the data.
start1-tmp1 <= max_preamble_distance) // Not too far.
Expand All @@ -273,6 +286,13 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
* transfer all that is needed, like a message
* terminator (that we don't detect). */

if (preamble_found)
FURI_LOG_E(TAG, "PREAMBLE AT: %lu", preamble_start);
FURI_LOG_E(TAG, "START: %lu", info->start_off);
FURI_LOG_E(TAG, "MSGBITS: %lu", msgbits);
FURI_LOG_E(TAG, "DATASTART: %lu", start1);
FURI_LOG_E(TAG, "PULSES: %lu", info->pulses_count);

/* We think there is a message and we know where it starts and the
* line code used. We can turn it into bits and bytes. */
uint32_t decoded;
Expand All @@ -293,11 +313,13 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
datalen = (decoded+7)/8;

char *linecode_name = get_linecode_name(linecode);
fieldset_add_str(info->fieldset,"line code",linecode_name,strlen(linecode_name));
fieldset_add_uint(info->fieldset,"preamble len",preamble_len,8);
fieldset_add_str(info->fieldset,"line code",
linecode_name,strlen(linecode_name));
fieldset_add_uint(info->fieldset,"data bits",decoded,8);
if (preamble_found)
fieldset_add_uint(info->fieldset,"preamble len",preamble_len,8);
fieldset_add_str(info->fieldset,"first symbol",symbol1,strlen(symbol1));
fieldset_add_str(info->fieldset,"second symbol",symbol2,strlen(symbol2));
fieldset_add_uint(info->fieldset,"data bits",decoded,8);
for (uint32_t j = 0; j < datalen; j++) {
char label[16];
snprintf(label,sizeof(label),"data[%lu]",j);
Expand Down
7 changes: 4 additions & 3 deletions signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,12 @@ void scan_for_signal(ProtoViewApp *app, RawSamplesBuffer *source, uint32_t min_d
/* Accept this signal as the new signal if either it's longer
* than the previous undecoded one, or the previous one was
* unknown and this is decoded. */
bool current_not_decoded = app->signal_decoded == false ||
bool oldsignal_not_decoded = app->signal_decoded == false ||
app->msg_info->decoder == &UnknownDecoder;

if (current_not_decoded &&
(thislen > app->signal_bestlen || decoded))
if (oldsignal_not_decoded &&
(thislen > app->signal_bestlen ||
(decoded && info->decoder != &UnknownDecoder)))
{
free_msg_info(app->msg_info);
app->msg_info = info;
Expand Down

0 comments on commit 9352b0b

Please sign in to comment.