Exemplo n.º 1
0
    void Awake()
    {
        bufferClient = new BufferClientClock();
        bufferEvents = new List <BufferEvent>();

        latestCapturedSample         = 0;
        newDataIn                    = false;
        dataPacketsLost              = 0;
        lastNumberOfEvents           = 0;
        latestNumebrOfEventsInBuffer = 0;
    }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes the connection to the buffer_bci framework.
        /// </summary>
        public void Initialize()
        {
            clock = new BufferClientClock();
            Header hdr = null;

            while (hdr == null)
            {
                try
                {
                    Console.Write("Connecting to 'localhost:1972'...");
                    clock.Connect("localhost", 1972);
                    Console.WriteLine(" done");

                    if (clock.IsConnected)
                    {
                        Console.WriteLine("GETHEADER");
                        hdr       = clock.GetHeader();
                        lastEvent = hdr.NumEvents;
                    }
                    else
                    {
                        Console.WriteLine("Not connected!");
                    }
                }
                catch
                {
                    hdr = null;
                }

                if (hdr == null)
                {
                    Console.WriteLine("Couldn't read header. Waiting.");
                    Thread.Sleep(5000);
                }
            }

            new Thread(RecvThread).Start();
        }
Exemplo n.º 3
0
        static void ConnectBufferBCI()
        {
            bci_client = new BufferClientClock();
            Header hdr = null;

            while (hdr == null)
            {
                try
                {
                    Console.Write("Connecting to '192.168.0.109:1972'...");
                    bci_client.Connect("192.168.0.109", 1972);
                    Console.WriteLine(" done");

                    if (bci_client.IsConnected)
                    {
                        Console.WriteLine("GETHEADER");
                        hdr       = bci_client.GetHeader();
                        lastEvent = hdr.NumEvents;
                    }
                    else
                    {
                        Console.WriteLine("Not connected!");
                    }
                }
                catch
                {
                    hdr = null;
                }

                if (hdr == null)
                {
                    Console.WriteLine("Couldn't read header. Waiting.");
                    Thread.Sleep(5000);
                }
            }
        }
Exemplo n.º 4
0
        public static void Main(string[] args)
        {
            BufferClientClock client = new BufferClientClock();
            Header            hdr    = null;

            while (hdr == null)
            {
                try
                {
                    client.connect("localhost", 1972);
                    if (client.isConnected())
                    {
                        hdr = client.getHeader();
                    }
                }
                catch
                {
                    hdr = null;
                }

                if (hdr == null)
                {
                    Console.WriteLine("Invalid Header... waiting");
                    Thread.Sleep(1000);
                }
            }
            Console.WriteLine("Connected, press ESC to quit.");
            Console.WriteLine(hdr);

            float axis = 0;

            bool running = true;

            new Thread(() =>
            {
                while (running)
                {
                    lock (client)
                    {
                        client.putEvent(new BufferEvent("AXIS_X", axis, -1));
                    }
                    Thread.Sleep(100);
                }
            }).Start();

            while (running)
            {
                var key = Console.ReadKey();
                if (key.Key == ConsoleKey.Escape)
                {
                    running = false;
                }
                else if (key.Key == ConsoleKey.Q)
                {
                    lock (client)
                    {
                        client.putEvent(new BufferEvent("BTN_FIRE", "down", -1));
                        Console.WriteLine("Pressed down");
                    }
                }
                else if (key.Key == ConsoleKey.W)
                {
                    lock (client)
                    {
                        client.putEvent(new BufferEvent("BTN_FIRE", "up", -1));
                        Console.WriteLine("Pressed up");
                    }
                }
                else if (key.Key == ConsoleKey.E)
                {
                    axis -= 0.2f;
                    Console.WriteLine("Pressed left");
                }
                else if (key.Key == ConsoleKey.R)
                {
                    axis += 0.2f;
                    Console.WriteLine("Pressed right");
                }
            }

            Console.WriteLine();
        }
Exemplo n.º 5
0
    public static int Main(String[] args)
    {
        String hostname = "localhost";
        int    port     = 1972;
        int    timeout  = 5000;

        if (args.Length >= 1)
        {
            hostname = args[0];
        }
        if (args.Length >= 2)
        {
            try {
                port = Convert.ToInt32(args[1]);
            }
            catch {     //  (NumberFormatException e)
                port = 0;
            }
            if (port <= 0)
            {
                System.Console.WriteLine("Second parameter (" + args[1] + ") is not a valid port number.");
                return(1);
            }
        }
        if (args.Length >= 3)
        {
            try {
                port = Convert.ToInt32(args[2]);
            }
            catch {     //  (NumberFormatException e)
                timeout = 5000;
            }
        }

        BufferClientClock C = new BufferClientClock();

        Header hdr = null;

        while (hdr == null)
        {
            try {
                System.Console.Write("Connecting to " + hostname + ":" + port + "...");
                C.connect(hostname, port);
                System.Console.WriteLine("done");
                System.Console.Write("Getting Header...");
                if (C.isConnected())
                {
                    hdr = C.getHeader();
                }
                System.Console.WriteLine("done");
            } catch {     //(IOException e)
                hdr = null;
            }
            if (hdr == null)
            {
                System.Console.WriteLine("Invalid Header... waiting");
                System.Threading.Thread.Sleep(1000);
            }
        }
        System.Console.WriteLine("#channels....: " + hdr.nChans);
        System.Console.WriteLine("#samples.....: " + hdr.nSamples);
        System.Console.WriteLine("#events......: " + hdr.nEvents);
        System.Console.WriteLine("Sampling Freq: " + hdr.fSample);
        System.Console.WriteLine("data type....: " + hdr.dataType);
        for (int n = 0; n < hdr.nChans; n++)
        {
            if (hdr.labels[n] != null)
            {
                System.Console.WriteLine("Ch. " + n + ": " + hdr.labels[n]);
            }
        }

        // Now do the echo-server
        int nEvents = hdr.nEvents;

        endExpt = false;
        while (!endExpt)
        {
            SamplesEventsCount sec = C.waitForEvents(nEvents, timeout);    // Block until there are new events
            if (sec.nEvents > nEvents)
            {
                // get the new events
                BufferEvent[] evs = C.getEvents(nEvents, sec.nEvents - 1);
                //float[][] data = C.getFloatData(0,sec.nSamples-1); // Example of how to get data also
                nEvents = sec.nEvents;// update record of which events we've seen
                // filter for ones we want
                System.Console.WriteLine("Got " + evs.Length + " events");
                for (int ei = 0; ei < evs.Length; ei++)
                {
                    BufferEvent evt     = evs[ei];
                    String      evttype = evt.getType().toString(); // N.B. to*S*tring, not upper case!
                    // only process if it's an event of a type we care about
                    // In our case, don't echo our own echo events....
                    if (!evttype.Equals("echo"))          // N.B. use equals, not == to compare string contents!
                    {
                        if (evttype.Equals("exit"))       // check for a finish event
                        {
                            endExpt = true;
                        }
                        // Print the event to the console
                        System.Console.WriteLine(ei + ") t:" + evt.getType().toString() + " v:" + evt.getValue().toString() + " s:" + evt.sample);
                        // Now create the echo event, with auto-completed sample number
                        // N.B. -1 for sample means auto-compute based on the real-time-clock
                        C.putEvent(new BufferEvent("echo", evt.getValue().toString(), -1));
                    }
                }
            }
            else         // timed out without new events
            {
                System.Console.WriteLine("Timeout waiting for events");
            }
        }
        System.Console.WriteLine("Normal Exit");
        C.disconnect();
        return(0);
    }