#!/bin/bash # nmiterm in tcl/tk # # This software is licensed under the GNU General Public Licence # # For details see http://www.gnu.org # # restart using local wish \ exec `which wish` "$0" "$@" # set up proc init {} { #fix dialog box text size option add *Dialog.msg.font {Helvetica 10} # globals global serialfd global sentbyte set serialfd 0 set sentbyte 0 } # menus the old way proc gui {} { global .tx.textarea frame .mainmenu -relief raised -borderwidth 1 # file menu menubutton .mainmenu.file -text "File" -menu .mainmenu.file.menu -underline 0 pack .mainmenu.file -side left menu .mainmenu.file.menu -tearoff 0 .mainmenu.file.menu add command -label "Clear Screen" \ -command { .tx.textarea delete 0.1 end } .mainmenu.file.menu add command -label "Send Raw File" -command { sendafile } .mainmenu.file.menu add command -label "Exit" -command { cleanup } # configure menu menubutton .mainmenu.configure -text "Configure" -menu .mainmenu.configure.menu pack .mainmenu.configure -side left menu .mainmenu.configure.menu -tearoff 0 .mainmenu.configure.menu add command -label "Port" -command { portdialog } # help menu menubutton .mainmenu.help -text "Help" -menu .mainmenu.help.menu pack .mainmenu.help -side right menu .mainmenu.help.menu -tearoff 0 .mainmenu.help.menu add command -label "About" \ -command { tk_messageBox -default ok -icon info -title "NMI Term" \ -message "A simple terminal application.\nCopyright 2004 Ian Thompson-Bell" } # create menu pack .mainmenu -side top -fill x -expand true # text area frame .tx text .tx.textarea -yscrollcommand ".tx.scroll set" scrollbar .tx.scroll -command ".tx.textarea yview" pack .tx.scroll -side right -fill y pack .tx.textarea -side left pack .tx -side top focus .tx.textarea # capture key events bind .tx.textarea { puts -nonewline $serialfd %A; break } } # send an Intel hex file proc sendafile {} { global serialfd global sentbyte set filename [tk_getOpenFile] if { [file exists $filename] } { set fd [open $filename r] set inteldata [read $fd] for { set i 0 } { $i < [string length $inteldata] } {incr i} { set char [string index $inteldata $i] if { $serialfd != 0 } then { set sentbyte 1 puts -nonewline $serialfd $char tkwait variable sentbyte } } close $fd } } proc rxchar {} { global serialfd global sentbyte .tx.textarea insert end [read $serialfd] if {$sentbyte != 0} then { set sentbyte 0 } } # port dialog proc portdialog { } { global serialfd toplevel .portdlg -class Dialog wm title .portdlg "Port Settings" wm geometry .portdlg +400+300 frame .portdlg.tf -relief raised -borderwidth 1 frame .portdlg.bf -relief raised -borderwidth 1 pack .portdlg.tf pack .portdlg.bf label .portdlg.tf.device -text "Device:" entry .portdlg.tf.deventry -width 20 -textvariable device .portdlg.tf.deventry delete 0 end .portdlg.tf.deventry insert 0 "/dev/ttyUSB0" label .portdlg.tf.params -text "Params:" entry .portdlg.tf.paramentry -width 20 -textvariable params .portdlg.tf.paramentry delete 0 end .portdlg.tf.paramentry insert 0 "9600,n,8,1" grid configure .portdlg.tf.device .portdlg.tf.deventry grid configure .portdlg.tf.params .portdlg.tf.paramentry button .portdlg.bf.cancel -text "Cancel" -command { destroy .portdlg } button .portdlg.bf.ok -text "OK" -command { if { $serialfd != 0 } { close $serialfd set serialfd 0 } if { [file exists $device ] } { if { [file owned $device ] } { set serialfd [open $device {RDWR NOCTTY} ] fconfigure $serialfd -mode $params -buffering none -blocking 0 -translation binary fileevent $serialfd readable rxchar } else { tk_messageBox -default ok -icon info -title "NMI Term"\ -message "You do not have permission to access $device" } } else { tk_messageBox -default ok -icon info -title "NMI Term"\ -message "Device $device does not exist" } destroy .portdlg } grid configure .portdlg.bf.cancel .portdlg.bf.ok } # clean up before exit proc cleanup {} { global serialfd if {$serialfd != 0} { close $serialfd } exit } proc main {} { init gui } main