public TJImage(TJStartFramePacket newFrame) { this.Rows = newFrame.Rows; this.Columns = newFrame.Cols; this.SegmentLength = newFrame.SegmentLength; if (((int)this.Rows) * ((int)this.Columns) % ((int)this.SegmentLength) > 0) // the last packet won't be complete { this.LastSegmentLength = (UInt16)(((int)this.Rows) * ((int)this.Columns) % ((int)this.SegmentLength)); } else // the last packet is same as previous packets { this.LastSegmentLength = this.SegmentLength; } this.LastSegmentLength = (UInt16)(((int)this.Rows) * ((int)this.Columns) % ((int)this.SegmentLength)); this.ImageArray = new byte[(this.Rows) * (this.Columns)]; }
/// <summary> /// This function is called by the RF controller when data has been received. Here we identify what type of packet /// it is and instantiate an appropriate packet class to hold the data. The packet instance is then forwarded to /// all listeners of the PacketReceived event /// </summary> /// <param name="args"></param> private static void ProcessNewPacket(PacketReceivedEventArgs args) { int now = (int)sw.ElapsedTicks; TJPacket packet = args.Packet; switch (packet.PID) { case 0x00: packet = new TJStatePacket(packet.RawPacket); if (DragonflyStateUpdate != null) DragonflyStateUpdate(packet as TJStatePacket); break; case 0x01: packet = new TJState2Packet(packet.RawPacket); if (DragonflyState2Update != null) DragonflyState2Update(packet as TJState2Packet); break; case 0x02: // Scale factors packets break; case 0x3A: packet = new TJBootloaderResponsePacket(packet.RawPacket); if (DragonflyBootloaderUpdate != null) DragonflyBootloaderUpdate(packet as TJBootloaderResponsePacket); break; case 0x90: packet = new TJCameraPacket(packet.RawPacket); if (DragonflyCameraUpdate != null) DragonflyCameraUpdate(packet as TJCameraPacket); break; case 0x91: packet = new TJStartFramePacket(packet.RawPacket); if (DragonflyStartFrame != null) DragonflyStartFrame(packet as TJStartFramePacket); break; case 0x92: if (DragonflyEndFrame != null) DragonflyEndFrame(); break; default: break; } if (PacketReceived != null) PacketReceived(packet); // The following code is just to keep track of how many packets have been lost. int dif = 2; if (packet.Seq < lastSeqNum) { int seqdif = packet.Seq + 256 - lastSeqNum; if (seqdif != dif) { missed += seqdif / dif; } } else if (packet.Seq - lastSeqNum != dif) { missed += packet.Seq - lastSeqNum; } received++; if (received == 100) { Console.WriteLine("s/Packet: {0:0.00000}, missed: {1}", ((now - lastTime) / (double)received) / Stopwatch.Frequency, missed); lastTime = now; missed = 0; received = 0; } lastSeqNum = packet.Seq; }
/// <summary> /// This method responds to the reception of a "StartFrame" packet by initialization an image array of the appropriate size. /// </summary> void InitializeFrame(TJStartFramePacket frame) { // Turn off button to avoid over-clicking this.Dispatcher.BeginInvoke((Action)delegate() { this.btnGetFrame.IsEnabled = false; }); TJImage newFrame = new TJImage(frame); ImageList.AddLast(newFrame); while (ImageList.Count > MaximumFrameCount) { ImageList.RemoveFirst(); } }