Skip to content

Commit

Permalink
Renault TPMS and other improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
antirez committed Jan 4, 2023
1 parent 341ceda commit d373b5d
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 7 deletions.
1 change: 1 addition & 0 deletions app.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ void reset_current_signal(ProtoViewApp *app);
void scan_for_signal(ProtoViewApp *app);
bool bitmap_get(uint8_t *b, uint32_t blen, uint32_t bitpos);
void bitmap_set(uint8_t *b, uint32_t blen, uint32_t bitpos, bool val);
void bitmap_set_pattern(uint8_t *b, uint32_t blen, const char *pat);
void bitmap_invert_bytes_bits(uint8_t *p, uint32_t len);
bool bitmap_match_bits(uint8_t *b, uint32_t blen, uint32_t bitpos, const char *bits);
uint32_t bitmap_seek_bits(uint8_t *b, uint32_t blen, uint32_t startpos, const char *bits);
Expand Down
4 changes: 3 additions & 1 deletion protocols/b4b1.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* This line code is used in many remotes such as Princeton chips
/* PT/SC remotes. Usually 443.92 Mhz OOK.
*
* This line code is used in many remotes such as Princeton chips
* named PT<number>, Silian Microelectronics SC5262 and others.
* Basically every 4 pulsee represent a bit, where 1000 means 0, and
* 1110 means 1. Usually we can read 24 bits of data.
Expand Down
10 changes: 8 additions & 2 deletions protocols/oregon2.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
/* Oregon remote termometers. Usually 443.92 Mhz OOK.
*
* The protocol is described here:
* https://wmrx00.sourceforge.net/Arduino/OregonScientific-RF-Protocols.pdf
* This implementation is not very complete. */

#include "../app.h"

static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo *info) {
if (numbits < 32) return false;
const char *sync_pattern = "01100110" "01100110" "10010110" "10010110";
uint64_t off = bitmap_seek_bits(bits,numbytes,0,sync_pattern);
if (off == BITMAP_SEEK_NOT_FOUND) return false;
FURI_LOG_E(TAG, "Oregon2 prelude+sync found");
FURI_LOG_E(TAG, "Oregon2 preamble+sync found");

off += 32; /* Skip preamble. */

Expand All @@ -23,7 +29,7 @@ static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoView
bitmap_get(buffer,8,j+1) << 1 |
bitmap_get(buffer,8,j+2) << 2 |
bitmap_get(buffer,8,j+3) << 3);
FURI_LOG_E(TAG, "Not inverted nibble[%d]: %x", j/4, (unsigned int)nib[0]);
if (DEBUG_MSG) FURI_LOG_E(TAG, "Not inverted nibble[%d]: %x", j/4, (unsigned int)nib[0]);
raw[j/8] |= nib[0] << (4-(j%4));
switch(j/4) {
case 1: deviceid[0] |= nib[0]; break;
Expand Down
63 changes: 63 additions & 0 deletions protocols/renault_tpms.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* Renault tires TPMS. Usually 443.92 Mhz FSK.
*
* Preamble + marshal-encoded bits. 9 Bytes in total if we don't
* count the preamble. */

#include "../app.h"

#define USE_TEST_VECTOR 0
static const char *test_vector =
"10101010" "10101010" "10101010" "10101001" // Preamble + sync.

/* The following is marshal encoded, so each two characters are
* actaully one bit. 01 = 1, 10 = 0. */
"010110010110" // Flags.
"10011001101010011001" // Pressure, multiply by 0.75 to obtain kpa.
// 244 kpa here.
"1010010110011010" // Temperature, subtract 30 to obtain celsius. 22C here.
"1001010101101001"
"0101100110010101"
"1001010101100110" // Tire ID. 0x7AD779 here.
"0101010101010101"
"0101010101010101" // Two FF bytes (usually). Unknown.
"0110010101010101"; // CRC8 with (poly 7, initialization 0).

static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo *info) {

if (USE_TEST_VECTOR) { /* Test vector to check that decoding works. */
bitmap_set_pattern(bits,numbytes,test_vector);
numbits = strlen(test_vector);
}

if (numbits < 13*8) return false;

const char *sync_pattern = "10101010" "10101010" "10101010" "10101001";
uint64_t off = bitmap_seek_bits(bits,numbytes,0,sync_pattern);
if (off == BITMAP_SEEK_NOT_FOUND) return false;
FURI_LOG_E(TAG, "Renault TPMS preamble+sync found");

off += 32; /* Skip preamble. */

uint8_t raw[9];
uint32_t decoded =
convert_from_line_code(raw,sizeof(raw),bits,numbytes,off,
"10","01"); /* Manchester. */
FURI_LOG_E(TAG, "Renault TPMS decoded bits: %lu", decoded);

if (decoded < 8*9) return false; /* Require the full 9 bytes. */

float kpa = 0.75 *((uint32_t)((raw[0]&3)<<8) | raw[1]);
int temp = raw[2]-30;

snprintf(info->name,sizeof(info->name),"%s","Renault TPMS");
snprintf(info->raw,sizeof(info->raw),"%02X%02X%02X%02X%02X%02X%02X%02X%02X",
raw[0],raw[1],raw[2],raw[3],raw[4],raw[5],
raw[6],raw[7],raw[8]);
snprintf(info->info1,sizeof(info->info1),"Pressure %.2f kpa", (double)kpa);
snprintf(info->info2,sizeof(info->info2),"Temperature %d C", temp);
return true;
}

