From cba7320b004250266e1761c2ad87f31a9c23254c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=96=E5=8F=81?= Date: Fri, 27 Dec 2024 15:56:14 +0800 Subject: [PATCH] fix: status led --- include/consts.example.h | 3 +++ include/led.h | 3 +++ platformio.ini | 4 +++- src/ble.cpp | 1 + src/ir.cpp | 1 + src/led.cpp | 18 +++++++++++++++++- src/motor.cpp | 2 ++ src/storage.cpp | 3 +++ src/utils.cpp | 22 ---------------------- 9 files changed, 33 insertions(+), 24 deletions(-) diff --git a/include/consts.example.h b/include/consts.example.h index 48de5e0..5785a5e 100644 --- a/include/consts.example.h +++ b/include/consts.example.h @@ -57,6 +57,9 @@ #define PID_KI 0.01 #define PID_KD 0.001 +// LED闪烁间隔(ms) +#define LED_FLASH_INTERVAL 1000 + // Nordic UART Service UUID #define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" #define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" diff --git a/include/led.h b/include/led.h index d28dd73..9ed3945 100644 --- a/include/led.h +++ b/include/led.h @@ -2,11 +2,14 @@ #define __LED_H__ #include +#include "consts.h" class LED { public: static int ledPin; + static unsigned long lastLedToggle; + static bool ledState; static void init(int pin); static void updateStatusLED(bool status); }; diff --git a/platformio.ini b/platformio.ini index 0e33a20..10022df 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,8 +12,10 @@ platform = espressif32 board = esp32dev framework = arduino +monitor_speed = 115200 [env:esp32s3] platform = espressif32 board = adafruit_feather_esp32s3_tft -framework = arduino \ No newline at end of file +framework = arduino +monitor_speed = 115200 diff --git a/src/ble.cpp b/src/ble.cpp index 42312e0..3c59fc7 100644 --- a/src/ble.cpp +++ b/src/ble.cpp @@ -36,6 +36,7 @@ class BLEManagerCharacteristicCallbacks : public BLECharacteristicCallbacks void BLEManager::init(String deviceName) { + Serial.println("BLE Init, deviceName: " + deviceName); BLEDevice::init(deviceName.c_str()); pServer = BLEDevice::createServer(); pServer->setCallbacks(new BLEManagerServerCallbacks()); diff --git a/src/ir.cpp b/src/ir.cpp index fc2d89b..e3f2ceb 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -5,6 +5,7 @@ IRData IR::data; void IR::init(IRConfig config) { + Serial.println("IR Init"); IR::config = config; if (config.mode == IRMode::IR_MODE_GPIO) { diff --git a/src/led.cpp b/src/led.cpp index a9d9e9a..232adc4 100644 --- a/src/led.cpp +++ b/src/led.cpp @@ -1,9 +1,12 @@ #include "led.h" int LED::ledPin = 0; +unsigned long LED::lastLedToggle = 0; +bool LED::ledState = false; void LED::init(int pin) { + Serial.println("LED Init"); ledPin = pin; pinMode(ledPin, OUTPUT); digitalWrite(ledPin, HIGH); @@ -11,5 +14,18 @@ void LED::init(int pin) void LED::updateStatusLED(bool status) { - digitalWrite(ledPin, status); + if (status) + { + digitalWrite(ledPin, HIGH); + } + else + { + unsigned long currentMillis = millis(); + if (currentMillis - lastLedToggle >= LED_FLASH_INTERVAL) + { + lastLedToggle = currentMillis; + ledState = !ledState; + digitalWrite(ledPin, ledState); + } + } } \ No newline at end of file diff --git a/src/motor.cpp b/src/motor.cpp index 669ecef..08e2e53 100644 --- a/src/motor.cpp +++ b/src/motor.cpp @@ -13,6 +13,8 @@ void MotorController::init( MotorPin cPin, MotorPin dPin) { + Serial.println("Motor Init"); + motorA.pin = aPin; motorB.pin = bPin; motorC.pin = cPin; diff --git a/src/storage.cpp b/src/storage.cpp index fdb06f7..80fbf70 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -10,7 +10,10 @@ void Storage::init() isMounted = false; } else + { + Serial.println("SPIFFS Mount Success"); isMounted = true; + } } unsigned int Storage::getSensitivity() diff --git a/src/utils.cpp b/src/utils.cpp index 40146c8..1a40359 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,9 +1,5 @@ #include "utils.h" -#define LED_FLASH_INTERVAL 1000 // LED闪烁间隔(ms) -unsigned long lastLedToggle = 0; // 上次LED切换状态的时间 -bool ledState = false; // LED当前状态 - void floatToBytes(float val, uint8_t *bytes) { union @@ -32,22 +28,4 @@ float bytesToFloat(uint8_t *bytes) u.bytes[i] = bytes[i]; } return u.f; -} - -void updateStatusLED(bool deviceConnected, int ledPin) -{ - if (deviceConnected) - { - digitalWrite(ledPin, HIGH); - } - else - { - unsigned long currentMillis = millis(); - if (currentMillis - lastLedToggle >= LED_FLASH_INTERVAL) - { - lastLedToggle = currentMillis; - ledState = !ledState; - digitalWrite(ledPin, ledState); - } - } } \ No newline at end of file