Skip to content

communication via I2C #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions EasyTransferI2C/EasyTransferI2C.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "EasyTransferI2C.h"




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

//Sends out struct in binary, with header, length info and checksum
void EasyTransferI2C::sendData(uint8_t i2c_address){
uint8_t CS = size;
_serial->beginTransmission(i2c_address);
_serial->send(0x06);
_serial->send(0x85);
_serial->send(size);
for(int i = 0; i<size; i++){
CS^=*(address+i);
_serial->send(*(address+i));
}
_serial->send(CS);
_serial->endTransmission();
}

boolean EasyTransferI2C::receiveData(){

//start off by looking for the header bytes. If they were already found in a previous call, skip it.
if(rx_len == 0){
//this size check may be redundant due to the size check below, but for now I'll leave it the way it is.
if(_serial->available() >= 3){
//this will block until a 0x06 is found or buffer size becomes less then 3.
while(_serial->receive() != 0x06) {
//This will trash any preamble junk in the serial buffer
//but we need to make sure there is enough in the buffer to process while we trash the rest
//if the buffer becomes too empty, we will escape and try again on the next call
if(_serial->available() < 3)
return false;
}
if (_serial->receive() == 0x85){
rx_len = _serial->receive();
//make sure the binary structs on both Arduinos are the same size.
if(rx_len != size){
rx_len = 0;
return false;
}
}
}
}

//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->receive();
}

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];
}

if(calc_CS == rx_array[rx_array_inx-1]){//CS good
memcpy(address,&rx_array,size);
rx_len = 0;
rx_array_inx = 0;
return true;
}

else{
//failed checksum, need to clear this out anyway
rx_len = 0;
rx_array_inx = 0;
return false;
}

}
}

return false;
}
65 changes: 65 additions & 0 deletions EasyTransferI2C/EasyTransferI2C.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/******************************************************************
* EasyTransferI2C Arduino Library
* details and example sketch:
* http://www.billporter.info/easytransfer-arduino-library/
*
* Brought to you by:
* Mathieu Alorent
* Forked from
* Bill Porter
* www.billporter.info
*
* See Readme for other info and version history
*
*
*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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
<http://www.gnu.org/licenses/>
*
*This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
*To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or
*send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
******************************************************************/
#ifndef EasyTransferI2C_h
#define EasyTransferI2C_h


//make it a little prettier on the front end.
#define details(name) (byte*)&name,sizeof(name)

//Not neccessary, but just in case.
#if ARDUINO > 22
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include "HardwareSerial.h"
//#include <NewSoftSerial.h>
#include <math.h>
#include <stdio.h>
#include <stdint.h>
#include <avr/io.h>
#include <Wire.h>

class EasyTransferI2C {
public:
void begin(uint8_t *, uint8_t, TwoWire *theSerial);
void sendData(uint8_t address);
boolean receiveData();
private:
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_array_inx; //index for RX parsing buffer
uint8_t calc_CS; //calculated Chacksum
};



#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <Wire.h>
#include <EasyTransferI2C.h>

//create object
EasyTransferI2C ET;

struct RECEIVE_DATA_STRUCTURE{
//put your variable definitions here for the data you want to receive
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
int blinks;
int pause;
};

//give a name to the group of data
RECEIVE_DATA_STRUCTURE mydata;

//define slave i2c address
#define I2C_SLAVE_ADDRESS 9

void setup(){
Wire.begin(I2C_SLAVE_ADDRESS);
//start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
ET.begin(details(mydata), &Wire);
//define handler function on receiving data
Wire.onReceive(receive);

pinMode(13, OUTPUT);

}

void loop() {}

void receive(int numBytes){
//check and see if a data packet has come in.
if(ET.receiveData()){
//this is how you access the variables. [name of the group].[variable name]
//since we have data, we will blink it out.
for(int i = mydata.blinks; i>0; i--){
digitalWrite(13, HIGH);
delay(mydata.pause * 100);
digitalWrite(13, LOW);
delay(mydata.pause * 100);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <Wire.h>
#include <EasyTransferI2C.h>

//create object
EasyTransferI2C ET;

struct SEND_DATA_STRUCTURE{
//put your variable definitions here for the data you want to send
//THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
int blinks;
int pause;
};

//give a name to the group of data
SEND_DATA_STRUCTURE mydata;

//define slave i2c address
#define I2C_SLAVE_ADDRESS 9

void setup(){
Wire.begin();
//start the library, pass in the data details and the name of the serial port. Can be Serial, Serial1, Serial2, etc.
ET.begin(details(mydata), &Wire);

pinMode(13, OUTPUT);

randomSeed(analogRead(0));

}

void loop(){
//this is how you access the variables. [name of the group].[variable name]
mydata.blinks = random(5);
mydata.pause = random(5);
//send the data
ET.sendData(I2C_SLAVE_ADDRESS);

//Just for fun, we will blink it out too
for(int i = mydata.blinks; i>0; i--){
digitalWrite(13, HIGH);
delay(mydata.pause * 100);
digitalWrite(13, LOW);
delay(mydata.pause * 100);
}

delay(5000);
}
23 changes: 23 additions & 0 deletions EasyTransferI2C/keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#######################################
# Syntax Coloring Map EasyTransfer
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

EasyTransferI2C KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################
Button KEYWORD2
sendData KEYWORD2
receiveData KEYWORD2
init KEYWORD2


#######################################
# Constants (LITERAL1)
#######################################
details LITERAL1