i have the following code , what will be the data looking like in dataBuffer: #include…
- User
i have the following code , what will be the data looking like in dataBuffer: #include <Wire.h> #include <esp_now.h> #include <WiFi.h> #include <esp_wifi.h>
#define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 #define BUFFER_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT / 8)
#define I2C_DEV_ADDR 0x3C #define CHANNEL 1 //#define MAX_POSSIBLE_DATA_SIZE 256 // This should be the largest data size you ever expect
uint8_t dataBuffer[BUFFER_SIZE]; unsigned int bufferHead = 0; unsigned int bufferTail = 0; unsigned int currentDataSize = 0; // Current size of data received
//uint8_t dataArray[MAX_POSSIBLE_DATA_SIZE]; //uint8_t currentDataSize = 0; // Current size of data received
esp_now_peer_info_t slave; bool isSlavePaired = false; // Track if slave is currently paired
void onReceive(int len) { currentDataSize = len; for (int i = 0; i < len; i++) { addToBuffer(Wire.read()); } sendData(); }
void addToBuffer(uint8_t data) { bufferHead = (bufferHead + 1) % BUFFER_SIZE; // Wrap around if buffer is full dataBuffer[bufferHead] = data; if (bufferHead == bufferTail) { bufferTail = (bufferTail + 1) % BUFFER_SIZE; // Remove oldest data when buffer is full } }
void sendData() { const uint8_t* peer_addr = slave.peer_addr; esp_err_t result = esp_now_send(peer_addr, dataBuffer + bufferTail, currentDataSize);
if (result == ESP_OK) { Serial.println("Success"); } else { Serial.println("Send Error"); } }
void InitESPNow() { WiFi.disconnect();
codefor(int retries = 0; retries < 3; retries++) { if (esp_now_init() == ESP_OK) { Serial.println("ESPNow Init Success"); break; } else { if(retries == 2) { Serial.println("ESPNow Init Failed 3 times, restarting..."); delay(1000); ESP.restart(); } delay(500); } }}
bool manageSlave() { if (slave.channel == CHANNEL) { bool exists = esp_now_is_peer_exist(slave.peer_addr); if (exists) { Serial.println("Already Paired"); return true; } else { esp_err_t addStatus = esp_now_add_peer(&slave); if (addStatus == ESP_OK) { Serial.println("Pair success"); return true; } else { Serial.println("Pairing Error"); } } } else { Serial.println("No Slave found to process"); } return false; }
void OnDataSent(const uint8_t* mac_addr, esp_now_send_status_t status) { char macStr[18]; snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]); Serial.print("Last Packet Sent to: "); Serial.println(macStr); Serial.print("Last Packet Send Status: "); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); }
- Assistant
The data in
dataBufferwill depend on the data received through the I2C interface and added to the buffer. TheonReceivefunction reads data from the I2C interface usingWire.read()and adds it to the buffer using theaddToBufferfunction.The
bufferHeadandbufferTailvariables control the position of the buffer head and tail, respectively. TheaddToBufferfunction adds data to the buffer by incrementing thebufferHeadand assigning the data todataBuffer[bufferHead]. If the buffer is full (i.e.bufferHead == bufferTail), it removes the oldest data by incrementingbufferTail.Therefore, the
dataBufferwill contain the data received through the I2C interface, with the most recent data at thebufferHeadposition and the oldest data at thebufferTailposition. The data in between represents the received data in chronological order. - User
give me a code snippit showing me that data on serial.print
- Assistant
To print the data in the
dataBufferarray usingSerial.print, you can add the following code snippet inside theloopfunction:cppvoid loop() { // ... // Print the data in the dataBuffer Serial.println("Data in dataBuffer:"); for (int i = 0; i < BUFFER_SIZE; i++) { Serial.print(dataBuffer[i], HEX); Serial.print(" "); if ((i + 1) % 16 == 0) { // Print 16 values per line Serial.println(); } } // ... }This code snippet will iterate through each element of the
dataBufferarray and print its hexadecimal value usingSerial.print. It will print 16 values per line. You can adjust theBUFFER_SIZEand the formatting of the printed data (e.g., usingDECinstead ofHEX) according to your requirements.