Files
ESP-Nodes/ESP-IDF_Robot_RC/main/common.c
2025-01-03 19:32:51 -05:00

38 lines
1.5 KiB
C

#include "freertos/FreeRTOS.h"
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)) 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_encapsulation_t;
/* WiFi should start before using ESPNOW */
static void wifi_init(void)
{
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
ESP_ERROR_CHECK( esp_wifi_set_mode(ESPNOW_WIFI_MODE) );
ESP_ERROR_CHECK( esp_wifi_start());
ESP_ERROR_CHECK( esp_wifi_set_channel(CONFIG_ESPNOW_CHANNEL, WIFI_SECOND_CHAN_NONE));
#if CONFIG_ESPNOW_ENABLE_LONG_RANGE
ESP_ERROR_CHECK( esp_wifi_set_protocol(ESPNOW_WIFI_IF, WIFI_PROTOCOL_11B|WIFI_PROTOCOL_11G|WIFI_PROTOCOL_11N|WIFI_PROTOCOL_LR) );
#endif
}