diff --git a/include/ir.h b/include/ir.h index 1b064f1..0cfecb8 100644 --- a/include/ir.h +++ b/include/ir.h @@ -1,7 +1,9 @@ #ifndef IR_H #define IR_H +#include #include +#include "consts.h" enum IRMode { @@ -9,24 +11,20 @@ enum IRMode IR_MODE_UART }; -struct IRConfig -{ - IRMode mode; - uint8_t pins[5]; -}; - struct IRData { - uint8_t length; + IRMode mode; + uint8_t pins[IR_COUNT]; + uint8_t count; uint8_t data[8]; }; class IR { public: - static IRConfig config; + static SoftwareSerial *uart; static IRData data; - static void init(IRConfig config); + static void init(); static void update(); }; diff --git a/platformio.ini b/platformio.ini index 10022df..ff42b80 100644 --- a/platformio.ini +++ b/platformio.ini @@ -13,9 +13,11 @@ platform = espressif32 board = esp32dev framework = arduino monitor_speed = 115200 +lib_deps = plerup/EspSoftwareSerial@^8.2.0 [env:esp32s3] platform = espressif32 board = adafruit_feather_esp32s3_tft framework = arduino monitor_speed = 115200 +lib_deps = plerup/EspSoftwareSerial@^8.2.0 diff --git a/src/ir.cpp b/src/ir.cpp index e3f2ceb..36e0f02 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -1,29 +1,45 @@ #include "ir.h" -IRConfig IR::config; IRData IR::data; +SoftwareSerial *IR::uart; -void IR::init(IRConfig config) +void IR::init() { Serial.println("IR Init"); - IR::config = config; - if (config.mode == IRMode::IR_MODE_GPIO) + data.mode = IR_MODE; + data.count = IR_COUNT; + if (IR_MODE == IRMode::IR_MODE_GPIO) { - data.length = 5; - for (int i = 0; i < 5; i++) + uint8_t pins[IR_COUNT] = IR_PINS; + for (int i = 0; i < IR_COUNT; i++) { - pinMode(config.pins[i], INPUT); - } + data.pins[i] = pins[i]; + }; + } + else if (IR_MODE == IRMode::IR_MODE_UART) + { + uint8_t pins[2] = IR_PINS; + data.pins[0] = pins[0]; + data.pins[1] = pins[1]; + uart = new SoftwareSerial(pins[0], pins[1]); + uart->begin(IR_BAUD); } } void IR::update() { - if (config.mode == IRMode::IR_MODE_GPIO) + if (data.mode == IRMode::IR_MODE_GPIO) { - for (int i = 0; i < 5; i++) + for (int i = 0; i < data.count; i++) { - data.data[i] = digitalRead(config.pins[i]); + data.data[i] = digitalRead(data.pins[i]); + } + } + else if (data.mode == IRMode::IR_MODE_UART) + { + if (uart->available()) + { + data.data[0] = uart->read(); } } } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ab49411..a623a93 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -28,7 +28,7 @@ void setup() {MOTOR_D_PWMB, MOTOR_D_BIN1, MOTOR_D_BIN2}); // 初始化红外循迹模块 - IR::init({IR_MODE, IR_PINS}); + IR::init(); // 初始化状态灯 if (STATUS_LED_ENABLE)