|
| 1 | +/******************************************************************************* |
| 2 | +* ArduinoRobotEasyTransfer library modified by Julian Sanin, |
| 3 | +* backported from: |
| 4 | +* |
| 5 | +* EasyTransfer Arduino Library v2.1 |
| 6 | +* details and example sketch: |
| 7 | +* http://www.billporter.info/easytransfer-arduino-library/ |
| 8 | +* |
| 9 | +* Brought to you by: |
| 10 | +* Bill Porter |
| 11 | +* www.billporter.info |
| 12 | +* |
| 13 | +* Major props to Mathieu Alorent (kumy) for |
| 14 | +* I2C version and the pretty pictures. |
| 15 | +* |
| 16 | +* |
| 17 | +* Lib version history |
| 18 | +* 1.0 Created |
| 19 | +* 1.1 Fixed dumb Copy-paste error in header file |
| 20 | +* Added a keyword file |
| 21 | +* 1.5 Forked lib into Software and Hardware Serial branches, I don't know a |
| 22 | +* better way added passing in of Serial port of different types |
| 23 | +* 1.6 Fixed bug where it wasn't clearing out the buffers if the CheckSum |
| 24 | +* failed, I'm good at dumb mistakes |
| 25 | +* 1.7 Fixed a bug where the receive function could block for too long and |
| 26 | +* never process data correctly |
| 27 | +* Organized the examples to be Arduino IDE compatible |
| 28 | +* 1.8 |
| 29 | +* Now Arduino 1.0 compatible! |
| 30 | +* 1.81 |
| 31 | +* Made it more cross compatible. Man, They really made us work for this |
| 32 | +* one. |
| 33 | +* 2.0 |
| 34 | +* Combined SoftEasyTransfer with the other two to make everything one |
| 35 | +* repo |
| 36 | +* Added EasyTransferVirtualWire library for use with Virtual Wire and |
| 37 | +* cheap radios. |
| 38 | +* 2.0.1 |
| 39 | +* VirtualWire version tested by garth@netram, bugs fixed. |
| 40 | +* 2.1 |
| 41 | +* Changes RX parsing buffer to dynamic allocation to conserve RAM. |
| 42 | +* 3.0 |
| 43 | +* Imported EasyTransfer library as ArduinoRobotEasyTransfer. |
| 44 | +* Backported ArduinoRobot modifications. |
| 45 | +* |
| 46 | +* |
| 47 | +* Limits of the Library |
| 48 | +* You can change the Serial port, |
| 49 | +* but the Struct size must not pass 255 bytes |
| 50 | +* VirtualWire Version Struct can'e be bigger then 26 bytes |
| 51 | +* |
| 52 | +* The protcol is as follows: |
| 53 | +* Header(0x06,0x85),SizeofPayload,Payload,Checksum |
| 54 | +* |
| 55 | +* |
| 56 | +* This program is free software: you can redistribute it and/or modify it under |
| 57 | +* the terms of the GNU General Public License as published by the Free Software |
| 58 | +* Foundation, either version 3 of the License, or(at your option) any later |
| 59 | +* version. |
| 60 | +* This program is distributed in the hope that it will be useful, |
| 61 | +* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 62 | +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 63 | +* GNU General Public License for more details. |
| 64 | +* <http://www.gnu.org/licenses/> |
| 65 | +* |
| 66 | +* This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 |
| 67 | +* Unported License. To view a copy of this license, visit |
| 68 | +* http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to Creative |
| 69 | +* Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA. |
| 70 | +*******************************************************************************/ |
| 71 | +#include "ArduinoRobotEasyTransfer.h" |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +//Captures address and size of struct |
| 77 | +void ArduinoRobotEasyTransfer::begin(uint8_t * ptr, uint8_t length, Stream *theStream){ |
| 78 | + address = ptr; |
| 79 | + size = length; |
| 80 | + _stream = theStream; |
| 81 | + |
| 82 | + //dynamic creation of rx parsing buffer in RAM |
| 83 | + rx_buffer = (uint8_t*) malloc(size+1); |
| 84 | +} |
| 85 | + |
| 86 | +//Sends out struct in binary, with header, length info and checksum |
| 87 | +void ArduinoRobotEasyTransfer::sendData(){ |
| 88 | + uint8_t CS = size; |
| 89 | + _stream->write(0x06); |
| 90 | + _stream->write(0x85); |
| 91 | + _stream->write(size); |
| 92 | + for(int i = 0; i<size; i++){ |
| 93 | + CS^=*(address+i); |
| 94 | + _stream->write(*(address+i)); |
| 95 | + } |
| 96 | + _stream->write(CS); |
| 97 | + |
| 98 | +} |
| 99 | + |
| 100 | +boolean ArduinoRobotEasyTransfer::receiveData(){ |
| 101 | + |
| 102 | + //start off by looking for the header bytes. If they were already found in a previous call, skip it. |
| 103 | + if(rx_len == 0){ |
| 104 | + //this size check may be redundant due to the size check below, but for now I'll leave it the way it is. |
| 105 | + if(_stream->available() >= 3){ |
| 106 | + //this will block until a 0x06 is found or buffer size becomes less then 3. |
| 107 | + while(_stream->read() != 0x06) { |
| 108 | + //This will trash any preamble junk in the serial buffer |
| 109 | + //but we need to make sure there is enough in the buffer to process while we trash the rest |
| 110 | + //if the buffer becomes too empty, we will escape and try again on the next call |
| 111 | + if(_stream->available() < 3) |
| 112 | + return false; |
| 113 | + } |
| 114 | + if (_stream->read() == 0x85){ |
| 115 | + rx_len = _stream->read(); |
| 116 | + //make sure the binary structs on both Arduinos are the same size. |
| 117 | + if(rx_len != size){ |
| 118 | + rx_len = 0; |
| 119 | + return false; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + //we get here if we already found the header bytes, the struct size matched what we know, and now we are byte aligned. |
| 126 | + if(rx_len != 0){ |
| 127 | + while(_stream->available() && rx_array_inx <= rx_len){ |
| 128 | + rx_buffer[rx_array_inx++] = _stream->read(); |
| 129 | + } |
| 130 | + |
| 131 | + if(rx_len == (rx_array_inx-1)){ |
| 132 | + //seem to have got whole message |
| 133 | + //last uint8_t is CS |
| 134 | + calc_CS = rx_len; |
| 135 | + for (int i = 0; i<rx_len; i++){ |
| 136 | + calc_CS^=rx_buffer[i]; |
| 137 | + } |
| 138 | + |
| 139 | + if(calc_CS == rx_buffer[rx_array_inx-1]){//CS good |
| 140 | + memcpy(address,rx_buffer,size); |
| 141 | + rx_len = 0; |
| 142 | + rx_array_inx = 0; |
| 143 | + return true; |
| 144 | + } |
| 145 | + |
| 146 | + else{ |
| 147 | + //failed checksum, need to clear this out anyway |
| 148 | + rx_len = 0; |
| 149 | + rx_array_inx = 0; |
| 150 | + return false; |
| 151 | + } |
| 152 | + |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return false; |
| 157 | +} |
0 commit comments