예제 #1
0
파일: HID.cs 프로젝트: sam210723/moRFctrl
 /// <summary>
 /// Handle report received from HID
 /// </summary>
 private void ReportReceived(object sender, EventArgs e)
 {
     // Read report into buffer
     while (reportReceiver.TryRead(reportBuffer, 0, out Report report))
     {
         moRFeus.ParseReport(reportBuffer);
     }
 }
예제 #2
0
        private void ReadLoop()
        {
            if (!inputReceiver.IsRunning)
            {
                return;
            }

            Report report;
            Dictionary <Usage, DataValue> changedIndexes = new Dictionary <Usage, DataValue>();

            for (int i = 0; i < limit && inputReceiver.TryRead(inputReportBuffer, 0, out report); i++)
            {
                if (inputParser.TryParseReport(inputReportBuffer, 0, report))
                {
                    while (inputParser.HasChanged)
                    {
                        int changedIndex = inputParser.GetNextChangedIndex();
                        var dataValue    = inputParser.GetValue(changedIndex);
                        if (dataValue.Usages.Count() > 0)
                        {
                            changedIndexes[(Usage)dataValue.Usages.FirstOrDefault()] = dataValue;
                        }
                    }
                }
            }
            var changedSources = sources.Where(s => s.Refresh(changedIndexes)).ToArray();

            inputChangedEventArgs.Refresh(changedSources);
            if (inputChangedEventArgs.ChangedValues.Any())
            {
                InputChanged?.Invoke(this, inputChangedEventArgs);
            }
        }
예제 #3
0
        private void OnDigitizerInputReceived(object sender, EventArgs e)
        {
            var inputReportBuffer = new byte[_digitizer.GetMaxInputReportLength()];

            while (_hidDeviceInputReceiver.TryRead(inputReportBuffer, 0, out Report report))
            {
                // Parse the report if possible.
                // This will return false if (for example) the report applies to a different DeviceItem.
                if (_hiddeviceInputParser.TryParseReport(inputReportBuffer, 0, report))
                {
                    BridgeFrameBufferPlatform.Threading.Send(() => ProcessEvent());
                }
            }
        }
예제 #4
0
        static void InputReceiverM_Received(object sender, EventArgs e)
        {
            Report report;
            HidDeviceInputReceiver inputReceiver = sender as HidDeviceInputReceiver;

            byte[] inputReportBuffer = new byte[5];
            while (inputReceiver.TryRead(inputReportBuffer, 0, out report))
            {
                if (inputReportBuffer[1] != 0)
                {
                    Console.WriteLine(inputReportBuffer[1]);
                    PageTable.CurrentPage.InputM(inputReportBuffer[1]);
                }
            }
        }
예제 #5
0
            public override Status CommXfer(out byte[] msg_rx, byte[] msg_tx, int rx_size)
            {
                msg_rx = new byte[0];
                if (usb == null)
                {
                    return(Utils.StatusCreate(633));
                }
                try
                {
                    if (msg_tx.Length > 0)
                    {
                        int blocks     = (int)(Math.Ceiling((double)msg_tx.Length / (double)max_output_len));
                        int last_batch = msg_tx.Length % max_output_len;
                        if (last_batch == 0)
                        {
                            last_batch = max_output_len;
                        }
                        for (int i = 0; i < blocks; i++)
                        {
                            usb.Write(msg_tx.Skip(i * max_output_len)
                                      .Take((i < blocks - 1) ? max_output_len :
                                            last_batch).ToArray());
                        }
                    }

                    if (rx_size > 0)  // todo check if parallel processing is needed
                    {
                        List <byte> data_list   = new List <byte>();
                        byte[]      buffer      = Enumerable.Repeat((byte)0xFF, max_input_len).ToArray();
                        var         inputParser = reportDescriptor.DeviceItems[0].CreateDeviceItemInputParser();

                        int       timeout = Const.TIMEOUT_READ;
                        Stopwatch sw      = new Stopwatch();
                        sw.Start();

                        while (timeout > 0)     // read all reports
                        {
                            while (timeout > 0) // read one report (64b)
                            {
                                int remaining = rx_size - data_list.Count;
                                int toread    = (remaining >= max_input_len) ? max_input_len : remaining;

                                if (!inputReceiver.IsRunning)
                                {
                                    return(Utils.StatusCreate(646));
                                }

                                // ANOTHER METHOD TO READ - TODO test
                                //int read = usb.Read(buffer, 0, toread);
                                //if (read >= toread)
                                //{
                                //    data_list.AddRange(buffer.Take(toread));
                                //    if (data_list.Count >= rx_size)
                                //    {
                                //        msg_rx = data_list.Take(rx_size).ToArray();
                                //        return Utils.StatusCreate(0);
                                //    }
                                //    break;
                                //}

                                while (inputReceiver.TryRead(buffer, 0, out HidSharp.Reports.Report input_report))
                                {
                                    if (inputParser.TryParseReport(buffer, 0, input_report))
                                    {
                                        if (inputParser.HasChanged)
                                        {
                                            int valueCount = inputParser.ValueCount;

                                            if (valueCount == 1)
                                            {
                                                DataValue dataValue = inputParser.GetValue(0);
                                                Usage     usage     = (Usage)dataValue.Usages.FirstOrDefault(); // todo process?


                                                data_list.AddRange(buffer.Take(toread));

                                                if (data_list.Count >= rx_size)
                                                {
                                                    msg_rx = data_list.Take(rx_size).ToArray();
                                                    return(Utils.StatusCreate(0));
                                                }
                                            }
                                            //for (int valueIndex = 0; valueIndex < valueCount; valueIndex++)  - TODO test
                                            //{
                                            //    var dataValue = inputParser.GetValue(valueIndex);
                                            //      Console.Write(string.Format("  {0}: {1}",
                                            //      (Usage)dataValue.Usages.FirstOrDefault(), dataValue.GetPhysicalValue()));
                                            //
                                            //}
                                        }
                                    }
                                }

                                if (BSL430NET.Interrupted)
                                {
                                    throw new Bsl430NetException(666);
                                }

                                Task.Delay(5).Wait();
                                timeout -= (int)sw.ElapsedMilliseconds;
                            }
                        }
                        return(Utils.StatusCreate(695));  // timeout
                    }
                    return(Utils.StatusCreate(0));
                }
                catch (Bsl430NetException ex)
                {
                    throw ex;
                }
                catch (Exception ex)
                {
                    return(Utils.StatusCreate(646, ex.Message));
                }
            }