From c# code to VL patch, using imported dll (Phidget22.NET.dll)

Hei, i have some c# code to read an analog voltage input from a phidget board.
https://www.phidgets.com/?view=api

Now i was wondering how do i patch this code in VL having the phidget dll imported and everything showing up in the nodebrowser? i gave it a try naively but getting nowhere.

I have attached my vl project and also the c# code i am trying to patch. The phidget dll is also included.

The simplest working code as v4 node is this (without the plugin and pindefinitions clutter etc.)

VoltageInput vin = new VoltageInput();

bool init = true;

public void Init()
{
    if (init)
    {                      
        vin.Open();
        init = false;
    }
}

//called when data for any output pin is requested
public void Evaluate(int SpreadMax)
{
    Init();
    FAttached[0] = vin.Attached;
}

How do i say that in VL?
I want to add eventhandlers and stuff later but at first i would like to see at least that “vin.Attached” turns true.

I know this might be difficult to answer when you do not have a phidget board around to try but maybe there is something pretty obvious you can point me to rightaway.

My naive approach that does nothing (also tried assigning create and update wildly, often ending in VL stalling):

phidgetInVL.zip (185.4 KB)

great example, this should really be easy.
i did two things to your patch that i’d say should get it working:

  • insert a pad between Open and Attached because the Create->Open part you only want to do once (on Create), then store the VoltageInput to the pad, then read the Attached state every frame. insert the pad via: SHIFT+dblclick on the link
  • you’ve manually assigned Attached to “Create” as well. rightclick → Assign → Pop it, so it falls back to its default operation (ie. Update)

in alphas this will give you an error because now still the pad is not initialized but it should then look like this and you should save:

to re-run the constructor (Create) you ALT+rightclick (reset) the vvvv wrapper node. now for me this crashes vvvv completely but i’m assuming this is because i don’t have a device attached and you’d have to take some more precautions for that.

hope that helps!

yay! thanks! hope to get back to it tonight and get the rest up running.

more yay ;) do i have to worry about the yellow link, what does that mean?

good. no need to worry about the yellow links in this case. hovering the link with the mouse will show you a warning message. it is about the fact that the VoltageInput type is mutable and in case two nodes downstream would write to it you’d need to be more careful…

just linked from the Create pad to both the Open node and the If region, now the links are not yellow anymore.
like so:

true, because VoltageInput apparently derives from Phidget you’d of course do it like that!

This is really fun and i did some more to the plugin but i got stuck now with the event handling.
Im am not sure what pointer or method this means and what to connect there. When updated, it says the method must not be null. Also not sure how to use the invoke regions from the events-category really.

Codewise i would like it to react on attach-events for example:

VoltageInput vin = new VoltageInput();
vin.Attach += vin_attach; 
        
void vin_attach(object sender, Phidget22.Events.AttachEventArgs e)
        {
            FLogger.Log(LogType.Debug, vin.Attached.ToString());
        }

Thats how it looks now, cool that it really works :)
VVVV.Value.phidget.vl (26.1 KB)

verygood.

now with events the thing is that they are not supported yet by the importer. so for now you’d have to write a node in c# to get to those, see: https://thegraybook.vvvv.org/reference/extending/writing-nodes.html#eventsobservables

hi tgd, until we have automatic event import you need to write a small helper method on C# to convert the event into an observable. for example:

public IOberservable<AttachEventArgs> GetAttachEvent(VoltageInput vIn)
{
    return Observable.FromEventPattern<AttachEventArgsHandler, AttachEventArgs>(
        h => vIn.Attach += h,
        h => vIn.Attach -= h)
        .Select(ep => ep.EventArgs);
}

sorry about that… but this will improve in the future.

ok i see. i will look into. very cool stuff anyway guys! thanks for the help

@tgd meanwhile latest alphas import the events and translate them to Observables. here is how to work with Observables: https://thegraybook.vvvv.org/reference/libraries/reactive.html please start a new topic if you try this again and need more help on it.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.