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

Detailed Description

Date
Oct 29, 2018
Author
adam

Design Philosophy

ESP-NOW is a kind of connection-less Wi-Fi communication protocol that is defined by Espressif. This component manages ESP-NOW so that you don't have to. It provides a simple wrapper to broadcast a packet, espNowSend(), and passes all received packets through a callback given to initEspNow().

Swadges do not use any ESP-NOW security and do not pair with each other using ESP-NOW. All transmissions are broadcasts and all Swadges will receive all other transmissions. Two Swadges may 'pair' with each other by including the recipient's MAC address in a transmitted packet. This pairing can be done using p2pConnection.c.

This component can also facilitate communication between to Swadges using a Universal Asynchronous Receiver/Transmitter (UART) wired connection. The wired UART is significantly faster and significantly more reliable than the wireless one, but it does require a physical wire.

Swadges have USB-C ports and the wired connection reconfigures the D+ and D- pins within the USB cable to be serial RX and TX instead. This means that while the serial connection is active, USB is non-functional. It also means that one Swadge has to treat D+ as RX and D- as TX, while the other has to do the opposite and treat D+ as TX and D- as RX. If a Swadge mode uses wired connections, it's UI must have an options to assume either role (commonly Swadge A and Swadge B).

Usage

You don't need to call initEspNow(), checkEspNowRxQueue(), or deinitEspNow() . The system does at the appropriate time.

espNowUseWireless() and espNowUseSerial() may be used to switch between wireless connections and wired connections, respectively. espNowUseSerial() is also used to configure the RX and TX pins for UART communication.

To send a packet, call espNowSend(). When the packet transmission finishes, the hostEspNowSendCb_t callback passed to initEspNow() is called with a status of either ESP_NOW_SEND_SUCCESS or ESP_NOW_SEND_FAIL.

When a packet is received, the hostEspNowRecvCb_t callback passed to initEspNow() is called with the received packet.

Example

static void swadgeModeEspNowRecvCb(const uint8_t* mac_addr, const char* data, uint8_t len, int8_t rssi)
{
printf("Received %d bytes\n", len);
}
static void swadgeModeEspNowSendCb(const uint8_t* mac_addr, esp_now_send_status_t status)
{
printf("Sent packet: %d\n", status);
}
...
{
initEspNow(&swadgeModeEspNowRecvCb, &swadgeModeEspNowSendCb,
GPIO_NUM_19, GPIO_NUM_20, UART_NUM_1, ESP_NOW);
while(1)
{
// Check for received packets
// Send a test packet
char testPacket[] = "TEST";
espNowSend(testPacket, ARRAY_SIZE(testPacket));
}
}
void checkEspNowRxQueue(void)
Definition hdw-esp-now.c:417
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)
Definition hdw-esp-now.c:119
void espNowSend(const char *data, uint8_t len)
Definition hdw-esp-now.c:559
@ ESP_NOW
ESP-NOW packets are delivered to Swadge modes from the main loop.
Definition hdw-esp-now.h:101
#define ARRAY_SIZE(arr)
Definition macros.h:66

Go to the source code of this file.

Typedefs

typedef void(* hostEspNowRecvCb_t) (const esp_now_recv_info_t *esp_now_info, const uint8_t *data, uint8_t len, int8_t rssi)
 A function typedef for a callback called when an ESP-NOW packet is received.
 
typedef void(* hostEspNowSendCb_t) (const uint8_t *mac_addr, esp_now_send_status_t status)
 A function typedef for a callback called when an ESP-NOW packet transmission finishes.
 

Enumerations

enum  wifiMode_t { NO_WIFI , ESP_NOW , ESP_NOW_IMMEDIATE }
 The different WiFi modes. More...
 

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)
 
void deinitEspNow (void)
 
esp_err_t espNowUseWireless (void)
 
void espNowUseSerial (bool crossoverPins)
 
void espNowSend (const char *data, uint8_t len)
 
void checkEspNowRxQueue (void)
 

Typedef Documentation

◆ hostEspNowRecvCb_t

typedef void(* hostEspNowRecvCb_t) (const esp_now_recv_info_t *esp_now_info, const uint8_t *data, uint8_t len, int8_t rssi)

A function typedef for a callback called when an ESP-NOW packet is received.

Parameters
esp_now_infoInformation about the transmission, including The MAC addresses
dataThe received packet
lenThe length of the received packet
rssiThe signal strength of the received packet

◆ hostEspNowSendCb_t

typedef void(* hostEspNowSendCb_t) (const uint8_t *mac_addr, esp_now_send_status_t status)

A function typedef for a callback called when an ESP-NOW packet transmission finishes.

Parameters
mac_addrThe MAC address which was transmitted to
statusThe transmission status, either ESP_NOW_SEND_SUCCESS or ESP_NOW_SEND_FAIL

Enumeration Type Documentation

◆ wifiMode_t

enum wifiMode_t

The different WiFi modes.

Enumerator
NO_WIFI 

WiFi is not used at all. This saves power.

ESP_NOW 

ESP-NOW packets are delivered to Swadge modes from the main loop.

ESP_NOW_IMMEDIATE 

ESP-NOW packets are delivered to Swadge modes from the interrupt.

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

◆ deinitEspNow()

void deinitEspNow ( void )

This function is called to de-initialize ESP-NOW

◆ 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.

◆ 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

◆ checkEspNowRxQueue()

void checkEspNowRxQueue ( void )

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