#!/usr/bin/wish
#
# copyright May 14, 2003, Marteinn Sverrisson, TF3MA\
  matti_at_raunvis_dot_hi_dot_is
# This software is available under the terms of the GNU Public License
#
# MATerm.tcl, is a simple MORSE interface for use with the K2.
# it uses the KY keyer in K2.
#
# $Id: MATerm.tcl,v 1.10 2003/06/06 21:03:53 matti Exp matti $
# usage: connect the K2 to the appopriate serial port
# use the buttons og type in text, when you have completed each word
# it will be sent to the K2.
#
# limit the string length sent to K2 to less than 24
#

set Rev "\$Revision: 1.10 $"

#
# special fonts
#
font create vfofont -family Courier -size 16 -weight bold
#
# Help
#

proc help {} {
    global .wint
    .t.wint.t insert end "\n
MATerm.tcl --- Tune Freq using MouseWheel\n\n MouseWheel --- 50Hz tuningrate\n\
      Shift-MouseWheel --- 1kHz tuningrate\n  Control-MouseWheel --- 10Hz\
      tuningrate\n  Left or Right Mouse button --- toggles A/B\n  Pressing A\
      or B buttons toggles the VFO selection\n  
      Press MouseWheel --- send AI2,,initstring to K2\n"
}

set MATerm_rcfile .MATermrc
#
# constants
#
set ButtonsL {3 4 5 6 7 8 9 10}
set ButtonsR {1 2 3 4 5 6 7 8 9 10}
set ButtonsB {1 2 3 4 5 6 7 8 9 10}
set ButtonsF {1 2 3 4 5 6 7 8}

set pre_cursor_pos 1.0
set ready 0
set speed 20
set live_key 1

set rig_initstring "k22;ai2;an;fa;fb;fr;ft;fw;gt;ks;lk;md;nb;pa;pc;ra;rc;rd;xt;"
#
# make an empty MATerm_rcfile if needed
#
proc mk_MATermrc {} {
    global MATerm_rcfile env ButtonsL ButtonsR ButtonsB ButtonsF
    set file [open $env(HOME)/$MATerm_rcfile w]
    puts $file "SERIAL_PORT /dev/ttyS0"
    foreach b $ButtonsL {
	puts $file "L$b \{CW MD3;\}"
    }
    foreach b $ButtonsR {
	puts $file "R$b \{R$b test\}"
    }
    foreach b $ButtonsB {
	puts $file "B$b \{B$b test\}"
    }
    foreach b $ButtonsF {
	puts $file "F$b \{Key test\}"
    }
    flush $file
    close $file
}
#
# read the MATerm_rcfile, with the buttontexts 
#
proc read_MATermrc {} {
    global MATerm_rcfile env f

    set file [open $env(HOME)/$MATerm_rcfile r]
    set rcinfo [split [read $file] \n]
    array set f [join $rcinfo]
    close $file
}

if {[file readable $env(HOME)/$MATerm_rcfile] == 0} {
    mk_MATermrc
}

read_MATermrc

