Page 1 of 1

Pi-Face + Java + Pi4J

Posted: Mon Feb 25, 2013 1:38 pm
by savageautomate
I just got my hands on a Pi-Face expansion board for my Raspberry Pi. My first objective was to get this board integrated into the Pi4J project and fully working with Java on my Raspberry Pi. The following article (in the link below) covers installing the Pi-Face board, setting up the SPI communication driver, and demonstrates programming the Pi-Face in Java. With the new Pi-Face Java API interface now available in the Pi4J libraries this makes it a breeze to program and work with in your own Java program.

FULL ARTICLE: http://www.savagehomeautomation.com/piface
DEMO VIDEO: http://youtu.be/Q-bihGjJ1lA

Image

Re: Pi-Face + Java + Pi4J

Posted: Tue Mar 05, 2013 10:19 am
by freedomotic
Hi Robert,
congratulation for your project.
I'm waiting my piface board to crete a plugin for our domotic framework.
I have a question: in this topic http://www.raspberrypi.org/phpBB3/viewt ... 3&start=25 a user asked me to control his piggy-back board with our software freedomotic.
I must study better your library, but is it possible to use it with that board?
Thanks in advance
Mauro

Re: Pi-Face + Java + Pi4J

Posted: Tue Mar 05, 2013 11:02 am
by savageautomate
Hi Mauro,

Unfortunately Pi4J does not currently support 1-wire, IR transmitter, IR receiver, CAN interface, Bluetooth, etc. This is a very interesting add-on board and I think Pi4J would be a good fit for these type of communication I/Os, but its just not currently in the project.
http://shop.basis.biz/shop/images/manuf ... n_Rev2.pdf

Thanks,
Robert

Re: Pi-Face + Java + Pi4J

Posted: Tue Mar 05, 2013 12:49 pm
by freedomotic
Thanks for your reply.
We'll adopt another solution based on restapi or xml file.
Regards
Mauro

Re: Pi-Face + Java + Pi4J

Posted: Sun Mar 17, 2013 3:55 pm
by cyberde
I am having some diffuculties with Pi4J and the PiFace, I've setup a listener on a switch which is working perfectly. But it seems like it's not always registering if the switchstate changed.
For example when I press the switch I see that the state changes to LOW, which is perfect, but when I let go of the switch my listener sometimes doesn't get's notified that the state has changed to HIGH again (unless I press the button a couple of more times).

See here:

Code: Select all

RX PIN EVENT INPUT 4 (SWITCH 4) : LOW
RX PIN EVENT INPUT 4 (SWITCH 4) : LOW
RX PIN EVENT INPUT 4 (SWITCH 4) : HIGH
RX PIN EVENT INPUT 4 (SWITCH 4) : LOW
RX PIN EVENT INPUT 4 (SWITCH 4) : LOW
RX PIN EVENT INPUT 4 (SWITCH 4) : HIGH
RX PIN EVENT INPUT 4 (SWITCH 4) : HIGH
RX PIN EVENT INPUT 4 (SWITCH 4) : LOW
RX PIN EVENT INPUT 4 (SWITCH 4) : LOW
RX PIN EVENT INPUT 4 (SWITCH 4) : HIGH
Is there any way I can increase the poll rate or something else to let it respond adequately?

The above is solved, I had multiple classes for different buttons with their own listeners. The only problem was that each class created a PiFace instance. Now that I'm giving the same PiFace instance to all classes in the constructor the problem has been solved.

Also another weird thing is that after the power has been off it doesn't even recognize the PiFace buttons untill i start the piface-emulator perl app or do a pfio.init() in Python.

Is there some sort of way I need to initialize the piface board with java as well?

The above is still unsolved, after powering on the PiFace does not get initialized by Pi4J so I first need to specifically piface/python/demos/toggle.py (or something that does something with the switches) and then my java app works.

If I run the code below after powering on my Pi, then my java app works. Without it, the app never responds to the switches & digital input

Code: Select all

#!/usr/bin/env python

import piface.pfio
import time

piface.pfio.init()

switch = [ piface.pfio.Switch(i) for i in range(0, 4) ]

for i in range(0, 4):
    print switch[i].value

Re: Pi-Face + Java + Pi4J

Posted: Fri Mar 22, 2013 2:56 pm
by savageautomate
There are several lines of initialization code that configure the MCP23S17 chip for use with Pi4J. These all happen in the class constructor. See this code:

