Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
|
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).
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.
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 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.
esp_now_info | Information about the transmission, including The MAC addresses |
data | The received packet |
len | The length of the received packet |
rssi | The signal strength of the received packet |
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.
mac_addr | The MAC address which was transmitted to |
status | The transmission status, either ESP_NOW_SEND_SUCCESS or ESP_NOW_SEND_FAIL |
enum wifiMode_t |
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
recvCb | A callback to call when data is sent |
sendCb | A callback to call when data is received |
rx | The receive pin when using serial communication instead of wifi. Use GPIO_NUM_NC for no GPIO |
tx | The transmit pin when using serial communication instead of wifi. Use GPIO_NUM_NC for no GPIO |
uart | The UART to use for serial communication. Use UART_NUM_MAX for no UART |
wifiMode | The WiFi mode. If ESP_NOW_IMMEDIATE, then recvCb is called directly from the interrupt. If ESP_NOW, then recvCb is called from checkEspNowRxQueue() |
void deinitEspNow | ( | void | ) |
This function is called to de-initialize ESP-NOW
esp_err_t espNowUseWireless | ( | void | ) |
Start wifi and use it for communication
void espNowUseSerial | ( | bool | crossoverPins | ) |
Start the UART and use it for communication
crossoverPins | true to crossover the rx and tx pins, false to use them as normal. |
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()
data | The data to broadcast using ESP NOW |
len | The length of the data to broadcast |
void checkEspNowRxQueue | ( | void | ) |
Check the ESP NOW receive queue. If there are any received packets, send them to hostEspNowRecvCb()