#!/usr/bin/wish
#
#

global line
global argv
set line ""
set VERSION ""


proc Serial_Init {ComPort ComRate} {
	set iChannel [open $ComPort w+]
	set rate $ComRate
	fconfigure $iChannel -mode $ComRate,n,8,1
	fconfigure $iChannel -blocking 0
	fconfigure $iChannel -buffering none
	fileevent $iChannel readable ""
	return $iChannel
}

proc send_channel {CHANNEL VALUE} {
	global Serial
	puts $Serial "SCH;$CHANNEL;$VALUE\n"
}

proc send_cmd {CMD} {
	global Serial
	puts -nonewline $Serial "$CMD"
}

proc get_update {} {
	global SYSINFO
	global VERSION
	global Serial
	global argv

	close $Serial

	set RUNNING_VERSION $VERSION
	set LAST_VERSION "0"
	catch {
		exec xterm -e "wget -O - http://www.rcopen24.org/targets/[lindex [split $SYSINFO "/"] 0]/[lindex [split $SYSINFO "/"] 1]/index.html > rx-tool_wget.TmP"
		set LAST_VERSION [string trimright [lindex [split [exec cat rx-tool_wget.TmP | grep "[lindex [split $SYSINFO "/"] 0]/[lindex [split $SYSINFO "/"] 1]/[lindex [split $SYSINFO "/"] 0]_[lindex [split $SYSINFO "/"] 1]-[lindex [split $SYSINFO "/"] 2].srec:VERSION="] "="] 1] " ->"]
	}
	catch {exec rm -rf rx-tool_wget.TmP}
	if {$LAST_VERSION != "0"} {
		if {$LAST_VERSION > $RUNNING_VERSION} {
			puts "## FOUND UPDATES ($LAST_VERSION > $RUNNING_VERSION) ##"
			set DOWNLOAD_OK 0
			catch {
				puts "wget http://www.rcopen24.org/targets/[lindex [split $SYSINFO "/"] 0]/[lindex [split $SYSINFO "/"] 1]/[lindex [split $SYSINFO "/"] 0]_[lindex [split $SYSINFO "/"] 1]-[lindex [split $SYSINFO "/"] 2].srec > rx-tool_wget-srec.TmP"
				exec xterm -e "wget -O - http://www.rcopen24.org/targets/[lindex [split $SYSINFO "/"] 0]/[lindex [split $SYSINFO "/"] 1]/[lindex [split $SYSINFO "/"] 0]_[lindex [split $SYSINFO "/"] 1]-[lindex [split $SYSINFO "/"] 2].srec > rx-tool_wget-srec.TmP"
				set DOWNLOAD_OK 1
			}
			if {$DOWNLOAD_OK == 1} {
				puts "start flashing new firmware (meshprog -t [lindex $argv 0] -f rx-tool_wget-srec.TmP)..."
				catch {
					exec xterm -e "meshprog -t [lindex $argv 0] -f rx-tool_wget-srec.TmP"
					after 1000
				}
			} else {
				puts "Error loading last Version from:"
				puts "	http://www.rcopen24.org/targets/[lindex [split $SYSINFO "/"] 0]/[lindex [split $SYSINFO "/"] 1]/[lindex [split $SYSINFO "/"] 0]_[lindex [split $SYSINFO "/"] 1]-[lindex [split $SYSINFO "/"] 2].srec"
			}
			catch {exec rm -rf rx-tool_wget-srec.TmP}
		} else {
			puts "You have allready the newest Firmware-Version: $RUNNING_VERSION"
		}
		puts "LAST_VERSION=$LAST_VERSION"
	} else {
		puts "Error reading last Version from:"
		puts "	http://www.rcopen24.org/targets/[lindex [split $SYSINFO "/"] 0]/[lindex [split $SYSINFO "/"] 1]/index.html"
	}

	if {[lindex $argv 0] != ""} {
		puts "Open Serial-Port: [lindex $argv 0]"
		if {[lindex $argv 1] != ""} {
			set Serial [Serial_Init "[lindex $argv 0]" [lindex $argv 1]]
		} else {
			set Serial [Serial_Init "[lindex $argv 0]" 38400]
		}
	} else {
		puts "USAGE: rx-tool COMPORT [BAUD]"
		exit 1
	}
	fileevent $Serial readable [list rd_chid $Serial]
	send_cmd "c"
	after 100
	send_cmd "I"
}