https://github.com/Pi4J/pi4j/blob/maste ... .java#L114

If you notice the pin interrupt commands, I suspect these are why you don't see any button changes. We use the interrupt behavior of the chip to determine when pin state changes occur rather than a brute force polling logic.

You could modify this code and create an "initialize()" function that you could call externally as well as gets called by the class constructor and move all this init code there. This would be a good test to see if that fixes the issue.

Robert

Re: Pi-Face + Java + Pi4J

Posted: Sun Mar 24, 2013 3:09 pm
by cyberde
I see, but it should work anyway right? I'm using the folowing code (very simple and removed a lot of stuff), basically it's like this:

Code: Select all

        private final PiFaceSwitch SWITCH = PiFaceSwitch.S1;

        PiFace piface = new PiFaceDevice(PiFace.DEFAULT_ADDRESS, Spi.CHANNEL_0);

        piface.getSwitch(SWITCH).addListener(new SwitchListener()
        {
            @Override
            public void onStateChange(SwitchStateChangeEvent event)
            {
                if (event.getNewState() == SwitchState.ON)
                {
                    System.out.println("Button changed to ON");
                }
            }
        });
It only prints the text when I first run the init script/sample in Perl (which I posted above). The same applies to the piface example: https://github.com/Pi4J/pi4j/blob/maste ... ample.java. It won't just work by running the java code.

The issue I was having with with not registering the button-press sometimes has been solved by not creating more than one PiFaceDevice instance.

Is there some other way to automatically initialize the buttons on the board? All the other things work, like the relays and the output pins/leds.

Re: Pi-Face + Java + Pi4J

Posted: Mon Mar 25, 2013 7:50 pm
by savageautomate
It should initialize on the constructor of 'PiFaceDevice'.
When I was working with mine, I never loaded any Python or Perl code, only Java.
I can retest it on my system in a few days.

-Robert

Re: Pi-Face + Java + Pi4J

Posted: Tue Mar 26, 2013 6:27 pm
by cyberde
savageautomate wrote:It should initialize on the constructor of 'PiFaceDevice'.
When I was working with mine, I never loaded any Python or Perl code, only Java.
I can retest it on my system in a few days.

-Robert
That would be awesome. By the way, everything else works, leds, output pins, relays just not the buttons.
I own a PiFace Digital 2.1 (element14).

Re: Pi-Face + Java + Pi4J

Posted: Tue Mar 26, 2013 8:15 pm
by savageautomate
Just for clarification ... is it just the button events that are not firing?

One thing you could try in the meantime is running these commands to make sure your system is up to date. I think there was an issue with the GPIO eventing is a earlier builds.

sudo apt-get update
sudo apt-get upgrade


Thanks, Robert

Re: Pi-Face + Java + Pi4J

Posted: Wed Mar 27, 2013 7:14 am
by cyberde
Correct, just the button events are not firing (as well as the wire-connectors that go with them). The rest works perfectly.

I've also done the apt-get update/upgrade last week, tonight when I get home I will do it again. But last time it didn't help. I also tried the firmware-update script available, also no avail.

Re: Pi-Face + Java + Pi4J

Posted: Wed May 15, 2013 3:33 pm
by eickler
Hi there,

I was just wondering if there was any followup on the events not firing. Today I also struggled a while with this and after finding this thread, I resolved it through starting the piface-emulator. It happens as well with the examples provided along with the pi4j installation.

Cheers,
André

Re: Pi-Face + Java + Pi4J

Posted: Tue May 28, 2013 6:00 pm
by deciojunior
Hello,

I have the same problem I can not receive events from INPUTS Pi Face with pi4j, the rest works.

Someone found the solution to this problem?
I appreciate a lot if you guys could help me.

Re: Pi-Face + Java + Pi4J

Posted: Sun Jul 21, 2013 7:33 pm
by Asher Waldfogel
Same issue. Inputs work in PiFaceExample.java after I run the Python example, but not before.

BTW, pi4j is the most professionally written library I've seen in this ecosystem. Nice work!

Re: Pi-Face + Java + Pi4J

Posted: Mon Jul 22, 2013 11:38 am
by savageautomate
Hi Guys,

There is some discussion on this over here on the Pi4J groups thread.
User "Pat" was able to get button events working with his Pi4J and PiFace.

