12-OBD-Speed: CAN logger page, shared SD mount, more PSRAM

- CAN log page: ELM327 monitor-mode survey/capture of broadcast frames with marks,
  written to /canlog on the SD card; serial "ls"/"get" commands to download logs
- sd_card: reference-counted SD_MMC mount shared by bg_store and the logger
- heap_caps_malloc_extmem_enable(64): internal RAM was down to ~10 KB, SD mounts
  failed with ESP_ERR_NO_MEM (DMA); now ~23 KB free, 13.8 KB DMA block
- docs/canlog: first test drive (Fiesta Mk8 2018) - no broadcast frames on the
  OBD port, only diagnostic replies (gateway)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Opus 5.5 committed 2026-11-08 19:14:07 +01:00
1 parent 21012adbec
commit 60cd7cf262
16 files changed
+1975 -47

No files matched your search

+26 -2
View File
@@ -16,6 +16,10 @@ Uses the shared board library and LVGL 9.6 (`../shared`), NimBLE-Arduino 2.5.1.
dimensions shown; not 360×360 in orange). Tap one: it is copied into the device's flash (progress shown) and
played from there, so the SD card is not needed afterwards. At boot the device checks (in the background)
whether the chosen file on the SD card changed (size/date) and re-copies it. **Rescan** re-reads the card.
- **Swipe left once more → CAN log** (temporary, to find the cruise/ACC set speed and speed-sign messages, see below).
**Start/Stop** (remembered: logging resumes by itself after power-up), **Mark**, the last frame of the three Ford
candidate IDs, and the file on the SD card. While logging, the speed page shows a red `REC <marks>` pill and
**a tap on the speed page adds a mark** (pill flashes `MARK n`).
- **Idle** — after that time without speed data (ignition off, adapter gone) the screen goes dark, the GIF pauses and
OBD polling stops; only `ATRV` every 30 s (answered by the adapter itself, doesn't wake the car's bus).
Wakes on a touch (the touch is swallowed) or by itself when the voltage rises above 13.2 V (engine started).
@@ -29,12 +33,32 @@ Service `FFF0`, notifications + commands on `FFF1` (verified with the user's "OB
Init: `ATZ`, `ATE0`, `ATL0`, `ATS0`, `ATH0`, `ATSP0`, `0100`; then `010D` (speed) is polled continuously.
Serial (115200) prints status changes as `[obd] ...`.
## CAN logger (finding ACC set speed / speed sign)
Neither value is a standard OBD PID; on Fords they are broadcast CAN messages. Candidates from opendbc
(`ford_lincoln_base_pt.dbc`, made for newer Fords; **not yet confirmed on the Fiesta Mk8**):
`0x165` EngBrakeData `Veh_V_DsplyCcSet` (set speed as shown) + `CcStat_D_Actl`, `0x202` `Veh_V_RqCcSet` (0.5 km/h),
`0x3D9` IPMA_Data2 `IsaVLim_D_Rq` (speed limit, 254 = no data) + `IsaVLimUnit_D_Rq` (1 km/h, 2 mph).
While logging, the OBD task (after `ATCSM1` silent monitoring, `ATH1`, `ATCAF0`):
1. **Survey**: listens to the ID ranges `0XX`..`7XX` (`ATCRA` with wildcards + `ATMA`, 1.3 s each); a range that
overflows the adapter (`BUFFER FULL` / 300 lines) is split into 16 smaller ones. Repeated every 10 min.
2. **Capture**: one frame of every ID found, in turn; the three candidates after every 8 others.
3. Every 2 s `ATAR` + `ATCAF1` + `010D` (speed, so the display keeps working, a bit laggy) and `ATRV`.
If the adapter can't filter by wildcard it falls back to unfiltered 2 s windows.
Log: `/canlog/log_NNN.txt` (new file per session, appended every 2 s; kept in RAM while the card is missing).
Lines `<ms> <type> <data>`: `C` adapter, `P` protocol, `V` speed, `B` voltage, `M` mark, `F id data` frame,
`N id` no frame, `I` survey IDs, `E` adapter message/note. Test drive: change the set speed and pass signs, tap
for a mark each time (passenger, or only when safe).
## Files
| File | What |
|---|---|
| `src/main.cpp` | LVGL UI (tileview: speed page + menu page) |
| `src/obd_link.cpp/.h` | BLE scan, connect/reconnect, ELM327 init and speed polling (background task on core 0) |
| `src/main.cpp` | LVGL UI (tileview: speed, adapter menu, settings, background, CAN log) |
| `src/obd_link.cpp/.h` | BLE scan, connect/reconnect, ELM327 init and speed polling, CAN logging steps (background task on core 0) |
| `src/font_speed_120.c` | Montserrat Medium 120 px, digits and `-`, 4 bpp (generated with `lv_font_conv`, see header) |
| `src/can_log.cpp/.h` | CAN log buffer + SD writer (background task), marks, values shown on the log page |
| `src/sd_card.cpp/.h` | Shared, reference-counted SD_MMC mount (background store + logger) |
| `src/bg_store.cpp/.h` | Background GIF from the SD card: list `/gifs`, copy into the `bggif` flash partition (header written last), mmap for playback, change check at boot (background task) |
| `partitions.csv` | 4 MB app (no OTA) + 11.9 MB `bggif` raw area + NVS |
| `data/bg.gif` | Built-in background (`ezgif-5-cb56986bc7.gif`, 360×360), linked into the firmware via `board_build.embed_files` |
+2
View File
@@ -0,0 +1,2 @@
# 12-OBD-Speed CAN log (t = ms since boot)
# C adapter, P protocol, V speed km/h, B battery V, M mark, F id data, N id (no frame), I survey IDs, E note
+3
View File
@@ -0,0 +1,3 @@
# 12-OBD-Speed CAN log (t = ms since boot)
# C adapter, P protocol, V speed km/h, B battery V, M mark, F id data, N id (no frame), I survey IDs, E note
71136 M 1
+353
View File
@@ -0,0 +1,353 @@
# 12-OBD-Speed CAN log (t = ms since boot)
# C adapter, P protocol, V speed km/h, B battery V, M mark, F id data, N id (no frame), I survey IDs, E note
7304 C OBDII ELM327 v2.1
7404 P A6
7504 E ATCSM1 OK
8305 V 0
8455 B 13.0
8905 E NO DATA
10505 E NO DATA
11105 V 0
11205 B 12.9
11655 E NO DATA
13206 E NO DATA
13856 V 0
13956 B 13.0
14406 E NO DATA
14869 M 1
15956 E NO DATA
16556 V 0
16656 B 13.0
17106 E NO DATA
18657 E NO DATA
19257 V 0
19357 B 13.0
19457 I
19803 E NO DATA
21404 E NO DATA
22954 E NO DATA
23555 V 0
23655 B 12.9
24106 E NO DATA
25656 E NO DATA
27256 E NO DATA
27956 V 0
28056 B 13.1
28506 E NO DATA
30057 E NO DATA
31607 E NO DATA
32207 V 0
32307 B 12.9
32757 E NO DATA
34357 E NO DATA
35957 E NO DATA
36553 V 1
36653 B 12.9
37153 E NO DATA
38753 E NO DATA
40354 E NO DATA
40954 V 3
41054 B 12.9
41506 E NO DATA
43057 E NO DATA
44657 E NO DATA
45357 V 5
45507 B 12.9
45957 E NO DATA
47507 E NO DATA
49057 E NO DATA
49707 V 9
49857 B 12.9
50307 E NO DATA
51903 E NO DATA
53503 E NO DATA
54155 V 8
54206 B 12.9
54357 E OK
54707 E NO DATA
56307 E NO DATA
57153 V 6
57253 B 12.9
57855 E NO DATA
59556 E NO DATA
61106 E NO DATA
61706 V 11
61806 B 12.9
62256 E NO DATA
63856 E NO DATA
65406 E NO DATA
66056 V 24
66156 B 12.9
66607 E NO DATA
68153 E NO DATA
69703 E NO DATA
70305 V 31
70405 B 12.9
70856 E NO DATA
72406 E NO DATA
73956 E NO DATA
74556 V 32
74656 B 12.9
75106 E NO DATA
76657 E NO DATA
78207 E NO DATA
78807 V 33
78907 B 12.9
79357 E NO DATA
80907 E NO DATA
82453 E NO DATA
83053 V 31
83153 B 12.9
83603 E NO DATA
85153 E NO DATA
86703 E NO DATA
87303 V 36
87403 B 12.9
87853 E NO DATA
89403 E NO DATA
90953 E NO DATA
91554 V 38
91654 B 12.9
92106 E NO DATA
93657 E NO DATA
95207 E NO DATA
95807 V 34
96007 B 12.9
96454 E NO DATA
98004 E NO DATA
99654 E NO DATA
100355 V 24
100505 B 12.9
100955 E NO DATA
102506 E NO DATA
104057 E NO DATA
104654 V 34
104754 B 12.9
105204 E NO DATA
106755 E NO DATA
108306 E NO DATA
108906 V 41
109006 B 12.9
117817 E disconnected
123303 C OBDII ELM327 v2.1
123403 P A6
123503 E ATCSM1 OK
124203 V 46
124303 B 12.9
124755 E NO DATA
126305 E NO DATA
126906 V 45
127006 B 12.9
127456 E NO DATA
129006 E NO DATA
129607 V 43
129757 B 12.9
130207 E NO DATA
131757 E NO DATA
132357 V 41
132457 B 12.9
132907 E NO DATA
134457 E NO DATA
135057 V 39
135157 B 12.9
135303 I
135604 E NO DATA
137154 E NO DATA
138704 E NO DATA
139304 V 22
139404 B 12.9
139854 E NO DATA
141423 M 2
141454 E NO DATA
143004 E NO DATA
143605 V 7
143705 B 13.0
144155 E NO DATA
145706 E NO DATA
147256 E NO DATA
147856 V 28
147956 B 12.9
148407 E NO DATA
149957 E NO DATA
151507 E NO DATA
152107 V 43
152207 B 12.9
152657 E NO DATA
154207 E NO DATA
155753 E NO DATA
156353 V 51
156453 B 13.0
156904 E NO DATA
158455 E NO DATA
160005 E NO DATA
160605 V 51
160705 B 12.9
161155 E NO DATA
162705 E NO DATA
164255 E NO DATA
164856 V 53
164956 B 12.9
165407 E NO DATA
166953 E NO DATA
168508 E NO DATA
169103 V 46
169253 B 12.9
169703 E NO DATA
171254 E NO DATA
172805 E NO DATA
173406 V 19
173506 B 12.9
173956 E NO DATA
175506 E NO DATA
177056 E NO DATA
177656 V 3
177756 B 12.9
178206 E NO DATA
179806 E NO DATA
181357 E NO DATA
181957 V 8
182057 B 12.9
182507 E NO DATA
184057 E NO DATA
185603 E NO DATA
186203 V 31
186304 B 12.9
186756 E NO DATA
188307 E NO DATA
189857 E NO DATA
190457 V 45
190557 B 13.0
191007 E NO DATA
192603 E NO DATA
194154 E NO DATA
194754 V 49
194854 B 12.9
195304 E NO DATA
196855 E NO DATA
198406 E NO DATA
199006 V 49
199106 B 12.9
199556 E NO DATA
201106 E NO DATA
202656 E NO DATA
203257 V 50
203357 B 12.9
203807 E NO DATA
205353 E NO DATA
206903 E NO DATA
207504 V 50
207604 B 12.9
208054 E NO DATA
209604 E NO DATA
211155 E NO DATA
211755 V 50
211855 B 12.9
212305 E NO DATA
213855 E NO DATA
215405 E NO DATA
216005 V 50
216105 B 12.9
216555 E NO DATA
218105 E NO DATA
219656 E NO DATA
220256 V 49
220356 B 12.9
220806 E NO DATA
222357 E NO DATA
223903 E NO DATA
224503 V 51
224603 B 12.9
225055 E NO DATA
226655 E NO DATA
228206 E NO DATA
228806 V 46
228906 B 12.9
229356 E NO DATA
230906 E NO DATA
232456 E NO DATA
233057 V 24
233157 B 12.9
233603 E NO DATA
235153 E NO DATA
236703 E NO DATA
237303 V 5
237403 B 12.9
237853 E NO DATA
239403 E NO DATA
240953 E NO DATA
241553 V 0
241653 B 12.9
242103 E NO DATA
243653 E NO DATA
245203 E NO DATA
245804 V 0
245904 B 12.9
246355 E NO DATA
247906 E NO DATA
249456 E NO DATA
250056 V 16
250156 B 13.0
250606 E NO DATA
252156 E NO DATA
253706 E NO DATA
254307 V 37
254407 B 12.9
254857 E NO DATA
256407 E NO DATA
257957 E NO DATA
258557 V 47
258657 B 13.0
259107 E NO DATA
260653 E NO DATA
262203 E NO DATA
262805 V 45
262905 B 12.9
263356 E NO DATA
264906 E NO DATA
266456 E NO DATA
267056 V 44
267156 B 13.0
267607 E NO DATA
269157 E NO DATA
270707 E NO DATA
271307 V 45
271457 B 12.9
271907 E NO DATA
273457 E NO DATA
275007 E NO DATA
275607 V 48
275707 B 12.9
276157 E NO DATA
277757 E NO DATA
279307 E NO DATA
279907 V 47
280007 B 12.9
280457 E NO DATA
282007 E NO DATA
283557 E NO DATA
284157 V 32
284257 B 13.0
284707 E NO DATA
286457 E NO DATA
288003 E NO DATA
288653 V 12
288753 B 13.0
289255 E NO DATA
290805 E NO DATA
292355 E NO DATA
292956 V 13
293056 B 13.0
293507 E NO DATA
295057 E NO DATA
296607 E NO DATA
297257 V 8
297357 B 13.0
297804 E NO DATA
299404 E NO DATA
301054 E NO DATA
301705 V 3
301805 B 13.0
302255 E NO DATA
303805 E NO DATA
305355 E NO DATA
305906 V -1
+210
View File
@@ -0,0 +1,210 @@
# 12-OBD-Speed CAN log (t = ms since boot)
# C adapter, P protocol, V speed km/h, B battery V, M mark, F id data, N id (no frame), I survey IDs, E note
7003 C OBDII ELM327 v2.1
7153 P A6
7303 E ATCSM1 OK
8253 V 0
8354 B 13.0
8804 E NO DATA
10504 E NO DATA
11205 V 4
11455 B 12.9
12006 E NO DATA
13806 E NO DATA
14457 V 5
14557 B 13.0
15007 E NO DATA
16557 E NO DATA
17157 V 5
17357 B 13.0
19257 E NO DATA
19907 V 6
20057 B 13.0
20507 E NO DATA
20507 I
22157 E NO DATA
23757 E NO DATA
25503 E NO DATA
26208 V 7
26253 B 13.0
26508 E OK
26908 E OK
26908 E >NO DATA
27053 E OK
28604 E NO DATA
30204 E NO DATA
31755 E NO DATA
32355 V 6
32455 B 12.9
32956 E NO DATA
34506 E NO DATA
36106 E NO DATA
36706 V 23
36806 B 13.0
37356 E NO DATA
38956 E NO DATA
40506 E NO DATA
41106 V 41
41206 B 12.9
41657 E NO DATA
43207 E NO DATA
44753 E NO DATA
45353 V 41
45454 B 12.9
45904 E NO DATA
47455 E NO DATA
49005 E NO DATA
49605 V 34
49705 B 12.9
50156 E NO DATA
51706 E NO DATA
53256 E NO DATA
53857 V 28
53957 B 12.9
54457 E NO DATA
54607 N 202
56157 E NO DATA
56753 V 33
56854 B 12.9
57304 E NO DATA
58904 E NO DATA
60455 E NO DATA
61055 V 36
61155 B 12.9
61606 E NO DATA
63256 E NO DATA
64807 E NO DATA
65407 V 40
65557 B 12.9
66007 E NO DATA
67557 E NO DATA
69153 E NO DATA
69753 V 43
69903 B 12.9
70354 E NO DATA
71904 E NO DATA
73504 E NO DATA
74255 V 43
74355 B 12.9
74856 E NO DATA
76456 E NO DATA
78106 E NO DATA
78756 V 42
78856 B 12.9
79306 E NO DATA
80856 E NO DATA
82406 E NO DATA
83006 V 40
83106 B 12.9
83556 E NO DATA
85107 E NO DATA
86653 E NO DATA
87253 V 35
87403 B 12.9
87853 E NO DATA
89454 E NO DATA
91004 E NO DATA
91605 V 26
91705 B 12.9
92156 E NO DATA
93706 E NO DATA
95256 E NO DATA
95856 V 6
95956 B 12.9
96406 E NO DATA
97957 E NO DATA
99507 E NO DATA
100103 V 0
100203 B 12.9
100653 E NO DATA
102254 E NO DATA
103854 E NO DATA
104505 V 15
104705 B 12.9
105156 E NO DATA
106706 E NO DATA
108256 E NO DATA
108857 V 32
108957 B 12.9
109507 E NO DATA
111057 E NO DATA
112603 E NO DATA
113203 V 42
113303 B 13.0
113754 E NO DATA
115354 E NO DATA
116904 E NO DATA
117556 V 42
117656 B 12.9
118107 E NO DATA
119657 E NO DATA
121257 E NO DATA
121907 V 39
122007 B 12.9
122457 E NO DATA
124057 E NO DATA
125603 E NO DATA
126253 V 34
126353 B 12.9
126803 E NO DATA
128353 E NO DATA
129953 E NO DATA
130653 V 29
130753 B 12.9
131204 E NO DATA
132904 E NO DATA
134554 E NO DATA
135155 V 21
135255 B 13.0
135705 E NO DATA
137255 E NO DATA
138856 E NO DATA
139556 V 16
139656 B 12.9
140156 E NO DATA
141756 E NO DATA
143306 E NO DATA
144006 V 10
144106 B 12.9
144556 E NO DATA
146156 E NO DATA
147706 E NO DATA
148306 V 12
148456 B 12.9
148956 E NO DATA
150506 E NO DATA
152156 E NO DATA
152806 V 3
152906 B 12.9
153357 E NO DATA
154907 E NO DATA
156453 E NO DATA
157153 V 4
157353 B 12.9
157804 E NO DATA
159404 E NO DATA
161005 E NO DATA
161606 V 0
161756 B 12.8
162207 E NO DATA
163807 E NO DATA
165503 E NO DATA
166204 V 1
166404 B 12.9
168455 E NO DATA
170106 E NO DATA
171706 E NO DATA
172306 V 0
172506 B 12.9
173007 E NO DATA
174653 E NO DATA
176204 E NO DATA
176804 V 0
176904 B 12.9
177355 E NO DATA
178905 E NO DATA
180455 E NO DATA
181055 V 0
181205 B 12.9
181656 E NO DATA
183206 E NO DATA
+473
View File
@@ -0,0 +1,473 @@
# 12-OBD-Speed CAN log (t = ms since boot)
# C adapter, P protocol, V speed km/h, B battery V, M mark, F id data, N id (no frame), I survey IDs, E note
7103 C OBDII ELM327 v2.1
7203 P A6
7303 E ATCSM1 OK
8003 V 0
8103 B 12.9
8555 E NO DATA
10155 E NO DATA
10755 V 0
10855 B 12.9
11405 E NO DATA
12956 E NO DATA
13556 V 0
13656 B 12.9
14156 E NO DATA
15706 E NO DATA
16306 V 0
16406 B 12.9
16856 E NO DATA
18406 E NO DATA
19006 V 0
19106 B 12.9
19206 I
19606 E NO DATA
21156 E NO DATA
22706 E NO DATA
23356 V 1
23506 B 12.9
23957 E NO DATA
25557 E NO DATA
27107 E NO DATA
27753 V 4
27853 B 12.9
28354 E NO DATA
29904 E NO DATA
31505 E NO DATA
32106 V 5
32206 B 12.7
32656 E NO DATA
34206 E NO DATA
35757 E NO DATA
36357 V 7
36457 B 12.9
36903 E NO DATA
38453 E NO DATA
40053 E NO DATA
40703 V 14
40803 B 12.9
41303 E NO DATA
42853 E NO DATA
44404 E NO DATA
45204 V 11
45304 B 12.9
45804 E NO DATA
47354 E NO DATA
48954 E NO DATA
49555 V 22
49655 B 12.9
50106 E NO DATA
51657 E NO DATA
53353 E NO DATA
53954 V 32
54054 B 12.9
54505 E NO DATA
56055 E NO DATA
57605 E NO DATA
58205 V 35
58305 B 12.9
58805 E NO DATA
60405 E NO DATA
61955 E NO DATA
62555 V 42
62655 B 12.9
63106 E NO DATA
64656 E NO DATA
66207 E NO DATA
66807 V 46
66907 B 12.9
67357 E NO DATA
68903 E NO DATA
70453 E NO DATA
71053 V 37
71153 B 12.9
71603 E NO DATA
73154 E NO DATA
74855 E NO DATA
75455 V 30
75555 B 12.8
76055 E NO DATA
77656 E NO DATA
79206 E NO DATA
79906 V 23
80106 B 12.9
80657 E NO DATA
82207 E NO DATA
83753 E NO DATA
84353 V 37
84453 B 12.9
84903 E NO DATA
86454 E NO DATA
88005 E NO DATA
88606 V 45
88706 B 12.9
89156 E NO DATA
90706 E NO DATA
92256 E NO DATA
92856 V 49
92956 B 12.9
93406 E NO DATA
94956 E NO DATA
96506 E NO DATA
97106 V 51
97206 B 12.8
97656 E NO DATA
99206 E NO DATA
100756 E NO DATA
101356 V 51
101456 B 12.9
101906 E NO DATA
103456 E NO DATA
105006 E NO DATA
105606 V 51
105706 B 12.9
106153 E NO DATA
107703 E NO DATA
109253 E NO DATA
109853 V 50
109953 B 12.8
110405 E NO DATA
111955 E NO DATA
113505 E NO DATA
114105 V 50
114205 B 12.9
114655 E NO DATA
116205 E NO DATA
117755 E NO DATA
118355 V 50
118455 B 12.9
118906 E NO DATA
120457 E NO DATA
122007 E NO DATA
122607 V 50
122707 B 12.9
123154 E NO DATA
124704 E NO DATA
126254 E NO DATA
126854 V 50
126954 B 12.9
127404 E NO DATA
128954 E NO DATA
130505 E NO DATA
131106 V 50
131206 B 12.9
140012 E disconnected
145303 C OBDII ELM327 v2.1
145403 P A6
145503 E ATCSM1 OK
146203 V 50
146303 B 12.9
146753 E NO DATA
148303 E NO DATA
148904 V 50
149004 B 12.8
149454 E NO DATA
151005 E NO DATA
151605 V 50
151705 B 12.9
152155 E NO DATA
153705 E NO DATA
154306 V 51
154406 B 12.8
154857 E NO DATA
156407 E NO DATA
157007 V 50
157157 B 12.9
157257 I
157607 E NO DATA
159157 E NO DATA
160753 E NO DATA
161353 V 50
161453 B 12.9
161905 E NO DATA
163456 E NO DATA
165006 E NO DATA
165606 V 50
165706 B 12.9
166156 E NO DATA
167707 E NO DATA
169253 E NO DATA
169854 V 50
169954 B 12.9
170405 E NO DATA
171955 E NO DATA
173506 E NO DATA
174106 V 51
174206 B 12.8
174657 E NO DATA
176203 E NO DATA
177753 E NO DATA
178453 V 48
178553 B 12.9
179003 E NO DATA
180654 E NO DATA
182354 E NO DATA
182954 V 32
183104 B 12.9
183604 E NO DATA
183755 E OK
185356 E NO DATA
185957 V 10
186057 B 12.9
186507 E NO DATA
188057 E NO DATA
189757 E NO DATA
190403 V 2
190503 B 12.9
190953 E NO DATA
192503 E NO DATA
194053 E NO DATA
194653 V 24
194803 B 12.8
195254 E NO DATA
196805 E NO DATA
198355 E NO DATA
198955 V 41
199055 B 12.8
199506 E NO DATA
201057 E NO DATA
202607 E NO DATA
203207 V 44
203307 B 12.9
203754 E NO DATA
205304 E NO DATA
206854 E NO DATA
207454 V 46
207554 B 12.9
208004 E NO DATA
209555 E NO DATA
211105 E NO DATA
211705 V 44
211805 B 12.9
212256 E NO DATA
213806 E NO DATA
215356 E NO DATA
215956 V 37
216056 B 12.9
216506 E NO DATA
218106 E NO DATA
219706 E NO DATA
220306 V 29
220406 B 12.9
220856 E NO DATA
222406 E NO DATA
223957 E NO DATA
224557 V 37
224657 B 12.9
225103 E NO DATA
226653 E NO DATA
228203 E NO DATA
228803 V 41
228903 B 12.9
229353 E NO DATA
230903 E NO DATA
232453 E NO DATA
233054 V 41
233154 B 12.9
233606 E NO DATA
235156 E NO DATA
236706 E NO DATA
237306 V 41
237406 B 12.9
237856 E NO DATA
239407 E NO DATA
240957 E NO DATA
241557 V 38
241657 B 12.9
242107 E NO DATA
243657 E NO DATA
245207 E NO DATA
245804 V 38
245904 B 12.9
246355 E NO DATA
247905 E NO DATA
249455 E NO DATA
250056 V 37
250156 B 12.9
250606 E NO DATA
252156 E NO DATA
253707 E NO DATA
254307 V 37
254407 B 12.8
254854 E NO DATA
256405 E NO DATA
257955 E NO DATA
258605 V 38
258705 B 12.9
259155 E NO DATA
260705 E NO DATA
262255 E NO DATA
262905 V 39
263005 B 12.8
263456 E NO DATA
265006 E NO DATA
266556 E NO DATA
267156 V 40
267256 B 12.9
267707 E NO DATA
269257 E NO DATA
270803 E NO DATA
271403 V 41
271503 B 12.9
271954 E NO DATA
273505 E NO DATA
275055 E NO DATA
275655 V 42
275755 B 12.9
276206 E NO DATA
277757 E NO DATA
279303 E NO DATA
279904 V 45
280004 B 12.9
280454 E NO DATA
282004 E NO DATA
283555 E NO DATA
284155 V 47
284255 B 12.9
284705 E NO DATA
286255 E NO DATA
287805 E NO DATA
288406 V 45
288506 B 12.9
288956 E NO DATA
290506 E NO DATA
292056 E NO DATA
292656 V 43
292756 B 12.9
293203 E NO DATA
294804 E NO DATA
296354 E NO DATA
297005 V 37
297105 B 12.9
297555 E NO DATA
299105 E NO DATA
300755 E NO DATA
301355 V 31
301505 B 12.9
301955 E NO DATA
303505 E NO DATA
305055 E NO DATA
305656 V 34
305756 B 12.9
306206 E NO DATA
307756 E NO DATA
309306 E NO DATA
309906 V 39
310006 B 12.9
310507 E NO DATA
312053 E NO DATA
313603 E NO DATA
314203 V 34
314303 B 12.9
314754 E NO DATA
316304 E NO DATA
317854 E NO DATA
318454 V 35
318554 B 12.9
319006 E NO DATA
320556 E NO DATA
322106 E NO DATA
322706 V 41
322806 B 12.9
323256 E NO DATA
324807 E NO DATA
326353 E NO DATA
327004 V 39
327104 B 12.9
327555 E NO DATA
329106 E NO DATA
330656 E NO DATA
331256 V 22
331356 B 12.9
331807 E NO DATA
333407 E NO DATA
335053 E NO DATA
335653 V 13
335753 B 12.9
337953 E NO DATA
339503 E NO DATA
341053 E NO DATA
341654 V 10
341754 B 12.9
342254 E NO DATA
343904 E NO DATA
345654 E NO DATA
346404 V 17
346554 B 12.9
347004 E NO DATA
348554 E NO DATA
350155 E NO DATA
350855 V 13
350905 B 12.9
351105 E NG...
351105 E OK
351556 E NO DATA
353156 E NO DATA
353856 V 19
354056 B 12.9
354503 E NO DATA
356053 E NO DATA
357603 E NO DATA
358203 V 20
358303 B 12.9
358754 E NO DATA
360354 E NO DATA
361904 E NO DATA
362554 V 16
362654 B 12.9
363104 E NO DATA
364704 E NO DATA
366255 E NO DATA
366856 V 12
367006 B 12.9
367456 E NO DATA
369007 E NO DATA
370657 E NO DATA
371458 V -1
372104 B 12.9
372654 E NO DATA
374205 E NO DATA
375756 E NO DATA
376457 V 12
376607 B 12.9
377053 E NO DATA
378703 E NO DATA
380303 E NO DATA
380903 V 12
381003 B 12.9
381453 E NO DATA
383004 E NO DATA
384554 E NO DATA
385204 V 12
385304 B 12.9
385754 E NO DATA
387304 E NO DATA
388954 E NO DATA
389655 V 6
389755 B 12.9
390355 E NO DATA
392005 E NO DATA
393855 E NO DATA
394455 V 0
394555 B 12.5
395006 E NO DATA
396556 E NO DATA
398207 E NO DATA
398857 V 3
398957 B 12.9
399407 E NO DATA
401007 E NO DATA
402707 E NO DATA
403407 V 1
403657 B 12.8
404105 E NO DATA
405806 E NO DATA
407506 E NO DATA
408206 V -1
408806 B 12.9
409306 E NO DATA
410907 E NO DATA
412553 E NO DATA
413153 V 0
413303 B 12.9
+3
View File
@@ -0,0 +1,3 @@
# 12-OBD-Speed CAN log (t = ms since boot)
# C adapter, P protocol, V speed km/h, B battery V, M mark, F id data, N id (no frame), I survey IDs, E note
124606 E idle
+2
View File
@@ -0,0 +1,2 @@
# 12-OBD-Speed CAN log (t = ms since boot)
# C adapter, P protocol, V speed km/h, B battery V, M mark, F id data, N id (no frame), I survey IDs, E note
+4 -9
View File
@@ -5,7 +5,7 @@
#include <SD_MMC.h>
#include <esp_partition.h>
#include "pincfg.h"
#include "sd_card.h"
#define BG_MAGIC 0x46494742 // "BGIF"
#define BG_DATA_OFFSET 4096 // header in the first sector, GIF data after it
@@ -78,14 +78,9 @@ static void unmap_partition()
map_valid = false;
}
// ---------- SD card ----------
static bool sd_mount()
{
SD_MMC.setPins(SD_MMC_CLK_PIN, SD_MMC_CMD_PIN, SD_MMC_D0_PIN, SD_MMC_D1_PIN, SD_MMC_D2_PIN, SD_MMC_D3_PIN);
return SD_MMC.begin() && SD_MMC.cardType() != CARD_NONE;
}
static void sd_unmount() { SD_MMC.end(); }
// ---------- SD card (shared with the CAN logger) ----------
static bool sd_mount() { return sd_acquire(); }
static void sd_unmount() { sd_release(); }
static void do_list()
{
+290
View File
@@ -0,0 +1,290 @@
#include "can_log.h"
#include <FS.h>
#include <Preferences.h>
#include <SD_MMC.h>
#include "sd_card.h"
#define FLUSH_INTERVAL_MS 2000
#define SD_RETRY_MS 10000
#define MAX_BUFFER (256 * 1024) // kept in RAM while the SD card is missing; beyond that lines are dropped
// ---------- shared state (protected by `mtx`) ----------
static SemaphoreHandle_t mtx;
static volatile bool enabled = false;
static String buffer;
static uint32_t dropped = 0;
static int marks = 0;
static String status = "Off";
static String phase;
static String frames[CANLOG_SLOTS];
// ---------- writer-owned ----------
static bool file_open = false;
static String path;
static uint32_t written = 0;
static void lock() { xSemaphoreTake(mtx, portMAX_DELAY); }
static void unlock() { xSemaphoreGive(mtx); }
static void set_status(const String &s)
{
lock();
status = s;
unlock();
}
static void update_status()
{
set_status(path.substring(path.lastIndexOf('/') + 1) + " " + String((written + 512) / 1024) + " KB" +
(dropped ? " dropped " + String(dropped) : String()));
}
// New file /canlog/log_NNN.txt with a header describing the format
static void open_session()
{
if (!sd_acquire())
{
set_status("SD card not found");
return;
}
SD_MMC.mkdir(CANLOG_FOLDER);
char name[40];
for (int i = 1; i <= 999; i++)
{
snprintf(name, sizeof(name), CANLOG_FOLDER "/log_%03d.txt", i);
if (!SD_MMC.exists(name))
break;
}
File f = SD_MMC.open(name, FILE_WRITE);
if (!f)
{
set_status("Can't create log file");
sd_release();
return;
}
f.print("# 12-OBD-Speed CAN log (t = ms since boot)\n"
"# C adapter, P protocol, V speed km/h, B battery V, M mark, F id data, N id (no frame), I survey IDs, E note\n");
f.close();
path = name;
written = 0;
file_open = true;
Serial.println("[log] " + path);
update_status();
}
static void flush()
{
lock();
String chunk = std::move(buffer);
buffer = String();
unlock();
if (chunk.length() == 0)
return;
File f = SD_MMC.open(path, FILE_APPEND);
size_t n = f ? f.print(chunk) : 0;
if (f)
f.close();
if (n != chunk.length())
{
// card gone or full: start over with a new file once it works again
file_open = false;
sd_release();
set_status("SD write failed");
return;
}
written += n;
update_status();
}
static void writer_task(void *)
{
uint32_t next_try = 0;
for (;;)
{
vTaskDelay(pdMS_TO_TICKS(FLUSH_INTERVAL_MS));
if (enabled && !file_open && (int32_t)(millis() - next_try) >= 0)
{
open_session();
next_try = millis() + SD_RETRY_MS;
}
if (file_open)
flush();
if (!enabled && file_open)
{
file_open = false;
sd_release();
set_status("Stopped, saved " + path.substring(path.lastIndexOf('/') + 1));
}
}
}
// ---------- public API ----------
void can_log_begin()
{
mtx = xSemaphoreCreateMutex();
Preferences p;
p.begin("canlog", true);
enabled = p.getBool("on", false);
p.end();
if (enabled)
status = "Waiting for SD card";
xTaskCreatePinnedToCore(writer_task, "can_log", 6144, nullptr, 1, nullptr, 0);
}
bool can_log_enabled() { return enabled; }
void can_log_set_enabled(bool on)
{
if (on == enabled)
return;
Preferences p;
p.begin("canlog", false);
p.putBool("on", on);
p.end();
lock();
if (on)
{
marks = 0;
dropped = 0;
status = "Waiting for SD card";
}
else if (!file_open)
{
buffer = ""; // never got a file: nothing to save
status = "Off";
}
unlock();
enabled = on;
}
void can_log_line(const String &text)
{
if (!enabled)
return;
String line = String(millis()) + " " + text + "\n";
lock();
if (buffer.length() + line.length() <= MAX_BUFFER)
buffer += line;
else
dropped++;
unlock();
}
int can_log_mark()
{
lock();
int n = ++marks;
unlock();
can_log_line("M " + String(n));
Serial.printf("[log] mark %d\n", n);
return n;
}
int can_log_marks()
{
lock();
int n = marks;
unlock();
return n;
}
String can_log_status()
{
lock();
String s = status;
unlock();
return s;
}
static void serial_command(String cmd)
{
cmd.trim();
if (cmd != "ls" && !cmd.startsWith("get "))
return;
if (!sd_acquire())
{
Serial.println("<<<ERROR SD card not found>>>");
return;
}
if (cmd == "ls")
{
Serial.println("<<<LS>>>");
File dir = SD_MMC.open(CANLOG_FOLDER);
if (dir && dir.isDirectory())
for (File f = dir.openNextFile(); f; f = dir.openNextFile())
Serial.printf("%s %u\n", f.name(), (unsigned)f.size());
Serial.println("<<<END>>>");
}
else
{
String name = cmd.substring(4);
name.trim();
File f = SD_MMC.open(String(CANLOG_FOLDER) + "/" + name);
if (!f)
Serial.println("<<<ERROR not found>>>");
else
{
Serial.printf("<<<BEGIN %s %u>>>\n", name.c_str(), (unsigned)f.size());
static uint8_t buf[1024];
int n;
while ((n = f.read(buf, sizeof(buf))) > 0)
Serial.write(buf, n);
f.close();
Serial.println("\n<<<END>>>");
}
}
sd_release();
}
void can_log_serial_poll()
{
static String line;
while (Serial.available())
{
char c = Serial.read();
if (c == '\n' || c == '\r')
{
if (line.length())
serial_command(line);
line = "";
}
else if (line.length() < 80)
line += c;
}
}
void can_log_set_phase(const String &s)
{
lock();
phase = s;
unlock();
}
String can_log_phase()
{
lock();
String s = phase;
unlock();
return s;
}
void can_log_set_frame(int slot, const String &s)
{
if (slot < 0 || slot >= CANLOG_SLOTS)
return;
lock();
frames[slot] = s;
unlock();
}
String can_log_frame(int slot)
{
if (slot < 0 || slot >= CANLOG_SLOTS)
return String();
lock();
String s = frames[slot];
unlock();
return s;
}
+33
View File
@@ -0,0 +1,33 @@
// CAN logger, to find the Ford cruise/ACC set speed and speed-sign messages: the OBD task records broadcast
// CAN frames (ELM327 monitor mode), speed and voltage; the UI adds marks. Lines are appended to
// /canlog/log_NNN.txt on the SD card every 2 s (new file per session). All functions are thread-safe.
//
// Line format: "<ms since boot> <type> <data>"
// C adapter info P protocol (ATDPN) V speed km/h (010D) B battery V M mark number
// F <id> <data> one CAN frame (hex) N <id> no frame seen I <ids> IDs found by the survey
// E <text> adapter message / note
#pragma once
#include <Arduino.h>
#define CANLOG_FOLDER "/canlog"
#define CANLOG_SLOTS 3 // frames shown on the log page (the Ford candidate IDs)
void can_log_begin();
bool can_log_enabled();
void can_log_set_enabled(bool on); // remembered in NVS
void can_log_line(const String &text); // adds the timestamp; ignored while logging is off
int can_log_mark(); // logs a mark, returns its number
int can_log_marks();
String can_log_status(); // file + size, or what is wrong
// Serial commands for reading the logs on the PC (call from loop()):
// "ls" -> one line per file in /canlog: "<name> <size>"
// "get <name>" -> "<<<BEGIN <name> <size>>>>", the file, "<<<END>>>"
void can_log_serial_poll();
// Shown on the log page, set by the OBD task
void can_log_set_phase(const String &s);
String can_log_phase();
void can_log_set_frame(int slot, const String &s);
String can_log_frame(int slot);
+132 -2
View File
@@ -4,11 +4,14 @@
// background page (built-in GIF or one from /gifs on the SD card, copied into flash).
// Battery voltage is shown as a small arc in the bottom gap; after a while without speed data the
// screen goes dark (idle) and wakes on touch or when the engine starts.
// Last page: CAN logger (to find the cruise set speed / speed-sign messages); while logging, a tap on
// the speed page adds a mark.
#include <Arduino.h>
#include <Preferences.h>
#include "jc3636w518.h"
#include "obd_link.h"
#include "bg_store.h"
#include "can_log.h"
#define SPEED_MAX 200
#define VOLT_MIN_X10 110 // voltage arc range 11.0 .. 15.0 V
@@ -28,7 +31,9 @@ static const lv_color_t COLOR_ARC = lv_color_hex(0x00C8FF);
static const lv_color_t COLOR_OBD = lv_color_hex(0x4CD964);
static const lv_color_t COLOR_PANEL = lv_color_hex(0x161C22);
static lv_obj_t *tileview, *tile_speed, *tile_menu, *tile_settings, *tile_bg;
static lv_obj_t *tileview, *tile_speed, *tile_menu, *tile_settings, *tile_bg, *tile_log;
static lv_obj_t *rec_label, *log_status_label, *log_phase_label, *log_frames_label, *log_toggle_label;
static uint32_t mark_shown_until = 0;
static lv_obj_t *bg_status_label, *bg_list;
static std::vector<BgFile> bg_files;
static uint32_t bg_list_version = 0, bg_source_version = 0;
@@ -209,6 +214,19 @@ static void on_bg_rescan_clicked(lv_event_t *e)
bg_request_list();
}
// ---------- CAN log ----------
static void add_mark()
{
if (!can_log_enabled())
return;
can_log_mark();
mark_shown_until = millis() + 1500;
}
static void on_speed_tapped(lv_event_t *e) { add_mark(); }
static void on_mark_clicked(lv_event_t *e) { add_mark(); }
static void on_log_toggle_clicked(lv_event_t *e) { can_log_set_enabled(!can_log_enabled()); }
// ---------- speed page ----------
static void build_speed_tile()
{
@@ -289,6 +307,19 @@ static void build_speed_tile()
lv_obj_set_style_pad_ver(status_label, 3, 0);
lv_label_set_text(status_label, "");
lv_obj_align(status_label, LV_ALIGN_CENTER, 0, 100);
// shown while the CAN logger runs; a tap on the page adds a mark
rec_label = lv_label_create(tile_speed);
lv_obj_set_style_text_font(rec_label, &lv_font_montserrat_16, 0);
lv_obj_set_style_text_color(rec_label, COLOR_TEXT, 0);
lv_obj_set_style_bg_color(rec_label, lv_color_hex(0xFF3B30), 0);
lv_obj_set_style_bg_opa(rec_label, LV_OPA_80, 0);
lv_obj_set_style_radius(rec_label, 12, 0);
lv_obj_set_style_pad_hor(rec_label, 10, 0);
lv_obj_set_style_pad_ver(rec_label, 3, 0);
lv_obj_align(rec_label, LV_ALIGN_TOP_MID, 0, 44);
lv_obj_add_flag(rec_label, LV_OBJ_FLAG_HIDDEN);
lv_obj_add_event_cb(tile_speed, on_speed_tapped, LV_EVENT_SHORT_CLICKED, NULL);
}
// ---------- menu page ----------
@@ -405,6 +436,60 @@ static void build_bg_tile()
make_button(tile_bg, "Done", 50, 296, 92, on_done_clicked);
}
static void build_log_tile()
{
lv_obj_t *title = lv_label_create(tile_log);
lv_obj_set_style_text_font(title, &lv_font_montserrat_20, 0);
lv_obj_set_style_text_color(title, COLOR_TEXT, 0);
lv_label_set_text(title, "CAN log");
lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 20);
log_status_label = lv_label_create(tile_log);
lv_obj_set_style_text_color(log_status_label, COLOR_TEXT, 0);
lv_obj_set_width(log_status_label, 260);
lv_label_set_long_mode(log_status_label, LV_LABEL_LONG_MODE_DOTS);
lv_obj_set_style_text_align(log_status_label, LV_TEXT_ALIGN_CENTER, 0);
lv_label_set_text(log_status_label, "");
lv_obj_align(log_status_label, LV_ALIGN_TOP_MID, 0, 48);
log_phase_label = lv_label_create(tile_log);
lv_obj_set_style_text_color(log_phase_label, COLOR_DIM, 0);
lv_obj_set_width(log_phase_label, 290);
lv_label_set_long_mode(log_phase_label, LV_LABEL_LONG_MODE_DOTS);
lv_obj_set_style_text_align(log_phase_label, LV_TEXT_ALIGN_CENTER, 0);
lv_label_set_text(log_phase_label, "");
lv_obj_align(log_phase_label, LV_ALIGN_TOP_MID, 0, 68);
// last frame of the Ford candidate IDs
log_frames_label = lv_label_create(tile_log);
lv_obj_set_style_text_color(log_frames_label, COLOR_TEXT, 0);
lv_obj_set_style_text_font(log_frames_label, &lv_font_montserrat_16, 0);
lv_obj_set_style_text_align(log_frames_label, LV_TEXT_ALIGN_CENTER, 0);
lv_label_set_text(log_frames_label, "");
lv_obj_align(log_frames_label, LV_ALIGN_TOP_MID, 0, 94);
lv_obj_t *toggle = make_button(tile_log, "", -62, 164, 112, on_log_toggle_clicked);
log_toggle_label = lv_obj_get_child(toggle, 0);
lv_obj_t *mark = lv_button_create(tile_log);
lv_obj_set_size(mark, 112, 34);
lv_obj_align(mark, LV_ALIGN_TOP_MID, 62, 164);
lv_obj_set_style_bg_color(mark, lv_color_hex(0xFF9500), 0);
lv_obj_add_event_cb(mark, on_mark_clicked, LV_EVENT_CLICKED, NULL);
lv_obj_t *l = lv_label_create(mark);
lv_label_set_text(l, "Mark");
lv_obj_set_style_text_font(l, &lv_font_montserrat_16, 0);
lv_obj_center(l);
lv_obj_t *hint = lv_label_create(tile_log);
lv_obj_set_style_text_color(hint, COLOR_DIM, 0);
lv_obj_set_style_text_align(hint, LV_TEXT_ALIGN_CENTER, 0);
lv_label_set_text(hint, "While logging: tap the speed page\nto add a mark (sign passed,\nset speed changed). Log files in\n" CANLOG_FOLDER " on the SD card.");
lv_obj_align(hint, LV_ALIGN_TOP_MID, 0, 208);
make_button(tile_log, "Done", 0, 296, 110, on_done_clicked);
}
static void build_ui()
{
lv_obj_set_style_bg_color(lv_screen_active(), lv_color_hex(0x000000), 0);
@@ -414,12 +499,14 @@ static void build_ui()
tile_speed = lv_tileview_add_tile(tileview, 0, 0, LV_DIR_RIGHT);
tile_menu = lv_tileview_add_tile(tileview, 1, 0, (lv_dir_t)(LV_DIR_LEFT | LV_DIR_RIGHT));
tile_settings = lv_tileview_add_tile(tileview, 2, 0, (lv_dir_t)(LV_DIR_LEFT | LV_DIR_RIGHT));
tile_bg = lv_tileview_add_tile(tileview, 3, 0, LV_DIR_LEFT);
tile_bg = lv_tileview_add_tile(tileview, 3, 0, (lv_dir_t)(LV_DIR_LEFT | LV_DIR_RIGHT));
tile_log = lv_tileview_add_tile(tileview, 4, 0, LV_DIR_LEFT);
lv_obj_add_event_cb(tileview, on_tile_changed, LV_EVENT_VALUE_CHANGED, NULL);
build_speed_tile();
build_menu_tile();
build_settings_tile();
build_bg_tile();
build_log_tile();
idle_overlay = lv_obj_create(lv_layer_top());
lv_obj_remove_style_all(idle_overlay);
@@ -513,6 +600,43 @@ static void update_speed()
}
}
static void set_label(lv_obj_t *label, const String &text)
{
if (text != lv_label_get_text(label))
lv_label_set_text(label, text.c_str());
}
static void update_log()
{
bool on = can_log_enabled();
if (on == lv_obj_has_flag(rec_label, LV_OBJ_FLAG_HIDDEN))
{
if (on)
lv_obj_remove_flag(rec_label, LV_OBJ_FLAG_HIDDEN);
else
lv_obj_add_flag(rec_label, LV_OBJ_FLAG_HIDDEN);
}
if (on)
{
bool flash = (int32_t)(millis() - mark_shown_until) < 0;
set_label(rec_label, flash ? "MARK " + String(can_log_marks()) : "REC " + String(can_log_marks()));
lv_obj_set_style_bg_color(rec_label, flash ? lv_color_hex(0xFF9500) : lv_color_hex(0xFF3B30), 0);
}
if (lv_tileview_get_tile_active(tileview) != tile_log)
return;
set_label(log_toggle_label, on ? "Stop" : "Start");
set_label(log_status_label, can_log_status() + (on ? " marks " + String(can_log_marks()) : String()));
set_label(log_phase_label, on ? can_log_phase() : String());
static const char *names[CANLOG_SLOTS] = {"165 set", "202 acc", "3D9 sign"};
String frames;
for (int i = 0; i < CANLOG_SLOTS; i++)
{
String f = can_log_frame(i);
frames += String(names[i]) + " " + (f.length() ? f : String("-")) + (i < CANLOG_SLOTS - 1 ? "\n" : "");
}
set_label(log_frames_label, frames);
}
static lv_obj_t *add_bg_item(const String &text, lv_color_t color, int idx)
{
lv_obj_t *btn = lv_list_add_button(bg_list, NULL, text.c_str());
@@ -630,10 +754,14 @@ void setup()
{
delay(200);
Serial.begin(115200);
// internal RAM is tight (draw buffers + BLE); put even small malloc blocks (LVGL objects, Strings) into
// PSRAM so the SD driver keeps enough DMA-capable memory. Explicit heap_caps allocations are not affected.
heap_caps_malloc_extmem_enable(64);
scr_lvgl_init();
setRotation(load_rotation());
build_ui();
obd_set_idle_timeout(load_idle_minutes());
can_log_begin();
obd_begin();
bg_begin();
apply_background();
@@ -657,9 +785,11 @@ void loop()
update_voltage();
update_idle();
update_bg_source();
update_log();
if (lv_tileview_get_tile_active(tileview) == tile_bg)
update_bg_page();
}
can_log_serial_poll();
if (now - last_menu >= 500 && menu_visible())
{
last_menu = now;
+384 -33
View File
@@ -2,6 +2,9 @@
#include <NimBLEDevice.h>
#include <Preferences.h>
#include <algorithm>
#include "can_log.h"
#define RETRY_DELAY_MS 3000
#define IDLE_RETRY_DELAY_MS 30000
@@ -9,6 +12,13 @@
#define VOLTAGE_INTERVAL_MS 2000 // ATRV while driving
#define IDLE_VOLTAGE_INTERVAL_MS 30000 // ATRV while idle (adapter-internal, doesn't wake the car's bus)
#define WAKE_VOLTAGE 13.2f // above this the alternator is charging: engine running
#define LOG_OBD_INTERVAL_MS 2000 // speed while logging (between monitor windows)
#define LOG_SURVEY_MS 1300 // per ID range; > 1 s so slow (1 Hz) messages are seen
#define LOG_SURVEY_MAX_LINES 300 // more in one window: split the range
#define LOG_CAPTURE_MS 1300 // waiting for one frame of an ID
#define LOG_RAW_MS 2000
#define LOG_RESURVEY_MS (10 * 60 * 1000)
#define LOG_TARGET_EVERY 8 // the candidate IDs after every 8 others
// ---------- shared state (protected by `mtx`) ----------
static SemaphoreHandle_t mtx;
@@ -34,6 +44,19 @@ static ObdDevInfo req_dev;
static NimBLEClient *client = nullptr;
static NimBLERemoteCharacteristic *chr_write = nullptr, *chr_notify = nullptr;
static Preferences prefs;
static uint32_t next_attempt = 0, next_voltage = 0;
static int fails = 0;
// CAN logging (see can_log.h), worker-owned
enum class LogMode
{
Off, // not logging (or not set up since the last connect)
Survey, // finding the IDs on the bus, range by range
Capture, // one frame of every known ID in turn
Raw, // adapter can't filter by wildcard: unfiltered monitor windows
NoCan, // not a CAN car: only speed/voltage are logged
};
static LogMode log_mode = LogMode::Off;
static void lock() { xSemaphoreTake(mtx, portMAX_DELAY); }
static void unlock() { xSemaphoreGive(mtx); }
@@ -122,14 +145,20 @@ static void on_notify(NimBLERemoteCharacteristic *c, uint8_t *data, size_t len,
unlock();
}
static bool elm_write(const String &s)
{
if (!chr_write) // dropped meanwhile (adapter not answering)
return false;
return chr_write->writeValue((const uint8_t *)s.c_str(), s.length(), !chr_write->canWriteNoResponse());
}
// Sends a command, waits for the '>' prompt. Returns the reply without prompt/CR, or "" on timeout.
static String elm_command(const char *cmd, uint32_t timeout_ms)
{
lock();
elm_rx = "";
unlock();
String line = String(cmd) + "\r";
if (!chr_write->writeValue((const uint8_t *)line.c_str(), line.length(), !chr_write->canWriteNoResponse()))
if (!elm_write(String(cmd) + "\r"))
return "";
uint32_t t0 = millis();
String rx;
@@ -152,6 +181,51 @@ static String elm_command(const char *cmd, uint32_t timeout_ms)
return "";
}
static String elm_command(const String &cmd, uint32_t timeout_ms) { return elm_command(cmd.c_str(), timeout_ms); }
// Monitor mode (ATMA) with the current CAN filter: collects received lines until `max_lines` arrived or `ms`
// passed, then stops it with a CR. Returns false if the adapter never came back with a prompt.
static bool elm_monitor(uint32_t ms, int max_lines, std::vector<String> &lines)
{
lines.clear();
lock();
elm_rx = "";
unlock();
if (!elm_write("ATMA\r"))
return false;
bool prompt = false, stop_sent = false;
uint32_t t0 = millis(), t_stop = 0;
for (;;)
{
lock();
int cr;
while ((cr = elm_rx.indexOf('\r')) >= 0)
{
String line = elm_rx.substring(0, cr);
elm_rx.remove(0, cr + 1);
line.trim();
if (line.length() && line != "STOPPED")
lines.push_back(line);
}
prompt = elm_rx.indexOf('>') >= 0; // monitoring ended (stopped by us, BUFFER FULL, error)
unlock();
if (prompt)
return true;
if (!client->isConnected())
return false;
if (!stop_sent && (millis() - t0 >= ms || (int)lines.size() >= max_lines))
{
if (!elm_write("\r"))
return false;
stop_sent = true;
t_stop = millis();
}
if (stop_sent && millis() - t_stop > 1500)
return false;
vTaskDelay(pdMS_TO_TICKS(5));
}
}
// "410D3C" (spaces/headers removed by ATS0/ATH0; multiple ECUs possible) -> 60
static int parse_speed(String reply)
{
@@ -187,6 +261,12 @@ static void disconnect()
if (client && client->isConnected())
client->disconnect();
speed_kmh = -1;
if (log_mode != LogMode::Off)
{
can_log_line("E disconnected");
can_log_set_phase("Adapter not connected");
}
log_mode = LogMode::Off; // ATZ on reconnect resets the adapter: set up again
}
// Finds the ELM UART characteristics: service FFF0 (verified: FFF1 notify + write), else any notify+write pair
@@ -271,6 +351,296 @@ static bool connect_and_init()
return true;
}
// ---------- speed polling ----------
// Counts a missing prompt; after a few the adapter is considered hung and the link is dropped.
static void note_no_reply()
{
if (++fails >= POLL_FAIL_LIMIT)
{
set_status("Adapter not answering");
disconnect();
next_attempt = millis() + RETRY_DELAY_MS;
}
}
static void poll_speed()
{
String reply = elm_command("010D", 1500);
int v = parse_speed(reply);
speed_kmh = v;
can_log_line("V " + String(v));
if (v >= 0)
{
fails = 0;
last_data_ms = millis();
set_status(saved_name.length() ? saved_name : saved_addr);
}
else if (reply.length() == 0)
{
// no prompt at all: adapter hung or link dead
note_no_reply();
}
else
{
// "NO DATA", "UNABLE TO CONNECT", "STOPPED"...: ignition off or ECU busy
fails = 0;
String r = reply;
r.replace("\n", " ");
set_status(r.substring(0, 24));
vTaskDelay(pdMS_TO_TICKS(500));
}
}
// ---------- CAN logging ----------
// Ford candidates from opendbc (ford_lincoln_base_pt.dbc, newer Fords - to be confirmed on the Fiesta Mk8):
// 0x165 EngBrakeData: Veh_V_DsplyCcSet (cruise set speed as shown), 0x202: Veh_V_RqCcSet (ACC set speed),
// 0x3D9 IPMA_Data2: IsaVLim_D_Rq (speed limit from the camera)
static const uint16_t LOG_TARGETS[CANLOG_SLOTS] = {0x165, 0x202, 0x3D9};
static std::vector<String> survey_queue; // ID patterns still to listen to ("3XX", "3DX", "3D9")
static std::vector<uint16_t> log_ids; // all IDs seen, sorted
static size_t capture_index = 0;
static uint32_t capture_count = 0;
static uint32_t next_log_obd = 0, next_survey = 0;
static bool exact_filter = false; // adapter supports ATCRA hhh
static int id_digits = 3; // 11-bit IDs; 29-bit in Raw mode
static bool caf_on = true; // CAN auto formatting (ISO-TP), the adapter's default
// Monitoring wants the raw frames (CAF off), OBD requests need CAF on (adds/strips the ISO-TP byte)
static void set_caf(bool on)
{
if (on != caf_on && elm_command(on ? "ATCAF1" : "ATCAF0", 1500).length())
caf_on = on;
}
// "3D9FE00000000000" (ATH1, ATS0) -> id "3D9", data "FE00000000000000"
static bool parse_frame(String line, String &id, String &data)
{
line.replace(" ", "");
if (line.length() < (unsigned)id_digits || (line.length() - id_digits) % 2)
return false;
for (unsigned i = 0; i < line.length(); i++)
if (!isxdigit((unsigned char)line[i]))
return false;
line.toUpperCase();
id = line.substring(0, id_digits);
data = line.substring(id_digits);
return true;
}
static void note_id(uint16_t id)
{
auto it = std::lower_bound(log_ids.begin(), log_ids.end(), id);
if (it == log_ids.end() || *it != id)
log_ids.insert(it, id);
}
// Logs the received lines: frames (at most `per_id` per ID, 0 = all) and adapter messages
static void log_lines(const std::vector<String> &lines, int per_id, bool *overflow)
{
std::vector<std::pair<String, int>> counts;
for (const String &line : lines)
{
String id, data;
if (!parse_frame(line, id, data))
{
if (line.indexOf("BUFFER FULL") >= 0 && overflow)
*overflow = true;
can_log_line("E " + line);
continue;
}
if (id_digits == 3)
note_id((uint16_t)strtol(id.c_str(), nullptr, 16));
int n = 0;
for (auto &c : counts)
if (c.first == id)
n = ++c.second;
if (n == 0)
counts.push_back({id, n = 1});
if (per_id == 0 || n <= per_id)
can_log_line("F " + id + " " + data);
for (int s = 0; s < CANLOG_SLOTS; s++)
if (id_digits == 3 && strtol(id.c_str(), nullptr, 16) == LOG_TARGETS[s])
can_log_set_frame(s, data);
}
}
static String id_hex(uint16_t id)
{
char b[4];
snprintf(b, sizeof(b), "%03X", id);
return b;
}
// Waits for one frame of `id`
static void capture_id(uint16_t id)
{
if (elm_command("ATCRA " + id_hex(id), 1500).length() == 0)
return note_no_reply();
std::vector<String> lines;
if (!elm_monitor(LOG_CAPTURE_MS, 1, lines))
return note_no_reply();
fails = 0;
if (lines.empty())
can_log_line("N " + id_hex(id));
log_lines(lines, 0, nullptr);
}
// Listens to one ID range ("3XX"); a range too busy for the adapter is split into 16 smaller ones
static void survey_range(const String &pattern)
{
if (elm_command("ATCRA " + pattern, 1500).length() == 0)
return note_no_reply();
std::vector<String> lines;
if (!elm_monitor(LOG_SURVEY_MS, LOG_SURVEY_MAX_LINES, lines))
return note_no_reply();
fails = 0;
bool overflow = (int)lines.size() >= LOG_SURVEY_MAX_LINES;
log_lines(lines, 2, &overflow);
int x = pattern.indexOf('X');
if (overflow && x >= 0)
for (int d = 0; d < 16; d++)
{
String child = pattern;
child.setCharAt(x, "0123456789ABCDEF"[d]);
survey_queue.push_back(child);
}
}
static void start_survey()
{
survey_queue.clear();
for (int i = 0; i < 8; i++)
survey_queue.push_back(String(i) + "XX");
log_mode = LogMode::Survey;
}
// Called once per connection while logging is on: checks the protocol and what the adapter can filter
static void log_setup()
{
can_log_line("C " + (saved_name.length() ? saved_name : saved_addr) + " " + elm_command("ATI", 1500));
String dp = elm_command("ATDPN", 1500); // "A6" = auto, ISO 15765-4 CAN 11 bit 500 kbit
can_log_line("P " + dp);
char p = dp.length() ? dp[dp.length() - 1] : '?';
next_log_obd = 0;
caf_on = true; // adapter was just reset (ATZ)
if (p < '6' || p > '9')
{
log_mode = LogMode::NoCan;
can_log_set_phase("Not a CAN protocol (" + dp + ")");
return;
}
// silent monitoring: the adapter never acknowledges or disturbs frames on the bus
can_log_line("E ATCSM1 " + elm_command("ATCSM1", 1500));
elm_command("ATH1", 1500); // IDs in front of the data
id_digits = (p == '7' || p == '9') ? 8 : 3;
exact_filter = id_digits == 3 && elm_command("ATCRA 3D9", 1500).indexOf('?') < 0;
bool wildcard = exact_filter && elm_command("ATCRA 7XX", 1500).indexOf('?') < 0;
elm_command("ATAR", 1500);
if (wildcard)
{
start_survey();
}
else
{
log_mode = LogMode::Raw;
can_log_line(String("E no wildcard filter") + (exact_filter ? "" : ", no ID filter") + ": unfiltered");
}
}
static void log_stop()
{
set_caf(true);
elm_command("ATAR", 1500);
elm_command("ATH0", 1500);
log_mode = LogMode::Off;
can_log_set_phase("");
}
// One unit of logging work (one ID range / one ID / one raw window), with speed + voltage every 2 s
static void log_step()
{
if (log_mode == LogMode::Off)
log_setup();
if ((int32_t)(millis() - next_log_obd) >= 0)
{
next_log_obd = millis() + LOG_OBD_INTERVAL_MS;
elm_command("ATAR", 1500); // the ID filter would hide the ECU's reply
set_caf(true);
poll_speed();
if ((int32_t)(millis() - next_voltage) >= 0)
{
next_voltage = millis() + VOLTAGE_INTERVAL_MS;
read_voltage();
can_log_line("B " + String(voltage, 1));
}
return;
}
if (!client->isConnected() || !chr_write)
return;
if (log_mode != LogMode::NoCan)
set_caf(false);
switch (log_mode)
{
case LogMode::Survey:
if (survey_queue.empty())
{
String ids;
for (uint16_t id : log_ids)
ids += " " + id_hex(id);
can_log_line("I" + ids);
log_mode = LogMode::Capture;
next_survey = millis() + LOG_RESURVEY_MS;
break;
}
{
String pattern = survey_queue.front();
survey_queue.erase(survey_queue.begin());
can_log_set_phase("Survey " + pattern + ": " + log_ids.size() + " IDs, " + survey_queue.size() + " ranges left");
survey_range(pattern);
}
break;
case LogMode::Capture:
if ((int32_t)(millis() - next_survey) >= 0)
{
start_survey(); // now and then look for IDs that were quiet before
break;
}
if (capture_count++ % (LOG_TARGET_EVERY + 1) == 0 || log_ids.empty())
{
for (uint16_t t : LOG_TARGETS)
capture_id(t);
}
else
{
uint16_t id = log_ids[capture_index++ % log_ids.size()];
if (std::find(std::begin(LOG_TARGETS), std::end(LOG_TARGETS), id) == std::end(LOG_TARGETS))
capture_id(id);
}
can_log_set_phase("Capturing " + String(log_ids.size()) + " IDs");
break;
case LogMode::Raw:
{
can_log_set_phase("Unfiltered (adapter can't filter)");
std::vector<String> lines;
if (!elm_monitor(LOG_RAW_MS, LOG_SURVEY_MAX_LINES, lines))
return note_no_reply();
fails = 0;
log_lines(lines, 0, nullptr);
if (exact_filter)
{
for (uint16_t t : LOG_TARGETS)
capture_id(t);
elm_command("ATAR", 1500);
}
break;
}
default: // NoCan: only speed and voltage
vTaskDelay(pdMS_TO_TICKS(100));
break;
}
}
// ---------- worker task ----------
static void load_saved()
{
@@ -283,9 +653,6 @@ static void load_saved()
static void worker_task(void *)
{
uint32_t next_attempt = 0;
uint32_t next_voltage = 0;
int fails = 0;
last_data_ms = millis();
for (;;)
{
@@ -304,6 +671,7 @@ static void worker_task(void *)
idle = true;
speed_kmh = -1;
Serial.println("[obd] idle");
can_log_line("E idle");
}
// requests from the UI
@@ -381,12 +749,22 @@ static void worker_task(void *)
idle = false;
last_data_ms = millis();
Serial.printf("[obd] wake (%.1f V)\n", voltage);
can_log_line("E wake " + String(voltage, 1) + " V");
}
}
vTaskDelay(pdMS_TO_TICKS(100));
continue;
}
// CAN logging on: monitor windows with speed/voltage in between
if (can_log_enabled())
{
log_step();
continue;
}
if (log_mode != LogMode::Off)
log_stop();
if ((int32_t)(millis() - next_voltage) >= 0)
{
next_voltage = millis() + VOLTAGE_INTERVAL_MS;
@@ -394,34 +772,7 @@ static void worker_task(void *)
}
// connected: poll the vehicle speed
String reply = elm_command("010D", 1500);
int v = parse_speed(reply);
speed_kmh = v;
if (v >= 0)
{
fails = 0;
last_data_ms = millis();
set_status(saved_name.length() ? saved_name : saved_addr);
}
else if (reply.length() == 0)
{
// no prompt at all: adapter hung or link dead
if (++fails >= POLL_FAIL_LIMIT)
{
set_status("Adapter not answering");
disconnect();
next_attempt = millis() + RETRY_DELAY_MS;
}
}
else
{
// "NO DATA", "UNABLE TO CONNECT", "STOPPED"...: ignition off or ECU busy
fails = 0;
String r = reply;
r.replace("\n", " ");
set_status(r.substring(0, 24));
vTaskDelay(pdMS_TO_TICKS(500));
}
poll_speed();
}
}
+42
View File
@@ -0,0 +1,42 @@
#include "sd_card.h"
#include <Arduino.h>
#include <SD_MMC.h>
#include "pincfg.h"
static int users = 0;
static SemaphoreHandle_t mutex()
{
static SemaphoreHandle_t m = xSemaphoreCreateMutex();
return m;
}
bool sd_acquire()
{
xSemaphoreTake(mutex(), portMAX_DELAY);
bool ok = true;
if (users == 0)
{
SD_MMC.setPins(SD_MMC_CLK_PIN, SD_MMC_CMD_PIN, SD_MMC_D0_PIN, SD_MMC_D1_PIN, SD_MMC_D2_PIN, SD_MMC_D3_PIN);
ok = SD_MMC.begin() && SD_MMC.cardType() != CARD_NONE;
if (!ok)
SD_MMC.end();
Serial.printf("[sd] mount %s, internal free %u largest %u, dma free %u largest %u\n", ok ? "ok" : "FAILED",
heap_caps_get_free_size(MALLOC_CAP_INTERNAL), heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL),
heap_caps_get_free_size(MALLOC_CAP_DMA), heap_caps_get_largest_free_block(MALLOC_CAP_DMA));
}
if (ok)
users++;
xSemaphoreGive(mutex());
return ok;
}
void sd_release()
{
xSemaphoreTake(mutex(), portMAX_DELAY);
if (users > 0 && --users == 0)
SD_MMC.end();
xSemaphoreGive(mutex());
}
+6
View File
@@ -0,0 +1,6 @@
// Shared SD card mount (SD_MMC, 4-bit), reference-counted so the background store and the CAN logger
// can use the card at the same time. Thread-safe.
#pragma once
bool sd_acquire(); // mounts the card on first use; false if there is no (working) card
void sd_release(); // unmounts when the last user is done