ProtoViewDecoder RenaultTPMSDecoder = {
"Renault TPMS", decode
};
25 changes: 21 additions & 4 deletions signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,20 @@ void scan_for_signal(ProtoViewApp *app) {
12 samples it's very easy to mistake
noise for signal. */

ProtoViewMsgInfo *info = malloc(sizeof(ProtoViewMsgInfo));
uint32_t i = 0;

while (i < copy->total-1) {
uint32_t thislen = search_coherent_signal(copy,i);

/* For messages that are long enough, attempt decoding. */
if (thislen > minlen) {
ProtoViewMsgInfo info;
initialize_msg_info(&info);
initialize_msg_info(info);
uint32_t saved_idx = copy->idx; /* Save index, see later. */
/* decode_signal() expects the detected signal to start
* from index .*/
raw_samples_center(copy,i);
bool decoded = decode_signal(copy,thislen,&info);
bool decoded = decode_signal(copy,thislen,info);
copy->idx = saved_idx; /* Restore the index as we are scanning
the signal in the loop. */

Expand All @@ -156,7 +157,7 @@ void scan_for_signal(ProtoViewApp *app) {
if (thislen > app->signal_bestlen ||
(app->signal_decoded == false && decoded))
{
app->signal_info = info;
app->signal_info = *info;
app->signal_bestlen = thislen;
app->signal_decoded = decoded;
raw_samples_copy(DetectedSamples,copy);
Expand All @@ -168,6 +169,7 @@ void scan_for_signal(ProtoViewApp *app) {
i += thislen ? thislen : 1;
}
raw_samples_free(copy);
free(info);
}

/* =============================================================================
Expand Down Expand Up @@ -242,6 +244,19 @@ uint32_t bitmap_seek_bits(uint8_t *b, uint32_t blen, uint32_t startpos, const ch
return BITMAP_SEEK_NOT_FOUND;
}

/* Set the pattern 'pat' into the bitmap 'b' of max length 'blen' bytes.
* The pattern is given as a string of 0s and 1s characters, like "01101001".
* This function is useful in order to set the test vectors in the protocol
* decoders, to see if the decoding works regardless of the fact we are able
* to actually receive a given signal. */
void bitmap_set_pattern(uint8_t *b, uint32_t blen, const char *pat) {
uint32_t i = 0;
while(pat[i]) {
bitmap_set(b,blen,i,pat[i] == '1');
i++;
}
}

/* Take the raw signal and turn it into a sequence of bits inside the
* buffer 'b'. Note that such 0s and 1s are NOT the actual data in the
* signal, but is just a low level representation of the line code. Basically
Expand Down Expand Up @@ -338,10 +353,12 @@ uint32_t convert_from_line_code(uint8_t *buf, uint64_t buflen, uint8_t *bits, ui

extern ProtoViewDecoder Oregon2Decoder;
extern ProtoViewDecoder B4B1Decoder;
extern ProtoViewDecoder RenaultTPMSDecoder;

ProtoViewDecoder *Decoders[] = {
&Oregon2Decoder, /* Oregon sensors v2.1 protocol. */
&B4B1Decoder, /* PT, SC, ... 24 bits remotes. */
&RenaultTPMSDecoder, /* Renault TPMS. */
NULL
};

Expand Down

0 comments on commit d373b5d

Please sign in to comment.