Skip to content

Commit 40d3a8f

Browse files
authored
Merge pull request #1 from amrikarisma/bugfix/delay
Bugfix/delay
2 parents 55725ca + fd90fd3 commit 40d3a8f

2 files changed

Lines changed: 119 additions & 99 deletions

File tree

.DS_Store

0 Bytes
Binary file not shown.

src/ESP32_DevkitC_3.5_SPI.ino

Lines changed: 119 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ uint32_t startupTime;
6868
uint32_t lazyUpdateTime;
6969
uint16_t spr_width = 0;
7070

71+
void canTask(void *pvParameters)
72+
{
73+
while (1)
74+
{
75+
handleCANCommunication();
76+
vTaskDelay(1);
77+
}
78+
}
79+
7180
void setup()
7281
{
7382
EEPROM.begin(EEPROM_SIZE);
@@ -84,13 +93,23 @@ void setup()
8493
{
8594

8695
CAN0.setCANPins(GPIO_NUM_17, GPIO_NUM_16); // RX, TX
87-
CAN0.begin(500000); // 500Kbps
88-
CAN0.watchFor(0x360); // RPM, MAP, TPS
89-
// CAN0.watchFor(0x361); // Fuel Pressure
90-
CAN0.watchFor(0x368); // AFR 01
96+
CAN0.begin(1000000); // 1Mbps
97+
CAN0.watchFor(0x360); // RPM, MAP, TPS
98+
CAN0.watchFor(0x361); // Fuel Pressure
99+
CAN0.watchFor(0x368); // AFR 01
91100
// CAN0.watchFor(0x370); // VSS
92101
CAN0.watchFor(0x372); // Voltage
93102
CAN0.watchFor(0x3E0); // CLT, IAT
103+
xTaskCreatePinnedToCore(
104+
canTask,
105+
"CAN Task",
106+
4096,
107+
NULL,
108+
1,
109+
NULL,
110+
0
111+
);
112+
94113
Serial.println("CAN mode aktif.");
95114
}
96115
else
@@ -142,69 +161,70 @@ void setup()
142161

143162
void loop()
144163
{
145-
if (millis() - lastPrintTime >= 1000)
146-
{ // Interval 1000ms
147-
lastPrintTime = millis();
148-
if (commMode == COMM_CAN)
149-
{
150-
Serial.print("CAN mode aktif. ");
151-
}
152-
else
153-
{
154-
Serial.print("Serial mode aktif. ");
155-
}
156-
Serial.printf("RPM: %d, MAP: %d, TPS: %d, VSS: %.2f, CLT: %.2f, IAT: %.2f, FP: %d, AFR: %.2f, Bat: %.2f\n", rpm, mapData, tps, vss, clt, iat, fp, afrConv, bat);
157-
}
158-
if (commMode == COMM_CAN)
159-
{
160-
handleCANCommunication();
161-
}
162-
else
164+
// if (millis() - lastPrintTime >= 1000)
165+
// { // Interval 1000ms
166+
// lastPrintTime = millis();
167+
// if (commMode == COMM_CAN)
168+
// {
169+
// Serial.print("CAN mode aktif. ");
170+
// }
171+
// else
172+
// {
173+
// Serial.print("Serial mode aktif. ");
174+
// }
175+
// Serial.printf("RPM: %d, MAP: %d, TPS: %d, VSS: %.2f, CLT: %.2f, IAT: %.2f, FP: %d, AFR: %.2f, Bat: %.2f\n", rpm, mapData, tps, vss, clt, iat, fp, afrConv, bat);
176+
// }
177+
if (commMode != COMM_CAN)
163178
{
164179
handleSerialCommunication();
165180
}
166181

167-
drawData();
168-
169-
if (millis() - lastClientCheck >= 1000)
170-
{
171-
lastClientCheck = millis();
172-
int clientCount = WiFi.softAPgetStationNum();
173-
174-
if (clientCount > 0)
175-
{
176-
clientConnected = true;
177-
lastClientCheck = millis(); // Reset timer jika ada koneksi
178-
}
179-
else if (millis() - lastClientCheckTimeout > wifiTimeout)
180-
{
181-
clientConnected = false;
182-
lastClientCheckTimeout = millis();
183-
}
184-
185-
// Matikan WiFi jika RPM > 100 atau tidak ada perangkat terkoneksi selama 30 detik
186-
if ((rpm > 100 || !clientConnected) && wifiActive)
187-
{
188-
WiFi.mode(WIFI_OFF);
189-
server.stop();
190-
wifiActive = false;
191-
}
192-
// Nyalakan kembali WiFi jika RPM ≤ 100 dan ada perangkat yang terkoneksi
193-
else if (rpm <= 100 && clientConnected && !wifiActive)
194-
{
195-
WiFi.mode(WIFI_AP);
196-
WiFi.softAPConfig(ip, ip, netmask);
197-
WiFi.softAP(ssid, password);
198-
server.begin();
199-
wifiActive = true;
200-
server.handleClient();
201-
}
182+
static uint32_t lastDraw = 0;
183+
if (millis() - lastDraw > 25)
184+
{ // Update every 100ms
185+
drawData();
186+
lastDraw = millis();
202187
}
203188

204-
if (rpm <= 100 && wifiActive)
205-
{
206-
server.handleClient();
207-
}
189+
// if (millis() - lastClientCheck >= 1000)
190+
// {
191+
// lastClientCheck = millis();
192+
// int clientCount = WiFi.softAPgetStationNum();
193+
194+
// if (clientCount > 0)
195+
// {
196+
// clientConnected = true;
197+
// lastClientCheck = millis(); // Reset timer jika ada koneksi
198+
// }
199+
// else if (millis() - lastClientCheckTimeout > wifiTimeout)
200+
// {
201+
// clientConnected = false;
202+
// lastClientCheckTimeout = millis();
203+
// }
204+
205+
// // Matikan WiFi jika RPM > 100 atau tidak ada perangkat terkoneksi selama 30 detik
206+
// if ((rpm > 100 || !clientConnected) && wifiActive)
207+
// {
208+
// WiFi.mode(WIFI_OFF);
209+
// server.stop();
210+
// wifiActive = false;
211+
// }
212+
// // Nyalakan kembali WiFi jika RPM ≤ 100 dan ada perangkat yang terkoneksi
213+
// else if (rpm <= 100 && clientConnected && !wifiActive)
214+
// {
215+
// WiFi.mode(WIFI_AP);
216+
// WiFi.softAPConfig(ip, ip, netmask);
217+
// WiFi.softAP(ssid, password);
218+
// server.begin();
219+
// wifiActive = true;
220+
// server.handleClient();
221+
// }
222+
// }
223+
224+
// if (rpm <= 100 && wifiActive)
225+
// {
226+
// server.handleClient();
227+
// }
208228
}
209229

210230
void handleCANCommunication()
@@ -219,25 +239,25 @@ void handleCANCommunication()
219239
CAN_FRAME can_message;
220240
if (CAN0.read(can_message))
221241
{
222-
// Serial.print("ID: ");
223-
// Serial.print(can_message.id, HEX);
224-
// Serial.print(" Data: ");
225-
// for (int i = 0; i < can_message.length; i++)
226-
// {
227-
// Serial.print(can_message.data.byte[i], HEX);
228-
// Serial.print(" ");
229-
// }
230-
// Serial.println();
242+
Serial.print("ID: ");
243+
Serial.print(can_message.id, HEX);
244+
Serial.print(" Data: ");
245+
for (int i = 0; i < can_message.length; i++)
246+
{
247+
Serial.print(can_message.data.byte[i], HEX);
248+
Serial.print(" ");
249+
}
250+
Serial.println();
231251

232252
// Proses data berdasarkan ID
233253
switch (can_message.id)
234254
{
235255
case 0x360:
236-
{ // RPM, MAP, TPS
237-
rpm = (can_message.data.byte[0] << 8) | can_message.data.byte[1]; // Byte 0-1
238-
uint16_t map = (can_message.data.byte[2] << 8) | can_message.data.byte[3]; // Byte 2-3
256+
{ // RPM, MAP, TPS
257+
rpm = (can_message.data.byte[0] << 8) | can_message.data.byte[1]; // Byte 0-1
258+
uint16_t map = (can_message.data.byte[2] << 8) | can_message.data.byte[3]; // Byte 2-3
239259
uint16_t tps_raw = (can_message.data.byte[4] << 8) | can_message.data.byte[5]; // Byte 4-5
240-
mapData = map / 10.0; // Konversi ke kPa
260+
mapData = map / 10.0; // Konversi ke kPa
241261
tps = tps_raw / 10.0; // Konversi ke kPa
242262
break;
243263
}
@@ -287,30 +307,30 @@ void handleCANCommunication()
287307
}
288308
}
289309

290-
if (currentTime - lastPrintTime >= 1000)
291-
{ // Interval 1000ms
292-
Serial.print("RPM: ");
293-
Serial.print(rpm);
294-
Serial.print(" MAP: ");
295-
Serial.print(mapData);
296-
Serial.print(" kPa TPS: ");
297-
Serial.print(tps);
298-
Serial.print(" % Fuel Pressure: ");
299-
Serial.print(fp);
300-
Serial.print(" kPa AFR: ");
301-
Serial.print(afrConv, 2);
302-
Serial.print(" VSS: ");
303-
Serial.print(vss);
304-
Serial.print(" km/h Voltage: ");
305-
Serial.print(bat, 2);
306-
Serial.print(" V CLT: ");
307-
Serial.print(clt);
308-
Serial.print(" °C IAT: ");
309-
Serial.print(iat);
310-
Serial.println(" °C");
311-
312-
lastPrintTime = currentTime;
313-
}
310+
// if (currentTime - lastPrintTime >= 1000)
311+
// { // Interval 1000ms
312+
// Serial.print("RPM: ");
313+
// Serial.print(rpm);
314+
// Serial.print(" MAP: ");
315+
// Serial.print(mapData);
316+
// Serial.print(" kPa TPS: ");
317+
// Serial.print(tps);
318+
// Serial.print(" % Fuel Pressure: ");
319+
// Serial.print(fp);
320+
// Serial.print(" kPa AFR: ");
321+
// Serial.print(afrConv, 2);
322+
// Serial.print(" VSS: ");
323+
// Serial.print(vss);
324+
// Serial.print(" km/h Voltage: ");
325+
// Serial.print(bat, 2);
326+
// Serial.print(" V CLT: ");
327+
// Serial.print(clt);
328+
// Serial.print(" °C IAT: ");
329+
// Serial.print(iat);
330+
// Serial.println(" °C");
331+
332+
// lastPrintTime = currentTime;
333+
// }
314334
}
315335

316336
void handleSerialCommunication()
@@ -583,7 +603,7 @@ void drawDataBox(int x, int y, const char *label, const float value, uint16_t la
583603
spr_width = spr.textWidth("333");
584604
spr.setTextColor(labelColor, TFT_BLACK, true);
585605
if (decimal > 0)
586-
{
606+
{
587607
spr.drawFloat(value, decimal, 50, 5);
588608
}
589609
else

0 commit comments

Comments
 (0)