- isMaster variable should not be set like this but now it is working - TODO: Rework it in a modular way! - shared.h is not very pretty but works that way - There should be a universal logger with the tag
106 lines
3.1 KiB
C
106 lines
3.1 KiB
C
#ifndef ESPNOW_HANDLER_H
|
|
#define ESPNOW_HANDLER_H
|
|
|
|
#include "shared.h"
|
|
static bool isMaster;
|
|
|
|
#define MAX_PAYLOAD_SIZE 250
|
|
#define ESPNOW_QUEUE_SIZE 6
|
|
#define IS_BROADCAST_ADDR(addr) \
|
|
(memcmp(addr, s_broadcast_mac, ESP_NOW_ETH_ALEN) == 0)
|
|
|
|
typedef enum {
|
|
EXAMPLE_ESPNOW_SEND_CB,
|
|
EXAMPLE_ESPNOW_RECV_CB,
|
|
} espnow_event_id_t;
|
|
|
|
typedef struct {
|
|
uint8_t mac_addr[ESP_NOW_ETH_ALEN];
|
|
esp_now_send_status_t status;
|
|
} espnow_event_send_cb_t;
|
|
|
|
typedef struct {
|
|
uint8_t mac_addr[ESP_NOW_ETH_ALEN];
|
|
uint8_t *data;
|
|
int data_len;
|
|
} espnow_event_recv_cb_t;
|
|
|
|
typedef union {
|
|
espnow_event_send_cb_t send_cb;
|
|
espnow_event_recv_cb_t recv_cb;
|
|
} espnow_event_info_t;
|
|
|
|
typedef struct {
|
|
espnow_event_id_t id;
|
|
espnow_event_info_t info;
|
|
} espnow_event_t;
|
|
|
|
enum {
|
|
EXAMPLE_ESPNOW_DATA_BROADCAST,
|
|
EXAMPLE_ESPNOW_DATA_UNICAST,
|
|
EXAMPLE_ESPNOW_DATA_MAX,
|
|
};
|
|
|
|
enum {
|
|
UNION_STATUS,
|
|
UNION_SENSORDATA,
|
|
};
|
|
|
|
typedef struct {
|
|
bool isMaster;
|
|
} __attribute__((packed)) payloadStatus;
|
|
|
|
typedef struct {
|
|
uint8_t dataPoint;
|
|
} __attribute__((packed)) payloadSensorData;
|
|
|
|
union realPayload {
|
|
payloadStatus status;
|
|
payloadSensorData sensorData;
|
|
};
|
|
|
|
/* User defined field of ESPNOW data in this example. */
|
|
typedef struct {
|
|
uint8_t type; // Broadcast or unicast ESPNOW data.
|
|
uint8_t state; // Indicate that if has received broadcast ESPNOW data or not.
|
|
uint16_t seq_num; // Sequence number of ESPNOW data.
|
|
uint16_t crc; // CRC16 value of ESPNOW data.
|
|
uint32_t magic; // Magic number which is used to determine which device to
|
|
// send unicast ESPNOW data.
|
|
uint8_t unionPage;
|
|
union realPayload realPayload; // Real payload of ESPNOW data.
|
|
} __attribute__((packed)) espnow_data_t;
|
|
|
|
static_assert(sizeof(espnow_data_t) <= MAX_PAYLOAD_SIZE,
|
|
"payloadData struct is too big to be sent in one part, keep it "
|
|
"under 250 Bytes!");
|
|
|
|
typedef struct {
|
|
bool unicast; // Send unicast ESPNOW data.
|
|
bool broadcast; // Send broadcast ESPNOW data.
|
|
uint8_t state; // Indicate that if has received broadcast ESPNOW data or not.
|
|
uint32_t magic; // Magic number which is used to determine which device to
|
|
// send unicast ESPNOW data.
|
|
uint16_t count; // Total count of unicast ESPNOW data to be sent.
|
|
uint16_t delay; // Delay between sending two ESPNOW data, unit: ms.
|
|
int len; // Length of ESPNOW data to be sent, unit: byte.
|
|
uint8_t *buffer; // Buffer pointing to ESPNOW data.
|
|
uint8_t dest_mac[ESP_NOW_ETH_ALEN]; // MAC address of destination device.
|
|
} espnow_send_param_t;
|
|
|
|
void wifi_init(void);
|
|
void espnow_deinit(espnow_send_param_t *send_param);
|
|
void espnow_data_prepare(espnow_send_param_t *send_param);
|
|
void espnow_send_cb(const uint8_t *mac_addr,
|
|
esp_now_send_status_t status);
|
|
void espnow_recv_cb(const esp_now_recv_info_t *recv_info,
|
|
const uint8_t *data, int len);
|
|
int espnow_data_parse(uint8_t *data, uint16_t data_len, uint8_t *state,
|
|
uint16_t *seq, uint32_t *magic);
|
|
esp_err_t espnow_init(void);
|
|
void espnow_task(void *pvParameter);
|
|
|
|
void setIsMaster(bool status);
|
|
|
|
#endif
|