GPIO inputs and Android

Started by Claus, December 30, 2014, 11:04:36 PM

Previous topic - Next topic

Claus

Hello everybody,
I do some app coding and now I am a little bit surprised...
when a GPIO is configured as in (direction > in)
and external voltage puts value from 0 to 1,
than it is not recognised by java class FileObserver.

The timestamp of file "value" is not touched during its content change.

A20 Tools is using os tool cat.

Does somebody know a way to recognise value changes with a java class?
or/and
How can I enable timestamp of modification for file "value"?

regards
Claus

MBR

I don't know how Java class FileObserver works internally, but there can be two reasons:

  • The FileObserver uses just timestamps (probably via syscal stat), but the GPIO files monitoring requires calling syscals select or (e)poll on them and the GPIO itself must be configured to detect edges, see, for example, https://www.olimex.com/forum/index.php?topic=3026.msg12656. I'm not sure if Java suports calling select/(e)poll somehow, but at least the select and plain poll are part of the POSIX standard, so it probably should.
  • The GPIO itself must support interupts, only if the GPIO directory in /sys/ contains file named edge, then the GPIO supports interrupts. If the file is missing, there is no way to detect status change automatically, only via polling, i.e. repeated reading (with some delay) and comparing.

dave-at-axon

GPIO under Android is quite easy and uses the same C calls as you would with Linux but you need to use the JNI and the Android NDK to build and use them as Java can't directly talk to the drivers.

There is some code on the Internet that I wrote a while back and I have since done some work on it and have now got a library that can do both I2C and GPIO on any Android system.

I'll update my blog soon with the source and build instructions for this but in the mean time, if you send me a PM with your email address I will send you the Java class and JNI code to build the library that you need to work with GPIO. You'll need to download the NDK from the Android development website to use it.