Skip to content

Commit ec03b6a

Browse files
committed
Added to Git, the right way this time.
Signed-off-by: Bill Porter <madsci1016@gmail.com>
0 parents  commit ec03b6a

File tree

8 files changed

+424
-0
lines changed

8 files changed

+424
-0
lines changed

EasyTransfer/EasyTransfer.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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, HardwareSerial *theSerial){
8+
address = ptr;
9+
size = length;
10+
_serial = theSerial;
11+
}
12+
13+
//Sends out struct in binary, with header, length info and checksum
14+
void EasyTransfer::sendData(){
15+
uint8_t CS = size;
16+
_serial->print(0x06, BYTE);
17+
_serial->print(0x85, BYTE);
18+
_serial->print(size, BYTE);
19+
for(int i = 0; i<size; i++){
20+
CS^=*(address+i);
21+
_serial->print(*(address+i), BYTE);
22+
}
23+
_serial->print(CS);
24+
25+
}
26+
27+
boolean EasyTransfer::receiveData(){
28+
29+
//start off by looking for the header bytes. If they were already found in a previous call, skip it.
30+
if(rx_len == 0){
31+
//this size check may be redundant due to the size check below, but for now I'll leave it the way it is.
32+
if(_serial->available() >= 3){
33+
//this will block until a 0x06 is found or buffer size becomes less then 3.
34+
while(_serial->read() != 0x06) {
35+
//This will trash any preamble junk in the serial buffer
36+
//but we need to make sure there is enough in the buffer to process while we trash the rest
37+
//if the buffer becomes too empty, we will escape and try again on the next call
38+
if(_serial->available() < 3)
39+
return false;
40+
}
41+
if (_serial->read() == 0x85){
42+
rx_len = _serial->read();
43+
//make sure the binary structs on both Arduinos are the same size.
44+
if(rx_len != size){
45+
rx_len = 0;
46+
return false;
47+
}
48+
}
49+
}
50+
}
51+
52+
//we get here if we already found the header bytes, the struct size matched what we know, and now we are byte aligned.
53+
if(rx_len != 0){
54+
while(_serial->available() && rx_array_inx <= rx_len){
55+
rx_array[rx_array_inx++] = _serial->read();
56+
}
57+
58+
if(rx_len == (rx_array_inx-1)){
59+
//seem to have got whole message
60+
//last uint8_t is CS
61+
calc_CS = rx_len;
62+
for (int i = 0; i<rx_len; i++){
63+
calc_CS^=rx_array[i];
64+
}
65+
66+
if(calc_CS == rx_array[rx_array_inx-1]){//CS good
67+
memcpy(address,&rx_array,size);
68+
rx_len = 0;
69+
rx_array_inx = 0;
70+
return true;
71+
}
72+
73+
else{
74+
//failed checksum, need to clear this out anyway
75+
rx_len = 0;
76+
rx_array_inx = 0;
77+
return false;
78+
}
79+
80+
}
81+
}
82+
83+
return false;
84+
}

