PiGlow
NAME
RPi::Device::PiGlow - Interface to the PiGlow board using i2c
SYNOPSIS
use RPi::Device::PiGlow;
my $pg = RPi::Device::PiGlow.new();
my $values = [0x01,0x02,0x04,0x08,0x10,0x18,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xA0,0xC0,0xE0,0xFF];
$pg.enable-output;
$pg.enable-all-leds;
$pg.write-all-leds($values);
sleep 10;
$pg.reset;
See the examples directory for more ways of using this.
DESCRIPTION
The PiGlow from Pimoroni
is a small board that plugs in to the Raspberry PI's GPIO header with
18 LEDs on that can be addressed individually via i2c. This module
uses RPi::Device::SMBus to abstract the interface to the device so
that it can be controlled from a Raku programme. It is assumed that
you have installed the OS packages required to make i2c work and have
configured and tested the i2c appropriately. The only difference that
seems to affect the PiGlow device is that it only seems to be reported
by i2cdetect
if you use the "quick write" probe flag:
sudo i2cdetect -y -q 1
(assuming you have a Rev B. Pi - if not you should supply 0 instead of 1.) I have no way of knowing the compatibility of the "quick write" with any other devices you may have plugged in to the Pi, so I wouldn't recommend doing this with any other devices unless you know that they won't be adversely affected by "quick write". The PiGlow has a fixed address anyway so the information isn't that useful.
A useful quick guide to setting up for the Rapberry Pi 2 can be found at https://blog.robseder.com/2015/04/12/getting-a-piglow-to-work-with-a-raspberry-pi-2/ though most of that will work for other versions.
METHODS
method new
The constructor. This takes two optional attributes which are passed on directly to the RPi::Device::SMBus constructor:
i2c-bus-device-path
This sets the device path, it defaults to /dev/i2c-1 (assuming a newer Raspberry PI,) You will want to set this if you are using an older PI or an OS that creates a different device.
i2c-device-address
This sets the i2c device address, this defaults to 0x54. Unless you have somehow altered the address you shouldn't need to change this.
method device-smbus
This is the RPi::Device::SMBus object we will be using to interact with i2c. It will be initialised with the attributes described above. You may want this if you need to do something to the PiGlow I haven't thought of.
method update
This updates the values set to the LED registers to the LEDs and changes the display.
method enable-output
This sets the state of the device to active.
method enable-all-leds
This turns on all three banks of LEDs.
method write-all-leds
method write-all-leds(@values is copy, :$fix) returns Int
This writes the PWM values supplied as an Array and immediately
calls update
to apply the values to the LEDs.
The array must be exactly 18 elements long. If the adverb :fix is
supplied then gamma correction will be applied to the values.
method all-off
This is convenience to turn off (set to brightness 0) all the LEDs at
once. It calls update
immediately.
method set-leds
method set-leds(@leds, Int $value is copy )
This sets the leds specified in the array @leds
( values 0 - 17 to index the LEDs ) all to the single $value
specified.
Gamma adjustment is applied. This does not call update
, this should be
done afterwards in order to update the LED values.
method led-table
This provides a mapping between the logical order of the leds (indexed 0 - 17 ) to the registers that control them. It returns an Array.
method ring-table
The arrangement of the LEDs can be thought of as being arrange logically as 6 "rings". This provides access to the rings indexed 0-5, containing an array of the led numbers in that ring.
method set-ring
method set-ring(Ring $ring, $value)
Sets all of the LEDs in the logical $ring
indexed 0 - 5 to the
$value
specified. Gamma correction is applied to the value.
This isn't immediately applied to the LEDs, update
should be called
after all the changes have been applied.
arm-table
This returns an Array of Array that reference the LEDs in each "arm" of the PiGlow.
method set-arm
method set-arm(Arm $arm, $value )
Sets the LEDs in the specified "arm" of the PiGlow to the specified
value. Value has gamma correction applied. Update isn't applied and
the update
method should be called when all the required updates have
been performed.
method colour-table
This returns a Hash mapping the names of the coloured LEDs to the groups
of LEDs of that colour. The delegate colours
returns the keys,
get-colour-leds
returns the list of LEDs for a named colour.
method get-colour-leds
method get-colour-leds(Colour $colour) returns Array
Returns an array of the LED numbers for the specified Colour.
method set-colour
method set-colour(Colour $colour, $value)
Sets the LEDs in the specified "colour" of the PiGlow to the specified value. Value has gamma correction applied. Update isn't applied and the update method should be called when all the required updates have been performed.
method gamma-table
This is a map of input PWM values (0 - 255) to gamma corrected values that produce a more even range of brightness in the LEDs. The values were lifted from the piglow library for Node.js which in turn borrowed them from elsewhere.
method gamma-fix-values
This applies the gamma adjustment mapping to the supplied Array of values returning the adjusted values as an Array
method reset
Resets the device to its default state. That is to say all LEDs off. It will be necessary to re-enable the groups of LEDs again after calling this.