Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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?


In reply to Re^11: Controlling USB on Raspberry Pi by Bod
in thread Controlling USB on Raspberry Pi by Bod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2022-08-18 00:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?