1+ #include " espnowcom.h"
2+ #include < esp_now.h>
3+ #include < esp_wifi.h>
4+ #include < WiFi.h>
5+ #include " faces.h"
6+ #include " oled_menu.h"
7+ #include < U8g2lib.h>
8+
9+ // Callback for receiving ESP-NOW data
10+ volatile bool faceUpdateRequested = false ;
11+ volatile char requestedCommand = 0 ;
12+
13+ void onDataReceived (const uint8_t * mac, const uint8_t * data, int len) {
14+ if (len > 0 ) {
15+ requestedCommand = static_cast <char >(data[0 ]);
16+ faceUpdateRequested = true ;
17+ } else {
18+ Serial.println (" Received empty data packet." );
19+ }
20+ }
21+
22+ void processFaceUpdate () {
23+ switch (requestedCommand) {
24+ case ' A' : currentFaceBitmap = Basic_face; currentFaceName = " Basic Face" ; break ;
25+ case ' B' : currentFaceBitmap = Calm_face; currentFaceName = " Calm" ; break ;
26+ case ' C' : currentFaceBitmap = engy_face; currentFaceName = " Engy" ; break ;
27+ case ' D' : currentFaceBitmap = Huh_face; currentFaceName = " Huh" ; break ;
28+ case ' E' : currentFaceBitmap = Love_face; currentFaceName = " Love" ; break ;
29+ case ' F' : currentFaceBitmap = Rly_face; currentFaceName = " Rly" ; break ;
30+ case ' G' : currentFaceBitmap = shy_face; currentFaceName = " Shy" ; break ;
31+ default : Serial.println (" Unknown command." ); return ;
32+ }
33+
34+ updateOLED (currentFaceBitmap, currentFaceName);
35+ }
36+
37+ // Initialize ESP-NOW
38+ void initESPNow () {
39+ WiFi.mode (WIFI_STA);
40+ if (esp_now_init () != ESP_OK) {
41+ Serial.println (" Error initializing ESP-NOW" );
42+ updateOLED (nullptr , " Unable to connect with ProtoPaw" );
43+ return ;
44+ }
45+
46+ Serial.println (" ESP-NOW initialized" );
47+ uint8_t mac[6 ];
48+ esp_wifi_get_mac (WIFI_IF_STA, mac);
49+ Serial.printf (" MAC Address: %02X:%02X:%02X:%02X:%02X:%02X\n " ,
50+ mac[0 ], mac[1 ], mac[2 ], mac[3 ], mac[4 ], mac[5 ]);
51+ updateOLED (nullptr , " ESP-NOW Ready" );
52+
53+ // Register the callback function for data reception
54+ esp_now_register_recv_cb (onDataReceived);
55+ }
56+ void loop () {
57+ if (faceUpdateRequested) {
58+ faceUpdateRequested = false ; // Reset the flag
59+ processFaceUpdate ();
60+ }
61+ }
0 commit comments