-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tpms.cpp
82 lines (73 loc) · 2.44 KB
/
Tpms.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <LittleFS.h>
#include "Tpms.h"
char raw_filename[100]="/dat/TPMS_Raw.dat";
NimBLEUUID TpmsServiceUuid(uint16_t(0xfbb0));
struct Tpms decodeTPMS(const char *dat){
struct Tpms rtn;
float_conv psi = {.raw = {dat[8],dat[9],dat[10],dat[11]}}; // {dat[11],dat[10],dat[9],dat[8]}}; //
rtn.psi = (psi.ui32 / 100000.0) * 14.503773773;
float_conv deg = {.raw = {dat[12],dat[13],dat[14],dat[15]}}; // {dat[15],dat[14],dat[13],dat[12]}}; //
rtn.deg = deg.ui32 / 100.0;
rtn.deg_f = rtn.deg * 9 / 5 + 32;
rtn.bat = dat[16];
rtn.alm = dat[17];
return rtn;
}
/*
struct Tpms decodeTPMS(const char *dat){
struct Tpms rtn;
tpms_conv tc = {.manif = *dat};
rtn.psi = (tc.tpms_rec.pres / 100000.0) * 14.503773773;
rtn.deg = tc.tpms_rec.tempC100 / 100.0;
rtn.deg_f = rtn.deg * 9 / 5 + 32;
rtn.bat = tc.tpms_rec.bat;
rtn.alm = tc.tpms_rec.alm;
return rtn;
}
*/
String hexStrAddr(const unsigned char dat[6]) {
char buffer[100];
sprintf(
buffer,"%02x:%02x:%02x:%02x:%02x:%02x",
// dat[5], dat[4], dat[3], dat[2], dat[1], dat[0] // internal nimBLE
dat[0], dat[1], dat[2], dat[3], dat[4], dat[5] // normal order
);
return String(buffer);
}
// void csvPrintRawDat(const struct tpms_raw &dat, bool lbls=true) {
void csvPrintRawDat(const struct tpms_raw &dat, bool lbls) {
struct Tpms tpms= decodeTPMS(dat.data);
const char *fmt=lbls
? "mils:%ld, tc_idx:%d, addr:%s, alm:0x%02x, psi:%0.1f, deg_f:%0.1f\n"
: "%ld, %d, %s, 0x%02x, %0.1f, %0.1f\n";
Serial.printf(
//"%ld, %d, %02x\n", dat.mils, dat.tc_idx, dat.alm
// "mils:%ld, tc_idx:%d, addr:%s, alm:%02x, psi:%0.1f, deg_f:%0.1f\n",
fmt, dat.mils, dat.tc_idx, hexStrAddr(dat.addr).c_str(), dat.alm, tpms.psi, tpms.deg_f
);
}
// void csvPrintRawFile(const char* filename=NULL, int tc_idx=-1) {
void csvPrintRawFile(const char* filename, int tc_idx) {
if(filename==NULL) filename = raw_filename;
File fp = LittleFS.open(filename, "r");
if(!fp) {
log_e("File open failed for: %s", filename);
return;
}
//Serial.printf("%s, %s\n", "psi", "deg_f");
struct tpms_raw dat;
int numb=fp.read((uint8_t *)&dat, sizeof(dat));
if(numb < sizeof(dat)) {
log_w("No bytes found in file: %s", filename);
fp.close();
return;
}
log_w("numb=%d", numb);
while(numb == sizeof(dat)) {
if(tc_idx<0 || tc_idx==dat.tc_idx)
csvPrintRawDat(dat);
numb=fp.read((uint8_t *)&dat, sizeof(dat));
// log_d("numb=%d", numb);
}
fp.close();
}