Skip to content

Commit e317b1e

Browse files
committed
improve performace for serial comm with serialTask
1 parent e13d20d commit e317b1e

3 files changed

Lines changed: 94 additions & 58 deletions

File tree

src/SerialHandler.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "SerialHandler.h"
2+
#include "Config.h"
3+
#include "DataTypes.h"
4+
#include "Comms.h"
5+
#include "GlobalVariables.h"
6+
#include "Arduino.h"
7+
8+
void setupSerial() {
9+
Serial1.begin(UART_BAUD, SERIAL_8N1, RXD, TXD);
10+
Serial.printf("Serial mode setup complete. Pins: RX=%d, TX=%d, Baud=%d\n", RXD, TXD, UART_BAUD);
11+
}
12+
13+
void serialTask(void *pvParameters) {
14+
Serial.println("Serial communication task started on core 0");
15+
16+
while (1) {
17+
handleSerialCommunication();
18+
vTaskDelay(1); // Allow other tasks to run
19+
}
20+
}
21+
22+
void handleSerialCommunication() {
23+
static uint32_t lastUpdate = millis();
24+
static uint32_t lastRefresh = millis();
25+
26+
// Request data every 15ms for faster response (was 20ms)
27+
if (millis() - lastUpdate > 15) {
28+
requestData(30); // Reduced timeout from 50ms to 30ms
29+
lastUpdate = millis();
30+
}
31+
32+
isCANMode = false; // We're in Serial mode when this function is called
33+
34+
uint32_t currentTime = millis();
35+
uint32_t elapsed = currentTime - lastRefresh;
36+
refreshRate = (elapsed > 0) ? (1000 / elapsed) : 0;
37+
lastRefresh = currentTime;
38+
39+
// Update temperature and voltage data every 150ms (was 200ms)
40+
if (currentTime - lazyUpdateTime > 150 || rpm < 100) {
41+
clt = getByte(7) - 40;
42+
iat = getByte(6) - 40;
43+
bat = getByte(9) * 0.1;
44+
lazyUpdateTime = currentTime;
45+
}
46+
47+
// Read primary engine data
48+
rpm = getWord(14);
49+
mapData = getWord(4) / 10.0;
50+
afrConv = getByte(10) * 0.1;
51+
tps = getByte(24) / 2.0;
52+
adv = (int8_t)getByte(23);
53+
fp = getByte(103);
54+
55+
// Read status bits
56+
syncStatus = getBit(31, 7);
57+
ase = getBit(2, 2);
58+
wue = getBit(2, 3);
59+
rev = getBit(31, 2);
60+
launch = getBit(31, 0);
61+
airCon = getByte(122);
62+
fan = getBit(106, 3);
63+
dfco = getBit(1, 4);
64+
65+
// Debug: Print data values occasionally
66+
static uint32_t lastDataDebug = 0;
67+
if (currentTime - lastDataDebug > 5000) { // Print every 5 seconds
68+
Serial.printf("[SERIAL] RPM: %d, MAP: %.1f, TPS: %.1f, CLT: %d, IAT: %d\n",
69+
rpm, mapData, tps, clt, iat);
70+
Serial.printf("[SERIAL] AFR: %.2f, FP: %d, ADV: %d, RefreshRate: %dHz\n",
71+
afrConv, fp, adv, refreshRate);
72+
lastDataDebug = currentTime;
73+
}
74+
}

src/SerialHandler.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef SERIAL_HANDLER_H
2+
#define SERIAL_HANDLER_H
3+
4+
#include "Arduino.h"
5+
6+
// Function declarations
7+
void setupSerial();
8+
void handleSerialCommunication();
9+
void serialTask(void *pvParameters);
10+
11+
#endif // SERIAL_HANDLER_H

src/main.cpp

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "DataTypes.h"
1010
#include "BacklightControl.h"
1111
#include "CANHandler.h"
12+
#include "SerialHandler.h"
1213
#include "DisplayManager.h"
1314
#include "WebServerHandler.h"
1415
#include "GlobalVariables.h"
@@ -24,57 +25,6 @@
2425
// Web server
2526
WebServer server(80);
2627

27-
// Serial communication handling
28-
void handleSerialCommunication()
29-
{
30-
static uint32_t lastUpdate = millis();
31-
// Reduce serial communication frequency from 10ms to 20ms for better performance
32-
if (millis() - lastUpdate > 20)
33-
{
34-
requestData(50);
35-
lastUpdate = millis();
36-
}
37-
38-
isCANMode = false; // We're in Serial mode when this function is called
39-
40-
static uint32_t lastRefresh = millis();
41-
uint32_t elapsed = millis() - lastRefresh;
42-
refreshRate = (elapsed > 0) ? (1000 / elapsed) : 0;
43-
lastRefresh = millis();
44-
45-
// Reduce lazy update frequency from 100ms to 200ms for better performance
46-
if (lastRefresh - lazyUpdateTime > 200 || rpm < 100)
47-
{
48-
clt = getByte(7) - 40;
49-
iat = getByte(6) - 40;
50-
bat = getByte(9)* 0.1;
51-
}
52-
53-
rpm = getWord(14);
54-
mapData = getWord(4) / 10;
55-
afrConv = getByte(10) *0.1;
56-
tps = getByte(24) / 2;
57-
adv = (int8_t)getByte(23);
58-
fp = getByte(103);
59-
60-
syncStatus = getBit(31, 7);
61-
ase = getBit(2, 2);
62-
wue = getBit(2, 3);
63-
rev = getBit(31, 2);
64-
launch = getBit(31, 0);
65-
airCon = getByte(122);
66-
fan = getBit(106, 3);
67-
dfco = getBit(1, 4);
68-
69-
// Debug: Print data values occasionally
70-
static uint32_t lastDataDebug = 0;
71-
if (millis() - lastDataDebug > 5000) { // Print every 5 seconds
72-
Serial.printf("[SERIAL] RPM: %d, MAP: %.1f, TPS: %.1f, CLT: %d, IAT: %d\n",
73-
rpm, mapData, tps, clt, iat);
74-
lastDataDebug = millis();
75-
}
76-
}
77-
7828
// Handle all serial commands in a single function to avoid conflicts
7929
void handleSerialCommands()
8030
{
@@ -285,14 +235,19 @@ void setup()
285235
// Initialize CAN communication
286236
setupCAN();
287237

288-
// Create CAN task
238+
// Create CAN task on core 0
289239
xTaskCreatePinnedToCore(canTask, "CAN Task", 4096, NULL, 1, NULL, 0);
290240

291241
Serial.println("CAN mode aktif.");
292242
}
293243
else
294244
{
295-
Serial1.begin(UART_BAUD, SERIAL_8N1, RXD, TXD);
245+
// Initialize Serial communication
246+
setupSerial();
247+
248+
// Create Serial task on core 0
249+
xTaskCreatePinnedToCore(serialTask, "Serial Task", 4096, NULL, 1, NULL, 0);
250+
296251
Serial.println("Serial mode aktif.");
297252
}
298253

@@ -371,11 +326,7 @@ void loop()
371326
#if ENABLE_SIMULATOR
372327
if (getSimulatorMode() == SIMULATOR_MODE_OFF) {
373328
#endif
374-
// Handle serial communication if not in CAN mode
375-
if (commMode != COMM_CAN)
376-
{
377-
handleSerialCommunication();
378-
}
329+
379330
#if ENABLE_SIMULATOR
380331
}
381332
#endif

0 commit comments

Comments
 (0)