App-Device-Chip-UI

 view release on metacpan or  search on metacpan

lib/App/Device/Chip/UI/GPIOBox.pm  view on Meta::CPAN

      my $hi = ( $_ eq "HI" );

      Tickit::Widget::Button->new(
         label    => $_,
         classes => [ $self->style_classes ],
         on_click => sub {
            my $btn = shift;
            return if $_readmode{$gpio};

            $_protocol->write_gpios( { $gpio => $hi ? 0xFF : 0 } )->get;

            $_->set_style_tag( current => 0 ) for @buttons;
            $btn->set_style_tag( current => 1 );
         },
      )
    } qw( HI LO );

    $check = Tickit::Widget::CheckButton->new(
       label => "read",
       classes => [ $self->style_classes ],
       on_toggle => sub {
          my $check = shift;
          if( $check->is_active ) {
             $_readmode{$gpio} = 1;
             $_->set_style_tag( current => 0 ) for @buttons;
             $_protocol->tris_gpios( [ $gpio ] )->get;
          }
          else {
             $_readmode{$gpio} = 0;
             $_->set_style_tag( read => 0 ) for @buttons;
             $_protocol->write_gpios( { $gpio =>  0 } )->get;
          }
       },
    ) if $dir eq "rw";

    $_readmode{$gpio} = 1 if $dir eq "r";

    return @buttons, $check;
}

method update ()
{
   my @read_gpios = grep { $_readmode{$_} } $_protocol->list_gpios;

   $_protocol->read_gpios( \@read_gpios )->on_done( sub {
      my ( $vals ) = @_;

      foreach my $gpio ( keys %$vals ) {
         next unless $_readmode{$gpio};
         my $bitval = $vals->{$gpio};

         # HI
         $_levelbuttons{$gpio}[0]->set_style_tag(
            read => !!$bitval,
         );
         # LO
         $_levelbuttons{$gpio}[1]->set_style_tag(
            read =>  !$bitval,
         );
      }
   })->get; # Yes, synchronous
}

0x55AA;



( run in 1.111 second using v1.01-cache-2.11-cpan-e1769b4cff6 )