#
# edit the button names and messages and save in the MATerm_rcfile
#
proc edit_MATermrc {} {
    global f ButtonsL ButtonsR ButtonsB ButtonsF Ln Le Rn Re Bn Be Fn Fe
    toplevel .w
    wm title .w "Edit Buttons"
    frame .w.l
    frame .w.l.c
    label .w.l.c.l -text Callsign -width 10
    entry .w.l.c.n -textvariable f(CALLSIGN) -width 10
    pack .w.l.c.l .w.l.c.n -side left
    pack .w.l.c -anchor w
    frame .w.l.0
    label .w.l.0.l -text Port -width 10
    entry .w.l.0.n -textvariable f(SERIAL_PORT) -width 10
    pack .w.l.0.l .w.l.0.n -side left
    pack .w.l.0 -anchor w
    foreach b $ButtonsL {
	set Ln($b) [lindex $f(L$b) 0]
	set Le($b) [lindex $f(L$b) 1]
	frame .w.l.$b
	label .w.l.$b.l -text L$b -width 3
	entry .w.l.$b.n -textvariable Ln($b) -width 6
	entry .w.l.$b.e -textvariable Le($b) -width 50
	pack .w.l.$b.l .w.l.$b.n .w.l.$b.e -side left
	pack .w.l.$b
    }
    frame .w.r
    foreach b $ButtonsR {
	set Rn($b) [lindex $f(R$b) 0]
	set Re($b) [lindex $f(R$b) 1]
	frame .w.r.$b
	label .w.r.$b.l -text R$b -width 3
	entry .w.r.$b.n -textvariable Rn($b) -width 6
	entry .w.r.$b.e -textvariable Re($b) -width 50
	pack .w.r.$b.l .w.r.$b.n .w.r.$b.e -side left
	pack .w.r.$b
    }
    frame .w.b
    foreach b $ButtonsB {
	set Bn($b) [lindex $f(B$b) 0]
	set Be($b) [lindex $f(B$b) 1]
	frame .w.b.$b
	label .w.b.$b.l -text B$b -width 3
	entry .w.b.$b.n -textvariable Bn($b) -width 6
	entry .w.b.$b.e -textvariable Be($b) -width 50
	pack .w.b.$b.l .w.b.$b.n .w.b.$b.e -side left
	pack .w.b.$b
    }
    frame .w.f
    foreach b $ButtonsF {
	set Fn($b) [lindex $f(F$b) 0]
	set Fe($b) [lindex $f(F$b) 1]
	frame .w.f.$b
	label .w.f.$b.l -text F$b -width 3
	label .w.f.$b.n -textvariable Fn($b) -width 6 -relief flat
	entry .w.f.$b.e -textvariable Fe($b) -width 50
	pack .w.f.$b.l .w.f.$b.n .w.f.$b.e -side left
	pack .w.f.$b
    }
    pack .w.l .w.r .w.b .w.f -in .w
    frame .w.c
    button .w.c.q -text Close -command {destroy .w}
    button .w.c.b -text Save -command write_MATermrc
    pack .w.c.q .w.c.b -in .w.c -side left
    pack .w.c

}

#
# save the MATerm_rcfile after editing buttons, and reread the buttons
#
proc write_MATermrc {} {
    global env f MATerm_rcfile ButtonsL ButtonsR ButtonsB ButtonsF Ln Le Rn Re\
      Bn Be Fn Fe
    set file [open $env(HOME)/$MATerm_rcfile w]
    puts $file "CALLSIGN $f(CALLSIGN)"
    puts $file "SERIAL_PORT $f(SERIAL_PORT)"
    foreach b $ButtonsL {
	puts $file "L$b \{\{$Ln($b)\} \{$Le($b)\}\}"
    }
    foreach b $ButtonsR {
	puts $file "R$b \{\{$Rn($b)\} \{$Re($b)\}\}"
    }
    foreach b $ButtonsB {
	puts $file "B$b \{\{$Bn($b)\} \{$Be($b)\}\}"
    }
    foreach b $ButtonsF {
	puts $file "F$b \{\{$Fn($b)\} \{$Fe($b)\}\}"
    }
    flush $file
    read_MATermrc
    morse
}

#
# set_speed, set the keyer speed WPM
#
proc set_speed {} {
    global speed
    destroy .spd
    toplevel .spd -relief raised -background black
    scale .spd.s -from 10 -to 50 -showvalue true -tickinterval 10\
      -variable speed -width 10 -orient horizontal -command {key_speed}
    pack .spd.s
}
proc key_speed {speed} {
    send_cmd "KS[format "%03u" $speed];"
}
#
# set_pwr, set the output power
#
proc set_pwr {} {
    global tx_pwr
    destroy .pwr
    toplevel .pwr -relief raised -background black
    scale .pwr.s -from 0 -to 150 -showvalue true -tickinterval 50\
      -variable tx_pwr -width 10 -orient horizontal -command {out_pwr}
    pack .pwr.s -side left
}
proc out_pwr {tx_pwr} {
    send_cmd "PC[format "%03u" $tx_pwr];"
}

