QC Control software

Dependencies:   mbed

Fork of dgps by Colin Stearns

Committer:
dylanembed123
Date:
Sat Apr 19 14:39:19 2014 +0000
Revision:
16:4f5d20b87dc3
Parent:
15:e3e03a9df89e
Child:
18:e72ee7aed088
Update code;

Who changed what in which revision?

UserRevisionLine numberNew 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 16:4f5d20b87dc3 39 ),next(NULL){
dylanembed123 16:4f5d20b87dc3 40 //outputDevice.attach(this,&PacketSender::handleUpdate,Serial::RxIrq);
dylanembed123 16:4f5d20b87dc3 41 lastValid=NULL;
dylanembed123 16:4f5d20b87dc3 42 }
dylanembed123 12:e42985e3ea64 43 Serial& outputDevice;
dylanembed123 12:e42985e3ea64 44 void sendPacket(PacketStruct& output){
dylanembed123 12:e42985e3ea64 45 for(int a=0;a<sizeof(PacketStruct);a++){
dylanembed123 14:6be57da62283 46 while(!outputDevice.writeable()){}
dylanembed123 12:e42985e3ea64 47 outputDevice.putc(((char*)(&output))[a]);
dylanembed123 15:e3e03a9df89e 48 //wait_us(10);
dylanembed123 13:a6d3cf2b018e 49 //USB::getSerial().putc(((char*)(&output))[a]);
dylanembed123 12:e42985e3ea64 50 }
dylanembed123 14:6be57da62283 51 //wait_ms(100);
dylanembed123 12:e42985e3ea64 52 }
dylanembed123 12:e42985e3ea64 53 unsigned int min(unsigned int a,unsigned int b){
dylanembed123 12:e42985e3ea64 54 return a<b ? a : b;
dylanembed123 12:e42985e3ea64 55 }
dylanembed123 12:e42985e3ea64 56 void sendPacket(unsigned int superPackID,char* data,unsigned int size,PACKET_TYPE type = PT_END){
dylanembed123 12:e42985e3ea64 57 if(data!=NULL && size>0){
dylanembed123 13:a6d3cf2b018e 58 for(int i=0;i<=(size-1)/PACKETSIZE;i++){
dylanembed123 12:e42985e3ea64 59 PacketStruct output;
dylanembed123 12:e42985e3ea64 60 output.type=PT_DEFAULT;
dylanembed123 12:e42985e3ea64 61 output.superPackID=superPackID;
dylanembed123 12:e42985e3ea64 62 output.size=min(PACKETSIZE,size-i*PACKETSIZE);
dylanembed123 14:6be57da62283 63 for(int a=0;a<4;a++){output.special[a]='\0';}output.special[3]=0xAA;
dylanembed123 14:6be57da62283 64 for(int a=0;a<output.size;a++){output.data[a]=data[a-i*PACKETSIZE];}
dylanembed123 13:a6d3cf2b018e 65 for(int a=output.size;a<PACKETSIZE;a++){output.data[a]='\0';}
dylanembed123 12:e42985e3ea64 66 sendPacket(output);
dylanembed123 12:e42985e3ea64 67 }
dylanembed123 12:e42985e3ea64 68 }else{
dylanembed123 12:e42985e3ea64 69 PacketStruct output;
dylanembed123 12:e42985e3ea64 70 output.type=type;
dylanembed123 13:a6d3cf2b018e 71 output.size=0;
dylanembed123 12:e42985e3ea64 72 output.superPackID=superPackID;
dylanembed123 14:6be57da62283 73 for(int a=0;a<4;a++){output.special[a]='\0';}output.special[3]=0xAA;
dylanembed123 14:6be57da62283 74 // Check for empty packet
dylanembed123 14:6be57da62283 75 if(output.type==PT_EMPTY){output.type=0;output.size=0;output.superPackID=0;output.special[3]=0xFF;}
dylanembed123 14:6be57da62283 76 for(int a=0;a<PACKETSIZE;a++){output.data[a]='\0';}
dylanembed123 12:e42985e3ea64 77 sendPacket(output);
dylanembed123 12:e42985e3ea64 78 }
dylanembed123 12:e42985e3ea64 79 }
dylanembed123 16:4f5d20b87dc3 80 PacketStruct* lastValid;
dylanembed123 16:4f5d20b87dc3 81 bool found;
dylanembed123 16:4f5d20b87dc3 82 void handleUpdate(){
dylanembed123 16:4f5d20b87dc3 83 USB::getSerial().printf("Interupt\n");
dylanembed123 16:4f5d20b87dc3 84 PacketStruct* next = getNextPacket();
dylanembed123 16:4f5d20b87dc3 85 if(next!=NULL){lastValid=next;}
dylanembed123 16:4f5d20b87dc3 86 }
dylanembed123 14:6be57da62283 87
dylanembed123 14:6be57da62283 88 // Number of consecutive zeros
dylanembed123 14:6be57da62283 89 unsigned int numZeros;
dylanembed123 14:6be57da62283 90 // Return true if a resync command has been received
dylanembed123 14:6be57da62283 91 bool resetCheck(char input){
dylanembed123 14:6be57da62283 92 if(input=='\0'){
dylanembed123 14:6be57da62283 93 numZeros++;
dylanembed123 14:6be57da62283 94 }else if(numZeros==sizeof(PacketStruct)-1&&input==0xFF){
dylanembed123 14:6be57da62283 95 return true;
dylanembed123 14:6be57da62283 96 }else{
dylanembed123 14:6be57da62283 97 numZeros=0;
dylanembed123 14:6be57da62283 98 }
dylanembed123 14:6be57da62283 99 return false;
dylanembed123 14:6be57da62283 100 }
dylanembed123 14:6be57da62283 101
dylanembed123 14:6be57da62283 102 // Temperary storage for next valid
dylanembed123 14:6be57da62283 103 PacketStruct* next;
dylanembed123 14:6be57da62283 104 // Number of valid bits in next packet
dylanembed123 14:6be57da62283 105 int nextValid;
dylanembed123 14:6be57da62283 106 /// \brief Grab the next packet
dylanembed123 12:e42985e3ea64 107 PacketStruct* getNextPacket(){
dylanembed123 14:6be57da62283 108 // Check for null packet
dylanembed123 14:6be57da62283 109 if(next==NULL){next=new PacketStruct();nextValid=0;}
dylanembed123 14:6be57da62283 110 // Create reset packet which resets on re-sync command
dylanembed123 14:6be57da62283 111 bool resetPacket=false;
dylanembed123 14:6be57da62283 112
dylanembed123 14:6be57da62283 113 // While there is data to read
dylanembed123 14:6be57da62283 114 while(0<outputDevice.readable()){
dylanembed123 14:6be57da62283 115 // Check if a full packet has been received
dylanembed123 14:6be57da62283 116 if(nextValid==sizeof(PacketStruct))break;
dylanembed123 14:6be57da62283 117 // Read in next char
dylanembed123 14:6be57da62283 118 char input=outputDevice.getc();
dylanembed123 16:4f5d20b87dc3 119 USB::getSerial().printf("Read ByteC %X %X\n",nextValid,input);
dylanembed123 14:6be57da62283 120 // Check for valid char
dylanembed123 14:6be57da62283 121 if(resetCheck(input)){resetPacket=true;break;}
dylanembed123 14:6be57da62283 122 // Set char
dylanembed123 14:6be57da62283 123 ((char*)next)[nextValid++] = input;
dylanembed123 14:6be57da62283 124 }
dylanembed123 14:6be57da62283 125
dylanembed123 14:6be57da62283 126 if(nextValid==sizeof(PacketStruct)||resetPacket){
dylanembed123 14:6be57da62283 127 // Reset packet
dylanembed123 14:6be57da62283 128 PacketStruct* output=next;next=NULL;
dylanembed123 14:6be57da62283 129 // Return
dylanembed123 14:6be57da62283 130 return resetPacket?NULL:output;
dylanembed123 14:6be57da62283 131 }
dylanembed123 14:6be57da62283 132 return NULL;
dylanembed123 14:6be57da62283 133 /*
dylanembed123 12:e42985e3ea64 134 int avail = outputDevice.readable();
dylanembed123 12:e42985e3ea64 135 if(avail <= 0)return NULL;
dylanembed123 12:e42985e3ea64 136 PacketStruct* output=new PacketStruct();
dylanembed123 12:e42985e3ea64 137 for(int i=0;i<sizeof(PacketStruct);i++){
dylanembed123 12:e42985e3ea64 138 // Wait for byte
dylanembed123 12:e42985e3ea64 139 while(outputDevice.readable()<=0){}
dylanembed123 12:e42985e3ea64 140 ((char*)output)[i] = outputDevice.getc();
dylanembed123 12:e42985e3ea64 141 }
dylanembed123 12:e42985e3ea64 142 return output;
dylanembed123 14:6be57da62283 143 */
dylanembed123 12:e42985e3ea64 144 }
dylanembed123 12:e42985e3ea64 145
dylanembed123 12:e42985e3ea64 146 };
dylanembed123 12:e42985e3ea64 147 static PacketSender* ps=NULL;
dylanembed123 12:e42985e3ea64 148 static PacketSender& getPS(){
dylanembed123 12:e42985e3ea64 149 if(ps==NULL)ps=new PacketSender();
dylanembed123 12:e42985e3ea64 150 return *ps;
dylanembed123 12:e42985e3ea64 151 }