Skip to content

Commit 3f2ca3d

Browse files
committed
add config for canspeed
1 parent ee6200a commit 3f2ca3d

5 files changed

Lines changed: 98 additions & 4 deletions

File tree

src/CANHandler.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#include "CANHandler.h"
2+
#include "DisplayConfig.h"
23
#include "Config.h"
34
#include "DataTypes.h"
45
#include <esp32_can.h>
56
#include "Arduino.h"
67

78
void setupCAN() {
89
CAN0.setCANPins(GPIO_NUM_17, GPIO_NUM_16); // RX, TX
9-
CAN0.begin(500000); // 500Kbps
10+
CAN0.begin(getCanSpeed()); // Use configurable CAN speed
1011
CAN0.watchFor(0x360); // RPM, MAP, TPS
1112
CAN0.watchFor(0x361); // Fuel Pressure
1213
CAN0.watchFor(0x362); // Ignition Angle (Leading)
@@ -18,7 +19,7 @@ void setupCAN() {
1819
CAN0.watchFor(0x3E4); // Indicator
1920

2021
isCANMode = true; // Set communication mode indicator
21-
Serial.println("CAN mode aktif.");
22+
Serial.printf("CAN mode aktif. Speed: %u bps\n", getCanSpeed());
2223
}
2324

2425
void canTask(void *pvParameters) {

src/DisplayConfig.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ DisplayConfiguration defaultDisplayConfig = {
3131
8, // activePanelCount
3232
8, // activeIndicatorCount
3333
0, // rpmDisplayMode (bar)
34-
true // showSystemIndicators
34+
true, // showSystemIndicators
35+
500000 // canSpeed default 500Kbps
3536
};
3637

3738
DisplayConfiguration currentDisplayConfig;
@@ -167,3 +168,17 @@ uint16_t getDataSourceColor(uint8_t dataSource, float value) {
167168
return TFT_WHITE;
168169
}
169170
}
171+
172+
uint32_t getCanSpeed() {
173+
if (currentDisplayConfig.canSpeed == 500000 || currentDisplayConfig.canSpeed == 1000000) {
174+
return currentDisplayConfig.canSpeed;
175+
} else {
176+
// Jika belum pernah di-set atau nilai tidak valid, pakai default 500000
177+
return 500000;
178+
}
179+
}
180+
181+
void setCanSpeed(uint32_t speed) {
182+
currentDisplayConfig.canSpeed = speed;
183+
saveDisplayConfig();
184+
}

src/DisplayConfig.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ struct DisplayConfiguration {
6868
uint8_t activeIndicatorCount; // Number of active indicators
6969
uint8_t rpmDisplayMode; // RPM display mode (0=bar, 1=digital)
7070
bool showSystemIndicators; // Show CAN/SER, DEBUG, SIM
71+
uint32_t canSpeed; // CAN speed in bps (e.g. 500000, 1000000)
7172
};
7273

7374
// Default configuration
@@ -84,5 +85,8 @@ bool getIndicatorValue(uint8_t indicator);
8485
const char* getDataSourceName(uint8_t dataSource);
8586
const char* getIndicatorName(uint8_t indicator);
8687
uint16_t getDataSourceColor(uint8_t dataSource, float value);
88+
// New CAN speed accessors
89+
uint32_t getCanSpeed();
90+
void setCanSpeed(uint32_t speed);
8791

8892
#endif // DISPLAY_CONFIG_H

src/WebServerHandler.cpp

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,35 @@ const char *uploadPage PROGMEM = R"rawliteral(
315315
});
316316
}
317317
318+
function updateCanSpeed() {
319+
const select = document.getElementById('canSpeedSelect');
320+
const speed = select.value;
321+
fetch('/canspeed', {
322+
method: 'POST',
323+
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
324+
body: 'speed=' + speed
325+
})
326+
.then(response => response.text())
327+
.then(data => {
328+
alert('CAN speed updated: ' + speed + ' bps');
329+
});
330+
}
331+
function loadCanSpeed() {
332+
fetch('/canspeed')
333+
.then(response => response.text())
334+
.then(speed => {
335+
const select = document.getElementById('canSpeedSelect');
336+
if (select) select.value = speed;
337+
});
338+
}
339+
318340
const startTime = Date.now()/1000;
319341
setInterval(refreshStatus, 1000);
320342
321343
// Load display config on page load
322344
window.onload = function() {
323345
loadDisplayConfig();
346+
loadCanSpeed();
324347
};
325348
</script>
326349
</head>
@@ -595,6 +618,21 @@ const char *uploadPage PROGMEM = R"rawliteral(
595618
WiFi will automatically turn off after 1 minute of inactivity to save power.
596619
</p>
597620
</div>
621+
622+
<div class="section">
623+
<h2>CAN Bus Configuration</h2>
624+
<div class="config-item">
625+
<label for="canSpeedSelect">CAN Speed:</label>
626+
<select id="canSpeedSelect" onchange="updateCanSpeed()">
627+
<option value="500000">500 Kbps</option>
628+
<option value="1000000">1 Mbps</option>
629+
</select>
630+
</div>
631+
<p style="font-size: 14px; opacity: 0.8;">
632+
Pilih kecepatan CAN sesuai kebutuhan hardware/ECU Anda.<br>
633+
Perubahan akan disimpan dan digunakan saat restart berikutnya.
634+
</p>
635+
</div>
598636
</div>
599637
</body>
600638
</html>
@@ -678,7 +716,7 @@ void startWebServer()
678716
server.on("/status", HTTP_GET, [&]()
679717
{
680718
String json = "{";
681-
json += "\"commMode\":\"" + String(isCANMode ? "CAN Bus" : "Serial") + "\",";
719+
json += "\"commMode\":\"" + String(commMode == COMM_CAN ? "CAN Bus" : "Serial") + "\",";
682720
json += "\"debugMode\":" + String(debugMode ? "true" : "false") + ",";
683721
#if ENABLE_SIMULATOR
684722
json += "\"simulatorMode\":" + String(getSimulatorMode()) + ",";
@@ -790,10 +828,14 @@ void startWebServer()
790828
server.send(200, "application/json", json);
791829
});
792830

