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@12:e42985e3ea64, 2014-04-05 (annotated)
- Committer:
- dylanembed123
- Date:
- Sat Apr 05 22:27:18 2014 +0000
- Revision:
- 12:e42985e3ea64
- Child:
- 13:a6d3cf2b018e
Update packet.h (interface to other c++ code).
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 | 12:e42985e3ea64 | 8 | |
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 | 12:e42985e3ea64 | 16 | PACKET_TYPE 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 | 12:e42985e3ea64 | 27 | PacketSender():superID(0),outputDevice(USB::getSerial()){} |
dylanembed123 | 12:e42985e3ea64 | 28 | Serial& outputDevice; |
dylanembed123 | 12:e42985e3ea64 | 29 | void sendPacket(PacketStruct& output){ |
dylanembed123 | 12:e42985e3ea64 | 30 | for(int a=0;a<sizeof(PacketStruct);a++){ |
dylanembed123 | 12:e42985e3ea64 | 31 | outputDevice.putc(((char*)(&output))[a]); |
dylanembed123 | 12:e42985e3ea64 | 32 | } |
dylanembed123 | 12:e42985e3ea64 | 33 | } |
dylanembed123 | 12:e42985e3ea64 | 34 | unsigned int min(unsigned int a,unsigned int b){ |
dylanembed123 | 12:e42985e3ea64 | 35 | return a<b ? a : b; |
dylanembed123 | 12:e42985e3ea64 | 36 | } |
dylanembed123 | 12:e42985e3ea64 | 37 | void sendPacket(unsigned int superPackID,char* data,unsigned int size,PACKET_TYPE type = PT_END){ |
dylanembed123 | 12:e42985e3ea64 | 38 | if(data!=NULL && size>0){ |
dylanembed123 | 12:e42985e3ea64 | 39 | for(int i=0;i<=size/PACKETSIZE;i++){ |
dylanembed123 | 12:e42985e3ea64 | 40 | PacketStruct output; |
dylanembed123 | 12:e42985e3ea64 | 41 | output.type=PT_DEFAULT; |
dylanembed123 | 12:e42985e3ea64 | 42 | output.superPackID=superPackID; |
dylanembed123 | 12:e42985e3ea64 | 43 | output.size=min(PACKETSIZE,size-i*PACKETSIZE); |
dylanembed123 | 12:e42985e3ea64 | 44 | for(int a=0;a<output.size;a++){ |
dylanembed123 | 12:e42985e3ea64 | 45 | //USB::getSerial().printf(">>%d/%d - %d\n",a,size,output.size); |
dylanembed123 | 12:e42985e3ea64 | 46 | output.data[a]=data[a-i*PACKETSIZE]; |
dylanembed123 | 12:e42985e3ea64 | 47 | } |
dylanembed123 | 12:e42985e3ea64 | 48 | |
dylanembed123 | 12:e42985e3ea64 | 49 | //memcpy(output.data,&(data[i*PACKETSIZE]),min(PACKETSIZE,size-i*PACKETSIZE)); |
dylanembed123 | 12:e42985e3ea64 | 50 | sendPacket(output); |
dylanembed123 | 12:e42985e3ea64 | 51 | } |
dylanembed123 | 12:e42985e3ea64 | 52 | |
dylanembed123 | 12:e42985e3ea64 | 53 | }else{ |
dylanembed123 | 12:e42985e3ea64 | 54 | PacketStruct output; |
dylanembed123 | 12:e42985e3ea64 | 55 | output.type=type; |
dylanembed123 | 12:e42985e3ea64 | 56 | output.superPackID=superPackID; |
dylanembed123 | 12:e42985e3ea64 | 57 | sendPacket(output); |
dylanembed123 | 12:e42985e3ea64 | 58 | } |
dylanembed123 | 12:e42985e3ea64 | 59 | } |
dylanembed123 | 12:e42985e3ea64 | 60 | PacketStruct* getNextPacket(){ |
dylanembed123 | 12:e42985e3ea64 | 61 | int avail = outputDevice.readable(); |
dylanembed123 | 12:e42985e3ea64 | 62 | if(avail <= 0)return NULL; |
dylanembed123 | 12:e42985e3ea64 | 63 | PacketStruct* output=new PacketStruct(); |
dylanembed123 | 12:e42985e3ea64 | 64 | for(int i=0;i<sizeof(PacketStruct);i++){ |
dylanembed123 | 12:e42985e3ea64 | 65 | // Wait for byte |
dylanembed123 | 12:e42985e3ea64 | 66 | while(outputDevice.readable()<=0){} |
dylanembed123 | 12:e42985e3ea64 | 67 | ((char*)output)[i] = outputDevice.getc(); |
dylanembed123 | 12:e42985e3ea64 | 68 | } |
dylanembed123 | 12:e42985e3ea64 | 69 | return output; |
dylanembed123 | 12:e42985e3ea64 | 70 | } |
dylanembed123 | 12:e42985e3ea64 | 71 | |
dylanembed123 | 12:e42985e3ea64 | 72 | }; |
dylanembed123 | 12:e42985e3ea64 | 73 | static PacketSender* ps=NULL; |
dylanembed123 | 12:e42985e3ea64 | 74 | static PacketSender& getPS(){ |
dylanembed123 | 12:e42985e3ea64 | 75 | if(ps==NULL)ps=new PacketSender(); |
dylanembed123 | 12:e42985e3ea64 | 76 | return *ps; |
dylanembed123 | 12:e42985e3ea64 | 77 | } |