Skip to content

Commit

Permalink
some memory improvements on the receivers
Browse files Browse the repository at this point in the history
  • Loading branch information
hpsaturn committed Jan 28, 2024
1 parent d7188c2 commit 32fbedc
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
14 changes: 8 additions & 6 deletions examples/core2-espnow-receiver/core2-espnow-receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ int32_t dw, dh;
uint8_t recv_buffer[256];
/// frame buffer
uint8_t *fb;
bool is_new_frame = true;

Frame msg = Frame_init_zero;

Expand All @@ -37,7 +38,7 @@ bool decodeMessage(uint16_t message_length) {
pb_istream_t stream = pb_istream_from_buffer(recv_buffer, message_length);
msg.data.funcs.decode = &decode_data;
// msg.data.arg = (void*) "array: \"%d\"\r\n";
bool status = pb_decode_delimited(&stream, Frame_fields, &msg);
bool status = pb_decode(&stream, Frame_fields, &msg);
if (!status) {
Serial.printf("Decoding msg failed: %s\r\n", PB_GET_ERROR(&stream));
return false;
Expand Down Expand Up @@ -65,16 +66,21 @@ void printFB(uint32_t len){

void msgReceiveCb(const uint8_t *macAddr, const uint8_t *data, int dataLen) {
int msgLen = min(ESP_NOW_MAX_DATA_LEN, dataLen);
// BE CAREFUL WITH IT, IF JPG LEVEL CHANGES, INCREASE IT
if (is_new_frame) fb = (uint8_t*) malloc(4000* sizeof( uint8_t ) ) ;
memcpy(recv_buffer, data, msgLen);
if (decodeMessage(msgLen)) {
is_new_frame = false;
// Serial.println();
if (msg.lenght > 0 ) {
// printFB(msg.lenght);
// Serial.printf("\r\nfb size: %i msg lenght: %i cksum: %u\r\n",fbpos,msg.lenght,cksum);
M5.Display.drawJpg(fb, msg.lenght , 0, 0, dw, dh);
// printFPS("ESPNow reception at:");
free(fb);
is_new_frame = true;
fbpos = 0;
// cksum = 0;
printFPS("ESPNow reception at:");
}
// Serial.printf("chunk len: %d\r\n",msg.lenght);
}
Expand Down Expand Up @@ -127,10 +133,6 @@ void setup() {
size_t psram_size = esp_spiram_get_size() / 1048576;
Serial.printf("PSRAM size: %dMb\r\n", psram_size);
}

// BE CAREFUL WITH IT, IF JPG LEVEL CHANGES, INCREASE IT
fb = (uint8_t*) malloc(3500* sizeof( uint8_t ) ) ;

delay(1000);
}

Expand Down
4 changes: 2 additions & 2 deletions examples/freenove-espnow/freenove-espnow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void printFPS(const char *msg) {

size_t encodeMsg(Frame msg) {
pb_ostream_t stream = pb_ostream_from_buffer(send_buffer, sizeof(send_buffer));
bool status = pb_encode_delimited(&stream, Frame_fields, &msg);
bool status = pb_encode(&stream, Frame_fields, &msg);
size_t message_length = stream.bytes_written;
if (!status) {
printf("Encoding failed: %s\r\n", PB_GET_ERROR(&stream));
Expand Down Expand Up @@ -162,7 +162,7 @@ void printJPGFrame(){

void processFrame() {
if (Camera.get()) {
frame2jpg(Camera.fb, 8, &out_jpg, &out_jpg_len);
frame2jpg(Camera.fb, 9, &out_jpg, &out_jpg_len);
// Display.pushImage(0, 0, dw, dh, (uint16_t *)CoreS3.Camera.fb->buf);
// printJPGFrame();
// Serial.println();
Expand Down
12 changes: 7 additions & 5 deletions examples/m5cores3-espnow-receiver/m5cores3-espnow-receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ int32_t dw, dh;
uint8_t recv_buffer[256];
/// frame buffer
uint8_t *fb;
bool is_new_frame = true;

Frame msg = Frame_init_zero;

Expand Down Expand Up @@ -64,16 +65,21 @@ void printFB(uint32_t len){

void msgReceiveCb(const uint8_t *macAddr, const uint8_t *data, int dataLen) {
int msgLen = min(ESP_NOW_MAX_DATA_LEN, dataLen);
// BE CAREFUL WITH IT, IF JPG LEVEL CHANGES, INCREASE IT
if (is_new_frame) fb = (uint8_t*) malloc(4000* sizeof( uint8_t ) ) ;
memcpy(recv_buffer, data, msgLen);
if (decodeMessage(msgLen)) {
is_new_frame = false;
// Serial.println();
if (msg.lenght > 0 ) {
// printFB(msg.lenght);
// Serial.printf("\r\nfb size: %i msg lenght: %i cksum: %u\r\n",fbpos,msg.lenght,cksum);
CoreS3.Display.drawJpg(fb, msg.lenght , 0, 0, dw, dh);
// printFPS("ESPNow reception at:");
free(fb);
is_new_frame = true;
fbpos = 0;
// cksum = 0;
printFPS("ESPNow reception at:");
}
// Serial.printf("chunk len: %d\r\n",msg.lenght);
}
Expand Down Expand Up @@ -130,10 +136,6 @@ void setup() {
size_t psram_size = esp_spiram_get_size() / 1048576;
Serial.printf("PSRAM size: %dMb\r\n", psram_size);
}

// BE CAREFUL WITH IT, IF JPG LEVEL CHANGES, INCREASE IT
fb = (uint8_t*) malloc(3500* sizeof( uint8_t ) ) ;

delay(1000);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/m5cores3-espnow/m5cores3-espnow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void printJPGFrame(){

void processFrame() {
if (CoreS3.Camera.get()) {
frame2jpg(CoreS3.Camera.fb, 8, &out_jpg, &out_jpg_len);
frame2jpg(CoreS3.Camera.fb, 6, &out_jpg, &out_jpg_len);
CoreS3.Display.drawJpg(out_jpg, out_jpg_len, 0, 0, dw, dh);
// printJPGFrame();
// Serial.println();
Expand Down
2 changes: 1 addition & 1 deletion lib/common/frame.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import "nanopb.proto";

message Frame {
required uint32 lenght = 1 [(nanopb).int_size = IS_16];
repeated uint32 data = 2 [(nanopb).int_size = IS_8, packed = true];
repeated uint32 data = 2 [(nanopb).int_size = IS_8, (nanopb).max_size = 1];
}

0 comments on commit 32fbedc

Please sign in to comment.