Skip to content

Commit d23ebd0

Browse files
author
Bill Porter
committed
Merge pull request #1 from kumy/master
communication via I2C
2 parents 3457aa0 + a93b1e3 commit d23ebd0

File tree

5 files changed

+265
-0
lines changed

5 files changed

+265
-0
lines changed

EasyTransferI2C/EasyTransferI2C.cpp

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

EasyTransferI2C/EasyTransferI2C.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/******************************************************************
2+
* EasyTransferI2C Arduino Library
3+
* details and example sketch:
4+
* http://www.billporter.info/easytransfer-arduino-library/
5+
*
6+
* Brought to you by:
7+
* Mathieu Alorent
8+
* Forked from
9+
* Bill Porter
10+
* www.billporter.info
11+
*
12+
* See Readme for other info and version history
13+
*
14+
*
15+
*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.
16+
This program is distributed in the hope that it will be useful,
17+
but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
GNU General Public License for more details.
20+
<http://www.gnu.org/licenses/>
21+
*
22+
*This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
23+
*To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or
24+
*send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
25+
******************************************************************/
26+
#ifndef EasyTransferI2C_h
27+
#define EasyTransferI2C_h
28+
29+
30+
//make it a little prettier on the front end.
31+
#define details(name) (byte*)&name,sizeof(name)
32+
33+
//Not neccessary, but just in case.
34+
#if ARDUINO > 22
35+
#include "Arduino.h"
36+
#else
37+
#include "WProgram.h"
38+
#endif
39+
#include "HardwareSerial.h"
40+
//#include <NewSoftSerial.h>
41+
#include <math.h>
42+
#include <stdio.h>
43+
#include <stdint.h>
44+
#include <avr/io.h>
45+
#include <Wire.h>
46+
47+
class EasyTransferI2C {
48+
public:
49+
void begin(uint8_t *, uint8_t, TwoWire *theSerial);
50+
void sendData(uint8_t address);
51+
boolean receiveData();
52+
private:
53+
TwoWire *_serial;
54+
//NewSoftSerial *_serial;
55+
uint8_t * address; //address of struct
56+
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
59+
uint8_t rx_array_inx; //index for RX parsing buffer
60+
uint8_t calc_CS; //calculated Chacksum
61+
};
62+
63+
64+
65+
#endif
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <Wire.h>
2+
#include <EasyTransferI2C.h>
3+
4+
//create object
5+
EasyTransferI2C ET;
6+
7+
struct RECEIVE_DATA_STRUCTURE{
8+
//put your variable definitions here for the data you want to receive
9+
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
10+
int blinks;
11+
int pause;
12+
};
13+
14+
//give a name to the group of data
15+
RECEIVE_DATA_STRUCTURE mydata;
16+
17+
//define slave i2c address
18+
#define I2C_SLAVE_ADDRESS 9
19+
20+
void setup(){
21+
Wire.begin(I2C_SLAVE_ADDRESS);
22+
//start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
23+
ET.begin(details(mydata), &Wire);
24+
//define handler function on receiving data
25+
Wire.onReceive(receive);
26+
27+
pinMode(13, OUTPUT);
28+
29+
}
30+
31+
void loop() {}
32+
33+
void receive(int numBytes){
34+
//check and see if a data packet has come in.
35+
if(ET.receiveData()){
36+
//this is how you access the variables. [name of the group].[variable name]
37+
//since we have data, we will blink it out.
38+
for(int i = mydata.blinks; i>0; i--){
39+
digitalWrite(13, HIGH);
40+
delay(mydata.pause * 100);
41+
digitalWrite(13, LOW);
42+
delay(mydata.pause * 100);
43+
}
44+
}
45+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <Wire.h>
2+
#include <EasyTransferI2C.h>
3+
4+
//create object
5+
EasyTransferI2C ET;
6+
7+
struct SEND_DATA_STRUCTURE{
8+
//put your variable definitions here for the data you want to send
9+
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
10+
int blinks;
11+
int pause;
12+
};
13+
14+
//give a name to the group of data
15+
SEND_DATA_STRUCTURE mydata;
16+
17+
//define slave i2c address
18+
#define I2C_SLAVE_ADDRESS 9
19+
20+
void setup(){
21+
Wire.begin();
22+
//start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
23+
ET.begin(details(mydata), &Wire);
24+
25+
pinMode(13, OUTPUT);
26+
27+
randomSeed(analogRead(0));
28+
29+
}
30+
31+
void loop(){
32+
//this is how you access the variables. [name of the group].[variable name]
33+
mydata.blinks = random(5);
34+
mydata.pause = random(5);
35+
//send the data
36+
ET.sendData(I2C_SLAVE_ADDRESS);
37+
38+
//Just for fun, we will blink it out too
39+
for(int i = mydata.blinks; i>0; i--){
40+
digitalWrite(13, HIGH);
41+
delay(mydata.pause * 100);
42+
digitalWrite(13, LOW);
43+
delay(mydata.pause * 100);
44+
}
45+
46+
delay(5000);
47+
}

EasyTransferI2C/keywords.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#######################################
2+
# Syntax Coloring Map EasyTransfer
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
EasyTransferI2C KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
Button KEYWORD2
15+
sendData KEYWORD2
16+
receiveData KEYWORD2
17+
init KEYWORD2
18+
19+
20+
#######################################
21+
# Constants (LITERAL1)
22+
#######################################
23+
details LITERAL1

0 commit comments

Comments
 (0)