mirror of
				https://github.com/alexandrebobkov/ESP-Nodes.git
				synced 2025-11-04 10:59:56 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			102 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			C
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			102 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			C
		
	
	
		
			Executable File
		
	
	
	
	
/* ESPNOW Example
 | 
						|
 | 
						|
   This example code is in the Public Domain (or CC0 licensed, at your option.)
 | 
						|
 | 
						|
   Unless required by applicable law or agreed to in writing, this
 | 
						|
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 | 
						|
   CONDITIONS OF ANY KIND, either express or implied.
 | 
						|
*/
 | 
						|
 | 
						|
#ifndef ESPNOW_EXAMPLE_H
 | 
						|
#define ESPNOW_EXAMPLE_H
 | 
						|
 | 
						|
/* ESPNOW can work in both station and softap mode. It is configured in menuconfig. */
 | 
						|
#if CONFIG_ESPNOW_WIFI_MODE_STATION
 | 
						|
#define ESPNOW_WIFI_MODE WIFI_MODE_STA
 | 
						|
#define ESPNOW_WIFI_IF   ESP_IF_WIFI_STA
 | 
						|
#else
 | 
						|
#define ESPNOW_WIFI_MODE WIFI_MODE_AP
 | 
						|
#define ESPNOW_WIFI_IF   ESP_IF_WIFI_AP
 | 
						|
#endif
 | 
						|
 | 
						|
#define ESPNOW_QUEUE_SIZE           6
 | 
						|
 | 
						|
#define IS_BROADCAST_ADDR(addr) (memcmp(addr, s_example_broadcast_mac, ESP_NOW_ETH_ALEN) == 0)
 | 
						|
 | 
						|
typedef enum {
 | 
						|
    EXAMPLE_ESPNOW_SEND_CB,
 | 
						|
    EXAMPLE_ESPNOW_RECV_CB,
 | 
						|
} example_espnow_event_id_t;
 | 
						|
 | 
						|
typedef struct {
 | 
						|
    uint8_t mac_addr[ESP_NOW_ETH_ALEN];
 | 
						|
    esp_now_send_status_t status;
 | 
						|
} example_espnow_event_send_cb_t;
 | 
						|
 | 
						|
typedef struct {
 | 
						|
    uint8_t mac_addr[ESP_NOW_ETH_ALEN];
 | 
						|
    uint8_t *data;
 | 
						|
    int data_len;
 | 
						|
} example_espnow_event_recv_cb_t;
 | 
						|
 | 
						|
typedef union {
 | 
						|
    example_espnow_event_send_cb_t send_cb;
 | 
						|
    example_espnow_event_recv_cb_t recv_cb;
 | 
						|
} example_espnow_event_info_t;
 | 
						|
 | 
						|
/* When ESPNOW sending or receiving callback function is called, post event to ESPNOW task. */
 | 
						|
typedef struct {
 | 
						|
    example_espnow_event_id_t id;
 | 
						|
    example_espnow_event_info_t info;
 | 
						|
} example_espnow_event_t;
 | 
						|
 | 
						|
enum {
 | 
						|
    EXAMPLE_ESPNOW_DATA_BROADCAST,
 | 
						|
    EXAMPLE_ESPNOW_DATA_UNICAST,
 | 
						|
    EXAMPLE_ESPNOW_DATA_MAX,
 | 
						|
};
 | 
						|
 | 
						|
/* 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 payload[2];                   // Real payload of ESPNOW data.
 | 
						|
} __attribute__((packed)) example_espnow_data_t;
 | 
						|
 | 
						|
/*typedef struct {
 | 
						|
    uint8_t     type;                       // Broadcast or unicast ESPNOW data.s
 | 
						|
    uint16_t    seq_num;                     // Sequence number of ESPNOW data.
 | 
						|
    uint16_t    crc;                         // CRC16 value of ESPNOW data.
 | 
						|
    uint8_t     x_axis;
 | 
						|
    uint8_t     y_axis;
 | 
						|
    bool        nav_bttn;
 | 
						|
    uint8_t     motor1_rpm_pcm;
 | 
						|
    uint8_t     motor2_rpm_pcm;
 | 
						|
    uint8_t     motor3_rpm_pcm;
 | 
						|
    uint8_t     motor4_rpm_pcm;
 | 
						|
} __attribute__((packed)) sensors_data_t;
 | 
						|
 | 
						|
typedef struct {
 | 
						|
    int len;                                // Length of ESPNOW data to be sent, unit: byte.
 | 
						|
    uint8_t     *buffer;                      // Buffer; pointer to the data struct.
 | 
						|
    uint8_t     dest_mac[ESP_NOW_ETH_ALEN]; // MAC address of destination device.
 | 
						|
} espnow_data_packet_t;*/
 | 
						|
 | 
						|
/* Parameters of sending ESPNOW data. */
 | 
						|
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.
 | 
						|
} example_espnow_send_param_t;
 | 
						|
 | 
						|
#endif
 |