[136] | 1 | #include "EasyTransfer2.h"
|
---|
| 2 |
|
---|
| 3 |
|
---|
| 4 |
|
---|
| 5 |
|
---|
| 6 | //Captures address and size of struct
|
---|
| 7 | void EasyTransfer2::begin(HardwareSerial *theSerial){
|
---|
| 8 | _serial = theSerial;
|
---|
| 9 |
|
---|
| 10 | //dynamic creation of rx parsing buffer in RAM
|
---|
| 11 | //rx_buffer = (uint8_t*) malloc(size);
|
---|
| 12 |
|
---|
| 13 | resetData();
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | void EasyTransfer2::writeByte(uint8_t dat){
|
---|
| 17 | if(position<20)
|
---|
| 18 | data[position++]=dat;
|
---|
| 19 | size++;
|
---|
| 20 | }
|
---|
| 21 | void EasyTransfer2::writeInt(int dat){
|
---|
| 22 | if(position<19){
|
---|
| 23 | data[position++]=dat>>8;
|
---|
| 24 | data[position++]=dat;
|
---|
| 25 | size+=2;
|
---|
| 26 | }
|
---|
| 27 | }
|
---|
| 28 | uint8_t EasyTransfer2::readByte(){
|
---|
| 29 | if(position>=size)return 0;
|
---|
| 30 | return data[position++];
|
---|
| 31 | }
|
---|
| 32 | int EasyTransfer2::readInt(){
|
---|
| 33 | if(position+1>=size)return 0;
|
---|
| 34 | int dat_1=data[position++]<<8;
|
---|
| 35 | int dat_2=data[position++];
|
---|
| 36 | int dat= dat_1 | dat_2;
|
---|
| 37 | return dat;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | void EasyTransfer2::resetData(){
|
---|
| 41 | for(int i=0;i<20;i++){
|
---|
| 42 | data[i]=0;
|
---|
| 43 | }
|
---|
| 44 | size=0;
|
---|
| 45 | position=0;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | //Sends out struct in binary, with header, length info and checksum
|
---|
| 49 | void EasyTransfer2::sendData(){
|
---|
| 50 | uint8_t CS = size;
|
---|
| 51 | _serial->write(0x06);
|
---|
| 52 | _serial->write(0x85);
|
---|
| 53 | _serial->write(size);
|
---|
| 54 | for(int i = 0; i<size; i++){
|
---|
| 55 | CS^=*(data+i);
|
---|
| 56 | _serial->write(*(data+i));
|
---|
| 57 | //Serial.print(*(data+i));
|
---|
| 58 | //Serial.print(",");
|
---|
| 59 | }
|
---|
| 60 | //Serial.println("");
|
---|
| 61 | _serial->write(CS);
|
---|
| 62 |
|
---|
| 63 | resetData();
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | boolean EasyTransfer2::receiveData(){
|
---|
| 67 |
|
---|
| 68 | //start off by looking for the header bytes. If they were already found in a previous call, skip it.
|
---|
| 69 | if(rx_len == 0){
|
---|
| 70 | //this size check may be redundant due to the size check below, but for now I'll leave it the way it is.
|
---|
| 71 | if(_serial->available() >= 3){
|
---|
| 72 | //this will block until a 0x06 is found or buffer size becomes less then 3.
|
---|
| 73 | while(_serial->read() != 0x06) {
|
---|
| 74 | //This will trash any preamble junk in the serial buffer
|
---|
| 75 | //but we need to make sure there is enough in the buffer to process while we trash the rest
|
---|
| 76 | //if the buffer becomes too empty, we will escape and try again on the next call
|
---|
| 77 | if(_serial->available() < 3)
|
---|
| 78 | return false;
|
---|
| 79 | }
|
---|
| 80 | //Serial.println("head");
|
---|
| 81 | if (_serial->read() == 0x85){
|
---|
| 82 | rx_len = _serial->read();
|
---|
| 83 | //Serial.print("rx_len:");
|
---|
| 84 | //Serial.println(rx_len);
|
---|
| 85 | resetData();
|
---|
| 86 |
|
---|
| 87 | //make sure the binary structs on both Arduinos are the same size.
|
---|
| 88 | /*if(rx_len != size){
|
---|
| 89 | rx_len = 0;
|
---|
| 90 | return false;
|
---|
| 91 | }*/
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | //Serial.println("nothing");
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | //we get here if we already found the header bytes, the struct size matched what we know, and now we are byte aligned.
|
---|
| 98 | if(rx_len != 0){
|
---|
| 99 |
|
---|
| 100 | while(_serial->available() && rx_array_inx <= rx_len){
|
---|
| 101 | data[rx_array_inx++] = _serial->read();
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | if(rx_len == (rx_array_inx-1)){
|
---|
| 105 | //seem to have got whole message
|
---|
| 106 | //last uint8_t is CS
|
---|
| 107 | calc_CS = rx_len;
|
---|
| 108 | //Serial.print("len:");
|
---|
| 109 | //Serial.println(rx_len);
|
---|
| 110 | for (int i = 0; i<rx_len; i++){
|
---|
| 111 | calc_CS^=data[i];
|
---|
| 112 | //Serial.print("m");
|
---|
| 113 | //Serial.print(data[i]);
|
---|
| 114 | //Serial.print(",");
|
---|
| 115 | }
|
---|
| 116 | //Serial.println();
|
---|
| 117 | //Serial.print(data[rx_array_inx-1]);
|
---|
| 118 | //Serial.print(" ");
|
---|
| 119 | //Serial.println(calc_CS);
|
---|
| 120 |
|
---|
| 121 | if(calc_CS == data[rx_array_inx-1]){//CS good
|
---|
| 122 | //resetData();
|
---|
| 123 | //memcpy(data,d,rx_len);
|
---|
| 124 | for(int i=0;i<20;i++){
|
---|
| 125 | //Serial.print(data[i]);
|
---|
| 126 | //Serial.print(",");
|
---|
| 127 | }
|
---|
| 128 | //Serial.println("");
|
---|
| 129 | size=rx_len;
|
---|
| 130 | rx_len = 0;
|
---|
| 131 | rx_array_inx = 0;
|
---|
| 132 | return true;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | else{
|
---|
| 136 | //Serial.println("CS");
|
---|
| 137 | resetData();
|
---|
| 138 | //failed checksum, need to clear this out anyway
|
---|
| 139 | rx_len = 0;
|
---|
| 140 | rx_array_inx = 0;
|
---|
| 141 | return false;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 | //Serial.print(rx_len);
|
---|
| 147 | //Serial.print(" ");
|
---|
| 148 | //Serial.print(rx_array_inx);
|
---|
| 149 | //Serial.print(" ");
|
---|
| 150 | //Serial.println("Short");
|
---|
| 151 | return false;
|
---|
| 152 | }
|
---|