Device-PCD8544

 view release on metacpan or  search on metacpan

lib/Device/PCD8544.pm  view on Meta::CPAN

);


sub init
{
    my ($self) = @_;
    return 1 if $self->_was_init_called;
    my $webio = $self->webio;

    foreach ($self->power, $self->rst, $self->dc ) {
        $webio->set_as_output( $_ );
        $webio->output_pin( $_, 1 );
    }

    $webio->spi_set_speed( $self->dev, $self->speed );

    $self->reset;
    $self->_was_init_called( 1 );
    return 1;
}

sub reset
{
    my ($self) = @_;
    my $webio = $self->webio;
    my $power = $self->power;
    my $rst   = $self->rst;

    # Created using suggestions from 'Kuy' on Sparkfun product comments:
    # https://www.sparkfun.com/products/10168
    #
    $webio->output_pin( $power, 1 );
    $webio->output_pin( $rst,   1 );
    Time::HiRes::usleep( 5_000 );

    $webio->output_pin( $rst, 0 );
    Time::HiRes::usleep( 1_000 );

    $webio->output_pin( $rst, 1 );
    Time::HiRes::usleep( 5_000 );

    say "Setting bias (" . SETBIAS . " | " . $self->bias . ")" if DEBUG;
    $self->_send_extended_command( SETBIAS | $self->bias );
    say "Setting contrast (" . SETVOP . " | " . $self->contrast . ")" if DEBUG;
    $self->_send_extended_command( SETVOP  | $self->contrast );
    say "Setting Y Addr (" . SETYADDR . ")" if DEBUG;
    $self->_send_command( SETYADDR | 0x00 );
    say "Setting X Addr (" . SETXADDR . ")" if DEBUG;
    $self->_send_command( SETXADDR | 0x00 );

    return 1;
}

sub set_image
{
    my ($self, $img) = @_;
    $self->_buffer( $img );
    return 1;
}

sub update
{
    my ($self) = @_;
    $self->_send_buffer;
    return 1;
}

sub display_blank
{
    my ($self) = @_;
    $self->_send_command( DISPLAY_CONTROL | DISPLAY_BLANK );
    return 1;
}

sub display_normal
{
    my ($self) = @_;
    $self->_send_command( DISPLAY_CONTROL | DISPLAY_NORMAL );
    return 1;
}

sub display_all_on
{
    my ($self) = @_;
    $self->_send_command( DISPLAY_CONTROL | DISPLAY_ALL_ON );
    return 1;
}

sub display_inverse
{
    my ($self) = @_;
    $self->_send_command( DISPLAY_CONTROL | DISPLAY_INVERTED );
    return 1;
}

sub _send_command
{
    my ($self, $cmd) = @_;
    say "Sending command $cmd" if DEBUG;

    my $webio = $self->webio;
    $webio->output_pin( $self->dc, 0 );

    my $fmt_cmd = pack 'n', $cmd;
    $webio->spi_write( $self->dev, $fmt_cmd );

    return 1;
}

sub _send_extended_command
{
    my ($self, $cmd) = @_;
    say "Sending extended command $cmd {" if DEBUG;
    $self->_send_command( FUNCTIONSET | EXTENDED_INSTRUCTION );
    $self->_send_command( $cmd );
    $self->_send_command( FUNCTIONSET );
    $self->_send_command( DISPLAY_CONTROL | DISPLAY_NORMAL );
    say "}" if DEBUG;
    return 1;
}



( run in 2.088 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )