Author Topic: Updated .Net bindings  (Read 380 times)

stevehayles

  • Newbie
  • *
  • Posts: 12
    • View Profile
Updated .Net bindings
« on: January 29, 2019, 16:42:33 »
Hi,

Been working with the Tinkerforge system as a prototyping platform for a commercial product. Very well designed and easy system to work with but the .Net bindings are tied to some legacy framework versions and quite 'ugly' with the requirement for 'out' variables etc.

I have just released a new set of bindings with three big updates.

Firstly there is full support for ValueTuple returns on all functions with full property naming.  It allows calls like

Code: [Select]
var result = imu.GetAllData();
var accel = result.Acceleration;
var angles = result.EulerAngle;

rather than having to deal with multiple 'out' variables. 

There is also a fully async version of every call implemented using the Async/Await pattern.  The IPConnection class implements all socket send and receive calls asynchronously and there are async overrides of each available call.  The combined effect is that the above example can be written as

Code: [Select]
var resultAsync = await imu.GetAllDataAsync();
var accel = resultAsync.Acceleration;
var angles = resultAsync.EulerAngle;

This code is properly async and not just wrapping synchronous calls in Task.Run().

The last addition is the use of Reactive.Linq and a full set of extension methods to allow the use of IObservable on every brick or bricklet callback.  RX Extensions allow a composable and functional approach to streams of data.

Time shifting of sequences, buffering, throttling and all sorts of manipulations become very easy with a Linq based approach.  The underlying callback event is converted to an IObservable and the disposal of event handlers is all handled automatically.

The example below subscribes to a stream of data coming from the imu but it's 'throttled' to only produce data every 500 milliseconds regardless of the incoming rate from the brick.

Code: [Select]
imu.WhenOrientation()
    .Sample(TimeSpan.FromMilliseconds(500))
    .Subscribe(async v =>
    {
        Console.WriteLine($"Heading: {v.Heading}");
        await Log.WriteAsync($"Roll: {v.Roll}");
    });

The nuget package is available at https://www.nuget.org/packages/FiftyOneNorth.Tinkerforge/ (FiftyOneNorth.Tinkerforge)

The complete set of bricks and bricklets are covered and I will try and maintain compatibility with future releases.  Keen to hear other's experiences and hope it's useful to someone

Thanks,

Steve Hayles
Fifty One North Ltd
« Last Edit: January 29, 2019, 16:44:17 by stevehayles »

borg

  • Administrator
  • Hero Member
  • *****
  • Posts: 3.051
    • View Profile
Re: Updated .Net bindings
« Reply #1 on: January 30, 2019, 10:38:12 »
Sweet, looks good!

The design of the C# bindings is indeed quite old by now and it shows in the API. We try to always maintain backwards compatibility, so it often is hard to add new programming language features over time.

Maybe it would make sense to have some kind of C# bindings 2.0 and maintain the old C# bindings for legacy applications for a while.
« Last Edit: January 30, 2019, 11:35:54 by borg »
Wir sind die Borg, Widerstand ist Spannung durch Stromstärke!

stevehayles

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Updated .Net bindings
« Reply #2 on: January 30, 2019, 11:28:35 »
Hi,

I don't want to 'pollute' your bindings in any way and at present my library is built as a combination of a modified "generate_csharp_bindings.py" python script and then a T4 template to generate all the extension methods.

This could all be done entirely within the current python framework but it's a much bigger change.  Maybe a completely separate "generate_csharp_bindings_V2.py" script with the original untouched would be the way to go ?

borg

  • Administrator
  • Hero Member
  • *****
  • Posts: 3.051
    • View Profile
Re: Updated .Net bindings
« Reply #3 on: January 30, 2019, 11:34:55 »
Yes, we would make a complete separate v2 version. We also would like to add .NET Core support if we do a major update of the C# bindings. For that we would have to exchange some of the internals with .NET Core compatible stuff too.
Wir sind die Borg, Widerstand ist Spannung durch Stromstärke!

stevehayles

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Updated .Net bindings
« Reply #4 on: January 30, 2019, 11:57:10 »
Great move to go to .Net Core, it's the future !

My bindings work very well in a .Net Core environment and I am currently running them on a Raspberry PI to great effect.  I have a custom RPI image running with the .Net Core run-time built in and my next move was to look at the Red Brick and try and do the same there but if you are considering it then I am sure you will do a better job.



 

stevehayles

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Updated .Net bindings
« Reply #5 on: February 04, 2019, 21:47:56 »
Updated to version 1.0.2 with following improvements

Improved callback/continuous callback documentation

Add RemoveCalibration function to Air Quality Bricklet API

stevehayles

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Updated .Net bindings
« Reply #6 on: February 09, 2019, 14:01:37 »
Updated to version 1.0.3 with following improvements

Air quality bricklet - Add API for changing background calibration duration