File size: 10,279 Bytes
96a5049
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#ifndef WEB_INTERFACE_H
#define WEB_INTERFACE_H

// HTML page for web interface
const char* webPage = R"rawliteral(
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Engine Management System</title>
    <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
    <style>
        body { font-family: Arial; margin: 20px; }
        .container { max-width: 1200px; margin: 0 auto; }
        .card { 
            border: 1px solid #ddd; 
            padding: 15px; 
            margin: 10px 0; 
            border-radius: 5px;
        }
        .value { font-size: 24px; font-weight: bold; }
        .label { font-size: 14px; color: #666; }
        .progress-bar {
            width: 100%;
            height: 20px;
            background: #eee;
            border-radius: 10px;
            overflow: hidden;
        }
        .progress {
            height: 100%;
            background: #4CAF50;
            transition: width 0.3s;
        }
        button {
            padding: 8px 16px;
            margin: 5px;
            border: none;
            border-radius: 4px;
            background: #4CAF50;
            color: white;
            cursor: pointer;
        }
        button:hover {
            background: #45a049;
        }
        input[type="number"] {
            padding: 5px;
            border: 1px solid #ddd;
            border-radius: 4px;
            width: 100px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Engine Management System</h1>
        
        <div class="card">
            <h2>Real-time Parameters</h2>
            <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 20px;">
                <div>
                    <div class="value" id="rpm">0</div>
                    <div class="label">RPM</div>
                </div>
                <div>
                    <div class="value" id="map">0</div>
                    <div class="label">MAP (kPa)</div>
                </div>
                <div>
                    <div class="value" id="tps">0</div>
                    <div class="label">TPS (%)</div>
                </div>
                <div>
                    <div class="value" id="lambda">0</div>
                    <div class="label">Lambda</div>
                </div>
            </div>
        </div>

        <div class="card">
            <h2>Learning System</h2>
            <div>
                <h3>Learning Progress</h3>
                <div class="progress-bar">
                    <div class="progress" id="fuelProgress" style="width: 0%"></div>
                </div>
                <div style="margin-top: 10px;">
                    <button onclick="toggleLearning()" id="learningBtn">Enable Learning</button>
                    <button onclick="resetLearning()">Reset Learning</button>
                </div>
                <div style="margin-top: 20px;">
                    <div>
                        <div class="value" id="fuelCorrection">1.00</div>
                        <div class="label">Fuel Correction Factor</div>
                    </div>
                    <div>
                        <div class="value" id="ignitionCorrection">0.0</div>
                        <div class="label">Ignition Correction (deg)</div>
                    </div>
                </div>
            </div>
        </div>

        <div class="card">
            <h2>Idle Control</h2>
            <div>
                <div>
                    <div class="value" id="idlePosition">0</div>
                    <div class="label">Idle Valve Position (%)</div>
                </div>
                <div>
                    <div class="value" id="targetRPM">800</div>
                    <div class="label">Target RPM</div>
                </div>
                <div style="margin-top: 10px;">
                    <input type="number" id="rpmInput" value="800" min="600" max="2000">
                    <button onclick="setTargetRPM()">Set RPM</button>
                    <button onclick="calibrateIdle()">Calibrate</button>
                </div>
            </div>
        </div>

        <div class="card">
            <h2>Diagnostics</h2>
            <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 20px;">
                <div>
                    <div class="value" id="knockEvents">0</div>
                    <div class="label">Knock Events</div>
                </div>
                <div>
                    <div class="value" id="engineTemp">0</div>
                    <div class="label">Engine Temp (C)</div>
                </div>
                <div>
                    <div class="value" id="batteryVoltage">0</div>
                    <div class="label">Battery Voltage (V)</div>
                </div>
            </div>
        </div>

        <div class="card">
            <h2>Settings</h2>
            <div>
                <button onclick="saveSettings()">Save Settings</button>
                <button onclick="loadSettings()">Load Settings</button>
                <button onclick="exportSettings()">Export Settings</button>
                <button onclick="importSettings()">Import Settings</button>
            </div>
        </div>
    </div>

    <script>
        var rpmData = {
            x: [],
            y: [],
            type: "scatter",
            name: "RPM"
        };

        var mapData = {
            x: [],
            y: [],
            type: "scatter",
            name: "MAP"
        };

        Plotly.newPlot("rpmChart", [rpmData], {
            title: "RPM History",
            xaxis: { title: "Time" },
            yaxis: { title: "RPM" }
        });

        Plotly.newPlot("mapChart", [mapData], {
            title: "MAP History",
            xaxis: { title: "Time" },
            yaxis: { title: "kPa" }
        });

        function updateData() {
            fetch("/status")
                .then(response => response.json())
                .then(data => {
                    document.getElementById("rpm").textContent = Math.round(data.rpm);
                    document.getElementById("map").textContent = data.map.toFixed(1);
                    document.getElementById("tps").textContent = data.tps.toFixed(1);
                    document.getElementById("lambda").textContent = data.lambda.toFixed(2);

                    document.getElementById("fuelProgress").style.width = data.learning_progress + "%";
                    document.getElementById("fuelCorrection").textContent = data.fuel_correction.toFixed(2);
                    document.getElementById("ignitionCorrection").textContent = data.ignition_correction.toFixed(1);

                    document.getElementById("idlePosition").textContent = data.idle_position.toFixed(1);
                    document.getElementById("targetRPM").textContent = data.target_rpm;

                    document.getElementById("knockEvents").textContent = data.knock_events;
                    document.getElementById("engineTemp").textContent = data.temp.toFixed(1);
                    document.getElementById("batteryVoltage").textContent = data.voltage.toFixed(1);

                    const now = new Date();
                    Plotly.extendTraces("rpmChart", {
                        x: [[now]],
                        y: [[data.rpm]]
                    }, [0]);

                    Plotly.extendTraces("mapChart", {
                        x: [[now]],
                        y: [[data.map]]
                    }, [0]);
                });
        }

        function toggleLearning() {
            fetch("/learning/toggle", { method: "POST" })
                .then(response => response.json())
                .then(data => {
                    alert(data.enabled ? "Learning enabled" : "Learning disabled");
                });
        }

        function resetLearning() {
            if (confirm("Are you sure you want to reset learning data?")) {
                fetch("/learning/reset", { method: "POST" })
                    .then(response => response.json())
                    .then(() => {
                        alert("Learning data reset");
                    });
            }
        }

        function setTargetRPM() {
            const rpm = document.getElementById("rpmInput").value;
            fetch("/idle/rpm", {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                },
                body: JSON.stringify({ rpm: parseInt(rpm) })
            });
        }

        function calibrateIdle() {
            if (confirm("Start idle calibration?")) {
                fetch("/idle/calibrate", { method: "POST" })
                    .then(response => response.json())
                    .then(() => {
                        alert("Calibration complete");
                    });
            }
        }

        function saveSettings() {
            fetch("/settings/save", { method: "POST" })
                .then(response => response.json())
                .then(() => {
                    alert("Settings saved");
                });
        }

        function loadSettings() {
            fetch("/settings/load")
                .then(response => response.json())
                .then(() => {
                    alert("Settings loaded");
                });
        }

        function exportSettings() {
            window.location.href = "/settings/export";
        }

        function importSettings() {
            const input = document.createElement("input");
            input.type = "file";
            input.onchange = function(e) {
                const file = e.target.files[0];
                const formData = new FormData();
                formData.append("settings", file);
                fetch("/settings/import", {
                    method: "POST",
                    body: formData
                })
                .then(response => response.json())
                .then(() => {
                    alert("Settings imported");
                });
            };
            input.click();
        }

        setInterval(updateData, 1000);
    </script>
</body>
</html>
)rawliteral";

#endif