/// <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; }
private void CheckBootloaderStatus(TJBootloaderResponsePacket obj) { //Console.WriteLine("MCU Response"); MCUResponded = true; // Handle send last and send next if (obj.SendNextLine == 1) MCURequestNext = true; }