#
# the main window
#
proc morse {} {
    global MALog Rev log f serial_port pre_cursor_pos speed freq vfo mode\
      splitmode ant xfil xfil_bw tx_pwr PA rig_initstring
    destroy .t
    #.morse .vfo .control .win .winr .wint .wins .buts .butc .butd
    if {![info exists ant]} {
	set ant 1
    }

    if {[info exists MALog]} {
	toplevel .t
	wm title .t "MATerm.tcl -- written by TF3MA -- $Rev"
    } else {
	frame .t
	wm title . "MATerm.tcl -- written by TF3MA -- $Rev"
    }
    frame .t.vfo -borderwidth 5.0
    frame .t.morse
    frame .t.control -relief raised -borderwidth 2 -background grey
    frame .t.win -relief raised -borderwidth 2
    frame .t.winr
    frame .t.wint -relief raised -borderwidth 2 -background grey
    frame .t.wins -relief raised -borderwidth 2 -background grey
    frame .t.winlog -relief raised -borderwidth 2
    frame .t.buts -relief raised -borderwidth 2
    frame .t.butc -relief raised -borderwidth 2
    frame .t.butd -relief raised -borderwidth 2

    #
    # PTT button
    #
    button .t.vfo.tx -text PTT -font vfofont -relief raised -width 3\
      -activebackground orange -borderwidth 5.0 -padx .25 -pady .25
    bind .t.vfo.tx <ButtonPress-1> {send_cmd "TX;"}
    bind .t.vfo.tx <ButtonRelease-1> {send_cmd "RX;"}
    label .t.vfo.tb -width 1
    #
    # K2 Frequency display
    #
    button .t.vfo.t -text Freq -font vfofont -width 6 -relief raised -padx .25\
      -pady .25 -background #bedcaa -activebackground #bedcaa -borderwidth 5.0\
      -command {send_cmd "SW09;"}
    bind .t.vfo.t <Button-2> {send_cmd $rig_initstring}
    bind .t.vfo.t <Button-3> {send_cmd "SW09;"}
    bind .t.vfo.t <Button-4> {send_cmd "DN2;"}
    bind .t.vfo.t <Shift-Button-4> {send_cmd "DN4;"}
    bind .t.vfo.t <Control-Button-4> {send_cmd "DN1;"}
    bind .t.vfo.t <Button-5> {send_cmd "UP2;"}
    bind .t.vfo.t <Shift-Button-5> {send_cmd "UP4;"}
    bind .t.vfo.t <Control-Button-5> {send_cmd "UP1;"}
    label .t.vfo.bb -width 1
    radiobutton .t.vfo.al -text A -variable vfo -value A -font vfofont\
      -relief sunken -padx .25 -pady .25 -borderwidth 1.0 -indicatoron false\
      -width 3 -command {send_cmd "SW09;"}
    label .t.vfo.a -textvariable freq(A) -width 12 -relief sunken\
      -font vfofont -background #bedcaa -foreground darkblue -borderwidth 5.0
    radiobutton .t.vfo.bl -text B -variable vfo -value B -font vfofont\
      -relief sunken -padx .25 -pady .25 -borderwidth 1.0 -indicatoron false\
      -width 3 -command {send_cmd "SW09;"}
    label .t.vfo.b -textvariable freq(B) -width 12 -relief sunken\
      -font vfofont -background #bedcaa -foreground darkblue -borderwidth 5.0
    label .t.vfo.m -textvariable mode -width 6 -relief sunken -font vfofont\
      -background #bedcaa -foreground darkblue -borderwidth 5.0
    checkbutton .t.vfo.sp -text Split -relief raised -indicatoron false\
      -offvalue 0 -onvalue 1 -variable splitmode -borderwidth 5.0 -pady 5.0\
      -command {send_cmd "SW26;"}
    pack .t.vfo.tx .t.vfo.tb .t.vfo.t .t.vfo.bb .t.vfo.al .t.vfo.a .t.vfo.bl\
      .t.vfo.b .t.vfo.m .t.vfo.sp -side left -padx .25 -pady .25 -fill both
    #pack .vfo -in .t
    #
    # K2 control panel buttons
    #
    button .t.control.tu -text "Tune" -activebackground red -command {send_cmd\
      "SW20;"} -width 4
    button .t.control.an -text "Ant" -command {send_cmd "SW04;"} -width 3
    label .t.control.ann -width 2 -textvariable ant -background #bedcaa\
      -foreground darkblue -relief sunken
    button .t.control.bp -text Band+ -command {send_cmd "SW01;"} -width 4
    button .t.control.bm -text Band- -command {send_cmd "SW03;"} -width 4
    button .t.control.md -text Mode -command {send_cmd "SW08;"} -width 4
    button .t.control.vox -text VOX -command {send_cmd "SW24;"} -width 3
    button .t.control.a/b -text A/B -command {send_cmd "SW09;"} -width 3
    button .t.control.a=b -text A=B -command {send_cmd "SW10;"} -width 3
    button .t.control.sp -text Spot -command {send_cmd "SW27;"} -width 4
    button .t.control.xf -text XFil -command {send_cmd "SW13;"} -width 3
    label .t.control.xfn -width 4 -textvariable xfil_bw -background #bedcaa\
      -foreground darkblue -relief sunken
    label .t.control.dum1 -width 1 -background grey -foreground #bedcaa
    label .t.control.el -text "Elecraft" -width 10 -background black\
      -foreground white
    label .t.control.k2 -text "K2" -width 3 -background black\
      -foreground #bedcaa
    label .t.control.dum2 -width 1 -background grey -foreground #bedcaa
    pack .t.control.tu .t.control.an .t.control.ann .t.control.bp\
      .t.control.bm .t.control.md .t.control.vox .t.control.a/b .t.control.a=b\
      .t.control.sp .t.control.xf .t.control.xfn .t.control.dum1 .t.control.el\
      .t.control.k2 .t.control.dum2 -side left -padx .25 -pady .25

    #
    # left side buttons, user commands
    #
    menubutton .t.butc.1 -text File -width 8 -menu .t.butc.1.m -relief raised
    menu .t.butc.1.m
    .t.butc.1.m add checkbutton -label "Live keys" -variable live_key\
      -onvalue 1 -offvalue 0
    .t.butc.1.m add separator
    .t.butc.1.m add command -label "Open file" -command {
	set textfile [tk_getOpenFile -initialdir . -filetypes {{text .txt}\
         {all .*}}]
	if {$textfile != ""} {
	    set file [open $textfile r]
	    set words [read -nonewline $file]
	    regsub -all {\n} $words { } words
	}
    }
    .t.butc.1.m add command -label "Send file" -command {
	set stop 0
	send_cmd "KS0$speed;"
	set wordslist [split $words]
	foreach word $wordslist {
	    send_mess $word
	    set pause [expr {10*[string length $word]}]
	    # 60 for 20WPM, 40 for 30WPM
	    set i 0
	    while {$i < [expr {1200/$speed}]} {
		after $pause
		incr i
		update
	    }
	    if {$stop} break
	}
    }
    .t.butc.1.m add command -label "Stop sending" -command {
	set stop 1
	send_mess "@"
    }
    .t.butc.1.m add separator
    .t.butc.1.m add command -label "Keyer Speed" -command {set_speed}
    .t.butc.1.m add command -label "TX Power" -command {set_pwr}
    .t.butc.1.m add checkbutton -label "PA On" -variable PA\
      -onvalue 1 -offvalue 0
    .t.butc.1.m add separator
    .t.butc.1.m add command -label "Edit buttons" -command {edit_MATermrc}
    .t.butc.1.m add separator
    .t.butc.1.m add command -label Quit -command {
	destroy .t
	if {![info exists MALog]} {exit}
    }
    button .t.butc.2 -text Break -width 6 -command {
	set stop 1
	send_mess "@"
    }
    button .t.butc.3 -text [lindex $f(L3) 0] -width 6 -command {send_cmd\
      "[lindex $f(L3) 1]"}
    button .t.butc.4 -text [lindex $f(L4) 0] -width 6 -command {send_cmd\
      "[lindex $f(L4) 1]"}
    button .t.butc.5 -text [lindex $f(L5) 0] -width 6 -command {send_cmd\
      "[lindex $f(L5) 1]"}
    button .t.butc.6 -text [lindex $f(L6) 0] -width 6 -command {send_cmd\
      "[lindex $f(L6) 1]"}
    button .t.butc.7 -text [lindex $f(L7) 0] -width 6 -command {send_cmd\
      "[lindex $f(L7) 1]"}
    button .t.butc.8 -text [lindex $f(L8) 0] -width 6 -command {send_cmd\
      "[lindex $f(L8) 1]"}
    button .t.butc.9 -text [lindex $f(L9) 0] -width 6 -command {send_cmd\
      "[lindex $f(L9) 1]"}
    button .t.butc.10 -text [lindex $f(L10) 0] -width 6 -command {send_cmd\
      "[lindex $f(L10) 1]"}

    pack .t.butc.1 .t.butc.2 .t.butc.3 .t.butc.4 .t.butc.5 .t.butc.6 .t.butc.7\
      .t.butc.8 .t.butc.9 .t.butc.10 -in .t.butc -side top -padx .25 -pady .25\
      -fill y

    #
    # rigth side buttons, user messages
    #
    button .t.buts.1 -text [lindex $f(R1) 0] -width 6 -command {send_mess\
      [lindex $f(R1) 1]}
    button .t.buts.2 -text [lindex $f(R2) 0] -width 6 -command {send_mess\
      [lindex $f(R2) 1]}
    button .t.buts.3 -text [lindex $f(R3) 0] -width 6 -command {send_mess\
      [lindex $f(R3) 1]}
    button .t.buts.4 -text [lindex $f(R4) 0] -width 6 -command {send_mess\
      [lindex $f(R4) 1]}
    button .t.buts.5 -text [lindex $f(R5) 0] -width 6 -command {send_mess\
      [lindex $f(R5) 1]}
    button .t.buts.6 -text [lindex $f(R6) 0] -width 6 -command {send_mess\
      [lindex $f(R6) 1]}
    button .t.buts.7 -text [lindex $f(R7) 0] -width 6 -command {send_mess\
      [lindex $f(R7) 1]}
    button .t.buts.8 -text [lindex $f(R8) 0] -width 6 -command {send_mess\
      [lindex $f(R8) 1]}
    button .t.buts.9 -text [lindex $f(R9) 0] -width 6 -command {send_mess\
      [lindex $f(R9) 1]}
    button .t.buts.10 -text [lindex $f(R10) 0] -width 6 -command {send_mess\
      [lindex $f(R10) 1]}

    pack .t.buts.1 .t.buts.2 .t.buts.3 .t.buts.4 .t.buts.5 .t.buts.6 .t.buts.7\
      .t.buts.8 .t.buts.9 .t.buts.10 -in .t.buts -side top -padx .25 -pady .25

    #
    # text windows
    #
    text .t.winr.r -width 80 -height 5 -yscrollcommand ".t.winr.txr set"\
      -wrap word
    scrollbar .t.winr.txr -orient vertical -command ".t.winr.r yview"

    text .t.wint.t -width 80 -height 11 -yscrollcommand ".t.wint.txs set"\
      -wrap word
    scrollbar .t.wint.txs -orient vertical -command ".t.wint.t yview"

    text .t.wins.s -width 80 -height 5 -yscrollcommand ".t.wins.tss set"\
      -wrap word
    scrollbar .t.wins.tss -orient vertical -command ".t.wins.s yview"

    pack .t.winr.r .t.winr.txr -in .t.winr -side left -fill y -padx .25\
      -pady .25
    pack .t.wint.t .t.wint.txs -in .t.wint -side left -fill y -padx .25\
      -pady .25
    pack .t.wins.s .t.wins.tss -in .t.wins -side left -fill y -padx .25\
      -pady .25
    #pack .t.wint .t.winr .t.wins -in .t.win -side top
    pack .t.wint .t.wins -in .t.win -side top

    bind .t.wint.t <KeyRelease> {
	set cursor_pos [.t.wint.t index insert]
	set key [.t.wint.t get "$cursor_pos -1 chars"]
	if {[.t.wint.t compare "$cursor_pos" > "$pre_cursor_pos"] == 1} {
	    if {$live_key == 0} {
		if {$key == " " || $key == "\n" || $key == "/" || $key == "-"} {
		    if {[info exists word]} {send_mess "$word "} 
		    if {$key == "/" || $key == "-"} {send_mess $key}
		    set word {}
		}
	    } else {
		send_live $key
	    }
	    }
	set word [.t.wint.t get "$cursor_pos -1 chars wordstart"\
	  "$cursor_pos"]
	    set pre_cursor_pos $cursor_pos
    }

    #
    # bottom side buttons, user commands
    #
    button .t.butd.1 -text [lindex $f(B1) 0] -width 4 -command {send_cmd\
      "[lindex $f(B1) 1]"}
    button .t.butd.2 -text [lindex $f(B2) 0] -width 4 -command {send_cmd\
      "[lindex $f(B2) 1]"}
    button .t.butd.3 -text [lindex $f(B3) 0] -width 4 -command {send_cmd\
      "[lindex $f(B3) 1]"}
    button .t.butd.4 -text [lindex $f(B4) 0] -width 4 -command {send_cmd\
      "[lindex $f(B4) 1]"}
    button .t.butd.5 -text [lindex $f(B5) 0] -width 4 -command {send_cmd\
      "[lindex $f(B5) 1]"}
    button .t.butd.6 -text [lindex $f(B6) 0] -width 4 -command {send_cmd\
      "[lindex $f(B6) 1]"}
    button .t.butd.7 -text [lindex $f(B7) 0] -width 4 -command {send_cmd\
      "[lindex $f(B7) 1]"}
    button .t.butd.8 -text [lindex $f(B8) 0] -width 4 -command {send_cmd\
      "[lindex $f(B8) 1]"}
    button .t.butd.9 -text [lindex $f(B9) 0] -width 4 -command {send_cmd\
      "[lindex $f(B9) 1]"}
    button .t.butd.10 -text [lindex $f(B10) 0] -width 4 -command {send_cmd\
      "[lindex $f(B10) 1]"}
    pack .t.butd.1 .t.butd.2 .t.butd.3 .t.butd.4 .t.butd.5 .t.butd.6 .t.butd.7\
      .t.butd.8 .t.butd.9 .t.butd.10 -in .t.butd -side left -padx .25 -pady .25

    #
    # log window
    #
    label .t.winlog.cl -text Call -background black -foreground white
    entry .t.winlog.ca -textvariable log(CALL) -width 9 -relief sunken
    label .t.winlog.rstsl -text Rst_S -background black -foreground white
    entry .t.winlog.rsts -textvariable log(RST_SENT) -width 3 -relief sunken
    entry .t.winlog.rstsx -textvariable log(STX) -width 3 -relief sunken
    label .t.winlog.rstrl -text Rst_R -background black -foreground white
    entry .t.winlog.rstr -textvariable log(RST_RCVD) -width 3 -relief sunken
    entry .t.winlog.rstrx -textvariable log(SRX) -width 3 -relief sunken
    label .t.winlog.naml -text Name -background black -foreground white
    entry .t.winlog.nam -textvariable log(NAME) -width 8 -relief sunken
    label .t.winlog.qthl -text Qth -background black -foreground white
    entry .t.winlog.qth -textvariable log(QTH) -width 16 -relief sunken
    button .t.winlog.log -text Log -width 1 -background black\
      -foreground white -command {
	if {[info exists MALog]} {
	    append_info
	    append_log
	    write_log
	} else {
	    puts stdout "QSO: $freq($vfo) $mode [clock format [clock seconds]\
	      -format "%Y-%M-%d %H%M"] $f(CALLSIGN) $log(RST_SENT) $log(CALL)\
	      $log(RST_RCVD) $log(NAME) $log(QTH)"
	    set log(RST_SENT) {}
	    set log(CALL) {}
	    set log(RST_RCVD) {}
	    set log(NAME) {}
	    set log(QTH) {}
	}
    }
    pack .t.winlog.cl .t.winlog.ca .t.winlog.rstsl .t.winlog.rsts\
      .t.winlog.rstsx .t.winlog.rstrl .t.winlog.rstr .t.winlog.rstrx\
      .t.winlog.naml .t.winlog.nam .t.winlog.qthl .t.winlog.qth .t.winlog.log\
      -in .t.winlog -side left -padx .25 -pady .25

    #
    # pack all 
    #
    #pack .t.winlog -in .t.win -after .t.wint
    pack .t.butc .t.win .t.buts -in .t.morse -side left
    #pack .t.vfo .t.control .t.morse .t.butd -in .t -side top
    pack .t.winlog -in .t.win -before .t.wint
    pack .t.butd -in .t.win
    #pack .t.control .t.vfo .t.morse .t.butd -in .t -side top
    pack .t.control .t.vfo .t.morse -in .t -side top
    #pack .t.winlog -in .t -after .t.vfo
    if {![info exists MALog]} {
	pack .t
    }
    focus .t.wint.t
    help
    send_cmd $rig_initstring
}

