http://qs1969.pair.com?node_id=11125086


in reply to Re^10: Controlling USB on Raspberry Pi
in thread Controlling USB on Raspberry Pi

Thanks afoken - that article was very, very useful and has moved me forward a long way. Probably far enough to complete the project.

I am slightly stuck on the "best way" for a Perl program to interact with the underlying system. I have written a test script that works but not how I originally intended.

#!usr/bin/perl use strict; open my $export, '>', "/sys/class/gpio/export" || die "Unable to open +export"; print $export "20"; close $export; open my $direction, '>', "/sys/class/gpio/gpio20/direction" || die "Un +able to open direction"; print $direction "out\n"; close $direction; open (my $value, '>', "/sys/class/gpio/gpio20/value") || die "Unable t +o open value"; sleep(1); print $value "0"; print "ON\n"; sleep(1); print $value "1"; print "OFF\n"; close $value;

The relay is designed such that it switches ON when the GPIO pin is taken low.

The relay on GPIO 20 switches on when the script starts. 1s later "ON" is written to STD and then a further 1s later, "OFF" is printed and the relay switches off. I would expect the relay to switch on at the same time as "ON" is printed - in other words 1s later than it does. Doing a bit of experimenting shows that it actually switches on not at print $value "0"; but when the direction is set to out at close $direction;. Strangely, it is necessary to set the direction even though it is already set before the script starts and remains set afterwards and the act of setting the direction switches on the relay.

So I have looked at the source code for HiPi which uses a combination of system calls to echo and sysopen. I've also looked at RPi::WiringPi which again uses system but this time calls the RPi utility gpio and also uses `echo.

I've been led to believe that system should not really be necessary in Perl if it is calling something like echo and I would rather implement a more Perlish solution. So some guidance would be very welcome.

If I were to use sysopen instead of open and therefore interact with the filesystem at a lower level, is it likely that I would be able to set the direction without changing the state of the relay at the same time?