--- tclpuks/prgsrc/puks.tcl 2005/08/29 19:28:13 1.5 +++ tclpuks/prgsrc/puks.tcl 2005/08/31 17:04:31 1.6 @@ -9,8 +9,9 @@ set waiting_for_key 0 set question_asked 0 set timer_started 0 -# Channel to talk to the MRC device -set MRC 0 +# Channel to talk to the MCR device +set MCR 0 +set MCR_DEVICE "/dev/ttyUSB0" # Mapping of MCR codes to keys: button(code) set buttoncode 256 @@ -21,41 +22,69 @@ for {set i 1} {$i <=8} {incr i} { ############################################################## -# Procedures for talking with MRC +# Procedures for talking with MCR ############################################################## -# Open the given device for MRC. Return 0 if successful, 1 otherwise -proc open_mrc {device} { - global MRC - set result [catch {set MRC [open $device r+]}] +# Open the given device for MCR. Return 0 if successful, 1 otherwise +proc open_mcr {device} { + global MCR + set result [catch {set MCR [open $device r+]} error] if {$result !=0} { + .status.mcr configure -text $error + .status.mcr configure -fg red return $result } - fconfigure $MRC -blocking 0 -translation binary \ - -encoding binary -buffering none -mode 57600,n,8,1 + fconfigure $MCR -blocking 0 -translation binary \ + -encoding binary -buffering none +#-mode 57600,n,8,1 + .status.mcr configure -text "Opened $device" + .status.mcr configure -fg black - puts $MRC s + puts $MCR s after 200 - read $MRC - puts $MRC r + read $MCR + puts $MCR r after 200 - read $MRC + read $MCR return 0 } -# Return the key pressed or 0 +# Return the list of key pressed or empty list proc readbuffer {} { - global MRC button - set key 0 - set message [read $MRC] - binary scan $message s key - set key [expr $key & 0xFFFF] - if {[catch {set key $button($key)}] == 0} { - return $key + global MCR button + set keys "" + set message [read $MCR 6] + while { [string length $message] > 0} { + binary scan $message b48 msg + .status.mcr -configure -text $msg + .status.mcr -configure -fg black + binary scan $message s key + set key [expr $key & 0xFFFF] + if {[catch {set key $button($key)}] == 0} { + lappend keys $key + } + set message [read $MCR 6] } - return 0 + return keys +} + +# Dialog for opening the device +proc open_mcr_dialog {} { + toplevel .mcr_dialog + label .mcr_dialog.label -text "Open MCR port:" + entry .mcr_dialog.entry -textvariable MCR_DEVICE + pack .mcr_dialog.label .mcr_dialog.entry + frame .mcr_dialog.buttons + pack .mcr_dialog.buttons + button .mcr_dialog.buttons.ok -command { + open_mcr $MCR_DEVICE + destroy .mcr_dialog + } -text OK + button .mcr_dialog.buttons.cancel -command {destroy .mcr_dialog}\ + -text "Cancel" + pack .mcr_dialog.buttons.ok .mcr_dialog.buttons.cancel -side left } ############################################################## @@ -69,6 +98,7 @@ proc readbuffer {} { menu .mbar -type menubar -relief ridge -borderwidth 2 pack .mbar -fill x + menubutton .mbar.file -text File -menu .mbar.file.m -underline 0 \ -relief raised -borderwidth 2 pack .mbar.file -side left @@ -83,6 +113,12 @@ menubutton .mbar.edit -text Edit -underl -relief raised -borderwidth 2 pack .mbar.edit -side left +menubutton .mbar.mcr -text "MCR" -menu .mbar.mcr.m -underline 0 \ + -relief raised -borderwidth 2 +pack .mbar.mcr -side left +set m [menu .mbar.mcr.m] +$m add command -label "Open MCR" -command open_mcr_dialog + menubutton .mbar.help -text Help -menu .mbar.help.m -underline 0\ -relief raised -borderwidth 2 pack .mbar.help -side right