#
# send_cmd, the command to K2 using the serial port
#
if {[info procs send_cmd] == {}} {

    proc rst_stx {rst} {
	set y [format "%03u" $rst]
	regsub -all "0" $y "T" y
	regsub -all "9" $y "N" y
	send_mess $y
    }

    bind all <F1> {send_mess [lindex $f(F1) 1]}
    bind all <F2> {send_mess [lindex $f(F2) 1]}
    bind all <F3> {send_mess [lindex $f(F3) 1]}
    bind all <F4> {send_mess [lindex $f(F4) 1]}
    bind all <F5> {send_mess [lindex $f(F5) 1]}
    bind all <F6> {send_mess [lindex $f(F6) 1]}
    bind all <F7> {send_mess [lindex $f(F7) 1]}
    bind all <F8> {send_mess [lindex $f(F8) 1]}
    bind all <Escape> {send_mess "@"}

    proc send_cmd {command} {
	global serial_port .wins ready f log

	set wordslist [split $command]
	foreach word $wordslist {
	    puts $serial_port "[subst $word]"
	    update
	}
    }
}
#
# send, the message to K2 keyer using the serial port
#
if {[info procs send_mess] == {}} {

    proc send_live {message} {
	global serial_port
	puts $serial_port "KY $message;"
	if {[winfo exists .t.wins]} {
	    .t.wins.s insert end "$message"
	    .t.wins.s see end
	    update
	}
    }

    proc send_mess {message} {
	global serial_port .t.wins ready f log

	set wordslist [split $message]
	foreach word $wordslist {
	    puts $serial_port "KY [subst $word] ;"
	    if {[winfo exists .t.wins]} {
		.t.wins.s insert end "[subst $word] "
		.t.wins.s see end
		update
	    }
	}
    }
}

