Skip to content

Instantly share code, notes, and snippets.

@MelbourneDeveloper
Created January 31, 2021 01:07
Show Gist options
  • Select an option

  • Save MelbourneDeveloper/c6db4b6f6319165652dc6294249c9a8d to your computer and use it in GitHub Desktop.

Select an option

Save MelbourneDeveloper/c6db4b6f6319165652dc6294249c9a8d to your computer and use it in GitHub Desktop.
Display Temperatur
private static async Task DisplayTemperature()
{
//Connect to the device by product id and vendor id
var temperDevice = await new FilterDeviceDefinition(vendorId: 0x413d, productId: 0x2107, usagePage: 65280)
.CreateWindowsHidDeviceFactory(_loggerFactory)
.ConnectFirstAsync()
.ConfigureAwait(false);
//Create the observable
var observable = Observable
.Timer(TimeSpan.Zero, TimeSpan.FromSeconds(.1))
.SelectMany(_ => Observable.FromAsync(() => temperDevice.WriteAndReadAsync(new byte[] { 0x00, 0x01, 0x80, 0x33, 0x01, 0x00, 0x00, 0x00, 0x00 })))
.Select(data => (data.Data[4] & 0xFF) + (data.Data[3] << 8))
//Only display the temperature when it changes
.Distinct()
.Select(temperatureTimesOneHundred => Math.Round(temperatureTimesOneHundred / 100.0m, 2, MidpointRounding.ToEven));
//Subscribe to the observable
_ = observable.Subscribe(t => Console.WriteLine($"Temperature is {t}"));
//Note: in a real scenario, we would dispose of the subscription afterwards. This method runs forever.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment