Colin Stearns
/
qcControl
QC Control software
Fork of dgps by
packet.h@14:6be57da62283, 2014-04-10 (annotated)
- Committer:
- dylanembed123
- Date:
- Thu Apr 10 02:19:07 2014 +0000
- Revision:
- 14:6be57da62283
- Parent:
- 13:a6d3cf2b018e
- Child:
- 15:e3e03a9df89e
Update GPS;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dylanembed123 | 12:e42985e3ea64 | 1 | #define PACKETSIZE 256 |
dylanembed123 | 12:e42985e3ea64 | 2 | |
dylanembed123 | 12:e42985e3ea64 | 3 | // Example |
dylanembed123 | 12:e42985e3ea64 | 4 | // Packet 1. (SuperPackid=5,type=PT_IMAGE,size=0) |
dylanembed123 | 12:e42985e3ea64 | 5 | // Packet 2. (SuperPackid=5,type=PT_DEFAULT,size=1024) |
dylanembed123 | 12:e42985e3ea64 | 6 | // Packet 3. (SuperPackid=5,type=PT_DEFAULT,size=1000) |
dylanembed123 | 12:e42985e3ea64 | 7 | // Packet 4. (SuperPackid=5,type=PT_END,size=0) |
dylanembed123 | 14:6be57da62283 | 8 | //#define XBEEON |
dylanembed123 | 12:e42985e3ea64 | 9 | enum PACKET_TYPE{ |
dylanembed123 | 14:6be57da62283 | 10 | PT_EMPTY=0, |
dylanembed123 | 14:6be57da62283 | 11 | PT_DEFAULT, |
dylanembed123 | 12:e42985e3ea64 | 12 | PT_END, |
dylanembed123 | 12:e42985e3ea64 | 13 | PT_IMAGE, |
dylanembed123 | 12:e42985e3ea64 | 14 | PT_SIZE |
dylanembed123 | 12:e42985e3ea64 | 15 | }; |
dylanembed123 | 12:e42985e3ea64 | 16 | typedef struct PacketStruct{ |
dylanembed123 | 13:a6d3cf2b018e | 17 | unsigned int type; |
dylanembed123 | 12:e42985e3ea64 | 18 | unsigned int size;// Number of valid bits |
dylanembed123 | 12:e42985e3ea64 | 19 | char data[PACKETSIZE]; |
dylanembed123 | 12:e42985e3ea64 | 20 | unsigned int superPackID;// |
dylanembed123 | 14:6be57da62283 | 21 | char special[4];// Set to FF when |
dylanembed123 | 12:e42985e3ea64 | 22 | }PacketStruct; |
dylanembed123 | 12:e42985e3ea64 | 23 | |
dylanembed123 | 12:e42985e3ea64 | 24 | class PacketSender{ |
dylanembed123 | 12:e42985e3ea64 | 25 | private: |
dylanembed123 | 12:e42985e3ea64 | 26 | unsigned int superID; |
dylanembed123 | 12:e42985e3ea64 | 27 | public: |
dylanembed123 | 12:e42985e3ea64 | 28 | unsigned int getSuperID(){return superID++;} |
dylanembed123 | 13:a6d3cf2b018e | 29 | PacketSender():superID(0),outputDevice( |
dylanembed123 | 13:a6d3cf2b018e | 30 | #ifdef XBEEON |
dylanembed123 | 13:a6d3cf2b018e | 31 | XBEE::getSerial() |
dylanembed123 | 13:a6d3cf2b018e | 32 | #else |
dylanembed123 | 13:a6d3cf2b018e | 33 | USB::getSerial() |
dylanembed123 | 13:a6d3cf2b018e | 34 | #endif |
dylanembed123 | 14:6be57da62283 | 35 | ),next(NULL){} |
dylanembed123 | 12:e42985e3ea64 | 36 | Serial& outputDevice; |
dylanembed123 | 12:e42985e3ea64 | 37 | void sendPacket(PacketStruct& output){ |
dylanembed123 | 12:e42985e3ea64 | 38 | for(int a=0;a<sizeof(PacketStruct);a++){ |
dylanembed123 | 14:6be57da62283 | 39 | while(!outputDevice.writeable()){} |
dylanembed123 | 12:e42985e3ea64 | 40 | outputDevice.putc(((char*)(&output))[a]); |
dylanembed123 | 14:6be57da62283 | 41 | //wait_ms(10); |
dylanembed123 | 13:a6d3cf2b018e | 42 | //USB::getSerial().putc(((char*)(&output))[a]); |
dylanembed123 | 12:e42985e3ea64 | 43 | } |
dylanembed123 | 14:6be57da62283 | 44 | //wait_ms(100); |
dylanembed123 | 12:e42985e3ea64 | 45 | } |
dylanembed123 | 12:e42985e3ea64 | 46 | unsigned int min(unsigned int a,unsigned int b){ |
dylanembed123 | 12:e42985e3ea64 | 47 | return a<b ? a : b; |
dylanembed123 | 12:e42985e3ea64 | 48 | } |
dylanembed123 | 12:e42985e3ea64 | 49 | void sendPacket(unsigned int superPackID,char* data,unsigned int size,PACKET_TYPE type = PT_END){ |
dylanembed123 | 12:e42985e3ea64 | 50 | if(data!=NULL && size>0){ |
dylanembed123 | 13:a6d3cf2b018e | 51 | for(int i=0;i<=(size-1)/PACKETSIZE;i++){ |
dylanembed123 | 12:e42985e3ea64 | 52 | PacketStruct output; |
dylanembed123 | 12:e42985e3ea64 | 53 | output.type=PT_DEFAULT; |
dylanembed123 | 12:e42985e3ea64 | 54 | output.superPackID=superPackID; |
dylanembed123 | 12:e42985e3ea64 | 55 | output.size=min(PACKETSIZE,size-i*PACKETSIZE); |
dylanembed123 | 14:6be57da62283 | 56 | for(int a=0;a<4;a++){output.special[a]='\0';}output.special[3]=0xAA; |
dylanembed123 | 14:6be57da62283 | 57 | for(int a=0;a<output.size;a++){output.data[a]=data[a-i*PACKETSIZE];} |
dylanembed123 | 13:a6d3cf2b018e | 58 | for(int a=output.size;a<PACKETSIZE;a++){output.data[a]='\0';} |
dylanembed123 | 12:e42985e3ea64 | 59 | sendPacket(output); |
dylanembed123 | 12:e42985e3ea64 | 60 | } |
dylanembed123 | 12:e42985e3ea64 | 61 | }else{ |
dylanembed123 | 12:e42985e3ea64 | 62 | PacketStruct output; |
dylanembed123 | 12:e42985e3ea64 | 63 | output.type=type; |
dylanembed123 | 13:a6d3cf2b018e | 64 | output.size=0; |
dylanembed123 | 12:e42985e3ea64 | 65 | output.superPackID=superPackID; |
dylanembed123 | 14:6be57da62283 | 66 | for(int a=0;a<4;a++){output.special[a]='\0';}output.special[3]=0xAA; |
dylanembed123 | 14:6be57da62283 | 67 | // Check for empty packet |
dylanembed123 | 14:6be57da62283 | 68 | if(output.type==PT_EMPTY){output.type=0;output.size=0;output.superPackID=0;output.special[3]=0xFF;} |
dylanembed123 | 14:6be57da62283 | 69 | for(int a=0;a<PACKETSIZE;a++){output.data[a]='\0';} |
dylanembed123 | 12:e42985e3ea64 | 70 | sendPacket(output); |
dylanembed123 | 12:e42985e3ea64 | 71 | } |
dylanembed123 | 12:e42985e3ea64 | 72 | } |
dylanembed123 | 14:6be57da62283 | 73 | |
dylanembed123 | 14:6be57da62283 | 74 | // Number of consecutive zeros |
dylanembed123 | 14:6be57da62283 | 75 | unsigned int numZeros; |
dylanembed123 | 14:6be57da62283 | 76 | // Return true if a resync command has been received |
dylanembed123 | 14:6be57da62283 | 77 | bool resetCheck(char input){ |
dylanembed123 | 14:6be57da62283 | 78 | if(input=='\0'){ |
dylanembed123 | 14:6be57da62283 | 79 | numZeros++; |
dylanembed123 | 14:6be57da62283 | 80 | }else if(numZeros==sizeof(PacketStruct)-1&&input==0xFF){ |
dylanembed123 | 14:6be57da62283 | 81 | return true; |
dylanembed123 | 14:6be57da62283 | 82 | }else{ |
dylanembed123 | 14:6be57da62283 | 83 | numZeros=0; |
dylanembed123 | 14:6be57da62283 | 84 | } |
dylanembed123 | 14:6be57da62283 | 85 | return false; |
dylanembed123 | 14:6be57da62283 | 86 | } |
dylanembed123 | 14:6be57da62283 | 87 | |
dylanembed123 | 14:6be57da62283 | 88 | // Temperary storage for next valid |
dylanembed123 | 14:6be57da62283 | 89 | PacketStruct* next; |
dylanembed123 | 14:6be57da62283 | 90 | // Number of valid bits in next packet |
dylanembed123 | 14:6be57da62283 | 91 | int nextValid; |
dylanembed123 | 14:6be57da62283 | 92 | /// \brief Grab the next packet |
dylanembed123 | 12:e42985e3ea64 | 93 | PacketStruct* getNextPacket(){ |
dylanembed123 | 14:6be57da62283 | 94 | // Check for null packet |
dylanembed123 | 14:6be57da62283 | 95 | if(next==NULL){next=new PacketStruct();nextValid=0;} |
dylanembed123 | 14:6be57da62283 | 96 | // Create reset packet which resets on re-sync command |
dylanembed123 | 14:6be57da62283 | 97 | bool resetPacket=false; |
dylanembed123 | 14:6be57da62283 | 98 | |
dylanembed123 | 14:6be57da62283 | 99 | // While there is data to read |
dylanembed123 | 14:6be57da62283 | 100 | while(0<outputDevice.readable()){ |
dylanembed123 | 14:6be57da62283 | 101 | // Check if a full packet has been received |
dylanembed123 | 14:6be57da62283 | 102 | if(nextValid==sizeof(PacketStruct))break; |
dylanembed123 | 14:6be57da62283 | 103 | // Read in next char |
dylanembed123 | 14:6be57da62283 | 104 | char input=outputDevice.getc(); |
dylanembed123 | 14:6be57da62283 | 105 | //USB::getSerial().printf("Read ByteC %X %X\n",nextValid,input); |
dylanembed123 | 14:6be57da62283 | 106 | // Check for valid char |
dylanembed123 | 14:6be57da62283 | 107 | if(resetCheck(input)){resetPacket=true;break;} |
dylanembed123 | 14:6be57da62283 | 108 | // Set char |
dylanembed123 | 14:6be57da62283 | 109 | ((char*)next)[nextValid++] = input; |
dylanembed123 | 14:6be57da62283 | 110 | } |
dylanembed123 | 14:6be57da62283 | 111 | |
dylanembed123 | 14:6be57da62283 | 112 | if(nextValid==sizeof(PacketStruct)||resetPacket){ |
dylanembed123 | 14:6be57da62283 | 113 | // Reset packet |
dylanembed123 | 14:6be57da62283 | 114 | PacketStruct* output=next;next=NULL; |
dylanembed123 | 14:6be57da62283 | 115 | // Return |
dylanembed123 | 14:6be57da62283 | 116 | return resetPacket?NULL:output; |
dylanembed123 | 14:6be57da62283 | 117 | } |
dylanembed123 | 14:6be57da62283 | 118 | return NULL; |
dylanembed123 | 14:6be57da62283 | 119 | /* |
dylanembed123 | 12:e42985e3ea64 | 120 | int avail = outputDevice.readable(); |
dylanembed123 | 12:e42985e3ea64 | 121 | if(avail <= 0)return NULL; |
dylanembed123 | 12:e42985e3ea64 | 122 | PacketStruct* output=new PacketStruct(); |
dylanembed123 | 12:e42985e3ea64 | 123 | for(int i=0;i<sizeof(PacketStruct);i++){ |
dylanembed123 | 12:e42985e3ea64 | 124 | // Wait for byte |
dylanembed123 | 12:e42985e3ea64 | 125 | while(outputDevice.readable()<=0){} |
dylanembed123 | 12:e42985e3ea64 | 126 | ((char*)output)[i] = outputDevice.getc(); |
dylanembed123 | 12:e42985e3ea64 | 127 | } |
dylanembed123 | 12:e42985e3ea64 | 128 | return output; |
dylanembed123 | 14:6be57da62283 | 129 | */ |
dylanembed123 | 12:e42985e3ea64 | 130 | } |
dylanembed123 | 12:e42985e3ea64 | 131 | |
dylanembed123 | 12:e42985e3ea64 | 132 | }; |
dylanembed123 | 12:e42985e3ea64 | 133 | static PacketSender* ps=NULL; |
dylanembed123 | 12:e42985e3ea64 | 134 | static PacketSender& getPS(){ |
dylanembed123 | 12:e42985e3ea64 | 135 | if(ps==NULL)ps=new PacketSender(); |
dylanembed123 | 12:e42985e3ea64 | 136 | return *ps; |
dylanembed123 | 12:e42985e3ea64 | 137 | } |