EasyTransfer/EasyTransfer.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
#include "WProgram.h"
33+
#include "HardwareSerial.h"
34+
//#include <NewSoftSerial.h>
35+
#include <math.h>
36+
#include <stdio.h>
37+
#include <stdint.h>
38+
#include <avr/io.h>
39+
40+
class EasyTransfer {
41+
public:
42+
void begin(uint8_t *, uint8_t, HardwareSerial *theSerial);
43+
//void begin(uint8_t *, uint8_t, NewSoftSerial *theSerial);
44+
void sendData();
45+
boolean receiveData();
46+
private:
47+
HardwareSerial *_serial;
48+
//NewSoftSerial *_serial;
49+
uint8_t * address; //address of struct
50+
uint8_t size; //size of struct
51+
uint8_t rx_len; //RX packet length according to the packet
52+
uint8_t rx_array[255]; //RX packet parsing buffer
53+
uint8_t rx_array_inx; //index for RX parsing buffer
54+
uint8_t calc_CS; //calculated Chacksum
55+
};
56+
57+
58+
59+
#endif
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
int 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+
int buttonstate;
34+
int 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+
pinMode(12, INPUT);
50+
//enable pull-up
51+
digitalWrite(12, HIGH);
52+
53+
}
54+
55+
void loop(){
56+
57+
//first, lets read our potentiometer and button and store it in our data structure
58+
txdata.servoval = analogRead(0);
59+
60+
if(!digitalRead(12))
61+
txdata.buttonstate = HIGH;
62+
else
63+
txdata.buttonstate = LOW;
64+
65+
//then we will go ahead and send that data out
66+
ETout.sendData();
67+
68+
//there's a loop here so that we run the recieve function more often then the
69+
//transmit function. This is important due to the slight differences in
70+
//the clock speed of different Arduinos. If we didn't do this, messages
71+
//would build up in the buffer and appear to cause a delay.
72+
for(int i=0; i<5; i++){
73+
//remember, you could use an if() here to check for new data, this time it's not needed.
74+
ETin.receiveData();
75+
76+
//set our LED on or off based on what we received from the other Arduino
77+
digitalWrite(13, rxdata.buttonstate);
78+
79+
//delay
80+
delay(10);
81+
}
82+
83+
//delay for good measure
84+
delay(10);
85+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*This is an example of the EasyTransfer Library 2way communications.
2+
3+
This sketch is for the Arduino with the servo attached to pin 9.
4+
5+
The other Arduino has a potentiometer attached to analog pin 0.
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+
#include <Servo.h>
17+
#include <EasyTransfer.h>
18+
19+
//create two objects
20+
EasyTransfer ETin, ETout;
21+
//create servo
22+
Servo myservo;
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+
int buttonstate;
28+
int servoval;
29+
};
30+
31+
struct SEND_DATA_STRUCTURE{
32+
//put your variable definitions here for the data you want to receive
33+
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
34+
int buttonstate;
35+
};
36+
37+
38+
//give a name to the group of data
39+
RECEIVE_DATA_STRUCTURE rxdata;
40+
SEND_DATA_STRUCTURE txdata;
41+
42+
43+
void setup(){
44+
Serial.begin(9600);
45+
//start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
46+
ETin.begin(details(rxdata), &Serial);
47+
ETout.begin(details(txdata), &Serial);
48+
49+
pinMode(13, OUTPUT);
50+
pinMode(12, INPUT);
51+
//enable pull-up
52+
digitalWrite(12, HIGH);
53+
54+
myservo.attach(9);
55+
}
56+
57+
void loop(){
58+
59+
//first, lets read our button and store it in our data structure
60+
if(!digitalRead(12))
61+
txdata.buttonstate = HIGH;
62+
else
63+
txdata.buttonstate = LOW;
64+
65+
//then we will go ahead and send that data out
66+
ETout.sendData();
67+
68+
//there's a loop here so that we run the recieve function more often then the
69+
//transmit function. This is important due to the slight differences in
70+
//the clock speed of different Arduinos. If we didn't do this, messages
71+
//would build up in the buffer and appear to cause a delay.
72+
73+
for(int i=0; i<5; i++){
74+
//remember, you could use an if() here to check for new data, this time it's not needed.
75+
ETin.receiveData();
76+
77+
//set our LED on or off based on what we received from the other Arduino
78+
digitalWrite(13, rxdata.buttonstate);
79+
80+
//set our servo position based on what we received from the other Arduino
81+
//we will also map the ADC value to a servo value
82+
myservo.write(map(rxdata.servoval, 0, 1023, 0, 179));
83+
84+
//delay
85+
delay(10);
86+
}
87+
88+
//delay for good measure
89+
delay(10);
90+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <EasyTransfer.h>
2+
3+
//create object
4+
EasyTransfer ET;
5+
6+
struct RECEIVE_DATA_STRUCTURE{
7+
//put your variable definitions here for the data you want to receive
8+
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
9+
int blinks;
10+
int pause;
11+
};
12+
13+
//give a name to the group of data
14+
RECEIVE_DATA_STRUCTURE mydata;
15+
16+
void setup(){
17+
Serial.begin(9600);
18+
//start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
19+
ET.begin(details(mydata), &Serial);
20+
21+
pinMode(13, OUTPUT);
22+
23+
}
24+
25+
void loop(){
26+
//check and see if a data packet has come in.
27+
if(ET.receiveData()){
28+
//this is how you access the variables. [name of the group].[variable name]
29+
//since we have data, we will blink it out.
30+
for(int i = mydata.blinks; i>0; i--){
31+
digitalWrite(13, HIGH);
32+
delay(mydata.pause * 100);
33+
digitalWrite(13, LOW);
34+
delay(mydata.pause * 100);
35+
}
36+
}
37+
38+
//you should make this delay shorter then your transmit delay or else messages could be lost
39+
delay(250);
40+
}

0 commit comments

Comments
 (0)