wip:
This commit is contained in:
53
zh_pcf8574.c
53
zh_pcf8574.c
@ -2,11 +2,29 @@
|
||||
|
||||
static const char *TAG = "zh_pcf8574";
|
||||
|
||||
#define ZH_PCF8574_LOGI(msg, ...) ESP_LOGI(TAG, msg, ##__VA_ARGS__)
|
||||
#define ZH_PCF8574_LOGW(msg, ...) ESP_LOGW(TAG, msg, ##__VA_ARGS__)
|
||||
#define ZH_PCF8574_LOGE(msg, ...) ESP_LOGE(TAG, msg, ##__VA_ARGS__)
|
||||
#define ZH_PCF8574_LOGE_ERR(msg, err, ...) ESP_LOGE(TAG, "[%s:%d:%s] " msg, __FILE__, __LINE__, esp_err_to_name(err), ##__VA_ARGS__)
|
||||
|
||||
#define ZH_PCF8574_CHECK(cond, err, msg, ...) \
|
||||
if (!(cond)) \
|
||||
{ \
|
||||
ZH_PCF8574_LOGE_ERR(msg, err); \
|
||||
return err; \
|
||||
}
|
||||
|
||||
static gpio_num_t _interrupt_gpio = GPIO_NUM_MAX;
|
||||
static SemaphoreHandle_t _interrupt_semaphore = {0};
|
||||
|
||||
static zh_vector_t _vector = {0};
|
||||
|
||||
// static esp_err_t _zh_espnow_init_wifi(const zh_espnow_init_config_t *config);
|
||||
// static esp_err_t _zh_espnow_init_resources(const zh_espnow_init_config_t *config);
|
||||
static esp_err_t _zh_pcf857_validate_config(const zh_pcf8574_init_config_t *config);
|
||||
// static esp_err_t _zh_espnow_register_callbacks(bool battery_mode);
|
||||
// static esp_err_t _zh_espnow_create_task(const zh_espnow_init_config_t *config);
|
||||
|
||||
static void _zh_isr_handler(void *arg);
|
||||
static void _zh_isr_processing_task(void *pvParameter);
|
||||
static esp_err_t _zh_read_register(zh_pcf8574_handle_t *handle, uint8_t *reg);
|
||||
@ -16,15 +34,28 @@ ESP_EVENT_DEFINE_BASE(ZH_PCF8574);
|
||||
|
||||
esp_err_t zh_pcf8574_init(const zh_pcf8574_init_config_t *config, zh_pcf8574_handle_t *handle)
|
||||
{
|
||||
ESP_LOGI(TAG, "PCF8574 initialization begin.");
|
||||
if (config == NULL || handle == NULL || config->i2c_address == 0xFF)
|
||||
ZH_PCF8574_LOGI("PCF8574 initialization started.");
|
||||
if (handle != NULL && handle->is_initialized == true)
|
||||
{
|
||||
ESP_LOGE(TAG, "PCF8574 initialization fail. Invalid argument.");
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
ZH_PCF8574_LOGW("PCF8574 initialization failed. PCF8574 is already initialized.");
|
||||
return ESP_OK;
|
||||
}
|
||||
esp_err_t err = _zh_pcf8574_validate_config(config);
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ZH_PCF8574_LOGE("PCF8574 initialization failed. Initial configuration check failed.");
|
||||
return err;
|
||||
}
|
||||
else
|
||||
{
|
||||
ZH_PCF8574_LOGI("PCF8574 initial configuration check completed successfully.");
|
||||
}
|
||||
|
||||
ZH_PCF8574_CHECK((config != NULL || handle != NULL || config->i2c_address != 0xFF), ESP_ERR_INVALID_ARG, "PCF8574 initialization fail. Invalid argument.");
|
||||
handle->i2c_address = config->i2c_address;
|
||||
handle->gpio_work_mode = (config->p7_gpio_work_mode << 7) | (config->p6_gpio_work_mode << 6) | (config->p5_gpio_work_mode << 5) | (config->p4_gpio_work_mode << 4) |
|
||||
(config->p3_gpio_work_mode << 3) | (config->p2_gpio_work_mode << 2) | (config->p1_gpio_work_mode << 1) | (config->p0_gpio_work_mode << 0);
|
||||
handle->gpio_work_mode = (config->p7_gpio_work_mode << 7) | (config->p6_gpio_work_mode << 6) | (config->p5_gpio_work_mode << 5) |
|
||||
(config->p4_gpio_work_mode << 4) | (config->p3_gpio_work_mode << 3) | (config->p2_gpio_work_mode << 2) |
|
||||
(config->p1_gpio_work_mode << 1) | (config->p0_gpio_work_mode << 0);
|
||||
handle->gpio_status = handle->gpio_work_mode;
|
||||
handle->i2c_port = config->i2c_port;
|
||||
#ifndef CONFIG_IDF_TARGET_ESP8266
|
||||
@ -117,6 +148,16 @@ esp_err_t zh_pcf8574_write_gpio(zh_pcf8574_handle_t *handle, zh_pcf8574_gpio_num
|
||||
}
|
||||
}
|
||||
|
||||
static esp_err_t _zh_pcf857_validate_config(const zh_pcf8574_init_config_t *config)
|
||||
{
|
||||
ZH_PCF8574_CHECK(config != NULL, ESP_ERR_INVALID_ARG, "Invalid configuration.");
|
||||
// ZH_ESPNOW_CHECK(config->wifi_channel > 0 && config->wifi_channel < 15, ESP_ERR_INVALID_ARG, "Invalid WiFi channel.");
|
||||
// ZH_ESPNOW_CHECK(config->task_priority >= 5 && config->stack_size >= 2048, ESP_ERR_INVALID_ARG, "Invalid task settings.");
|
||||
// ZH_ESPNOW_CHECK(config->queue_size >= 16, ESP_ERR_INVALID_ARG, "Invalid queue size.");
|
||||
// ZH_ESPNOW_CHECK(config->attempts > 0, ESP_ERR_INVALID_ARG, "Invalid number of attempts.");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void IRAM_ATTR _zh_isr_handler(void *arg)
|
||||
{
|
||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||
|
Reference in New Issue
Block a user