Skip to content

Commit c7b1413

Browse files
committed
Use Stream instead of *Serial/TwoWire
for newer arduinos, - HardwareSerial - NewSoftSerial - TwoWire - Serial_ (USB CDC) are all subclass of the Stream abstract class. and this way we automagically supports all transfer method which is derivered from Stream.
1 parent c4e728f commit c7b1413

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

EasyTransfer/EasyTransfer.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55

66
//Captures address and size of struct
7-
void EasyTransfer::begin(uint8_t * ptr, uint8_t length, HardwareSerial *theSerial){
7+
void EasyTransfer::begin(uint8_t * ptr, uint8_t length, Stream *theStream){
88
address = ptr;
99
size = length;
10-
_serial = theSerial;
10+
_stream = theStream;
1111

1212
//dynamic creation of rx parsing buffer in RAM
1313
rx_buffer = (uint8_t*) malloc(size);
@@ -16,14 +16,14 @@ void EasyTransfer::begin(uint8_t * ptr, uint8_t length, HardwareSerial *theSeria
1616
//Sends out struct in binary, with header, length info and checksum
1717
void EasyTransfer::sendData(){
1818
uint8_t CS = size;
19-
_serial->write(0x06);
20-
_serial->write(0x85);
21-
_serial->write(size);
19+
_stream->write(0x06);
20+
_stream->write(0x85);
21+
_stream->write(size);
2222
for(int i = 0; i<size; i++){
2323
CS^=*(address+i);
24-
_serial->write(*(address+i));
24+
_stream->write(*(address+i));
2525
}
26-
_serial->write(CS);
26+
_stream->write(CS);
2727

2828
}
2929

@@ -32,17 +32,17 @@ boolean EasyTransfer::receiveData(){
3232
//start off by looking for the header bytes. If they were already found in a previous call, skip it.
3333
if(rx_len == 0){
3434
//this size check may be redundant due to the size check below, but for now I'll leave it the way it is.
35-
if(_serial->available() >= 3){
35+
if(_stream->available() >= 3){
3636
//this will block until a 0x06 is found or buffer size becomes less then 3.
37-
while(_serial->read() != 0x06) {
37+
while(_stream->read() != 0x06) {
3838
//This will trash any preamble junk in the serial buffer
3939
//but we need to make sure there is enough in the buffer to process while we trash the rest
4040
//if the buffer becomes too empty, we will escape and try again on the next call
41-
if(_serial->available() < 3)
41+
if(_stream->available() < 3)
4242
return false;
4343
}
44-
if (_serial->read() == 0x85){
45-
rx_len = _serial->read();
44+
if (_stream->read() == 0x85){
45+
rx_len = _stream->read();
4646
//make sure the binary structs on both Arduinos are the same size.
4747
if(rx_len != size){
4848
rx_len = 0;
@@ -54,8 +54,8 @@ boolean EasyTransfer::receiveData(){
5454

5555
//we get here if we already found the header bytes, the struct size matched what we know, and now we are byte aligned.
5656
if(rx_len != 0){
57-
while(_serial->available() && rx_array_inx <= rx_len){
58-
rx_buffer[rx_array_inx++] = _serial->read();
57+
while(_stream->available() && rx_array_inx <= rx_len){
58+
rx_buffer[rx_array_inx++] = _stream->read();
5959
}
6060

6161
if(rx_len == (rx_array_inx-1)){

EasyTransfer/EasyTransfer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ GNU General Public License for more details.
3434
#else
3535
#include "WProgram.h"
3636
#endif
37-
#include "HardwareSerial.h"
37+
#include "Stream.h"
3838
//#include <NewSoftSerial.h>
3939
#include <math.h>
4040
#include <stdio.h>
@@ -43,12 +43,12 @@ GNU General Public License for more details.
4343

4444
class EasyTransfer {
4545
public:
46-
void begin(uint8_t *, uint8_t, HardwareSerial *theSerial);
46+
void begin(uint8_t *, uint8_t, Stream *theStream);
4747
//void begin(uint8_t *, uint8_t, NewSoftSerial *theSerial);
4848
void sendData();
4949
boolean receiveData();
5050
private:
51-
HardwareSerial *_serial;
51+
Stream *_stream;
5252
//NewSoftSerial *_serial;
5353
uint8_t * address; //address of struct
5454
uint8_t size; //size of struct

0 commit comments

Comments
 (0)