in reply to Saving Options
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(); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Saving Options
by PilotinControl (Pilgrim) on Dec 20, 2015 at 22:08 UTC |