using System;
using static MAXT.MXFoundation.mxf;
using System.Runtime.InteropServices;
using System.Text;
namespace ar429_example
{
   class ar429_embedded_discrete
   {
      const int BUFFER_SIZE = 4096; 
      
      const int USER_COMMAND_ID_START = 0;
      const int USER_COMMAND_ID_STOP = 1;
      const int C50ms = 50 * 1000 * 1000;
      const int C10ms = 10 * 1000 * 1000;
      static void Main(string[] args)
      {
         UInt32 rc;
         UInt64 server;
         UInt64 device = 0;
         UInt64[] moduleDIO = new UInt64[1];
         UInt64[] module429 = new UInt64[1];
         UInt64 count = 0;
         UInt64[] txDIO = new UInt64[1], rxDIO = new UInt64[1], tx429 = new UInt64[1], rx429 = new UInt64[1];
         UInt64 txDIOBuffer = 0;
         UInt64 tx429Buffer = 0;
         UInt64 rx429Buffer = 0;
         UInt64 dev, mod = 0, port = 0;
         var parameters = new UInt32[3];
         IntPtr rec429 = IntPtr.Zero;
         
#if LOCAL
#else
#endif
         if (rc != MAXT_SUCCESS)
         {
            Console.Write("Failed to connect; rc=0x{0:x8}", rc);
            Console.Write("\nPress a key to terminate\n");
            Console.ReadKey();
            return;
         }
         
         Console.Write("\nStarting\n");
         if (rc == MAXT_SUCCESS)
         
         if (rc == MAXT_SUCCESS)
         
         if (rc == MAXT_SUCCESS)
         
         if (rc == MAXT_SUCCESS && count > 0)
         
         if (rc == MAXT_SUCCESS && count > 0)
         
         if (rc == MAXT_SUCCESS && count == 0)
            rc = MAXT_ERROR_NOT_FOUND;
         
         if (rc == MAXT_SUCCESS)
         
         if (rc == MAXT_SUCCESS && count > 0)
         
         if (rc == MAXT_SUCCESS && count > 0)
         
         if (rc == MAXT_SUCCESS && count == 0)
            rc = MAXT_ERROR_NOT_FOUND;
         
         if (rc == MAXT_SUCCESS)
         
         if (rc == MAXT_SUCCESS)
         if (rc == MAXT_SUCCESS)
         
#if LOOPBACK
         if (rc == MAXT_SUCCESS)
         if (rc == MAXT_SUCCESS)
#endif
         
         if (rc == MAXT_SUCCESS)
         if (rc == MAXT_SUCCESS)
         
         if (rc == MAXT_SUCCESS)
         
         if (rc == MAXT_SUCCESS)
         {
            try
            {
               rec429 = Marshal.AllocHGlobal((int)BUFFER_SIZE);
            }
            catch (OutOfMemoryException)
            {
               rc = MAXT_ERROR_MEM;
            }
         }
         
         if (rc == MAXT_SUCCESS)
         if (rc == MAXT_SUCCESS)
         {
            if (rc == MAXT_SUCCESS)
               Console.Write("\nAcquisition started\n\r");
         }
         
         if (rc == MAXT_SUCCESS)
         
         if (rc == MAXT_SUCCESS)
         if (rc == MAXT_SUCCESS)
         
         if (rc == MAXT_SUCCESS)
         {
            parameters[0] = (UInt32)mod;
            parameters[1] = (UInt32)port;
            parameters[2] = (uint)MXF_TXAPERIODIC_PRIORITY_HIGH;
            
            rc = 
mxfEmbeddedCommandSend(device, USER_COMMAND_ID_START, (UInt64)(parameters.GetLength(0) * 4), parameters);
         }
         if (rc == MAXT_SUCCESS)
         {
            Console.Write("Sending Periodic Pulses\n");
            rc = TXDioSendAperiodicPulses(device, txDIOBuffer);
         }
         
         if (rc == MAXT_SUCCESS)
         
         if (rc == MAXT_SUCCESS)
         if (rc == MAXT_SUCCESS)
            rc = RX429ReadAcquisitionData(rx429Buffer, rec429);
         
         if (rc != MAXT_SUCCESS)
         {
            StringBuilder buffer = new StringBuilder(256);
            {
               buffer.Clear();
               buffer.Append(string.Format("ERROR # 0x{0:x8}", rc));
            }
            Console.Write(buffer + "\n\r");
         }
         Console.Write("Stopping\n");
         
         if (tx429Buffer > 0)
         if (txDIOBuffer > 0)
         if (rx429Buffer > 0)
         if (rec429 != IntPtr.Zero)
            Marshal.FreeHGlobal(rec429);
         
         Console.Write("\nPress enter to terminate\n");
         Console.ReadKey();
         return;
      }
      
      
      
