You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@flex.apache.org by James Ong <ya...@gmail.com> on 2012/02/11 14:11:55 UTC

[Native Extension] Interval/Polling response from C to AIR

Hi,

I'm have been experimenting on Native Extension by invoke some C function
from AIR application, example "Hello World return from C" was output
on the application.

In other way, have anyone tried this experiment:
AIR <- Native Extension <- C

An example, the function in C will detect when a musician depress a MIDI
keyboard, in turn, will capture the key and invoke Native Extension so
it will output to the application. As it will be difficult to use MIDI code
for a start, shall anyone know how to create a "windows process" when an AIR
app is launch and will monitoring any response from a computer keyboard (In
this example, we will not using AS3 to capture the keyboard events)?

Thanks.

Re: [Native Extension] Interval/Polling response from C to AIR

Posted by James Ong <ya...@gmail.com>.
Thank Nick, will read it up.
Appreciate.

On Sun, Feb 12, 2012 at 12:34 AM, Nicholas Kwiatkowski <ni...@spoon.as>wrote:

> Sorry, the auto-correct corrected pThreads to gThreads.   pThreads are
> POSIX threads, which is supported on pretty much every operating system.  I
> use it with the ArduinoConnector ANE (if you want to see an example of how
> to use it within an ANE).  A quick google came up with this site that is a
> pretty good doc :
> http://softpixel.com/~cwright/programming/threads/threads.c.php
>
> -Nick
>
> On Sat, Feb 11, 2012 at 11:30 AM, James Ong <ya...@gmail.com> wrote:
>
> > I get it thanks! It can be done on C side. I quite curious about the
> > gthread, is it referring to the libraries that is part of the GTK? Could
> > you advice which starting point I can learn about gThread with some basic
> > tutorials?
> >
>

Re: [Native Extension] Interval/Polling response from C to AIR

Posted by Nicholas Kwiatkowski <ni...@spoon.as>.
Sorry, the auto-correct corrected pThreads to gThreads.   pThreads are
POSIX threads, which is supported on pretty much every operating system.  I
use it with the ArduinoConnector ANE (if you want to see an example of how
to use it within an ANE).  A quick google came up with this site that is a
pretty good doc :
http://softpixel.com/~cwright/programming/threads/threads.c.php

-Nick

On Sat, Feb 11, 2012 at 11:30 AM, James Ong <ya...@gmail.com> wrote:

> I get it thanks! It can be done on C side. I quite curious about the
> gthread, is it referring to the libraries that is part of the GTK? Could
> you advice which starting point I can learn about gThread with some basic
> tutorials?
>

Re: [Native Extension] Interval/Polling response from C to AIR

Posted by James Ong <ya...@gmail.com>.
I get it thanks! It can be done on C side. I quite curious about the
gthread, is it referring to the libraries that is part of the GTK? Could
you advice which starting point I can learn about gThread with some basic
tutorials?

Re: [Native Extension] Interval/Polling response from C to AIR

Posted by Tomislav Pokrajcic <to...@svemir.net>.
Hi,
we made an AIR app that visualizes velocity and position of some high speed motors by using ANE written in C. 
There are PLC and manufacturer provided dll libs involved, but the general principle is that there's a C++ code that creates a monitoring thread when ANE calls its run(params) function.
We poll data from AIR in 20ms intervals and it works pretty nice.
When AIR app closes, it calls ANE function that tells C++ class to clean up memory and close monitoring thread. 
I wasn't involved in writing C++ code and can't tell give you a code snippet for running monitoring thread but I hope this description helped you a bit.
Cheers,

Tomislav


tomislav @ iPhone


On 11. 2. 2012., at 14:11, James Ong <ya...@gmail.com> wrote:

> Hi,
> 
> I'm have been experimenting on Native Extension by invoke some C function
> from AIR application, example "Hello World return from C" was output
> on the application.
> 
> In other way, have anyone tried this experiment:
> AIR <- Native Extension <- C
> 
> An example, the function in C will detect when a musician depress a MIDI
> keyboard, in turn, will capture the key and invoke Native Extension so
> it will output to the application. As it will be difficult to use MIDI code
> for a start, shall anyone know how to create a "windows process" when an AIR
> app is launch and will monitoring any response from a computer keyboard (In
> this example, we will not using AS3 to capture the keyboard events)?
> 
> Thanks.

Re: [Native Extension] Interval/Polling response from C to AIR

Posted by Nicholas Kwiatkowski <ni...@spoon.as>.
The approach I took was to fire an event from C once the internal buffer
went from empty to having some content.  That way I wasn't flooding the AVM
with lots of events that were redundant.  The fastest it
could possibly fire would be once per frame.  The issue I had with the
ArduinoConnector was making sure that the UART didn't flood (which can
happen quick when you only have 2k buffer and a 115200 transmission speed).


Timer calls happen in their own thread, so that is not a bad way to do it
-- I just didn't want to keep diving into my ANE if there was no data.

-Nick

On Sat, Feb 11, 2012 at 12:33 PM, Tomislav Pokrajcic <to...@svemir.net>wrote:

> We didn't experience any problems with frequent ANE polling, but event
> driven approach is probably better for working with MIDI signals.
>
> Our data is changing constantly and much faster than we poll it, so
> working with timer was the only way for us.
>
> However wandering if it would be a better approach to trigger data updates
> by status events from C or have timer in as3 as we did it?

Re: [Native Extension] Interval/Polling response from C to AIR

Posted by Tomislav Pokrajcic <to...@svemir.net>.
We didn't experience any problems with frequent ANE polling, but event driven approach is probably better for working with MIDI signals. 

Our data is changing constantly and much faster than we poll it, so working with timer was the only way for us.

However wandering if it would be a better approach to trigger data updates by status events from C or have timer in as3 as we did it?

Re: [Native Extension] Interval/Polling response from C to AIR

Posted by Nicholas Kwiatkowski <ni...@spoon.as>.
James,

I wouldn't recommend you do any polling or timers to get the data from the
ANE to your as3.  The ANE C/C++ libraries give your the functionality to
fire Events right into the AVM (your as3 code), and you can pick those up.

When the musician presses the key, you should get a MIDI message in your C
code.  You can then store that message into a buffer of some kind, and fire
an Event to let your as3 code know you got something.  Your as3 code would
then have to call a function to pull back that buffer.

I wouldn't use a "Window Process" to collect those MIDI messages.  I would
launch a new thread on the OS to watch for those messages.  You can use a
library like gThreads to spawn a new process and dispatch those events.

-Nick

On Sat, Feb 11, 2012 at 8:11 AM, James Ong <ya...@gmail.com> wrote:

> Hi,
>
> I'm have been experimenting on Native Extension by invoke some C function
> from AIR application, example "Hello World return from C" was output
> on the application.
>
> In other way, have anyone tried this experiment:
> AIR <- Native Extension <- C
>
> An example, the function in C will detect when a musician depress a MIDI
> keyboard, in turn, will capture the key and invoke Native Extension so
> it will output to the application. As it will be difficult to use MIDI code
> for a start, shall anyone know how to create a "windows process" when an
> AIR
> app is launch and will monitoring any response from a computer keyboard (In
> this example, we will not using AS3 to capture the keyboard events)?
>
> Thanks.
>