Skip to content

Commit 4f42cc0

Browse files
committed
Merge branch 'develop' of ../Arduino-EasyTransfer into develop
2 parents d2ad0dd + 73dbf0b commit 4f42cc0

File tree

10 files changed

+503
-0
lines changed

10 files changed

+503
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include "EasyTransfer.h"
2+
3+
4+
5+
6+
//Captures address and size of struct
7+
void EasyTransfer::begin(uint8_t * ptr, uint8_t length, Stream *theStream){
8+
address = ptr;
9+
size = length;
10+
_stream = theStream;
11+
12+
//dynamic creation of rx parsing buffer in RAM
13+
rx_buffer = (uint8_t*) malloc(size+1);
14+
}
15+
16+
//Sends out struct in binary, with header, length info and checksum
17+
void EasyTransfer::sendData(){
18+
uint8_t CS = size;
19+
_stream->write(0x06);
20+
_stream->write(0x85);
21+
_stream->write(size);
22+
for(int i = 0; i<size; i++){
23+
CS^=*(address+i);
24+
_stream->write(*(address+i));
25+
}
26+
_stream->write(CS);
27+
28+
}
29+
30+
boolean EasyTransfer::receiveData(){
31+
32+
//start off by looking for the header bytes. If they were already found in a previous call, skip it.
33+
if(rx_len == 0){
34+
//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(_stream->available() >= 3){
36+
//this will block until a 0x06 is found or buffer size becomes less then 3.
37+
while(_stream->read() != 0x06) {
38+
//This will trash any preamble junk in the serial buffer
39+
//but we need to make sure there is enough in the buffer to process while we trash the rest
40+
//if the buffer becomes too empty, we will escape and try again on the next call
41+
if(_stream->available() < 3)
42+
return false;
43+
}
44+
if (_stream->read() == 0x85){
45+
rx_len = _stream->read();
46+
//make sure the binary structs on both Arduinos are the same size.
47+
if(rx_len != size){
48+
rx_len = 0;
49+
return false;
50+
}
51+
}
52+
}
53+
}
54+
55+
//we get here if we already found the header bytes, the struct size matched what we know, and now we are byte aligned.
56+
if(rx_len != 0){
57+
while(_stream->available() && rx_array_inx <= rx_len){
58+
rx_buffer[rx_array_inx++] = _stream->read();
59+
}
60+
61+
if(rx_len == (rx_array_inx-1)){
62+
//seem to have got whole message
63+
//last uint8_t is CS
64+
calc_CS = rx_len;
65+
for (int i = 0; i<rx_len; i++){
66+
calc_CS^=rx_buffer[i];
67+
}
68+
69+
if(calc_CS == rx_buffer[rx_array_inx-1]){//CS good
70+
memcpy(address,rx_buffer,size);
71+
rx_len = 0;
72+
rx_array_inx = 0;
73+
return true;
74+
}
75+
76+
else{
77+
//failed checksum, need to clear this out anyway
78+
rx_len = 0;
79+
rx_array_inx = 0;
80+
return false;
81+
}
82+
83+
}
84+
}
85+
86+
return false;
87+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/******************************************************************
2+
* EasyTransfer Arduino Library
3+
* details and example sketch:
4+
* http://www.billporter.info/easytransfer-arduino-library/
5+
*
6+
* Brought to you by:
7+
* Bill Porter
8+
* www.billporter.info
9+
*
10+
* See Readme for other info and version history
11+
*
12+
*
13+
*This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or(at your option) any later version.
14+
This program is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
GNU General Public License for more details.
18+
<http://www.gnu.org/licenses/>
19+
*
20+
*This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
21+
*To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or
22+
*send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
23+
******************************************************************/
24+
#ifndef EasyTransfer_h
25+
#define EasyTransfer_h
26+
27+
28+
//make it a little prettier on the front end.
29+
#define details(name) (byte*)&name,sizeof(name)
30+
31+
//Not neccessary, but just in case.
32+
#if ARDUINO > 22
33+
#include "Arduino.h"
34+
#else
35+
#include "WProgram.h"
36+
#endif
37+
#include "Stream.h"
38+
//#include <NewSoftSerial.h>
39+
//#include <math.h>
40+
//#include <stdio.h>
41+
//#include <stdint.h>
42+
//#include <avr/io.h>
43+
44+
class EasyTransfer {
45+
public:
46+
void begin(uint8_t *, uint8_t, Stream *theStream);
47+
//void begin(uint8_t *, uint8_t, NewSoftSerial *theSerial);
48+
void sendData();
49+
boolean receiveData();
50+
private:
51+
Stream *_stream;
52+
//NewSoftSerial *_serial;
53+
uint8_t * address; //address of struct
54+
uint8_t size; //size of struct
55+
uint8_t * rx_buffer; //address for temporary storage and parsing buffer
56+
uint8_t rx_array_inx; //index for RX parsing buffer
57+
uint8_t rx_len; //RX packet length according to the packet
58+
uint8_t calc_CS; //calculated Chacksum
59+
};
60+
61+
62+
63+
#endif
43.9 KB
Loading

