/// <summary> /// Stops the read. /// </summary> public void Stop() { if (IsReading && CurReadWaitResult != null && !CurReadWaitResult.IsCompleted && (CounterReader != null || AnalogReader != null)) { try { if (IsCounter) { CounterReader.EndReadMultiSampleUInt32(CurReadWaitResult); } else { AnalogReader.EndReadMultiSample(CurReadWaitResult); } } catch (Exception ex) { /// attempt to end he read failed. } // clear the async result. CurReadWaitResult = null; } IsReading = false; DestroyReaderTask(); DestroyTimebaseTask(); }
/// <summary> /// Async call to read the data. /// </summary> protected void ReadData() { if (IsCounter) { CurReadWaitResult = CounterReader.BeginReadMultiSampleUInt32(SamplesPerReadTick, OnDataRead, null); } else { CurReadWaitResult = AnalogReader.BeginReadMultiSample(SamplesPerReadTick, OnDataRead, null); } }
/// <summary> /// On data was read. /// </summary> /// <param name="rslt"></param> protected void OnDataRead(IAsyncResult rslt) { if (!rslt.IsCompleted || !IsReading) { return; } DataChunk c = null; TimeSpan offset = TimeSpan.FromSeconds(ReadTicks * 1.0 / SamplingFrequency); if (IsCounter) { // data is counter. uint[,] data = CounterReader.EndReadMultiSampleUInt32(rslt); ReadTicks += data.GetLength(1); c = new DataChunk(offset, data); } else { // data is analog. double[,] data = AnalogReader.EndReadMultiSample(rslt); // adding to the read ticks. ReadTicks += data.GetLength(1); c = new DataChunk(offset, data); } // call async again. if (IsReading) { ReadData(); } // No data. if (c.DataCount == 0) { return; } m_dataQ.Enqueue(c); // call to process the queue if needed. DoQueueProcessing(); }
static void TestAnalog() { Console.WriteLine("Wooting Analog reader testing!"); Console.WriteLine($"wooting_kbd_connected: {AnalogReader.IsConnected()}"); Console.WriteLine("Set disconnected cb"); AnalogReader.SetDisconnectedCallback((DisconnectedCallback)dc_cb); Stopwatch watch = new Stopwatch(); watch.Start(); Console.WriteLine("Reading Analog data from the Esc key for 10 seconds. Press any key to start..."); Console.ReadKey(); int lastValue = -1; while (true) { int value = AnalogReader.ReadAnalog(WootingKey.Keys.Esc); if (lastValue != value) { lastValue = value; Console.WriteLine(value); } if (watch.Elapsed.Seconds >= 10) { watch.Stop(); break; } } Console.WriteLine("Going to read the buffer, please press down some keys, starting in 3 seconds"); Thread.Sleep(3000); List <AnalogReader.AnalogRaw> buffer = AnalogReader.ReadFullBuffer(16); Console.WriteLine($"{buffer.Count} items read"); for (int i = 0; i < buffer.Count; i++) { AnalogReader.AnalogRaw raw = buffer[i]; Console.WriteLine($"Scan code: {raw.scan_code}, Value: {raw.analog_value}"); } Console.ReadKey(); }