Need a Perl Guru.

CptKrf

New Member
Joined
Dec 6, 2022
Messages
7
Reaction score
6
Credits
93
GURU = Anyone who knows more than me. i.e. almost everyone.

I am making good progress in learning Perl, but am stuck on an item that I can't seem to find in the Mastering Perl books. (Or more likely, don't recognize.)

I am doing I/O projects on a PI 4, using the GPIO for all kinds of stuff. The access for the pins via Perl is with the Device-BCM2835 module from CPAN. It works fine.

In it are very verbose calls to do something (set a pin for input/output, read, write, etc. The following is one.

Device::BCM2835::gpio_fsel(&Device::BCM2835::RPI_V2_GPIO_P1_07, $inputcmd)

So far I have been making a separate hardcoded call for each of the pins that I want to set to input, but that is cumbersome and obviously not the proper way to do such. I want to call this routine with a list of pins, not just number 07 or 19 or whatever. In other words, something like this...

$pinno = RPI_V2_GPIO_P1_07;
Device::BCM2835::gpio_fsel(&Device::BCM2835::$pinno, $inputcmd);
Or even better...
$pinno = "07";
Device::BCM2835::gpio_fsel(&Device::BCM2835::RPI_V2_GPIO_P1_ . "$pinno", $inputcmd)

(I know the above is incorrect. I am just showing what I am trying to do.)

I have been through the topics on aliasing and pointers but I am still not seeing the forest for the trees.

Anybody?
 


Alexzee

Well-Known Member
Joined
Jun 1, 2019
Messages
2,915
Reaction score
1,466
Credits
16,263
I'm not good with Perl however; I'm curious and have a question after looking here:

Are you trying to invoke a hardcoded module with perl's -M option?

Is BCM2853 by chance a broadcom chip and your trying to make a call for all the pins in a list that pertains to the module?
 

Alexzee

Well-Known Member
Joined
Jun 1, 2019
Messages
2,915
Reaction score
1,466
Credits
16,263
Last edited:
OP
C

CptKrf

New Member
Joined
Dec 6, 2022
Messages
7
Reaction score
6
Credits
93
I already use Device::BCM2835. It works fine and my code to access the GPIO pins has no problem with it. But, I apparently didn't make my wants clear.

It isn't that I need help in getting the driver to work, but that I don't quite understand the Perl calls that use it.

To change a pin to input from output something like the following is used...

Device::BCM2835::gpio_fsel(&Device::BCM2835::RPI_V2_GPIO_P1_07, $inputcmd)

It works fine, but is hardcoded to Pin 07. If I want to use pin 13 also, I have to include another hardcoded statement...

Device::BCM2835::gpio_fsel(&Device::BCM2835::RPI_V2_GPIO_P1_13, $inputcmd)

Obviously a clunky way to code, but I can't figure out how to include a variable in the statement so that a loop can access one code line with several pin numbers.

So, not a question on how to make it work, but how to make it work like proper Perl code.
 

MattWinter

Active Member
Joined
Dec 5, 2022
Messages
149
Reaction score
190
Credits
1,084
I'm not familiar with Perl either, but most languages have a way of defining a function.
Can you create your own wrapped function that does what you want? One that accepts an array of values corresponding to pin numbers?
 

Alexzee

Well-Known Member
Joined
Jun 1, 2019
Messages
2,915
Reaction score
1,466
Credits
16,263

JasKinasis

Well-Known Member
Joined
Apr 25, 2017
Messages
1,704
Reaction score
2,471
Credits
13,677
It's been a while since I did any serious Perl. I haven't got time to get myself fully back up to speed with Perl right now either. I'm also unfamiliar with the GPIO module. I know what GPIO is and what it does. I'm just unfamiliar with its Perl interface.

But off the top of my head - assuming you're going to use the same command for each GPIO pin ID, you could simply set up an array of pin ID's and then loop through each pin ID and call the gpio_fsel function, passing each pin reference and the command string:
Perl:
my $inputCmd = "Your  command string here" # assuming it is a string?!
my @pinIDs = ( &Device::BCM2835::RPI_V2_GPIO_P1_07, &Device::BCM2835::RPI_V2_GPIO_P1_13 ) # Add any other GPIO pin IDs/references here, comma separated!
foreach my $pinID (@pinIDs) {
    Device::BCM2835::gpio_fsel($pinID, $inputCmd)
}
Something like that.

Otherwise, if you're going to use different commands for different pins, you'd have to set up a data structure that holds a GPIO pin ID and a command and then create an array to contain multiple instances of that data structure. But I can't remember how to define structures in Perl offhand, so I'll skip that for now!
 
MALIBAL Linux Laptops

Linux Laptops Custom Built for You
MALIBAL is an innovative computer manufacturer that produces high-performance, custom laptops for Linux.

For more info, visit: https://www.malibal.com

Members online


Top