#
# get the rig_info, from K2
#

if {[info procs rig_info] == {}} {
    #puts stderr rig_info
    proc rig_info {} {
	global serial_port freq vfo mode inn inline splitmode ant xfil xfil_bw tx_pwr PA\
	  speed
	array set RMODES {1 LSB 2 USB 3 CW 6 RTTY 7 CW-REV 9 RTTY-R}
	array set VFO {0 A 1 B}

	after 200
	append inn [read $serial_port]
	#puts stderr "Rcv $inn"
	if {[string index $inn end] == {;}} {
	    set inline [string trimright $inn {;}]
	    set inn {}
	} else {
	    return 1
	}
	set svar [split $inline {;}]
	#puts stderr $svar
	foreach inline $svar {
	    #puts stderr $inline
	    switch [string range $inline 0 1] {
	    "IF" {
		    set vfo $VFO([string index $inline 30])
		    set ifreq [string trimleft [string range $inline 5 12] 0]
		    set freq($vfo) [format "%.3f" [expr {$ifreq/1000.0}]]
		    set MHz [expr {$ifreq/1000000}]
		    set mode $RMODES([string index $inline 29])
		    set splitmode [string index $inline 32]
		    #puts stderr "[array get freq] $mode $splitmode"
		}
	    "FR" {
		    set vfo $VFO([string index $inline 2])
		}
	    "FA" {
		    set ifreq [string trimleft [string range $inline 5 12] 0]
		    set freq(A) [format "%.3f" [expr {$ifreq/1000.0}]]
		    set MHz [expr {$ifreq/1000000}]
		}
	    "FB" {
		    set ifreq [string trimleft [string range $inline 5 12] 0]
		    set freq(B) [format "%.3f" [expr {$ifreq/1000.0}]]
		    set MHz [expr {$ifreq/1000000}]
		}
	    "MD" {
		    set mode $RMODES([string index $inline 2])
		}
	    "AN" {
		    set ant [string index $inline 2]
		}
	    "PC" {
		    set tx_pwr [string range $inline 2 4]
		    if {[string index $inline 5] == 1} {
			set log(TX_PWR) [string range $inline 2 4]
			set PA 1
		    } else {
			set log(TX_PWR) "[string range $inline 2 3].[string\
			  index $inline 4]"
			set PA 0
		    }
		    set log(TX_PWR) [string trimleft $log(TX_PWR) 0]
		}
	    "KS" {
		    set speed [string range $inline 2 4]
		}
	    "FW" {
		    set xfil [string index $inline 6]
		    set xfil_bw [string trimleft [string range $inline 2 5] 0]
		}
	    "?;" {
		    puts stderr Busy
		}
	    "K2" {
		    after 1000
		    .t.wins.s insert end "Connected to Elecraft -- K2\n"
		    .t.wins.s insert end "Using Auto-Info Mode 2\n"
		}
	    default {
		    #puts stderr $inline
		}
	    }
	}
	return 0
    }
}

#
# initialize the serial interface
#
if {[info procs init_serial] == {}} {
    #puts stderr init_serial
    proc init_serial {} {
	global f serial_port .wins

	if {[catch "open $f(SERIAL_PORT) RDWR" serial_port]} {
	    puts stderr "$f(SERIAL_PORT) busy"
	    return 1
	}\
	elseif {[catch "fconfigure $serial_port -mode 4800,n,8,2\
	  -blocking false  -buffering line"]} {
	    puts stderr "can't set $f(SERIAL_PORT)"
	    return 1
	}
	#puts stderr  [fconfigure $serial_port]
	fileevent $serial_port readable rig_info
	return 0
    }
    init_serial
}

#
# start the program
#


morse