      private static UInt32 EmbeddedMemoryAllocationHandler(UInt64 server, UInt64 cardIndex, UInt64 moduleIndex, UInt64 channelIndex, UInt64 attrib, ref UInt64 value)
      {
         
         UInt64 embedded_memory_size = 65536;  
         if (attrib == KMXF_DEVICE_EMBEDDED_CODEANDDATA_SIZE)
         {
            Console.Write("Reserving {0}K of memory for embedded application\n", embedded_memory_size / 1024);
            value = embedded_memory_size;
            return 1;
         }
         return 0;
      }
      
      
      
      private static UInt32 TXDioSendAperiodicPulses(UInt64 device, UInt64 txDIOBuffer)
      {
         IntPtr recPtr = IntPtr.Zero;
         IntPtr tempPtr = IntPtr.Zero;
         UInt64 timer;
         UInt64 pulseCount;
         UInt32 rc;
         
         if (rc != MAXT_SUCCESS)
            return rc;
         try
         {
            recPtr = Marshal.AllocHGlobal(BUFFER_SIZE);
         }
         catch (OutOfMemoryException)
         {
            rc = MAXT_ERROR_MEM;
         }
         tempPtr = recPtr;
         timer += C50ms;
         dioRec[0].timeTag = timer;
         dioRec[0].control = 0;
         dioRec[0].repeatCount = 1;
         dioRec[0].highDuration = 0;
         dioRec[0].lowDuration = 0;
         dioRec[0].data = 0x0001;
         dioRec[0].edge = 0xFFFF;
         Marshal.StructureToPtr(dioRec[0], tempPtr, true);
         timer += C10ms;
         dioRec[1].timeTag = timer;
         dioRec[1].control = 0;
         dioRec[1].repeatCount = 1;
         dioRec[1].highDuration = 0;
         dioRec[1].lowDuration = 0;
         dioRec[1].data = 0x0000;
         dioRec[1].edge = 0xFFFF;
         Marshal.StructureToPtr(dioRec[1], tempPtr, true);
         
         for (pulseCount = 0; rc == MAXT_SUCCESS && pulseCount < 10; pulseCount++)
         {
            if (rc != MAXT_SUCCESS)
            {
               Console.Write("mxfDiscreteTxAperiodicWrite failed ! rc=0x{0:x8}\n (pulse={1})", rc, pulseCount);
               break;
            }
            tempPtr = recPtr;
            timer += C50ms;
            dioRec[0].timeTag = timer;
            dioRec[0].control = 0;
            Marshal.StructureToPtr(dioRec[0], tempPtr, true);
            timer += C10ms;
            dioRec[1].timeTag = timer;
            dioRec[1].control = 0;
            Marshal.StructureToPtr(dioRec[1], tempPtr, true);
         }
         
         Marshal.FreeHGlobal(recPtr);
         return rc;
      }
      
      
      
      private static UInt32 RX429ReadAcquisitionData(UInt64 rxBuffer, IntPtr rec429)
      {
         IntPtr tempPtr = rec429;
         UInt64 status, msgsCount, bytesCount;
         UInt64 label, sdi, data, ssm, parity;
         UInt64 j;
         UInt32 rc;
         
         rc = 
mxfA429RxAcqRead(rxBuffer, 0, BUFFER_SIZE, out status, out msgsCount, out bytesCount, rec429);
         for (j = 0; j < msgsCount && rc == MAXT_SUCCESS; j++)
         {
            Console.Write("{0:00}: Timetag {1} - ARINC word=[{2},{3},{4:X5},{5},{6:000}]\n",
                     j, rec.
timeTag, parity, ssm, data, sdi, label);
         }
         return rc;
      }
   }
}