831+
server.on("/canspeed", HTTP_GET, handleCanSpeed);
832+
server.on("/canspeed", HTTP_POST, handleCanSpeed);
833+
793834
server.begin();
794835
wifiActive = true;
795836
Serial.println("Web server aktif.");
796837
Serial.printf("WiFi AP: %s\n", ssid);
838+
797839
Serial.printf("AP IP Address: %s\n", WiFi.softAPIP().toString().c_str());
798840
Serial.printf("Access web server at: http://%s/\n", WiFi.softAPIP().toString().c_str());
799841
esp_wifi_set_max_tx_power(78);
@@ -884,6 +926,29 @@ void handleToggle()
884926
}
885927
}
886928

929+
void handleCanSpeed() {
930+
if (server.method() == HTTP_GET) {
931+
char buf[16];
932+
snprintf(buf, sizeof(buf), "%u", getCanSpeed());
933+
server.send(200, "text/plain", buf);
934+
} else if (server.method() == HTTP_POST) {
935+
if (server.hasArg("speed")) {
936+
uint32_t speed = server.arg("speed").toInt();
937+
if (speed == 500000 || speed == 1000000) {
938+
setCanSpeed(speed);
939+
server.send(200, "text/plain", "OK");
940+
Serial.printf("CAN speed set to %u bps via webserver\n", speed);
941+
} else {
942+
server.send(400, "text/plain", "Invalid speed");
943+
}
944+
} else {
945+
server.send(400, "text/plain", "Missing speed param");
946+
}
947+
} else {
948+
server.send(405, "text/plain", "Method Not Allowed");
949+
}
950+
}
951+
887952
void handleWebServerClients()
888953
{
889954
static uint32_t lastClientCheck = 0;

src/WebServerHandler.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,14 @@ void handleRoot();
1515
void handleUpdate();
1616
void handleToggle();
1717
void handleWebServerClients();
18+
#ifdef __cplusplus
19+
extern "C" {
20+
#endif
21+
22+
void handleCanSpeed();
23+
24+
#ifdef __cplusplus
25+
}
26+
#endif
1827

1928
#endif // WEB_SERVER_HANDLER_H

0 commit comments

Comments
 (0)