Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
Loading...
Searching...
No Matches
hdw-esp-now.c File Reference

Data Structures

struct  espNowPacket_t
 A packet received over ESP-NOW. More...
 

Macros

#define ESPNOW_CHANNEL   11
 The WiFi channel to operate on.
 
#define WIFI_PHY   WIFI_PHY_MODE_HT20
 
#define WIFI_RATE   WIFI_PHY_RATE_MCS6_SGI
 The WiFi rate to run at, MCS6 with short GI, 65 Mbps for 20MHz, 135 Mbps for 40MHz.
 
#define FRAMING_START_1   251
 The first random byte used as a 'start' byte for packets transmitted over UART.
 
#define FRAMING_START_2   63
 The second random byte used as a 'start' byte for packets transmitted over UART.
 
#define FRAMING_START_3   114
 The third random byte used as a 'start' byte for packets transmitted over UART.
 
#define ESP_NOW_SERIAL_RX_BUF_SIZE   256
 The size of the UART receive buffer in bytes.
 
#define ESP_NOW_SERIAL_RINGBUF_SIZE   512
 The size of the ringbuffer used to decode packets in bytes.
 

Enumerations

enum  decodeState_t {
  EU_PARSING_FR_START_1 , EU_PARSING_FR_START_2 , EU_PARSING_FR_START_3 , EU_PARSING_MAC ,
  EU_PARSING_LEN , EU_PARSING_PAYLOAD
}
 

Functions

esp_err_t initEspNow (hostEspNowRecvCb_t recvCb, hostEspNowSendCb_t sendCb, gpio_num_t rx, gpio_num_t tx, uart_port_t uart, wifiMode_t wifiMode)
 
esp_err_t espNowUseWireless (void)
 
void espNowUseSerial (bool crossoverPins)
 
void checkEspNowRxQueue (void)
 
void espNowSend (const char *data, uint8_t len)
 
void deinitEspNow (void)
 

Data Structure Documentation

◆ espNowPacket_t

struct espNowPacket_t
Data Fields
int8_t rssi The received signal strength indicator for the packet.
uint8_t mac[6] The MAC address of the sender.
uint8_t len The length of the received bytes.
uint8_t data[255] The received bytes.

Macro Definition Documentation

◆ ESPNOW_CHANNEL

#define ESPNOW_CHANNEL   11

The WiFi channel to operate on.

◆ WIFI_PHY

#define WIFI_PHY   WIFI_PHY_MODE_HT20

◆ WIFI_RATE

#define WIFI_RATE   WIFI_PHY_RATE_MCS6_SGI

The WiFi rate to run at, MCS6 with short GI, 65 Mbps for 20MHz, 135 Mbps for 40MHz.

◆ FRAMING_START_1

#define FRAMING_START_1   251

The first random byte used as a 'start' byte for packets transmitted over UART.

◆ FRAMING_START_2

#define FRAMING_START_2   63

The second random byte used as a 'start' byte for packets transmitted over UART.

◆ FRAMING_START_3

#define FRAMING_START_3   114

The third random byte used as a 'start' byte for packets transmitted over UART.

◆ ESP_NOW_SERIAL_RX_BUF_SIZE

#define ESP_NOW_SERIAL_RX_BUF_SIZE   256

The size of the UART receive buffer in bytes.

◆ ESP_NOW_SERIAL_RINGBUF_SIZE

#define ESP_NOW_SERIAL_RINGBUF_SIZE   512

The size of the ringbuffer used to decode packets in bytes.

Enumeration Type Documentation

◆ decodeState_t

Enumerator
EU_PARSING_FR_START_1 

Parsing state for the first framing byte.

EU_PARSING_FR_START_2 

Parsing state for the second framing byte.

EU_PARSING_FR_START_3 

Parsing state for the third framing byte.

EU_PARSING_MAC 

Parsing state for the six MAC bytes.

EU_PARSING_LEN 

Parsing state for the one length byte.

EU_PARSING_PAYLOAD 

Parsing state for the payload bytes.

Function Documentation

◆ initEspNow()

esp_err_t initEspNow ( hostEspNowRecvCb_t recvCb,
hostEspNowSendCb_t sendCb,
gpio_num_t rx,
gpio_num_t tx,
uart_port_t uart,
wifiMode_t wifiMode )

Initialize ESP-NOW and attach callback functions. This uses wifi by default, but espNowUseSerial() may be called later to communicate over the given UART instead

Parameters
recvCbA callback to call when data is sent
sendCbA callback to call when data is received
rxThe receive pin when using serial communication instead of wifi. Use GPIO_NUM_NC for no GPIO
txThe transmit pin when using serial communication instead of wifi. Use GPIO_NUM_NC for no GPIO
uartThe UART to use for serial communication. Use UART_NUM_MAX for no UART
wifiModeThe WiFi mode. If ESP_NOW_IMMEDIATE, then recvCb is called directly from the interrupt. If ESP_NOW, then recvCb is called from checkEspNowRxQueue()
Returns
The esp_err_t that occurred

◆ espNowUseWireless()

esp_err_t espNowUseWireless ( void )

Start wifi and use it for communication

Returns
ESP_OK or an error that occurred

◆ espNowUseSerial()

void espNowUseSerial ( bool crossoverPins)

Start the UART and use it for communication

Parameters
crossoverPinstrue to crossover the rx and tx pins, false to use them as normal.

◆ checkEspNowRxQueue()

void checkEspNowRxQueue ( void )

Check the ESP NOW receive queue. If there are any received packets, send them to hostEspNowRecvCb()

◆ espNowSend()

void espNowSend ( const char * data,
uint8_t len )

This is a wrapper for esp_now_send(). It also sets the wifi power with wifi_set_user_fixed_rate()

Parameters
dataThe data to broadcast using ESP NOW
lenThe length of the data to broadcast

◆ deinitEspNow()

void deinitEspNow ( void )

This function is called to de-initialize ESP-NOW