snooty
Posts: 24
Joined: Thu Nov 22, 2012 3:21 am

pi4j simple for loop

Wed Dec 05, 2012 9:16 pm

Hi everyone,

Can someone please put me out of my misery please, I haven't been near plain java in about 5 years and I'm trying to alter the pi4j trigger example http://pi4j.com/example/trigger.html a little bit to pulse the led 4 times. I was just trying to insert a for loop to do this but I can't succeed.

Changing this:

Code: Select all

myButton.addTrigger(new GpioPulseStateTrigger(PinState.HIGH, myLed[2], 1000));
To something like this:

Code: Select all

myButton.addTrigger(for (int i = 0; i < 4; i++){new GpioPulseStateTrigger(PinState.HIGH, myLed[2], 1000);});
Any help would be great :cry:

BobbyBob
Posts: 6
Joined: Fri Dec 07, 2012 11:56 am

Re: pi4j simple for loop

Fri Dec 07, 2012 12:08 pm

Hello,

Unfortunately I haven't got my raspberry pi setup with pi4j, but I can help with a general java question.

You have modified the original example in a Lambda style which java don't support, see http://en.wikipedia.org/wiki/Anonymous_function.

Something like this should work instead

for (int i = 0; i < 4; i++){
myButton.addTrigger(new GpioPulseStateTrigger(PinState.HIGH, myLed[2], 1000));
}

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: pi4j simple for loop

Fri Dec 07, 2012 4:18 pm

Rather than using pulse triggers, you maybe better off to just create a simple listener on the button or use a callback trigger instead. Then in the listener or callback method just add the code to pulse the LED 4 times. Using the 4 pulse triggers, I don't think they will work sequentially. The pulse trigger executions will enqueue 4 pulse tasks in a scheduled executor service where they will be worked off in a thread pool. So its likely they will attempt to pulse in parallel which would have undesired results. That said, you also cannot just call four sequential .pulse() methods in your code as they too will be executed in the thread pool. The intent of the pulse() method was to be a non-blocking call so you program can continue and the pulse task is carried out in the background. There is a new blink() method that may work better for your needs.

Here is a simple listener example:
http://pi4j.com/example/listener.html

We have also added a GpioPinDigitalOutput.blink(delay,duration) function where you can specify the pulse duration and then amount of time the LED should remain blinking. You could use this single call to pulse the LED four times based on the provided timing arguments.

Here is a simple blink example:
https://github.com/Pi4J/pi4j/blob/maste ... ample.java

There is also a new GpioBlinkStateTrigger class; however it does not currently support the blink duration parameter to tell the blink operation to stop after the elapsed time, so this could be added to better support your goal with a blink trigger, but as-is it won't work for your goal.

Thanks, Robert
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

snooty
Posts: 24
Joined: Thu Nov 22, 2012 3:21 am

Re: pi4j simple for loop

Fri Dec 07, 2012 6:58 pm

Thanks for the replies and the education. In the meantime I went with the simple option avoiding loops, shown below.

Code: Select all

fiveInput.addTrigger(new GpioCallbackTrigger(PinState.HIGH,new Callable<Void>()
        {        
            public Void call() throws Exception
            {
                System.out.println(" --> Received 5 input, outputting 5 x 1 outputs ");
                
                        //1
                        oneOut.high();
                        Thread.sleep(100);
                        oneOut.low();
                        Thread.sleep(100);
                        System.out.println(" --> 1 ");
                        
                       //same as above for 2,3,4

                        //5
                        oneOut.high();
                        Thread.sleep(100);
                        oneOut.low();
                        Thread.sleep(500);
                        System.out.println(" --> 5 - Done, Thanks! ");
                        
                return null;
            }
        }));
As suggested looping pulses did not work as expected, all I really needed to do was multiply the input pulses and output them, I know it looks awful and is inefficient but the above works fine so I'm happy with that :lol:

I will also have a look the blink() method you linked me to Robert as it seems to be ideal, am I right in saying pi4j is your creation? Thanks for sharing it, it's nice to be working with code (very) vaguely familiar to access the gpio.

Next step is to record the in and out's and display in a gui...... more struggles.

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: pi4j simple for loop

Sat Dec 08, 2012 9:39 am

snooty wrote:I will also have a look the blink() method you linked me to Robert as it seems to be ideal, am I right in saying pi4j is your creation? Thanks for sharing it, it's nice to be working with code (very) vaguely familiar to access the gpio.
I started the project, but there have been other contributors .. so I don't want to take all the credit :-)

I am responsible for most of the GPIO work that builds on top of Gordon's WiringPi project.
Glad the project has been helpful for you. There are still a lot of things planned for the project, so it will continue to evolve.

Thanks, Robert
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

User avatar
savageautomate
Posts: 225
Joined: Thu Aug 16, 2012 3:20 pm
Location: USA
Contact: Website

Re: pi4j simple for loop

Tue Dec 18, 2012 3:22 pm

Hi snooty,

Just an update to let you know that as of version 0.0.4, Pi4J now does support a blocking pulse:
http://pi4j.com/apidocs/com/pi4j/io/gpi ... 20boolean)

GpioPinDigitalOutput.pulse(long duration, boolean blocking)
GpioPinDigitalOutput.pulse(long duration, PinState pulseState, boolean blocking)

Pi4J Version 0.0.4 release announcement >>
http://www.savagehomeautomation.com/pro ... eased.html
Robert Savage | Follow me @savageautomate
http://www.pi4j.com | http://www.pislices.com
http://www.savagehomeautomation.com

Return to “Other programming languages”