This commit is contained in:
玖叁 2025-01-01 13:21:24 +08:00
parent 1bdd51db00
commit 80f7f8f234
4 changed files with 37 additions and 21 deletions

View File

@ -1,7 +1,9 @@
#ifndef IR_H #ifndef IR_H
#define IR_H #define IR_H
#include <SoftwareSerial.h>
#include <Arduino.h> #include <Arduino.h>
#include "consts.h"
enum IRMode enum IRMode
{ {
@ -9,24 +11,20 @@ enum IRMode
IR_MODE_UART IR_MODE_UART
}; };
struct IRConfig
{
IRMode mode;
uint8_t pins[5];
};
struct IRData struct IRData
{ {
uint8_t length; IRMode mode;
uint8_t pins[IR_COUNT];
uint8_t count;
uint8_t data[8]; uint8_t data[8];
}; };
class IR class IR
{ {
public: public:
static IRConfig config; static SoftwareSerial *uart;
static IRData data; static IRData data;
static void init(IRConfig config); static void init();
static void update(); static void update();
}; };

View File

@ -13,9 +13,11 @@ platform = espressif32
board = esp32dev board = esp32dev
framework = arduino framework = arduino
monitor_speed = 115200 monitor_speed = 115200
lib_deps = plerup/EspSoftwareSerial@^8.2.0
[env:esp32s3] [env:esp32s3]
platform = espressif32 platform = espressif32
board = adafruit_feather_esp32s3_tft board = adafruit_feather_esp32s3_tft
framework = arduino framework = arduino
monitor_speed = 115200 monitor_speed = 115200
lib_deps = plerup/EspSoftwareSerial@^8.2.0

View File

@ -1,29 +1,45 @@
#include "ir.h" #include "ir.h"
IRConfig IR::config;
IRData IR::data; IRData IR::data;
SoftwareSerial *IR::uart;
void IR::init(IRConfig config) void IR::init()
{ {
Serial.println("IR Init"); Serial.println("IR Init");
IR::config = config; data.mode = IR_MODE;
if (config.mode == IRMode::IR_MODE_GPIO) data.count = IR_COUNT;
if (IR_MODE == IRMode::IR_MODE_GPIO)
{ {
data.length = 5; uint8_t pins[IR_COUNT] = IR_PINS;
for (int i = 0; i < 5; i++) 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() 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();
} }
} }
} }

View File

@ -28,7 +28,7 @@ void setup()
{MOTOR_D_PWMB, MOTOR_D_BIN1, MOTOR_D_BIN2}); {MOTOR_D_PWMB, MOTOR_D_BIN1, MOTOR_D_BIN2});
// 初始化红外循迹模块 // 初始化红外循迹模块
IR::init({IR_MODE, IR_PINS}); IR::init();
// 初始化状态灯 // 初始化状态灯
if (STATUS_LED_ENABLE) if (STATUS_LED_ENABLE)