Device-Chip-MAX7219

 view release on metacpan or  search on metacpan

lib/Device/Chip/MAX7219Panel.pm  view on Meta::CPAN


=head2 draw_hline

   $panel->draw_hline( $x1, $x2, $y, $val = 1 )

Draw a horizontal line in the given I<$y> row, between the columns I<$x1> and
I<$x2> (inclusive). If the fourth argument is false, the pixels will be
cleared instead of set.

=cut

method draw_hline ( $x1, $x2, $y, $val = 1 )
{
   $_display[$y][$_] = $val for $x1 .. $x2;
   $_is_display_dirty = 1;
}

=head2 draw_vline

   $panel->draw_vline( $x, $y1, $y2, $val = 1 )

Draw a vertical line in the given I<$x> column, between the rows I<$y1> and
I<$y2> (inclusive). If the fourth argument is false, the pixels will be
cleared instead of set.

=cut

method draw_vline ( $x, $y1, $y2, $val = 1 )
{
   $_display[$_][$x] = $val for $y1 .. $y2;
   $_is_display_dirty = 1;
}

=head2 draw_blit

   $panel->draw_blit( $x, $y, @lines );

Draws a bitmap pattern by copying the data given in lines, starting at the
given position.

Each value in I<@lines> should be a string giving a horizontal line of bitmap
data, each character corresponding to a single pixel of the display. Pixels
corresponding to spaces will be left alone, a hyphen will be cleared, and any
other character (for example a C<#>) will be set.

For example, to draw a rightward-pointing arrow:

   $panel->draw_blit( 6, 1,
      "   #  ",
      "   ## ",
      "######",
      "######",
      "   ## ",
      "   #  " );

=cut

method draw_blit ( $x0, $y, @lines )
{
   for( ; @lines; $y++ ) {
      my @pixels = split m//, shift @lines;
      @pixels or next;

      my $x = $x0;
      for( ; @pixels; $x++ ) {
         my $p = shift @pixels;

         $p eq " " ? next :
         $p eq "-" ? ( $_display[$y][$x] = 0 ) :
                     ( $_display[$y][$x] = 1 );

         $_is_display_dirty = 1;
      }
   }
}

=head2 refresh

   await $panel->refresh;

Sends the framebuffer to the panel chips.

=cut

async method refresh ()
{
   return unless $_is_display_dirty;

   await $self->_all_writereg( REG_SHUTDOWN, 0 );

   my @digits;

   # Write rows in reverse order, so the data appears in the right order if $_rows > 8
   foreach my $row ( reverse 0 .. $_rows-1 ) {
      my $data = "";
      my $v = 0;
      foreach my $col ( 0 .. $_columns-1 ) {
         if( !$_xflip ) {
            $v >>= 1;
            $v |= 0x80 if $_display[$row][$col];
         }
         else {
            $v <<= 1;
            $v |= 1 if $_display[$row][$col];
         }

         $data .= chr( $v ), $v = 0 if $col % 8 == 7;
      }

      # Data should be written final chip first in normal circumstances
      $data = reverse $data unless $_xflip;

      $digits[$row % 8] .= $data;
   }

   foreach my $digit ( 0 .. 7 ) {
      await $self->_write_raw( $_yflip ? $digit : ( 7 - $digit ), $digits[$digit] );
   }

   await $self->_all_writereg( REG_SHUTDOWN, 1 );
   $_is_display_dirty = 0;



( run in 1.635 second using v1.01-cache-2.11-cpan-71847e10f99 )