SwadgePass is a feature where Swadges can transmit and receive small packets of data while idle. This feature was inspired by Nintendo's StreetPass. SwadgePass data may include things like avatar data or high scores from all Swadge Modes.
SwadgePass uses ESP-NOW, not WiFi or Bluetooth. See hdw-esp-now.h.
Transmission and Reception
Packets are transmitted with sendSwadgePass(). To preserve battery life, this should only be called when the Swadge is idling in it's LED animation mode (dance.c). In this state, the Swadge will transmit every 7.5s, +/- 1.875s. Randomness ensures two Swadges don't get locked out-of-sync while transmitting and receiving. After transmitting, the Swadge stays awake and listening for 700ms. While not transmitting or listening, the Swadge light sleeps for 100ms increments, updating the LEDs ten times per second.
The receiver is initialized with initSwadgePassReceiver() and packets are received with receiveSwadgePass(). Any incoming packet may be passed to this function, including from modes which are using ESP-NOW for other purposes, as long as the receiver was initialized. deinitSwadgePassReceiver() frees memory allocated for the receiver.
During normal operation, if not explicitly used the WiFi radio is turned off and SwadgePass data is neither transmitted nor received.
Usage
Transmission and reception of SwadgePass data is automatic in dance.c and does not need to be managed by each SwadgeMode (see Transmission and Reception). Other modes may transmit and receive SwadgePass data, but it is discouraged for battery reasons.
Swadge Modes may check received SwadgePass data by giving an empty list_t to getSwadgePasses() to fill. getSwadgePasses() may fill the list with all received SwadgePass data, or only SwadgePass data which has not been used by the given mode yet. After being filled, the list contain pointers to swadgePassData_t and may be iterated through. swadgePassData_t.key is the string representation of the source MAC address. SwadgePass data will not change during Swadge Modes (aside from dance.c), so the data should be loaded once and saved for the mode's lifetime. getSwadgePasses() should not be called repeatedly.
If the Swadge Mode wants to use each SwadgePass data from a source only once, it can be checked with isPacketUsedByMode(). This may be useful for an RPG game where each received SwadgePass gets to make one action. Once the data is used, it can be marked as such with setPacketUsedByMode(). This will save the used state to non-volatile storage, which persists reboots.
When the Swadge Mode is finished with the SwadgePass data, it must be freed with freeSwadgePasses().
Receive an ESP NOW packet and save it if it is a SwadgePass packet.
This limits the number of received SwadgePasses to MAX_NUM_SWADGE_PASSES. If a SwadgePass is received while at capacity, the most used SwadgePass will be deleted first, as determined by the most number of bits set in swadgePassNvs_t.usedModeMask.
If a SwadgePass is received from a Swadge for which there already is data, the old data will be overwritten if it's different and the swadgePassNvs_t.usedModeMask will always be cleared.
Parameters
esp_now_info
Metadata for the packet, including src and dst MAC addresses