NAME

RPi::WiringPi::Util - Utility methods for RPi::WiringPi Raspberry Pi interface

DESCRIPTION

WARNING: Until version 1.00 is released, the API and other functionality of this module may change, and things may break from time-to-time.

This module contains various utilities for RPi::WiringPi that don't necessarily fit anywhere else. It is a base class, and is not designed to be used independently.

METHODS

pin_scheme()

Returns the current pin mapping in use. Returns 0 for wiringPi scheme, 1 for GPIO, 2 for System GPIO, 3 for physical board and -1 if a scheme has not yet been configured (ie. one of the setup*() methods has not yet been called.

If using RPi::WiringPi::Constant, these map out to:

0  => RPI_MODE_WPI
1  => RPI_MODE_GPIO
2  => RPI_MODE_GPIO_SYS
3  => RPI_MODE_PHYS
-1 => RPI_MODE_UNINIT

pin_map($scheme)

Returns a hash reference in the following format:

$map => {
    phys_pin_num => pin_num,
    ...
};

If no scheme is in place or one isn't sent in, return will be an empty hash reference.

Parameters:

$scheme

Optional: By default, we'll check if you've already run a setup routine, and if so, we'll use the scheme currently in use. If one is not in use and no $scheme has been sent in, we'll return an empty hash reference, otherwise if a scheme is sent in, the return will be:

For 'wiringPi' scheme:

$map = {
    phys_pin_num => wiringPi_pin_num,
    ....
};

For 'GPIO' scheme:

$map = {
    phys_pin_num => gpio_pin_num,
    ...
};

pin_to_gpio($pin, $scheme)

Converts the specified pin from the specified scheme (RPI_MODE_WPI (wiringPi), or <RPI_MODE_PHYS> (physical board numbering scheme) to the GPIO number format.

If $scheme is not sent in, we'll attempt to fetch the scheme currently in use and use that.

Example:

my $num = pin_to_gpio(6, RPI_MODE_WPI);

That will understand the pin number 6 to be the wiringPi representation, and will return the GPIO representation.

wpi_to_gpio($pin_num)

Converts a pin number from wiringPi notation to GPIO notation.

Parameters:

$pin_num

Mandatory: The wiringPi representation of a pin number.

phys_to_gpio($pin_num)

Converts a pin number as physically documented on the Raspberry Pi board itself to GPIO notation, and returns it.

Parameters:

$pin_num

Mandatory: The pin number printed on the physical Pi board.

export_pin($pin_num)

Exports a pin. Only needed if using the setup_sys() initialization method.

Pin number must be the GPIO pin number representation.

unexport_pin($pin_num)

Unexports a pin.

Pin number must be the GPIO pin number representation.

registered_pins()

Returns an array of RPi::WiringPi::Pin objects that are currently registered, and deemed to be in use.

register_pin($pin_obj)

Registers a pin within the system for error checking, and proper resetting of the pins in use when required.

Parameters:

$pin_obj

Mandatory: An object instance of RPi::WiringPi::Pin class.

unregister_pin($pin_obj)

Pretty much the opposite of register_pin(), however we do not destroy the pin object, we simply reset it to INPUT mode, LOW state and disable any pull up/down resistors that may have been set.

ENVIRONMENT VARIABLES

There are certain environment variables available to aid in testing on non-Raspberry Pi boards.

NO_BOARD

Set to true, will bypass the wiringPi board checks. False will re-enable them.

IMPORTANT NOTES

- wiringPi must be installed prior to installing/using this module.
- By default, we use wiringPi's interpretation of GPIO pin mapping. See new method to change this behaviour.
- This module hijacks fatal errors with $SIG{__DIE__}, as well as $SIG{INT}. This is so that in the case of a fatal error, the Raspberry Pi pins are never left in an inconsistent state. By default, we trap the die(), reset all pins to their default (INPUT, LOW), then we exit(). Look at the fatal_exit param in new() to change the behaviour.

cleanup()

Resets all registered pins back to default settings (off). It's important that this method be called in each application.

AUTHOR

Steve Bertrand, <steveb@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2016 by Steve Bertrand

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.18.2 or, at your option, any later version of Perl 5 you may have available.