https://groups.google.com/forum/#!topic ... lKYpbUXuTI

Thanks, Robert

Re: Pi-Face + Java + Pi4J

Posted: Mon Jul 22, 2013 11:39 am
by savageautomate
Asher Waldfogel wrote:BTW, pi4j is the most professionally written library I've seen in this ecosystem. Nice work!
Thanks for the compliment! :ugeek:

Re: Pi-Face + Java + Pi4J

Posted: Mon Jul 22, 2013 8:41 pm
by Asher Waldfogel
I found a workaround. Read the INTCAPB register (address 0x11) just once at initialization, then all the examples work correctly until the Pi is powered down and powered back on. I'm guessing the Python library reads the INTCAPB register for some reason and explains why pi4j works after running the Python modules.

I found this by adding code to the PiFaceExample.java to read out the chip registers (and see if anything changed before/after running the Python code). After I read the entire bank 0x00 to 0x15 everything worked correctly - so I changed to code to read out a register range. All it takes is to read register 0x11 once after initialization, and the input triggers work normally.

I'm running OS 3.6.11+ (Jun 17 2013 20:49:11) I'm running the soft float ABI to work with Oracle Java 1.7.0_21

Everything else is stock - Pi is ModelB_Rev2 board, PiFace Digital 2.1.

Re: Pi-Face + Java + Pi4J

Posted: Sun Jul 28, 2013 8:55 am
by davidi74
Could you please post an example of how you did this? I was not able to get the inputs working either.

Thank you.
Asher Waldfogel wrote:I found a workaround. Read the INTCAPB register (address 0x11) just once at initialization, then all the examples work correctly until the Pi is powered down and powered back on. I'm guessing the Python library reads the INTCAPB register for some reason and explains why pi4j works after running the Python modules.

I found this by adding code to the PiFaceExample.java to read out the chip registers (and see if anything changed before/after running the Python code). After I read the entire bank 0x00 to 0x15 everything worked correctly - so I changed to code to read out a register range. All it takes is to read register 0x11 once after initialization, and the input triggers work normally.

I'm running OS 3.6.11+ (Jun 17 2013 20:49:11) I'm running the soft float ABI to work with Oracle Java 1.7.0_21

Everything else is stock - Pi is ModelB_Rev2 board, PiFace Digital 2.1.

Re: Pi-Face + Java + Pi4J

Posted: Sun Jul 28, 2013 7:58 pm
by Asher Waldfogel
Little bit of a hack, but try adding these lines of code to PiFaceExample.java just before the first button listener:

byte packet[] = {0x41, 0x13, 0x00};
int result = Spi.wiringPiSPIDataRW(0, packet, 3);

This code reads the GPIOB control register. After you read it once, the original PiFaceExample.java code works on subsequent runs until you power cycle the board.

Re: Pi-Face + Java + Pi4J

Posted: Thu Oct 03, 2013 5:43 am
by savageautomate
Asher,

Thanks for the info you provided. I was able to use that and try to debug the problem.

I have posted a brand new 1.0-SNAPSHOT build that should include this button feedback fix.
https://code.google.com/p/pi4j/downloads/list

Once it's independently confirmed to be working, I'll write more on the details of the underlying issue.

Defect Ticket : https://github.com/Pi4J/pi4j/issues/53

Thanks, Robert

Re: Pi-Face + Java + Pi4J

Posted: Tue Jan 07, 2014 9:10 pm
by suipaste
Hi Robert, this thread looks pretty dead but I can independently confirm that your latest snapshot fixes the problem.

It was a long trail to find the fix from your original tutorial here: http://www.savagehomeautomation.com/piface to the posts in this google group https://groups.google.com/forum/#!topic ... lKYpbUXuTI so it probably wouldn't be a bad idea to update that tutorial to include this new fix.

Also maybe change the stuff about SPI drivers to include the alternate option of enabling them through the configuration menu you get when you boot the pi for the first time.

As for me I'm just chuffed that I've finally got the relays working using java code!

Re: Pi-Face + Java + Pi4J

Posted: Tue May 27, 2014 7:51 pm
by gaetancollaud
Hi,

The 1.0.0-SNAPSHOT fixed the problem for me too.

Thank you very much for this lib by the way. ;) And thank you for the maven repository, it's very useful !

Gaétan