Example #1
0
        private async void WriteDeviceItemInputParserResult(HidSharp.Reports.Input.DeviceItemInputParser parser)
        {
            if (parser.HasChanged)
            {
                int    valueCount = parser.ValueCount;
                string Value      = "";
                for (int valueIndex = 0; valueIndex < valueCount; valueIndex++)
                {
                    var dataValue = parser.GetValue(valueIndex);
                    var Data      = dataValue.GetLogicalValue().ToString("X2");
                    if (Value == "")
                    {
                        Value += Data;
                    }
                    else
                    {
                        Value += string.Format(" {0}", Data);
                    }
                }

                await Task.Run(() =>
                {
                    var Movement = this.Machine.MovementList.Where(o => Value.IndexOf(o.RecieverTag) != -1).FirstOrDefault();
                    if (Movement != null)
                    {
                        Movement.Value = Value;
                    }
                });
            }
        }
        private void WriteDeviceItemInputParserResult(HidSharp.Reports.Input.DeviceItemInputParser parser)
        {
            while (parser.HasChanged)
            {
                int  changedIndex = parser.GetNextChangedIndex();
                var  dataValue    = parser.GetValue(changedIndex);
                uint key          = dataValue.Usages.FirstOrDefault();
                if (!values.ContainsKey(key))
                {
                    string disp = ((Usage)key).ToString();
                    if (disp.StartsWith("427"))
                    {
                        Console.WriteLine("SKIP: " + disp);
                        continue;
                    }
                    DeviceValue tmp = new DeviceValue();
                    tmp.item    = dataValue.DataItem;
                    tmp.name    = this.name + disp;
                    values[key] = tmp;
                }
                DeviceValue v = values[key];
                v.time = GrafanaQuery.ToUnixTime(DateTime.Now);
                if (v.item.ElementBits == 1)
                {
                    v.value = (dataValue.GetLogicalValue() > 0) ? true : false;
                }
                else
                {
                    v.value = dataValue.GetLogicalValue();
                }

                if (sockets != null)
                {
                    sockets.write(v);
                }
                //Console.WriteLine(v.ToJSON());
            }
        }
        // ==========================================================================================
        public static void PollReports()
        {
            ReportDescriptor reportDescriptor = device.GetReportDescriptor();

            foreach (DeviceItem deviceItem in reportDescriptor.DeviceItems)
            {
                if (device.TryOpen(out HidStream hidStream))
                {
                    hidStream.ReadTimeout = Timeout.Infinite;

                    using (hidStream)
                    {
                        byte[] inputReportBuffer = new byte[device.GetMaxInputReportLength()];
                        HidSharp.Reports.Input.HidDeviceInputReceiver inputReceiver = reportDescriptor.CreateHidDeviceInputReceiver();
                        HidSharp.Reports.Input.DeviceItemInputParser  inputParser   = deviceItem.CreateDeviceItemInputParser();
                        inputReceiver.Start(hidStream);

                        int startTime = Environment.TickCount;
                        while (true)
                        {
                            if (!inputReceiver.IsRunning)
                            {
                                break;
                            }              // Disconnected?

                            Report report; // Periodically check if the receiver has any reports.
                            while (inputReceiver.TryRead(inputReportBuffer, 0, out report))
                            {
                                bool first = true;
                                // Parse the report if possible.
                                // This will return false if (for example) the report applies to a different DeviceItem.
                                if (inputParser.TryParseReport(inputReportBuffer, 0, report))
                                {
                                    while (inputParser.HasChanged || first)
                                    {
                                        first = false;
                                        int changedIndex = inputParser.GetNextChangedIndex();
                                        try
                                        {
                                            DataValue previousDataValue = inputParser.GetPreviousValue(changedIndex);

                                            DataValue dataValue = inputParser.GetValue(changedIndex);
                                            string    Type      = ((Usage)dataValue.Usages.FirstOrDefault()).ToString();
                                            status    _status   = new status((double)previousDataValue.GetPhysicalValue(),
                                                                             (double)dataValue.GetPhysicalValue());

                                            lock (EventBufferLock)
                                            {
                                                if (Type.Contains("Button"))
                                                {
                                                    int btn = Convert.ToByte(Type.Last()) - 49;
                                                    if (_status.newval == 1)
                                                    {
                                                        CurrentButtons = (byte)(CurrentButtons | (1 << btn));
                                                        lastbtn        = btn;
                                                    }
                                                    else
                                                    {
                                                        CurrentButtons = (byte)(CurrentButtons & ~(1 << btn));
                                                    }
                                                }
                                                try
                                                {
                                                    AxisAndButtons[Type] = _status;
                                                }
                                                catch (Exception)
                                                {
                                                    AxisAndButtons.Add(Type, _status);
                                                }
                                            }
                                        }
                                        catch (Exception)
                                        { }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }