From 62060e254e38d21addf0fd6b4948cb1277618fd7 Mon Sep 17 00:00:00 2001 From: chenxin Date: Tue, 1 Jul 2025 14:31:37 +0800 Subject: [PATCH] auto --- .vscode/settings.json | 5 +++- src/LightControl.cpp | 62 ++++++++++++++++++++++++++++++++++++------- src/LightControl.h | 2 ++ src/TJC_Show.cpp | 33 +++++++++++++++++++---- src/main.cpp | 14 +++++----- 5 files changed, 94 insertions(+), 22 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 286704c..7603bb8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,6 @@ { - "window.zoomLevel": 0 + "window.zoomLevel": 0, + "files.associations": { + "random": "cpp" + } } \ No newline at end of file diff --git a/src/LightControl.cpp b/src/LightControl.cpp index 0c7b6e2..da034aa 100644 --- a/src/LightControl.cpp +++ b/src/LightControl.cpp @@ -14,46 +14,88 @@ float LightControl::getCurrentLight() { return lightSensor.readLightLevel(); } -void LightControl::runUntilTargetReached(float target, int maxAttempts) { +// 新增:持续调节函数,每次只调节一步 +void LightControl::adjustToTarget() { + static unsigned long lastAdjustTime = 0; + const unsigned long adjustInterval = 300; // 每300ms调节一次 + + unsigned long currentTime = millis(); + if (currentTime - lastAdjustTime < adjustInterval) { + return; // 还没到调节时间 + } + const float tolerance = 100.0f; // 容许误差范围(±100 lux) + float currentLight = getCurrentLight(); + float res = targetLightLevel - currentLight; + + if (abs(res) <= tolerance) { + // 已达到目标,但不退出,继续监控 + lastAdjustTime = currentTime; + return; + } + + int lastWiperValue = digitalPot.getWiper(); + + if (res > 0) { + // 需要增加亮度 -> 增加电位器阻值 + if (lastWiperValue < 127) { + lastWiperValue++; + digitalPot.setWiper(lastWiperValue); + Serial.printf("Auto: Current light: %.0f, Target: %.0f, Wiper: %d\n", currentLight, targetLightLevel, lastWiperValue); + } + } else if (res < 0) { + // 需要减少亮度 -> 减少电位器阻值 + if (lastWiperValue > 0) { + lastWiperValue--; + digitalPot.setWiper(lastWiperValue); + Serial.printf("Auto: Current light: %.0f, Target: %.0f, Wiper: %d\n", currentLight, targetLightLevel, lastWiperValue); + } + } + + lastAdjustTime = currentTime; +} + +void LightControl::runUntilTargetReached(float target, int maxAttempts) { + // 保留原有函数,但现在主要用于设置目标值 + setTargetLight(target); + Serial.printf("Target light set to: %.0f\n", target); + + // 可选:立即进行一次快速调节 + const float tolerance = 100.0f; float currentLight; float res; int attempt = 0; - const int max_retries = maxAttempts; // 最大尝试次数,防止死循环 + const int max_retries = min(maxAttempts, 10); // 限制快速调节次数 do { currentLight = getCurrentLight(); res = target - currentLight; if (abs(res) <= tolerance) { - Serial.println("Target reached."); + Serial.println("Initial target reached."); break; } int lastWiperValue = digitalPot.getWiper(); if (res > 0) { - // 需要增加亮度 -> 增加电位器阻值 if (lastWiperValue <= 127) { lastWiperValue++; } } else if (res < 0) { - // 需要减少亮度 -> 减少电位器阻值 if (lastWiperValue > 0) { lastWiperValue--; } } digitalPot.setWiper(lastWiperValue); - Serial.printf("Current light: %.0f, Target: %.0f, Res: %.0f, Wiper: %d\n", currentLight, target, res, lastWiperValue); - delay(200); // 等待响应,必要!! + Serial.printf("Quick adjust: Current light: %.0f, Target: %.0f, Wiper: %d\n", currentLight, target, lastWiperValue); + delay(200); attempt++; - - // 超出最大尝试次数退出 if (attempt >= max_retries) { - Serial.println("Max attempts reached. Exiting..."); + Serial.println("Quick adjust completed, continuing with auto mode."); break; } diff --git a/src/LightControl.h b/src/LightControl.h index 03bcd2e..fa06fdb 100644 --- a/src/LightControl.h +++ b/src/LightControl.h @@ -19,6 +19,8 @@ public: void runUntilTargetReached(float target, int maxAttempts = 500); + void adjustToTarget(); // 新增:持续调节函数 + private: BH1750& lightSensor; // 引用 BH1750 传感器对象 DS3502& digitalPot; // 引用 DS3502 数字电位器对象 diff --git a/src/TJC_Show.cpp b/src/TJC_Show.cpp index 0c04dcf..fc51c19 100644 --- a/src/TJC_Show.cpp +++ b/src/TJC_Show.cpp @@ -105,11 +105,14 @@ void TJC_Show::showInfo() { sendCommand(str); // === autolight 页面 === + extern bool isAutoAdjustEnabled; snprintf(str, sizeof(str), "autolight.t1.txt=\"照度A:%d\"\xff\xff\xff" - "autolight.t2.txt=\"照度B:%d\"\xff\xff\xff", + "autolight.t2.txt=\"照度B:%d\"\xff\xff\xff" + "autolight.c0.val=%d\xff\xff\xff", sensorData.lightA, - sensorData.lightB + sensorData.lightB, + isAutoAdjustEnabled ? 1 : 0 ); sendCommand(str); @@ -284,6 +287,7 @@ void TJC_Show::processSerial() { LightOpen(); } else if (buffer[2] == 0x00 && buffer[3] == 0x00) { LightClose(); + isAutoAdjustEnabled = false; } else { Serial.printf("Unknown cmd: %02X %02X\n", buffer[2], buffer[3]); } @@ -294,14 +298,33 @@ void TJC_Show::processSerial() { // 自动调节指令,根据帧长度输出 ASCII 字符串 // Serial.print("Received ASCII: "); // Serial.println(asciiStr); - if(totalLength == 7 && buffer[3] == 0) + // if(totalLength == 7 && buffer[3] == 0) + // { + // isAutoAdjustEnabled = false; + // Serial.printf("AutoLightControl is %s\n", isAutoAdjustEnabled ? "on" : "off"); + // } + // else{ + // lightCtrl.runUntilTargetReached(atoi(asciiStr), 150); + // Serial.printf("Set TargetLight %d\n", atoi(asciiStr)); + // } + // break; + // 自动调节指令 new + if(totalLength == 7 && buffer[3] == 0x00) { + // 明确的关闭指令 isAutoAdjustEnabled = false; Serial.printf("AutoLightControl is %s\n", isAutoAdjustEnabled ? "on" : "off"); } else{ - lightCtrl.runUntilTargetReached(atoi(asciiStr), 150); - Serial.printf("Set TargetLight %d\n", atoi(asciiStr)); + // 设置目标并启用自动调节 + int targetLux = atoi(asciiStr); + if (targetLux > 0) { + lightCtrl.setTargetLight(targetLux); + isAutoAdjustEnabled = true; + Serial.printf("AutoLightControl enabled, target: %d lux\n", targetLux); + } else { + Serial.println("Invalid target lux value"); + } } break; diff --git a/src/main.cpp b/src/main.cpp index e4cc526..111a1c2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -264,8 +264,9 @@ void WebServer_Init(void) { if (request->hasParam("target", true)) { int targetLux = request->getParam("target", true)->value().toInt(); if (targetLux > 0) { - lightCtrl.runUntilTargetReached(targetLux, 150); - //lightCtrl.setTargetLight(targetLux); + lightCtrl.setTargetLight(targetLux); + //lightCtrl.runUntilTargetReached(targetLux, 150); + lightCtrl.adjustToTarget(); isAutoAdjustEnabled = true; request->send(200, "text/plain", "Auto adjust started"); } else { @@ -422,10 +423,11 @@ void loop(void) Buzzer(); ButtonControl(); -// if (isAutoAdjustEnabled) { -// lightCtrl.runUntilTargetReached(lightCtrl.getTargetLight(), 100); // 每次最多尝试100次 -// } - } + // 修改:如果启用自动调节,持续进行调节 + if (isAutoAdjustEnabled) { + lightCtrl.adjustToTarget(); // 使用新的持续调节函数 + } +}