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@13:a6d3cf2b018e, 2014-04-07 (annotated)
- Committer:
- dylanembed123
- Date:
- Mon Apr 07 01:30:04 2014 +0000
- Revision:
- 13:a6d3cf2b018e
- Parent:
- 12:e42985e3ea64
- Child:
- 14:6be57da62283
Send Image over Xbee
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 | 13:a6d3cf2b018e | 8 | #define XBEEON |
dylanembed123 | 12:e42985e3ea64 | 9 | enum PACKET_TYPE{ |
dylanembed123 | 12:e42985e3ea64 | 10 | PT_DEFAULT=0, |
dylanembed123 | 12:e42985e3ea64 | 11 | PT_END, |
dylanembed123 | 12:e42985e3ea64 | 12 | PT_IMAGE, |
dylanembed123 | 12:e42985e3ea64 | 13 | PT_SIZE |
dylanembed123 | 12:e42985e3ea64 | 14 | }; |
dylanembed123 | 12:e42985e3ea64 | 15 | typedef struct PacketStruct{ |
dylanembed123 | 13:a6d3cf2b018e | 16 | unsigned int type; |
dylanembed123 | 12:e42985e3ea64 | 17 | unsigned int size;// Number of valid bits |
dylanembed123 | 12:e42985e3ea64 | 18 | char data[PACKETSIZE]; |
dylanembed123 | 12:e42985e3ea64 | 19 | unsigned int superPackID;// |
dylanembed123 | 12:e42985e3ea64 | 20 | }PacketStruct; |
dylanembed123 | 12:e42985e3ea64 | 21 | |
dylanembed123 | 12:e42985e3ea64 | 22 | class PacketSender{ |
dylanembed123 | 12:e42985e3ea64 | 23 | private: |
dylanembed123 | 12:e42985e3ea64 | 24 | unsigned int superID; |
dylanembed123 | 12:e42985e3ea64 | 25 | public: |
dylanembed123 | 12:e42985e3ea64 | 26 | unsigned int getSuperID(){return superID++;} |
dylanembed123 | 13:a6d3cf2b018e | 27 | PacketSender():superID(0),outputDevice( |
dylanembed123 | 13:a6d3cf2b018e | 28 | #ifdef XBEEON |
dylanembed123 | 13:a6d3cf2b018e | 29 | XBEE::getSerial() |
dylanembed123 | 13:a6d3cf2b018e | 30 | #else |
dylanembed123 | 13:a6d3cf2b018e | 31 | USB::getSerial() |
dylanembed123 | 13:a6d3cf2b018e | 32 | #endif |
dylanembed123 | 13:a6d3cf2b018e | 33 | ){} |
dylanembed123 | 12:e42985e3ea64 | 34 | Serial& outputDevice; |
dylanembed123 | 12:e42985e3ea64 | 35 | void sendPacket(PacketStruct& output){ |
dylanembed123 | 12:e42985e3ea64 | 36 | for(int a=0;a<sizeof(PacketStruct);a++){ |
dylanembed123 | 12:e42985e3ea64 | 37 | outputDevice.putc(((char*)(&output))[a]); |
dylanembed123 | 13:a6d3cf2b018e | 38 | //USB::getSerial().putc(((char*)(&output))[a]); |
dylanembed123 | 12:e42985e3ea64 | 39 | } |
dylanembed123 | 12:e42985e3ea64 | 40 | } |
dylanembed123 | 12:e42985e3ea64 | 41 | unsigned int min(unsigned int a,unsigned int b){ |
dylanembed123 | 12:e42985e3ea64 | 42 | return a<b ? a : b; |
dylanembed123 | 12:e42985e3ea64 | 43 | } |
dylanembed123 | 12:e42985e3ea64 | 44 | void sendPacket(unsigned int superPackID,char* data,unsigned int size,PACKET_TYPE type = PT_END){ |
dylanembed123 | 12:e42985e3ea64 | 45 | if(data!=NULL && size>0){ |
dylanembed123 | 13:a6d3cf2b018e | 46 | for(int i=0;i<=(size-1)/PACKETSIZE;i++){ |
dylanembed123 | 12:e42985e3ea64 | 47 | PacketStruct output; |
dylanembed123 | 12:e42985e3ea64 | 48 | output.type=PT_DEFAULT; |
dylanembed123 | 12:e42985e3ea64 | 49 | output.superPackID=superPackID; |
dylanembed123 | 12:e42985e3ea64 | 50 | output.size=min(PACKETSIZE,size-i*PACKETSIZE); |
dylanembed123 | 12:e42985e3ea64 | 51 | for(int a=0;a<output.size;a++){ |
dylanembed123 | 12:e42985e3ea64 | 52 | //USB::getSerial().printf(">>%d/%d - %d\n",a,size,output.size); |
dylanembed123 | 12:e42985e3ea64 | 53 | output.data[a]=data[a-i*PACKETSIZE]; |
dylanembed123 | 12:e42985e3ea64 | 54 | } |
dylanembed123 | 13:a6d3cf2b018e | 55 | for(int a=output.size;a<PACKETSIZE;a++){output.data[a]='\0';} |
dylanembed123 | 12:e42985e3ea64 | 56 | //memcpy(output.data,&(data[i*PACKETSIZE]),min(PACKETSIZE,size-i*PACKETSIZE)); |
dylanembed123 | 12:e42985e3ea64 | 57 | sendPacket(output); |
dylanembed123 | 12:e42985e3ea64 | 58 | } |
dylanembed123 | 12:e42985e3ea64 | 59 | |
dylanembed123 | 12:e42985e3ea64 | 60 | }else{ |
dylanembed123 | 12:e42985e3ea64 | 61 | PacketStruct output; |
dylanembed123 | 12:e42985e3ea64 | 62 | output.type=type; |
dylanembed123 | 13:a6d3cf2b018e | 63 | output.size=0; |
dylanembed123 | 12:e42985e3ea64 | 64 | output.superPackID=superPackID; |
dylanembed123 | 13:a6d3cf2b018e | 65 | for(int a=0;a<PACKETSIZE;a++){output.data[a]=a;} |
dylanembed123 | 12:e42985e3ea64 | 66 | sendPacket(output); |
dylanembed123 | 12:e42985e3ea64 | 67 | } |
dylanembed123 | 12:e42985e3ea64 | 68 | } |
dylanembed123 | 12:e42985e3ea64 | 69 | PacketStruct* getNextPacket(){ |
dylanembed123 | 12:e42985e3ea64 | 70 | int avail = outputDevice.readable(); |
dylanembed123 | 12:e42985e3ea64 | 71 | if(avail <= 0)return NULL; |
dylanembed123 | 12:e42985e3ea64 | 72 | PacketStruct* output=new PacketStruct(); |
dylanembed123 | 12:e42985e3ea64 | 73 | for(int i=0;i<sizeof(PacketStruct);i++){ |
dylanembed123 | 12:e42985e3ea64 | 74 | // Wait for byte |
dylanembed123 | 12:e42985e3ea64 | 75 | while(outputDevice.readable()<=0){} |
dylanembed123 | 12:e42985e3ea64 | 76 | ((char*)output)[i] = outputDevice.getc(); |
dylanembed123 | 12:e42985e3ea64 | 77 | } |
dylanembed123 | 12:e42985e3ea64 | 78 | return output; |
dylanembed123 | 12:e42985e3ea64 | 79 | } |
dylanembed123 | 12:e42985e3ea64 | 80 | |
dylanembed123 | 12:e42985e3ea64 | 81 | }; |
dylanembed123 | 12:e42985e3ea64 | 82 | static PacketSender* ps=NULL; |
dylanembed123 | 12:e42985e3ea64 | 83 | static PacketSender& getPS(){ |
dylanembed123 | 12:e42985e3ea64 | 84 | if(ps==NULL)ps=new PacketSender(); |
dylanembed123 | 12:e42985e3ea64 | 85 | return *ps; |
dylanembed123 | 12:e42985e3ea64 | 86 | } |