private void CreateSendingQueue() { if (sendingQueue != null) { return; } sendingQueue = new QueueWorker <Wrapper <XInputReport> >(1, (report) => { if (report != null) { DeviceReportEvent threadSafeEvent = DeviceReport; threadSafeEvent?.Invoke(report.Value); } }, $"ReportThread {DevicePath}"); }
private bool verifyChecksum() { self_checksumOK = false; if (rx_buffer_len > 6) { byte flipped67 = currentBitIndex == 1 ? bitreverse((byte)((_MergedGlobals__rx_buffer[6] << 1) | (_MergedGlobals__rx_buffer[7] & 0x01))) : bitreverse((byte)((_MergedGlobals__rx_buffer[6] << 5) | (_MergedGlobals__rx_buffer[7] & 0x1F))); byte N = 0; for (int i = 0; i < 7; i++) { byte val = _MergedGlobals__rx_buffer[i]; if (i == 6) { val = (byte)(_MergedGlobals__rx_buffer[i] & 0xF8); } N ^= val; } byte flippedN = bitreverse(N); byte flipped3 = bitreverse((byte)(_MergedGlobals__rx_buffer[3])); byte flipped2 = bitreverse((byte)(_MergedGlobals__rx_buffer[2])); if (currentBitIndex == 1) { self_receivedChecksum = (byte)((flipped67 >> 4) & 0xF); self_expectedChecksum = (byte)(chksumindex[(byte)(flipped2 ^ flipped3 ^ chksumindex[flippedN])] >> 4); } else { self_receivedChecksum = flipped67; self_expectedChecksum = chksumindex[(byte)(flipped2 ^ flipped3 ^ chksumindex[flippedN])]; } self_checksumOK = self_receivedChecksum == self_expectedChecksum; { //finishedPacket(); DeviceReportEvent threadSafeEvent = DeviceReport; threadSafeEvent?.Invoke(new GenericBytesReport() { CodeString = "SXTYBEAT", ReportBytes = _MergedGlobals__rx_buffer }); // TODO: consider a `60BT` report type instead //Console.WriteLine(); } } return(self_checksumOK); }
public void StartReading() { lock (readingLock) { if (DeviceReport == null) { reading = false; } if (reading) { return; } reading = true; sendingQueue = new QueueWorker <Wrapper <HidReport> >(5, (report) => { if (report != null) { DeviceReportEvent threadSafeEvent = DeviceReport; threadSafeEvent?.Invoke(report.Value); } }, $"ReportThread {GetUniqueKey(DevicePath)}"); readingThread = new Thread(() => { while (reading) { if (DeviceReport == null) { break; } try { HidSharp.HidStream _stream = GetStream(); lock (_stream) { byte[] data = _stream.Read(); //DeviceReportEvent threadSafeEvent = DeviceReport; ////new Thread(() => ////{ //// threadSafeEvent?.Invoke(new HidReport() { ReportId = data[0], ReportType = HidReportType.Input, ReportBytes = data.Skip(1).ToArray() }); ////}).Start(); // //ThreadPool.QueueUserWorkItem((stateInfo) => //{ // threadSafeEvent?.Invoke(new HidReport() { ReportId = data[0], ReportType = HidReportType.Input, ReportBytes = data.Skip(1).ToArray() }); //}); sendingQueue.EnqueueTask(new HidReport() { ReportId = data[0], ReportType = HidReportType.Input, ReportBytes = data.Skip(1).ToArray() }); } if (PollingRate > 0) { int SleepTime = 0; while (SleepTime < (PollingRate / 1000)) { Thread.Sleep(1000); SleepTime++; } Thread.Sleep(PollingRate % 1000); } } catch (System.TimeoutException) { } catch (Exception) // for example System.IO.IOException: 'Operation failed after some time.' { reading = false; } } reading = false; }); readingThread.Start(); } }