File size: 2,382 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
#include <WiFi.h>
#include <WebServer.h>
#include <ArduinoJson.h>
#include "engine_control.h"
#include "idle_control.h"
#include "settings_manager.h"
#include "config.h"

// Создаем объекты
EngineControl engineControl;
IdleController idleControl;
SettingsManager settingsManager;
WebServer server(80);

void handleStatus() {
    StaticJsonDocument<512> doc;
    
    doc["rpm"] = engineControl.getRPM();
    doc["map"] = engineControl.getMAP();
    doc["tps"] = engineControl.getTPS();
    doc["lambda"] = engineControl.getLambda();
    doc["knock_level"] = engineControl.knockLevel;
    doc["knock_events"] = engineControl.getKnockEvents();
    doc["fuel_correction"] = engineControl.getCurrentFuelCorrection();
    doc["ignition_correction"] = engineControl.getCurrentIgnitionCorrection();
    doc["engine_temp"] = engineControl.getEngineTemp();
    doc["voltage"] = engineControl.getVoltage();
    doc["learning_enabled"] = engineControl.isLearningEnabled();
    
    String response;
    serializeJson(doc, response);
    server.send(200, "application/json", response);
}

void handleIdleRPM() {
    if (server.hasArg("rpm")) {
        int rpm = server.arg("rpm").toInt();
        if (rpm >= 700 && rpm <= 2000) {
            idleControl.setTargetRPM(rpm);
            server.send(200, "text/plain", "OK");
            return;
        }
    }
    server.send(400, "text/plain", "Invalid RPM value");
}

void setup() {
    Serial.begin(115200);
    
    // Устанавливаем связь между объектами
    idleControl.setEngineControl(&engineControl);
    
    // Инициализация компонентов
    engineControl.begin();
    idleControl.begin();
    
    // Настройка WiFi
    WiFi.softAP("ECU_Config", "12345678");
    
    // Настройка веб-сервера
    server.on("/status", HTTP_GET, handleStatus);
    server.on("/idle_rpm", HTTP_POST, handleIdleRPM);
    
    server.begin();
}

void loop() {
    // Обработка веб-запросов
    server.handleClient();
    
    // Обновление состояния двигателя
    engineControl.update();
    
    // Обновление РХХ
    if (engineControl.getRPM() > 0) {
        idleControl.update(engineControl.getRPM());
    }
    
    // Задержка для стабильной работы
    delay(10);
}