avr/libraries/EasyTransfer/README.txt

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/******************************************************************
2+
* EasyTransfer Arduino Library v2.1
3+
* details and example sketch:
4+
* http://www.billporter.info/easytransfer-arduino-library/
5+
*
6+
* Brought to you by:
7+
* Bill Porter
8+
* www.billporter.info
9+
*
10+
* Major props to Mathieu Alorent (kumy) for
11+
* I2C version and the pretty pictures.
12+
*
13+
*
14+
* Lib version history
15+
* 1.0 Created
16+
* 1.1 Fixed dumb Copy-paste error in header file
17+
* Added a keyword file
18+
* 1.5 Forked lib into Software and Hardware Serial branches, I don't know a better way
19+
* added passing in of Serial port of different types
20+
* 1.6 Fixed bug where it wasn't clearing out the buffers if the CheckSum failed,
21+
* I'm good at dumb mistakes
22+
* 1.7 Fixed a bug where the receive function could block for too long and never process data correctly
23+
* Organized the examples to be Arduino IDE compatible
24+
* 1.8
25+
* Now Arduino 1.0 compatible!
26+
* 1.81
27+
* Made it more cross compatible. Man, They really made us work for this one.
28+
* 2.0
29+
* Combined SoftEasyTransfer with the other two to make everything one repo
30+
* Added EasyTransferVirtualWire library for use with Virtual Wire and cheap radios.
31+
* 2.0.1
32+
* VirtualWire version tested by garth@netram, bugs fixed.
33+
* 2.1
34+
* Changes RX parsing buffer to dynamic allocation to conserve RAM.
35+
*
36+
*
37+
* Limits of the Library
38+
* You can change the Serial port,
39+
* but the Struct size must not pass 255 bytes
40+
* VirtualWire Version Struct can'e be bigger then 26 bytes
41+
*
42+
* The protcol is as follows:
43+
* Header(0x06,0x85),SizeofPayload,Payload,Checksum
44+
*
45+
*
46+
*This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or(at your option) any later version.
47+
This program is distributed in the hope that it will be useful,
48+
but WITHOUT ANY WARRANTY; without even the implied warranty of
49+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50+
GNU General Public License for more details.
51+
<http://www.gnu.org/licenses/>
52+
*
53+
*This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
54+
*To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or
55+
*send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
56+
******************************************************************/
57+
58+
59+
********************To Install*************************************
60+
61+
To install, unzip and place 'EasyTransfer' folder into your 'C:\Users\{user name}\Documents\Arduino\libraries' folder or '{Arduino IDE path}\hardware\libraries" or {Arduino IDE path}\libraries" directory.
62+
63+
Restart the Arduino IDE, look for the Library under "Sketch" -> "Import Library". You can also try the examples by finding them
64+
under "File" -> "Examples" -> "EasyTransfer".
65+
66+
All uses of the library are in the example sketchs.
67+
68+
69+
*******************************************************************
70+
71+
72+
Library now has two versions, one for regular hardware Serial, one for use with the NewSoftSerial library
73+
making any Arduino pin capable of transfering data back and forth easily.
74+
75+
See the examples to find out how to use the library.
43.8 KB
Loading
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*This is an example of the EasyTransfer Library 2way communications.
2+
3+
The sketch is for the Arduino with a potentiometer attached to analog pin 0.
4+
5+
This other Arduino has the servo attached to pin 9.
6+
Both have a putton attached to pin 12 and output a status using the LED on pin 13.
7+
8+
The idea is each arduino will read the status of the button attached to it, and send it
9+
to the other Arduino, which will toggle it's LED based on the others button. The button
10+
should connect pin 12 to ground when pushed.
11+
12+
And the Arduino with the potentiometer will send it's value to the one with the servo.
13+
The servo will move to the position based on the potentiometer.
14+
*/
15+
16+
17+
18+
#include <EasyTransfer.h>
19+
20+
//create two objects
21+
EasyTransfer ETin, ETout;
22+
23+
24+
struct RECEIVE_DATA_STRUCTURE{
25+
//put your variable definitions here for the data you want to receive
26+
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
27+
int16_t buttonstate;
28+
};
29+
30+
struct SEND_DATA_STRUCTURE{
31+
//put your variable definitions here for the data you want to receive
32+
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
33+
int16_t buttonstate;
34+
int16_t servoval;
35+
};
36+
37+
//give a name to the group of data
38+
RECEIVE_DATA_STRUCTURE rxdata;
39+
SEND_DATA_STRUCTURE txdata;
40+
41+
42+
void setup(){
43+
Serial.begin(9600);
44+
//start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
45+
ETin.begin(details(rxdata), &Serial);
46+
ETout.begin(details(txdata), &Serial);
47+
48+
pinMode(13, OUTPUT);
49+
//enable pull-up
50+
pinMode(12, INPUT_PULLUP);
51+
52+
}
53+
54+
void loop(){
55+
56+
//first, lets read our potentiometer and button and store it in our data structure
57+
txdata.servoval = analogRead(0);
58+
59+
if(!digitalRead(12))
60+
txdata.buttonstate = HIGH;
61+
else
62+
txdata.buttonstate = LOW;
63+
64+
//then we will go ahead and send that data out
65+
ETout.sendData();
66+
67+
//there's a loop here so that we run the recieve function more often then the
68+
//transmit function. This is important due to the slight differences in
69+
//the clock speed of different Arduinos. If we didn't do this, messages
70+
//would build up in the buffer and appear to cause a delay.
71+
for(int i=0; i<5; i++){
72+
//remember, you could use an if() here to check for new data, this time it's not needed.
73+
ETin.receiveData();
74+
75+
//set our LED on or off based on what we received from the other Arduino
76+
digitalWrite(13, rxdata.buttonstate);
77+
78+
//delay
79+
delay(10);
80+
}
81+
82+
//delay for good measure
83+
delay(10);
84+
}

0 commit comments

Comments
 (0)