File: IS31FL3730.pm

package info (click to toggle)
libhipi-perl 0.93-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 20,048 kB
  • sloc: perl: 471,917; ansic: 22; makefile: 10
file content (133 lines) | stat: -rw-r--r-- 3,558 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#########################################################################################
# Package        HiPi::Interface::IS31FL3730
# Description  : Interface to IS31FL3730 matrix LED driver
# Copyright    : Copyright (c) 2018 Mark Dootson
# License      : This is free software; you can redistribute it and/or modify it under
#                the same terms as the Perl 5 programming language system itself.
#########################################################################################

package HiPi::Interface::IS31FL3730;

#########################################################################################

use strict;
use warnings;
use parent qw( HiPi::Interface );
use HiPi qw( :i2c :rpi :fl3730);
use HiPi::RaspberryPi;
use Carp;
use Try::Tiny;

our $VERSION ='0.81';

__PACKAGE__->create_accessors( qw( ) );

use constant {
    REG_CONFIG   => 0x00,
    REG_MATRIX_1 => 0x01,
    REG_MATRIX_2 => 0x0E,
    REG_UPD_COL  => 0x0C,
    REG_LIGHTING => 0x0D,
    REG_PWM      => 0x19,
    REG_RESET    => 0xFF,
};


sub new {
    my ($class, %userparams) = @_;
    my $pi = HiPi::RaspberryPi->new();
    
    my %params = (
        devicename   => ( $pi->board_type == RPI_BOARD_TYPE_1 ) ? '/dev/i2c-0' : '/dev/i2c-1',
        address     => 0x60,
        device      => undef,
        backend     => 'smbus',
    );
    
    # get user params
    foreach my $key( keys (%userparams) ) {
        $params{$key} = $userparams{$key};
    }
    
    unless( defined($params{device}) ) {
        if ( $params{backend} eq 'bcm2835' ) {
            require HiPi::BCM2835::I2C;
            $params{device} = HiPi::BCM2835::I2C->new(
                address    => $params{address},
                peripheral => ( $params{devicename} eq '/dev/i2c-0' ) ? HiPi::BCM2835::I2C::BB_I2C_PERI_0() : HiPi::BCM2835::I2C::BB_I2C_PERI_1(),
            );
        } else {
            require HiPi::Device::I2C;
            $params{device} = HiPi::Device::I2C->new(
                devicename  => $params{devicename},
                address     => $params{address},
                busmode     => $params{backend},
            );
        }
    }
    
    my $self = $class->SUPER::new(%params);
    
    return $self;
}

sub configure {
    my($self, $mask ) = @_;
    $self->send_command( REG_CONFIG, $mask );
}

sub matrix_1_data {
    my($self, @data ) = @_;
    $self->send_command( REG_MATRIX_1, @data );
}

sub matrix_2_data {
    my($self, @data ) = @_;
    $self->send_command( REG_MATRIX_2, @data );
}

sub lighting_effect {
    my($self, $mask) = @_;
    $self->send_command( REG_LIGHTING, $mask );
}

sub brightness {
    my($self, $value) = @_;
    $value //= 127; # undefined get default 127
    my $mask = ( $value > 127 ) ? 0x80 : $value & 0x7F;
    $self->send_command( REG_PWM, $mask );
}

sub update {
    my $self = shift;
    $self->send_command( REG_UPD_COL, 0x00 );
}

sub reset {
    my $self = shift;
    $self->send_command( REG_RESET, 0x00 );
}

sub send_command {
    my($self, $register, @data ) = @_;
    # Timing issue - pullup values if not mounted as pHAT ?
    # Clock too high on RPi 3 ??
    # Need to resolve - but for now catch and retry
    my $continue = 10;
    while( $continue ) {
        try {
            $self->device->bus_write( $register, @data );
            $continue = 0;
        } catch {
            $continue --;
            if( $continue <= 0 ) {
                croak sprintf('IO Error writing to register 0x%X', $register);
            }
            $self->delay(5);
        };
    }
}

1;

__END__