Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of dgps by
packet.h@15:e3e03a9df89e, 2014-04-10 (annotated)
- Committer:
- dylanembed123
- Date:
- Thu Apr 10 05:38:45 2014 +0000
- Revision:
- 15:e3e03a9df89e
- Parent:
- 14:6be57da62283
- Child:
- 16:4f5d20b87dc3
Demo prep - The GPS, camera and XBEE stream with packet work.
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 | 15:e3e03a9df89e | 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 | 15:e3e03a9df89e | 14 | PT_IMAGEHEAD, |
dylanembed123 | 15:e3e03a9df89e | 15 | PT_REQLOC, |
dylanembed123 | 15:e3e03a9df89e | 16 | PT_SENDLOC, |
dylanembed123 | 15:e3e03a9df89e | 17 | PT_WAY, |
dylanembed123 | 12:e42985e3ea64 | 18 | PT_SIZE |
dylanembed123 | 12:e42985e3ea64 | 19 | }; |
dylanembed123 | 12:e42985e3ea64 | 20 | typedef struct PacketStruct{ |
dylanembed123 | 13:a6d3cf2b018e | 21 | unsigned int type; |
dylanembed123 | 12:e42985e3ea64 | 22 | unsigned int size;// Number of valid bits |
dylanembed123 | 12:e42985e3ea64 | 23 | char data[PACKETSIZE]; |
dylanembed123 | 12:e42985e3ea64 | 24 | unsigned int superPackID;// |
dylanembed123 | 14:6be57da62283 | 25 | char special[4];// Set to FF when |
dylanembed123 | 12:e42985e3ea64 | 26 | }PacketStruct; |
dylanembed123 | 12:e42985e3ea64 | 27 | |
dylanembed123 | 12:e42985e3ea64 | 28 | class PacketSender{ |
dylanembed123 | 12:e42985e3ea64 | 29 | private: |
dylanembed123 | 12:e42985e3ea64 | 30 | unsigned int superID; |
dylanembed123 | 12:e42985e3ea64 | 31 | public: |
dylanembed123 | 12:e42985e3ea64 | 32 | unsigned int getSuperID(){return superID++;} |
dylanembed123 | 13:a6d3cf2b018e | 33 | PacketSender():superID(0),outputDevice( |
dylanembed123 | 13:a6d3cf2b018e | 34 | #ifdef XBEEON |
dylanembed123 | 13:a6d3cf2b018e | 35 | XBEE::getSerial() |
dylanembed123 | 13:a6d3cf2b018e | 36 | #else |
dylanembed123 | 13:a6d3cf2b018e | 37 | USB::getSerial() |
dylanembed123 | 13:a6d3cf2b018e | 38 | #endif |
dylanembed123 | 14:6be57da62283 | 39 | ),next(NULL){} |
dylanembed123 | 12:e42985e3ea64 | 40 | Serial& outputDevice; |
dylanembed123 | 12:e42985e3ea64 | 41 | void sendPacket(PacketStruct& output){ |
dylanembed123 | 12:e42985e3ea64 | 42 | for(int a=0;a<sizeof(PacketStruct);a++){ |
dylanembed123 | 14:6be57da62283 | 43 | while(!outputDevice.writeable()){} |
dylanembed123 | 12:e42985e3ea64 | 44 | outputDevice.putc(((char*)(&output))[a]); |
dylanembed123 | 15:e3e03a9df89e | 45 | //wait_us(10); |
dylanembed123 | 13:a6d3cf2b018e | 46 | //USB::getSerial().putc(((char*)(&output))[a]); |
dylanembed123 | 12:e42985e3ea64 | 47 | } |
dylanembed123 | 14:6be57da62283 | 48 | //wait_ms(100); |
dylanembed123 | 12:e42985e3ea64 | 49 | } |
dylanembed123 | 12:e42985e3ea64 | 50 | unsigned int min(unsigned int a,unsigned int b){ |
dylanembed123 | 12:e42985e3ea64 | 51 | return a<b ? a : b; |
dylanembed123 | 12:e42985e3ea64 | 52 | } |
dylanembed123 | 12:e42985e3ea64 | 53 | void sendPacket(unsigned int superPackID,char* data,unsigned int size,PACKET_TYPE type = PT_END){ |
dylanembed123 | 12:e42985e3ea64 | 54 | if(data!=NULL && size>0){ |
dylanembed123 | 13:a6d3cf2b018e | 55 | for(int i=0;i<=(size-1)/PACKETSIZE;i++){ |
dylanembed123 | 12:e42985e3ea64 | 56 | PacketStruct output; |
dylanembed123 | 12:e42985e3ea64 | 57 | output.type=PT_DEFAULT; |
dylanembed123 | 12:e42985e3ea64 | 58 | output.superPackID=superPackID; |
dylanembed123 | 12:e42985e3ea64 | 59 | output.size=min(PACKETSIZE,size-i*PACKETSIZE); |
dylanembed123 | 14:6be57da62283 | 60 | for(int a=0;a<4;a++){output.special[a]='\0';}output.special[3]=0xAA; |
dylanembed123 | 14:6be57da62283 | 61 | for(int a=0;a<output.size;a++){output.data[a]=data[a-i*PACKETSIZE];} |
dylanembed123 | 13:a6d3cf2b018e | 62 | for(int a=output.size;a<PACKETSIZE;a++){output.data[a]='\0';} |
dylanembed123 | 12:e42985e3ea64 | 63 | sendPacket(output); |
dylanembed123 | 12:e42985e3ea64 | 64 | } |
dylanembed123 | 12:e42985e3ea64 | 65 | }else{ |
dylanembed123 | 12:e42985e3ea64 | 66 | PacketStruct output; |
dylanembed123 | 12:e42985e3ea64 | 67 | output.type=type; |
dylanembed123 | 13:a6d3cf2b018e | 68 | output.size=0; |
dylanembed123 | 12:e42985e3ea64 | 69 | output.superPackID=superPackID; |
dylanembed123 | 14:6be57da62283 | 70 | for(int a=0;a<4;a++){output.special[a]='\0';}output.special[3]=0xAA; |
dylanembed123 | 14:6be57da62283 | 71 | // Check for empty packet |
dylanembed123 | 14:6be57da62283 | 72 | if(output.type==PT_EMPTY){output.type=0;output.size=0;output.superPackID=0;output.special[3]=0xFF;} |
dylanembed123 | 14:6be57da62283 | 73 | for(int a=0;a<PACKETSIZE;a++){output.data[a]='\0';} |
dylanembed123 | 12:e42985e3ea64 | 74 | sendPacket(output); |
dylanembed123 | 12:e42985e3ea64 | 75 | } |
dylanembed123 | 12:e42985e3ea64 | 76 | } |
dylanembed123 | 14:6be57da62283 | 77 | |
dylanembed123 | 14:6be57da62283 | 78 | // Number of consecutive zeros |
dylanembed123 | 14:6be57da62283 | 79 | unsigned int numZeros; |
dylanembed123 | 14:6be57da62283 | 80 | // Return true if a resync command has been received |
dylanembed123 | 14:6be57da62283 | 81 | bool resetCheck(char input){ |
dylanembed123 | 14:6be57da62283 | 82 | if(input=='\0'){ |
dylanembed123 | 14:6be57da62283 | 83 | numZeros++; |
dylanembed123 | 14:6be57da62283 | 84 | }else if(numZeros==sizeof(PacketStruct)-1&&input==0xFF){ |
dylanembed123 | 14:6be57da62283 | 85 | return true; |
dylanembed123 | 14:6be57da62283 | 86 | }else{ |
dylanembed123 | 14:6be57da62283 | 87 | numZeros=0; |
dylanembed123 | 14:6be57da62283 | 88 | } |
dylanembed123 | 14:6be57da62283 | 89 | return false; |
dylanembed123 | 14:6be57da62283 | 90 | } |
dylanembed123 | 14:6be57da62283 | 91 | |
dylanembed123 | 14:6be57da62283 | 92 | // Temperary storage for next valid |
dylanembed123 | 14:6be57da62283 | 93 | PacketStruct* next; |
dylanembed123 | 14:6be57da62283 | 94 | // Number of valid bits in next packet |
dylanembed123 | 14:6be57da62283 | 95 | int nextValid; |
dylanembed123 | 14:6be57da62283 | 96 | /// \brief Grab the next packet |
dylanembed123 | 12:e42985e3ea64 | 97 | PacketStruct* getNextPacket(){ |
dylanembed123 | 14:6be57da62283 | 98 | // Check for null packet |
dylanembed123 | 14:6be57da62283 | 99 | if(next==NULL){next=new PacketStruct();nextValid=0;} |
dylanembed123 | 14:6be57da62283 | 100 | // Create reset packet which resets on re-sync command |
dylanembed123 | 14:6be57da62283 | 101 | bool resetPacket=false; |
dylanembed123 | 14:6be57da62283 | 102 | |
dylanembed123 | 14:6be57da62283 | 103 | // While there is data to read |
dylanembed123 | 14:6be57da62283 | 104 | while(0<outputDevice.readable()){ |
dylanembed123 | 14:6be57da62283 | 105 | // Check if a full packet has been received |
dylanembed123 | 14:6be57da62283 | 106 | if(nextValid==sizeof(PacketStruct))break; |
dylanembed123 | 14:6be57da62283 | 107 | // Read in next char |
dylanembed123 | 14:6be57da62283 | 108 | char input=outputDevice.getc(); |
dylanembed123 | 14:6be57da62283 | 109 | //USB::getSerial().printf("Read ByteC %X %X\n",nextValid,input); |
dylanembed123 | 14:6be57da62283 | 110 | // Check for valid char |
dylanembed123 | 14:6be57da62283 | 111 | if(resetCheck(input)){resetPacket=true;break;} |
dylanembed123 | 14:6be57da62283 | 112 | // Set char |
dylanembed123 | 14:6be57da62283 | 113 | ((char*)next)[nextValid++] = input; |
dylanembed123 | 14:6be57da62283 | 114 | } |
dylanembed123 | 14:6be57da62283 | 115 | |
dylanembed123 | 14:6be57da62283 | 116 | if(nextValid==sizeof(PacketStruct)||resetPacket){ |
dylanembed123 | 14:6be57da62283 | 117 | // Reset packet |
dylanembed123 | 14:6be57da62283 | 118 | PacketStruct* output=next;next=NULL; |
dylanembed123 | 14:6be57da62283 | 119 | // Return |
dylanembed123 | 14:6be57da62283 | 120 | return resetPacket?NULL:output; |
dylanembed123 | 14:6be57da62283 | 121 | } |
dylanembed123 | 14:6be57da62283 | 122 | return NULL; |
dylanembed123 | 14:6be57da62283 | 123 | /* |
dylanembed123 | 12:e42985e3ea64 | 124 | int avail = outputDevice.readable(); |
dylanembed123 | 12:e42985e3ea64 | 125 | if(avail <= 0)return NULL; |
dylanembed123 | 12:e42985e3ea64 | 126 | PacketStruct* output=new PacketStruct(); |
dylanembed123 | 12:e42985e3ea64 | 127 | for(int i=0;i<sizeof(PacketStruct);i++){ |
dylanembed123 | 12:e42985e3ea64 | 128 | // Wait for byte |
dylanembed123 | 12:e42985e3ea64 | 129 | while(outputDevice.readable()<=0){} |
dylanembed123 | 12:e42985e3ea64 | 130 | ((char*)output)[i] = outputDevice.getc(); |
dylanembed123 | 12:e42985e3ea64 | 131 | } |
dylanembed123 | 12:e42985e3ea64 | 132 | return output; |
dylanembed123 | 14:6be57da62283 | 133 | */ |
dylanembed123 | 12:e42985e3ea64 | 134 | } |
dylanembed123 | 12:e42985e3ea64 | 135 | |
dylanembed123 | 12:e42985e3ea64 | 136 | }; |
dylanembed123 | 12:e42985e3ea64 | 137 | static PacketSender* ps=NULL; |
dylanembed123 | 12:e42985e3ea64 | 138 | static PacketSender& getPS(){ |
dylanembed123 | 12:e42985e3ea64 | 139 | if(ps==NULL)ps=new PacketSender(); |
dylanembed123 | 12:e42985e3ea64 | 140 | return *ps; |
dylanembed123 | 12:e42985e3ea64 | 141 | } |