The IF EXISTS can be stored in a STATE hash, like so:

#!/usr/bin/perl use warnings; use strict; use Tk; my $mw = new MainWindow(); my %STATE=( 'lights'=>0, 'bell'=>0 ); my %ENABLE=( 'lights'=>144, 'bell'=>136, 'lights&bell' => 144|136, # not sure here 'nothing' => 128 # enable nothing ); my $lights; my $bell; # Exit-Button my $message = $mw->Entry()->pack; $mw->Button(-text=>'End', -command=>sub {exit} )->pack; $lights = $mw->Button(-text=>'Turn LIGHTS on', -command=>sub { # toggle lights $STATE{'lights'} = 1 - $STATE{'lights'}; # can also do != # send data to turn on/off lights calculateInstruction(); $lights->configure(-text => 'Turn LIGHTS '.($STATE{'lights'}?'off' +:'on')); updateMessage(); } )->pack; $bell = $mw->Button(-text=>'Turn BELL on', -command=>sub { # toggle lights $STATE{'bell'} = 1 - $STATE{'bell'}; # can also do != # send data to turn on/off lights calculateInstruction(); $bell->configure(-text => 'Turn BELL '.($STATE{'bell'}?'off':'on') +); updateMessage(); } )->pack; sub calculateInstruction{ my $n; if($STATE{'lights'} && $STATE{'bell'}){ $n = $ENABLE{'lights&bell'} }elsif($STATE{'lights'}){ $n = $ENABLE{'lights'} }elsif($STATE{'bell'}){ $n = $ENABLE{'bell'} }else{ $n = $ENABLE{'nothing'} } my $instruction = "<f 1108 $n>"; print "Would send $instruction to serial\n"; # $port->write($instruction); # $port->lookclear(); } sub updateMessage{ my $msg = 'LIGHTS='.$STATE{'lights'}." BELL=".$STATE{'bell'}; $message->configure(-text => $msg); } MainLoop;

Not sure about the exact codes you need to send, check your manual. Would sending <f 1108 144> then <f 1108 136> turn on both lights and horn? If you send the same command twice, would it toggle the lights? or would this work:

sub calculateInstruction{ my $instruction; if($STATE{'lights'} && $STATE{'bell'}){ $instruction = "<f 1108 144><f 1108 136>" }elsif($STATE{'lights'}){ $instruction = "<f 1108 128><f 1108 144>" }elsif($STATE{'bell'}){ $instruction = "<f 1108 128><f 1108 136>" }else{ $instruction = "<f 1108 128>" } print "Would send $instruction to serial\n"; # $port->write($instruction); # $port->lookclear(); }

If it is bitwise, like cragapito thinks, then this should work:

sub calculateInstruction{ my $n = $ENABLE{'nothing'}; for my $k (keys %STATE){ $n |= $ENABLE{$k} if ($STATE{$k}); } my $instruction = "<f 1108 $n>"; print "Would send $instruction to serial\n"; # $port->write($instruction); # $port->lookclear(); }

In reply to Re: Saving Options by FreeBeerReekingMonk
in thread Saving Options by PilotinControl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.