proc rd_chid {chid} {
	global line
	global VERSION
	global SYSINFO
	set msg [read $chid 1]
	if {$msg == "\n"} {
		puts "$line"
		if {[string match "SYSINFO:*" $line]} {
			set SYSINFO [lrange [split $line ":"] 1 end]
			set VERSION [lindex [split $SYSINFO "/"] 3]
			wm title . "RCOPEN24 RX-Tool ($SYSINFO)"

		} elseif {[string match "ME:*:*" $line]} {
			set TEXT [lindex [split [lrange [split $line ":"] 2 end] "="] 0]
			set VALUE [lrange [split [lrange [split $line ":"] 2 end] "="] 1 end]
			set ID [lindex [split $line ":"] 1]
			puts "$ID: $TEXT == $VALUE"
			catch {
				eval .frame_list.me$ID.n configure -text \"$TEXT\"
				eval .frame_list.me$ID.v configure -text \"$VALUE\"
				eval .frame_list.me$ID.n configure -relief flat
				eval .frame_list.me$ID.v configure -relief flat
				eval .frame_list.me[expr $ID + 1].n configure -text \"\"
				eval .frame_list.me[expr $ID + 1].v configure -text \"\"
				eval .frame_list.me[expr $ID + 1].n configure -relief flat
				eval .frame_list.me[expr $ID + 1].v configure -relief flat
				eval .frame_list.me[expr $ID + 2].n configure -text \"\"
				eval .frame_list.me[expr $ID + 2].v configure -text \"\"
				eval .frame_list.me[expr $ID + 2].n configure -relief flat
				eval .frame_list.me[expr $ID + 2].v configure -relief flat
				eval .frame_list.me[expr $ID + 3].n configure -text \"\"
				eval .frame_list.me[expr $ID + 3].v configure -text \"\"
				eval .frame_list.me[expr $ID + 3].n configure -relief flat
				eval .frame_list.me[expr $ID + 3].v configure -relief flat
				eval .frame_list.me[expr $ID + 4].n configure -text \"\"
				eval .frame_list.me[expr $ID + 4].v configure -text \"\"
				eval .frame_list.me[expr $ID + 4].n configure -relief flat
				eval .frame_list.me[expr $ID + 4].v configure -relief flat
				eval .frame_list.me[expr $ID + 5].n configure -text \"\"
				eval .frame_list.me[expr $ID + 5].v configure -text \"\"
				eval .frame_list.me[expr $ID + 5].n configure -relief flat
				eval .frame_list.me[expr $ID + 5].v configure -relief flat
			}
			if {[string match ">*" $TEXT]} {
				catch {
					eval .frame_list.me$ID.n configure -relief raised
					eval .frame_list.me$ID.v configure -relief raised
				}
			}
		}
		set line ""
	} else {
		set line "$line$msg"
	}
}


wm geometry . 320x240
wm title . "RCOPEN24 RX-Menu"


frame .frame_list
pack .frame_list -side top -fill x

	label .frame_list.space -text ""
	pack .frame_list.space -side top -fill x -expand yes

	frame .frame_list.me0
	pack .frame_list.me0 -side top -fill x

		label .frame_list.me0.n -text ""
		pack .frame_list.me0.n -side left -fill x -expand yes

		label .frame_list.me0.v -text ""
		pack .frame_list.me0.v -side right -fill x -expand yes

	frame .frame_list.me1
	pack .frame_list.me1 -side top -fill x

		label .frame_list.me1.n -text ""
		pack .frame_list.me1.n -side left -fill x -expand yes

		label .frame_list.me1.v -text ""
		pack .frame_list.me1.v -side right -fill x -expand yes

	frame .frame_list.me2
	pack .frame_list.me2 -side top -fill x

		label .frame_list.me2.n -text ""
		pack .frame_list.me2.n -side left -fill x -expand yes

		label .frame_list.me2.v -text ""
		pack .frame_list.me2.v -side right -fill x -expand yes

	frame .frame_list.me3
	pack .frame_list.me3 -side top -fill x

		label .frame_list.me3.n -text ""
		pack .frame_list.me3.n -side left -fill x -expand yes

		label .frame_list.me3.v -text ""
		pack .frame_list.me3.v -side right -fill x -expand yes

	frame .frame_list.me4
	pack .frame_list.me4 -side top -fill x

		label .frame_list.me4.n -text ""
		pack .frame_list.me4.n -side left -fill x -expand yes

		label .frame_list.me4.v -text ""
		pack .frame_list.me4.v -side right -fill x -expand yes


frame .frameB
pack .frameB -side top -fill x -expand yes


	button .frameB.m -text "Left" -command {
		send_cmd "1"
	}
	pack .frameB.m -side left -fill both -expand yes

	frame .frameB.frame3
	pack .frameB.frame3 -side left -fill x -expand yes
	
		button .frameB.frame3.up -text "Up" -command {
			send_cmd "8"
		}
		pack .frameB.frame3.up -side top -fill x -expand yes
	
		button .frameB.frame3.down -text "Down" -command {
			send_cmd "2"
		}
		pack .frameB.frame3.down -side bottom -fill x -expand yes
	
		button .frameB.frame3.left -text "-" -command {
			send_cmd "4"
		}
		pack .frameB.frame3.left -side left -fill x -expand yes
	
		button .frameB.frame3.right -text "+" -command {
			send_cmd "6"
		}
		pack .frameB.frame3.right -side right -fill x -expand yes


	button .frameB.p -text "Right" -command {
		send_cmd "3"
	}
	pack .frameB.p -side right -fill both -expand yes

if {[lindex $argv 0] != ""} {
	puts "Open Serial-Port: [lindex $argv 0]"
	if {[lindex $argv 1] != ""} {
		set Serial [Serial_Init "[lindex $argv 0]" [lindex $argv 1]]
	} else {
		set Serial [Serial_Init "[lindex $argv 0]" 38400]
	}
} else {
	puts "USAGE: rx-tool COMPORT [BAUD]"
	exit 1
}
fileevent $Serial readable [list rd_chid $Serial]
send_cmd "s"
after 100
send_cmd "I"


