/** * @file * Header file for the zh_rf24 component. * */ #pragma once #include "string.h" #include "esp_err.h" #include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" #include "esp_log.h" #include "esp_heap_caps.h" #include "driver/spi_master.h" #include "driver/gpio.h" /** * @brief Unique identifier of RF24 interface events base. Used when registering the event handler. * */ #define ESP_EVENT_BASE ZH_RF24 /** * @brief Default values for zh_rf24_init_config_t structure for initial initialization of RF24 interface. * */ #define ZH_RF24_INIT_CONFIG_DEFAULT() \ { \ .ce_pin = 1, \ .csn_pin = 2, \ .sck_pin = 3, \ .mosi_pin = 4, \ .miso_pin = 5, \ .irq_pin = 6, \ .work_mode = ZH_RF24_RECEIVER, \ .channel = 100, \ .pa_level = ZH_RF24_PA_MAX, \ .data_rate = ZH_RF24_250KBPS, \ .tx_address = {0xE7, 0xE7, 0xE7, 0xE7, 0xE7}, \ .rx_address_preamble = {0xC2, 0xC2, 0xC2, 0xC2}, \ .rx_pipe_1_address = 0xC2, \ .rx_pipe_2_address = 0xC3, \ .rx_pipe_3_address = 0xC4, \ .rx_pipe_4_address = 0xC5, \ .rx_pipe_5_address = 0xC6, \ .task_priority = 4, \ .stack_size = 2048, \ .queue_size = 16 \ } #ifdef __cplusplus extern "C" { #endif /** * @brief Structure for initial initialization of RF24 interface. * * @note Before initialize cc interface recommend initialize zh_rf24_init_config_t structure with default values. * * @code * zh_rf24_init_config_t config = ZH_RF24_INIT_CONFIG_DEFAULT() * @endcode */ typedef struct { uint8_t ce_pin; uint8_t csn_pin; uint8_t sck_pin; uint8_t mosi_pin; uint8_t miso_pin; uint8_t irq_pin; zh_rf24_work_mode_t work_mode; uint8_t channel; zh_rf24_pa_level_t pa_level; zh_rf24_data_rate_t data_rate; uint8_t tx_address[5]; uint8_t rx_address_preamble[4]; uint8_t rx_pipe_1_address; uint8_t rx_pipe_2_address; uint8_t rx_pipe_3_address; uint8_t rx_pipe_4_address; uint8_t rx_pipe_5_address; uint8_t task_priority; ///< Task priority for the ESP-NOW messages processing. @note It is not recommended to set a value less than 4. uint16_t stack_size; ///< Stack size for task for the ESP-NOW messages processing. @note The minimum size is 3072 bytes. uint8_t queue_size; ///< Queue size for task for the ESP-NOW messages processing. @note The size depends on the number of messages to be processed. It is not recommended to set the value less than 32. } __attribute__((packed)) zh_rf24_init_config_t; /// \cond ESP_EVENT_DECLARE_BASE(ESP_EVENT_BASE); /// \endcond /** * Power Amplifier level. * * For use with setPALevel() */ typedef enum { ZH_RF24_PA_MIN, ZH_RF24_PA_LOW, ZH_RF24_PA_HIGH, ZH_RF24_PA_MAX, } __attribute__((packed)) zh_rf24_pa_level_t; /** * Data rate. How fast data moves through the air. * * For use with setDataRate() */ typedef enum { ZH_RF24_1MBPS, ZH_RF24_2MBPS, ZH_RF24_250KBPS } __attribute__((packed)) zh_rf24_data_rate_t; /** * CRC Length. How big (if any) of a CRC is included. * * For use with setCRCLength() */ typedef enum { ZH_RF24_TRANSMITTER, ZH_RF24_RECEIVER } __attribute__((packed)) zh_rf24_work_mode_t; /** * @brief Enumeration of possible RF24 events. * */ typedef enum { ZH_RF24_ON_RECV_EVENT, ///< The event when the RF24 message was received. ZH_RF24_ON_SEND_EVENT ///< The event when the RF24 message was sent. } __attribute__((packed)) zh_rf24_event_type_t; /** * @brief Enumeration of possible status of sent ESP-NOW message. * */ typedef enum { ZH_RF24_SEND_SUCCESS, ///< If RF24 message was sent success. ZH_RF24_SEND_FAIL ///< If RF24 message was sent fail. } __attribute__((packed)) zh_rf24_on_send_event_type_t; /** * @brief Structure for sending data to the event handler when an ESP-NOW message was sent. * * @note Should be used with ZH_NETWORK event base and ZH_NETWORK_ON_SEND_EVENT event. * */ typedef struct { zh_rf24_on_send_event_type_t status; ///< Status of sent RF24 message. @note } __attribute__((packed)) zh_rf24_event_on_send_t; /** * @brief Structure for sending data to the event handler when an ESP-NOW message was received. * * @note Should be used with ZH_NETWORK event base and ZH_NETWORK_ON_RECV_EVENT event. */ typedef struct { uint8_t pipe; ///< MAC address of the sender ESP-NOW message. @note uint8_t *data; ///< Pointer to the data of the received ESP-NOW message. @note uint8_t data_len; ///< Size of the received ESP-NOW message. @note } __attribute__((packed)) zh_rf24_event_on_recv_t; /** * @brief Initialize ESP-NOW interface. * * @note Before initialize ESP-NOW interface recommend initialize zh_network_init_config_t structure with default values. * * @code * zh_network_init_config_t config = ZH_NETWORK_INIT_CONFIG_DEFAULT() * @endcode * * @param[in] config Pointer to ESP-NOW initialized configuration structure. Can point to a temporary variable. * * @return * - ESP_OK if initialization was success * - ESP_ERR_INVALID_ARG if parameter error * - ESP_ERR_WIFI_NOT_INIT if WiFi is not initialized * - ESP_FAIL if any internal error */ esp_err_t zh_rf24_init(zh_rf24_init_config_t *config); /** * @brief Deinitialize ESP-NOW interface. * * @return * - ESP_OK if deinitialization was success */ esp_err_t zh_rf24_deinit(void); /** * @brief Send ESP-NOW data. * * @param[in] target Pointer to a buffer containing an eight-byte target MAC. Can be NULL for broadcast. * @param[in] data Pointer to a buffer containing the data for send. * @param[in] data_len Sending data length. * * @note The function will return an ESP_ERR_INVALID_STATE error if less than 50% of the size set at initialization remains in the message queue. * * @return * - ESP_OK if sent was success * - ESP_ERR_INVALID_ARG if parameter error * - ESP_ERR_INVALID_STATE if queue for outgoing data is almost full * - ESP_FAIL if ESP-NOW is not initialized */ esp_err_t zh_rf24_send(const uint8_t *data, const uint8_t data_len, const bool confirm); #ifdef __cplusplus } #endif