Skip to content

Commit

Permalink
Changed RX buffer to dynamic memory allocation to save RAM
Browse files Browse the repository at this point in the history
Signed-off-by: Bill Porter <madsci1016@gmail.com>
  • Loading branch information
madsci1016 committed Feb 11, 2012
1 parent 3ab8e9a commit c4e728f
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 32 deletions.
17 changes: 10 additions & 7 deletions EasyTransfer/EasyTransfer.cpp
Expand Up @@ -5,9 +5,12 @@

//Captures address and size of struct
void EasyTransfer::begin(uint8_t * ptr, uint8_t length, HardwareSerial *theSerial){
address = ptr;
size = length;
_serial = theSerial;
address = ptr;
size = length;
_serial = theSerial;

//dynamic creation of rx parsing buffer in RAM
rx_buffer = (uint8_t*) malloc(size);
}

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

if(rx_len == (rx_array_inx-1)){
//seem to have got whole message
//last uint8_t is CS
calc_CS = rx_len;
for (int i = 0; i<rx_len; i++){
calc_CS^=rx_array[i];
calc_CS^=rx_buffer[i];
}

if(calc_CS == rx_array[rx_array_inx-1]){//CS good
memcpy(address,&rx_array,size);
if(calc_CS == rx_buffer[rx_array_inx-1]){//CS good
memcpy(address,rx_buffer,size);
rx_len = 0;
rx_array_inx = 0;
return true;
Expand Down
4 changes: 2 additions & 2 deletions EasyTransfer/EasyTransfer.h
Expand Up @@ -52,9 +52,9 @@ HardwareSerial *_serial;
//NewSoftSerial *_serial;
uint8_t * address; //address of struct
uint8_t size; //size of struct
uint8_t rx_len; //RX packet length according to the packet
uint8_t rx_array[255]; //RX packet parsing buffer
uint8_t * rx_buffer; //address for temporary storage and parsing buffer
uint8_t rx_array_inx; //index for RX parsing buffer
uint8_t rx_len; //RX packet length according to the packet
uint8_t calc_CS; //calculated Chacksum
};

Expand Down
19 changes: 11 additions & 8 deletions EasyTransferI2C/EasyTransferI2C.cpp
Expand Up @@ -5,9 +5,12 @@

//Captures address and size of struct
void EasyTransferI2C::begin(uint8_t * ptr, uint8_t length, TwoWire *theSerial){
address = ptr;
size = length;
_serial = theSerial;
address = ptr;
size = length;
_serial = theSerial;

//dynamic creation of rx parsing buffer in RAM
rx_buffer = (uint8_t*) malloc(size);
}

//Sends out struct in binary, with header, length info and checksum
Expand Down Expand Up @@ -77,9 +80,9 @@ boolean EasyTransferI2C::receiveData(){
if(rx_len != 0){
while(_serial->available() && rx_array_inx <= rx_len){
#if ARDUINO >= 100
rx_array[rx_array_inx++] = _serial->read();
rx_buffer[rx_array_inx++] = _serial->read();
#else
rx_array[rx_array_inx++] = _serial->receive();
rx_buffer[rx_array_inx++] = _serial->receive();
#endif
}

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

if(calc_CS == rx_array[rx_array_inx-1]){//CS good
memcpy(address,&rx_array,size);
if(calc_CS == rx_buffer[rx_array_inx-1]){//CS good
memcpy(address,rx_buffer,size);
rx_len = 0;
rx_array_inx = 0;
return true;
Expand Down
4 changes: 2 additions & 2 deletions EasyTransferI2C/EasyTransferI2C.h
Expand Up @@ -54,9 +54,9 @@ TwoWire *_serial;
//NewSoftSerial *_serial;
uint8_t * address; //address of struct
uint8_t size; //size of struct
uint8_t rx_len; //RX packet length according to the packet
uint8_t rx_array[255]; //RX packet parsing buffer
uint8_t * rx_buffer; //address for temporary storage and parsing buffer
uint8_t rx_array_inx; //index for RX parsing buffer
uint8_t rx_len; //RX packet length according to the packet
uint8_t calc_CS; //calculated Chacksum
};

Expand Down
4 changes: 3 additions & 1 deletion README.txt
@@ -1,5 +1,5 @@
/******************************************************************
* EasyTransfer Arduino Library v2.0.1
* EasyTransfer Arduino Library v2.1
* details and example sketch:
* http://www.billporter.info/easytransfer-arduino-library/
*
Expand Down Expand Up @@ -30,6 +30,8 @@
* Added EasyTransferVirtualWire library for use with Virtual Wire and cheap radios.
* 2.0.1
* VirtualWire version tested by garth@netram, bugs fixed.
* 2.1
* Changes RX parsing buffer to dynamic allocation to conserve RAM.
*
*
* Limits of the Library
Expand Down
26 changes: 16 additions & 10 deletions SoftEasyTransfer/SoftEasyTransfer.cpp
Expand Up @@ -6,17 +6,23 @@
#if ARDUINO > 22
//Captures address and size of struct
void SoftEasyTransfer::begin(uint8_t * ptr, uint8_t length, SoftwareSerial *theSerial){
address = ptr;
size = length;
_serial = theSerial;
address = ptr;
size = length;
_serial = theSerial;

//dynamic creation of rx parsing buffer in RAM
rx_buffer = (uint8_t*) malloc(size);
}

#else
//Captures address and size of struct
void SoftEasyTransfer::begin(uint8_t * ptr, uint8_t length, NewSoftSerial *theSerial){
address = ptr;
size = length;
_serial = theSerial;
address = ptr;
size = length;
_serial = theSerial;

//dynamic creation of rx parsing buffer in RAM
rx_buffer = (uint8_t*) malloc(size);
}

#endif
Expand Down Expand Up @@ -80,19 +86,19 @@ boolean SoftEasyTransfer::receiveData(){

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

if(rx_len == (rx_array_inx-1)){
//seem to have got whole message
//last uint8_t is CS
calc_CS = rx_len;
for (int i = 0; i<rx_len; i++){
calc_CS^=rx_array[i];
calc_CS^=rx_buffer[i];
}

if(calc_CS == rx_array[rx_array_inx-1]){//CS good
memcpy(address,&rx_array,size);
if(calc_CS == rx_buffer[rx_array_inx-1]){//CS good
memcpy(address,rx_buffer,size);
rx_len = 0;
rx_array_inx = 0;
return true;
Expand Down
4 changes: 2 additions & 2 deletions SoftEasyTransfer/SoftEasyTransfer.h
Expand Up @@ -66,9 +66,9 @@ NewSoftSerial *_serial;

uint8_t * address; //address of struct
uint8_t size; //size of struct
uint8_t rx_len; //RX packet length according to the packet
uint8_t rx_array[255]; //RX packet parsing buffer
uint8_t * rx_buffer; //address for temporary storage and parsing buffer
uint8_t rx_array_inx; //index for RX parsing buffer
uint8_t rx_len; //RX packet length according to the packet
uint8_t calc_CS; //calculated Chacksum
};

Expand Down

0 comments on commit c4e728f

Please sign in to comment.