Ramka: odrzucaj nieprawidlowe ID i usun zgadywany lowbat
This commit is contained in:
1 parent
3dad173002
commit
e2ffb8d8cf
2 files changed
+63
-115
No files matched your search
+48
-63
@@ -1,97 +1,82 @@
|
|||||||
/*
|
/* Generator ramek EG53MA4. Sztuczne dane wylacznie do diagnostyki. */
|
||||||
* tpms_frame.cpp - generator ramek TPMS Schrader EG53MA4 (bez zaleznosci
|
|
||||||
* od Arduino - kompilowalne takze natywnie w testach).
|
|
||||||
*
|
|
||||||
* Zastrzezenie: symulator generuje sztuczne dane cisnienia/temperatury
|
|
||||||
* i nie zastepuje prawdziwych czujnikow TPMS.
|
|
||||||
*/
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "config.h"
|
|
||||||
|
#include "settings.h"
|
||||||
#include "tpms_frame.h"
|
#include "tpms_frame.h"
|
||||||
|
|
||||||
static void put_bit(uint8_t *buf, unsigned *nbits, unsigned v)
|
static void put_bit(uint8_t *buf, unsigned *nbits, unsigned value)
|
||||||
{
|
{
|
||||||
if (v) {
|
if (value != 0u) {
|
||||||
buf[*nbits >> 3] |= (uint8_t)(0x80u >> (*nbits & 7u));
|
buf[*nbits >> 3] |= (uint8_t)(0x80u >> (*nbits & 7u));
|
||||||
}
|
}
|
||||||
(*nbits)++;
|
++(*nbits);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* G.E.T. Manchester: 0 -> "01", 1 -> "10" */
|
/* Polaryzacja potwierdzona nagraniami i rtl_433 OOK_MC_ZEROBIT: 0=01, 1=10. */
|
||||||
static void put_cell(uint8_t *buf, unsigned *nbits, unsigned bit)
|
static void put_cell(uint8_t *buf, unsigned *nbits, unsigned bit)
|
||||||
{
|
{
|
||||||
if (bit) {
|
put_bit(buf, nbits, bit != 0u ? 1u : 0u);
|
||||||
put_bit(buf, nbits, 1);
|
put_bit(buf, nbits, bit != 0u ? 0u : 1u);
|
||||||
put_bit(buf, nbits, 0);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
put_bit(buf, nbits, 0);
|
|
||||||
put_bit(buf, nbits, 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t tpms_build_payload(uint8_t out[TPMS_PAYLOAD_LEN], uint32_t id,
|
bool tpms_build_payload(uint8_t out[TPMS_PAYLOAD_LEN], uint32_t id,
|
||||||
uint16_t press_kpa10, int8_t temp_c,
|
uint16_t press_kpa10, int8_t temp_c, uint32_t flags)
|
||||||
uint32_t flags, bool low_batt)
|
|
||||||
{
|
{
|
||||||
if (low_batt) {
|
if (out == NULL || !settings_valid_id(id)) {
|
||||||
flags |= TPMS_FLAGS_LOWBAT_MASK;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* cisnienie: 25 mbar/bit = 2.5 kPa/bit, zaokraglenie w gore do polowy bitu */
|
int32_t pressure = ((int32_t)press_kpa10 + 12) / 25;
|
||||||
int32_t press = ((int32_t)press_kpa10 + 12) / 25;
|
if (pressure > 255) {
|
||||||
if (press < 0) press = 0;
|
pressure = 255;
|
||||||
if (press > 255) press = 255;
|
}
|
||||||
|
|
||||||
/* temperatura: protokol podaje stopnie Fahrenheita */
|
int32_t numerator = 9 * (int32_t)temp_c + 160;
|
||||||
int32_t num = 9 * (int32_t)temp_c + 160;
|
int32_t temperature_f = numerator >= 0
|
||||||
int32_t tf = (num >= 0) ? (num + 2) / 5 : -((-num + 2) / 5);
|
? (numerator + 2) / 5 : -((-numerator + 2) / 5);
|
||||||
if (tf < 0) tf = 0;
|
if (temperature_f < 0) {
|
||||||
if (tf > 255) tf = 255;
|
temperature_f = 0;
|
||||||
|
} else if (temperature_f > 255) {
|
||||||
|
temperature_f = 255;
|
||||||
|
}
|
||||||
|
|
||||||
out[0] = (uint8_t)(flags >> 24);
|
out[0] = (uint8_t)(flags >> 24);
|
||||||
out[1] = (uint8_t)(flags >> 16);
|
out[1] = (uint8_t)(flags >> 16);
|
||||||
out[2] = (uint8_t)(flags >> 8);
|
out[2] = (uint8_t)(flags >> 8);
|
||||||
out[3] = (uint8_t)(flags);
|
out[3] = (uint8_t)flags;
|
||||||
out[4] = (uint8_t)(id >> 16);
|
out[4] = (uint8_t)(id >> 16);
|
||||||
out[5] = (uint8_t)(id >> 8);
|
out[5] = (uint8_t)(id >> 8);
|
||||||
out[6] = (uint8_t)(id);
|
out[6] = (uint8_t)id;
|
||||||
out[7] = (uint8_t)press;
|
out[7] = (uint8_t)pressure;
|
||||||
out[8] = (uint8_t)tf;
|
out[8] = (uint8_t)temperature_f;
|
||||||
|
uint8_t checksum = 0;
|
||||||
uint8_t crc = 0;
|
for (uint8_t i = 0; i < 9u; ++i) {
|
||||||
for (unsigned i = 0; i < 9; i++) {
|
checksum = (uint8_t)(checksum + out[i]);
|
||||||
crc = (uint8_t)(crc + out[i]); // suma mod 256 (add_bytes w rtl_433)
|
|
||||||
}
|
}
|
||||||
out[9] = crc;
|
out[9] = checksum;
|
||||||
return crc;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void tpms_build_frame(uint8_t out[TPMS_TX_BYTES], uint32_t id,
|
bool tpms_build_frame(uint8_t out[TPMS_TX_BYTES], uint32_t id,
|
||||||
uint16_t press_kpa10, int8_t temp_c,
|
uint16_t press_kpa10, int8_t temp_c, uint32_t flags)
|
||||||
uint32_t flags, bool low_batt)
|
|
||||||
{
|
{
|
||||||
uint8_t payload[TPMS_PAYLOAD_LEN];
|
uint8_t payload[TPMS_PAYLOAD_LEN];
|
||||||
tpms_build_payload(payload, id, press_kpa10, temp_c, flags, low_batt);
|
if (out == NULL || !tpms_build_payload(payload, id, press_kpa10, temp_c, flags)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
memset(out, 0, TPMS_TX_BYTES);
|
memset(out, 0, TPMS_TX_BYTES);
|
||||||
unsigned nbits = 0;
|
unsigned nbits = 0;
|
||||||
|
put_bit(out, &nbits, 1u); /* chip startowy */
|
||||||
put_bit(out, &nbits, 1); // chip rozbiegowy (carrier on)
|
for (unsigned i = 0; i < TPMS_SYNC_BITS - 1u; ++i) {
|
||||||
|
put_cell(out, &nbits, 0u);
|
||||||
/* 39 bitow sync - pierwszy (zerowy) bit dopisuje odbiornik (MC_ZEROBIT) */
|
|
||||||
for (unsigned i = 0; i < TPMS_SYNC_BITS - 1; i++) {
|
|
||||||
put_cell(out, &nbits, 0);
|
|
||||||
}
|
}
|
||||||
|
for (unsigned bit = 0; bit < 8u * TPMS_PAYLOAD_LEN; ++bit) {
|
||||||
/* 80 bitow payloadu */
|
unsigned value = (payload[bit >> 3] >> (7u - (bit & 7u))) & 1u;
|
||||||
for (unsigned b = 0; b < 8u * TPMS_PAYLOAD_LEN; b++) {
|
put_cell(out, &nbits, value);
|
||||||
unsigned bit = (payload[b >> 3] >> (7 - (b & 7u))) & 1u;
|
|
||||||
put_cell(out, &nbits, bit);
|
|
||||||
}
|
}
|
||||||
|
while ((nbits & 7u) != 0u) {
|
||||||
/* padding do granicy bajtu - dla OOK sa to chipy "off" = cisza w eterze */
|
put_bit(out, &nbits, 0u);
|
||||||
while (nbits % 8u) {
|
|
||||||
put_bit(out, &nbits, 0);
|
|
||||||
}
|
}
|
||||||
|
return nbits == TPMS_TX_CHIPS;
|
||||||
}
|
}
|
||||||
+15
-52
@@ -1,70 +1,33 @@
|
|||||||
/*
|
/* Schrader EG53MA4: 40 bitow sync + 80 bitow danych, OOK Manchester. */
|
||||||
* tpms_frame.h - generator ramek TPMS Schrader EG53MA4
|
|
||||||
*
|
|
||||||
* Zastrzezenie: dane sa sztuczne (diagnostyka/testy), nie zastepuja
|
|
||||||
* prawdziwych czujnikow TPMS.
|
|
||||||
*
|
|
||||||
* Ramka na zrzuconej warstwie bitowej (120 bitow, po dekodowaniu
|
|
||||||
* Manchester przez rtl_433 OOK_MC_ZEROBIT):
|
|
||||||
*
|
|
||||||
* 40 bit sync (zera) | flags(4B) | id(3B) | pressure(1B) | temp(1B) | crc(1B)
|
|
||||||
*
|
|
||||||
* - flags: 32 bity, wg dekodera "moze zawierac preamble/status/battery"
|
|
||||||
* - pressure: 25 mbar/bit (cisnienie w kPa = bajt * 2.5)
|
|
||||||
* - temperature: stopnie Fahrenheita
|
|
||||||
* - crc: suma 9 poprzednich bajtow mod 256 (add_bytes w rtl_433)
|
|
||||||
*
|
|
||||||
* Kodowanie na zrzuconej warstwie RF (G.E.T. Manchester):
|
|
||||||
* bit 0 -> chipy "01", bit 1 -> chipy "10", chip = 120 us
|
|
||||||
* Odbiornik rtl_433 (MC_ZEROBIT) dopisuje pierwszy bit ramki samodzielnie,
|
|
||||||
* wiec nadajnik wysyla ramke BEZ pierwszego bitu, poprzedzona jednym
|
|
||||||
* chipem rozbiegowym '1'. Bufor wyjsciowy jest dopełniany zerami do
|
|
||||||
* granicy bajtu (dla OOK sa to chipy "wylaczone" = cisza).
|
|
||||||
*/
|
|
||||||
#ifndef TPMS_FRAME_H
|
#ifndef TPMS_FRAME_H
|
||||||
#define TPMS_FRAME_H
|
#define TPMS_FRAME_H
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define TPMS_ROW_BITS 120 // dlugosc dekodowanej ramki (wymagana przez rtl_433)
|
#define TPMS_ROW_BITS 120
|
||||||
#define TPMS_SYNC_BITS 40
|
#define TPMS_SYNC_BITS 40
|
||||||
#define TPMS_PAYLOAD_LEN 10 // flags(4)+id(3)+press+temp+crc
|
#define TPMS_PAYLOAD_LEN 10
|
||||||
#define TPMS_TX_BYTES 30 // 240 chipow = 1 lead + 238 danych + 1 padding
|
#define TPMS_TX_BYTES 30
|
||||||
#define TPMS_TX_CHIPS (TPMS_TX_BYTES * 8)
|
#define TPMS_TX_CHIPS (TPMS_TX_BYTES * 8)
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
/* Zaleznosc bufora nadawczego od dlugosci ramki: 1 chip rozbiegowy +
|
static_assert(TPMS_TX_CHIPS == 1 + 2 * (TPMS_ROW_BITS - 1) + 1,
|
||||||
* 2 chipy na kazdy z (TPMS_ROW_BITS-1) bitow (pierwszy bit dopisuje
|
"Nieprawidlowa dlugosc bufora Manchester");
|
||||||
* odbiornik) + padding do granicy bajtu (< 8 chipow). */
|
|
||||||
static_assert(TPMS_TX_CHIPS >= 1 + 2 * (TPMS_ROW_BITS - 1)
|
|
||||||
&& TPMS_TX_CHIPS < 1 + 2 * (TPMS_ROW_BITS - 1) + 8,
|
|
||||||
"TPMS_TX_BYTES musi miescic 1 + 2*(TPMS_ROW_BITS-1) chipow");
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct {
|
/* Zwraca false dla ID 0, 0xFFFFFF i wartosci spoza 24 bitow. */
|
||||||
uint32_t id; // 24-bit identyfikator czujnika
|
bool tpms_build_payload(uint8_t out[TPMS_PAYLOAD_LEN], uint32_t id,
|
||||||
uint16_t press_kpa10; // cisnienie w kPa * 10 (np. 2600 = 260.0 kPa)
|
uint16_t press_kpa10, int8_t temp_c, uint32_t flags);
|
||||||
int8_t temp_c; // temperatura w st. C
|
bool tpms_build_frame(uint8_t out[TPMS_TX_BYTES], uint32_t id,
|
||||||
const char *name; // "FL", "FR", "RR", "RL"
|
uint16_t press_kpa10, int8_t temp_c, uint32_t flags);
|
||||||
} tpms_sensor_t;
|
|
||||||
|
|
||||||
/* Zbuduj 10-bajtowy payload (bez sync). Zwraca crc (tez wpisane do out[9]). */
|
|
||||||
uint8_t tpms_build_payload(uint8_t out[TPMS_PAYLOAD_LEN], uint32_t id,
|
|
||||||
uint16_t press_kpa10, int8_t temp_c,
|
|
||||||
uint32_t flags, bool low_batt);
|
|
||||||
|
|
||||||
/* Zbuduj kompletny bufor nadawczy (TPMS_TX_BYTES) gotowy do CC1101 FIFO. */
|
|
||||||
void tpms_build_frame(uint8_t out[TPMS_TX_BYTES], uint32_t id,
|
|
||||||
uint16_t press_kpa10, int8_t temp_c,
|
|
||||||
uint32_t flags, bool low_batt);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /* TPMS_FRAME_H */
|
#endif
|
||||||
Reference in new issue
Block a user