Skip to content

Commit c4e728f

Browse files
committed
Changed RX buffer to dynamic memory allocation to save RAM
Signed-off-by: Bill Porter <madsci1016@gmail.com>
1 parent 3ab8e9a commit c4e728f

File tree

7 files changed

+46
-32
lines changed

7 files changed

+46
-32
lines changed

EasyTransfer/EasyTransfer.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55

66
//Captures address and size of struct
77
void EasyTransfer::begin(uint8_t * ptr, uint8_t length, HardwareSerial *theSerial){
8-
address = ptr;
9-
size = length;
10-
_serial = theSerial;
8+
address = ptr;
9+
size = length;
10+
_serial = theSerial;
11+
12+
//dynamic creation of rx parsing buffer in RAM
13+
rx_buffer = (uint8_t*) malloc(size);
1114
}
1215

1316
//Sends out struct in binary, with header, length info and checksum
@@ -52,19 +55,19 @@ boolean EasyTransfer::receiveData(){
5255
//we get here if we already found the header bytes, the struct size matched what we know, and now we are byte aligned.
5356
if(rx_len != 0){
5457
while(_serial->available() && rx_array_inx <= rx_len){
55-
rx_array[rx_array_inx++] = _serial->read();
58+
rx_buffer[rx_array_inx++] = _serial->read();
5659
}
5760

5861
if(rx_len == (rx_array_inx-1)){
5962
//seem to have got whole message
6063
//last uint8_t is CS
6164
calc_CS = rx_len;
6265
for (int i = 0; i<rx_len; i++){
63-
calc_CS^=rx_array[i];
66+
calc_CS^=rx_buffer[i];
6467
}
6568

66-
if(calc_CS == rx_array[rx_array_inx-1]){//CS good
67-
memcpy(address,&rx_array,size);
69+
if(calc_CS == rx_buffer[rx_array_inx-1]){//CS good
70+
memcpy(address,rx_buffer,size);
6871
rx_len = 0;
6972
rx_array_inx = 0;
7073
return true;

EasyTransfer/EasyTransfer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ HardwareSerial *_serial;
5252
//NewSoftSerial *_serial;
5353
uint8_t * address; //address of struct
5454
uint8_t size; //size of struct
55-
uint8_t rx_len; //RX packet length according to the packet
56-
uint8_t rx_array[255]; //RX packet parsing buffer
55+
uint8_t * rx_buffer; //address for temporary storage and parsing buffer
5756
uint8_t rx_array_inx; //index for RX parsing buffer
57+
uint8_t rx_len; //RX packet length according to the packet
5858
uint8_t calc_CS; //calculated Chacksum
5959
};
6060

EasyTransferI2C/EasyTransferI2C.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55

66
//Captures address and size of struct
77
void EasyTransferI2C::begin(uint8_t * ptr, uint8_t length, TwoWire *theSerial){
8-
address = ptr;
9-
size = length;
10-
_serial = theSerial;
8+
address = ptr;
9+
size = length;
10+
_serial = theSerial;
11+
12+
//dynamic creation of rx parsing buffer in RAM
13+
rx_buffer = (uint8_t*) malloc(size);
1114
}
1215

1316
//Sends out struct in binary, with header, length info and checksum
@@ -77,9 +80,9 @@ boolean EasyTransferI2C::receiveData(){
7780
if(rx_len != 0){
7881
while(_serial->available() && rx_array_inx <= rx_len){
7982
#if ARDUINO >= 100
80-
rx_array[rx_array_inx++] = _serial->read();
83+
rx_buffer[rx_array_inx++] = _serial->read();
8184
#else
82-
rx_array[rx_array_inx++] = _serial->receive();
85+
rx_buffer[rx_array_inx++] = _serial->receive();
8386
#endif
8487
}
8588

@@ -88,11 +91,11 @@ boolean EasyTransferI2C::receiveData(){
8891
//last uint8_t is CS
8992
calc_CS = rx_len;
9093
for (int i = 0; i<rx_len; i++){
91-
calc_CS^=rx_array[i];
94+
calc_CS^=rx_buffer[i];
9295
}
9396

94-
if(calc_CS == rx_array[rx_array_inx-1]){//CS good
95-
memcpy(address,&rx_array,size);
97+
if(calc_CS == rx_buffer[rx_array_inx-1]){//CS good
98+
memcpy(address,rx_buffer,size);
9699
rx_len = 0;
97100
rx_array_inx = 0;
98101
return true;

EasyTransferI2C/EasyTransferI2C.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ TwoWire *_serial;
5454
//NewSoftSerial *_serial;
5555
uint8_t * address; //address of struct
5656
uint8_t size; //size of struct
57-
uint8_t rx_len; //RX packet length according to the packet
58-
uint8_t rx_array[255]; //RX packet parsing buffer
57+
uint8_t * rx_buffer; //address for temporary storage and parsing buffer
5958
uint8_t rx_array_inx; //index for RX parsing buffer
59+
uint8_t rx_len; //RX packet length according to the packet
6060
uint8_t calc_CS; //calculated Chacksum
6161
};
6262

README.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/******************************************************************
2-
* EasyTransfer Arduino Library v2.0.1
2+
* EasyTransfer Arduino Library v2.1
33
* details and example sketch:
44
* http://www.billporter.info/easytransfer-arduino-library/
55
*
@@ -30,6 +30,8 @@
3030
* Added EasyTransferVirtualWire library for use with Virtual Wire and cheap radios.
3131
* 2.0.1
3232
* VirtualWire version tested by garth@netram, bugs fixed.
33+
* 2.1
34+
* Changes RX parsing buffer to dynamic allocation to conserve RAM.
3335
*
3436
*
3537
* Limits of the Library

SoftEasyTransfer/SoftEasyTransfer.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,23 @@
66
#if ARDUINO > 22
77
//Captures address and size of struct
88
void SoftEasyTransfer::begin(uint8_t * ptr, uint8_t length, SoftwareSerial *theSerial){
9-
address = ptr;
10-
size = length;
11-
_serial = theSerial;
9+
address = ptr;
10+
size = length;
11+
_serial = theSerial;
12+
13+
//dynamic creation of rx parsing buffer in RAM
14+
rx_buffer = (uint8_t*) malloc(size);
1215
}
1316

1417
#else
1518
//Captures address and size of struct
1619
void SoftEasyTransfer::begin(uint8_t * ptr, uint8_t length, NewSoftSerial *theSerial){
17-
address = ptr;
18-
size = length;
19-
_serial = theSerial;
20+
address = ptr;
21+
size = length;
22+
_serial = theSerial;
23+
24+
//dynamic creation of rx parsing buffer in RAM
25+
rx_buffer = (uint8_t*) malloc(size);
2026
}
2127

2228
#endif
@@ -80,19 +86,19 @@ boolean SoftEasyTransfer::receiveData(){
8086

8187
if(rx_len != 0){
8288
while(_serial->available() && rx_array_inx <= rx_len){
83-
rx_array[rx_array_inx++] = _serial->read();
89+
rx_buffer[rx_array_inx++] = _serial->read();
8490
}
8591

8692
if(rx_len == (rx_array_inx-1)){
8793
//seem to have got whole message
8894
//last uint8_t is CS
8995
calc_CS = rx_len;
9096
for (int i = 0; i<rx_len; i++){
91-
calc_CS^=rx_array[i];
97+
calc_CS^=rx_buffer[i];
9298
}
9399

94-
if(calc_CS == rx_array[rx_array_inx-1]){//CS good
95-
memcpy(address,&rx_array,size);
100+
if(calc_CS == rx_buffer[rx_array_inx-1]){//CS good
101+
memcpy(address,rx_buffer,size);
96102
rx_len = 0;
97103
rx_array_inx = 0;
98104
return true;

SoftEasyTransfer/SoftEasyTransfer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ NewSoftSerial *_serial;
6666

6767
uint8_t * address; //address of struct
6868
uint8_t size; //size of struct
69-
uint8_t rx_len; //RX packet length according to the packet
70-
uint8_t rx_array[255]; //RX packet parsing buffer
69+
uint8_t * rx_buffer; //address for temporary storage and parsing buffer
7170
uint8_t rx_array_inx; //index for RX parsing buffer
71+
uint8_t rx_len; //RX packet length according to the packet
7272
uint8_t calc_CS; //calculated Chacksum
7373
};
7474

0 commit comments

Comments
 (0)