Files
bolar_2_lcd/main/tca9555.h
2026-11-27 16:24:26 +08:00

97 lines
2.8 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#pragma once
#include <driver/i2c_master.h>
#include <esp_err.h>
#include <cstdint>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/timers.h>
// TCA9555 寄存器地址定义
#define TCA9555_ADDR 0x20U
#define TCA9555_REG_IN0 0x00U // Port0(P00~P07) 输入只读
#define TCA9555_REG_IN1 0x01U // Port1(P10~P17) 输入只读
#define TCA9555_REG_OUT0 0x02U // Port0 输出寄存器
#define TCA9555_REG_OUT1 0x03U // Port1 输出寄存器
#define TCA9555_REG_POL0 0x04U // Port0 极性反转寄存器
#define TCA9555_REG_POL1 0x05U // Port1 极性反转寄存器
#define TCA9555_REG_CFG0 0x06U // Port0 方向配置:bit=1输入 / bit=0输出
#define TCA9555_REG_CFG1 0x07U // Port1 方向配置
// 硬件引脚定义(ESP32-S3 I2C总线)
#define TCA_I2C_NUM I2C_NUM_0
#define TCA_SCL_GPIO GPIO_NUM_1
#define TCA_SDA_GPIO GPIO_NUM_2
// 原理图引脚映射宏
// Port0 P0~P7
#define TCA_P0_LEDA 0U
#define TCA_P0_SC_RESET 1U
#define TCA_P0_ADD_IO 2U
#define TCA_P0_OV_RESET 3U
#define TCA_P0_FLASH 6U
#define TCA_P0_PWDN 7U
// Port1 P10~P17
#define TCA_P1_KEY 0U //输入引脚 P10 Port1 bit0
#define TCA_P1_MCU_VCC 2U
#define TCA_P1_5V_EN 3U
#define TCA_P1_PREP_VCC 5U
#define TCA_P1_IMU_INT 6U
#define TCA_P1_PA_CTL 7U
// 按键参数配置(单位ms)
#define KEY_DEBOUNCE_MS 20 // 消抖时间
#define KEY_DOUBLE_INTERVAL 300 // 双击最大间隔
#define KEY_LONG_PRESS_MS 5000 // 长按5秒阈值
// 按键事件枚举
enum class KeyEvent
{
KEY_NONE,
KEY_CLICK, // 单击
KEY_DOUBLE, // 双击
KEY_LONG_PRESS// 长按5秒
};
class TCA9555
{
private:
TCA9555(){};
i2c_master_bus_handle_t bus_handle = nullptr;
i2c_master_dev_handle_t dev_handle_ = nullptr;
// 底层寄存器读写
esp_err_t write_reg(uint8_t reg_addr, uint8_t data);
public:
static TCA9555& getInstance()
{
static TCA9555 instance;
return instance;
}
//禁止拷贝
TCA9555(const TCA9555&)=delete;
void operator=(const TCA9555&)=delete;
// 初始化I2C总线 + TCA9555设备
esp_err_t init(void);
// 释放I2C资源
esp_err_t deinit(void);
// Port0 位操作 P0~P7
esp_err_t p0_set_bit(uint8_t bit); // 置高
esp_err_t p0_clr_bit(uint8_t bit); // 置低
esp_err_t p0_write_all(uint8_t val); // 整端口输出
// Port1 位操作 P10~P17
esp_err_t p1_set_bit(uint8_t bit);
esp_err_t p1_clr_bit(uint8_t bit);
esp_err_t p1_write_all(uint8_t val);
// 读取输入电平
esp_err_t read_p0_input(uint8_t* val);
esp_err_t read_p1_input(uint8_t* val);
esp_err_t read_reg(uint8_t reg_addr, uint8_t* out_data);
};