#!/usr/bin/wish
#
#

global line
set line ""

proc draw {TITLE TAG LEN} {
	set Y 2
	set H 2
	set R 4
	frame .$TAG
	pack .$TAG -side left -fill x
	label .$TAG.errors1 -text "$TITLE"
	pack .$TAG.errors1 -side top -fill x -expand yes
	canvas .$TAG.screen -relief raised -width 140 -height [expr $Y + (($LEN) * $R)]
	pack .$TAG.screen -side top -fill x
	set N 1
	while {$N <= $LEN} {
		eval .$TAG.screen create rectangle 7 [expr $Y + ($N - 1) * $R] [expr 128 + 10] [expr $Y + ($N - 1) * $R + $H] -width 1
		eval .$TAG.screen create rectangle 7 [expr $Y + ($N - 1) * $R] 64 [expr $Y + ($N - 1) * $R + $H] -width 1 -tags $TAG$N -fill red
		incr N
	}
}

proc text_draw {TITLE TAG LEN} {
	set Y 2
	set H 2
	set R 8
	frame .$TAG
	pack .$TAG -side left -fill x
	label .$TAG.errors1 -text "$TITLE"
	pack .$TAG.errors1 -side top -fill x -expand yes
	canvas .$TAG.screen -relief raised -width 60 -height [expr $Y + (($LEN + 1) * $R)]
	pack .$TAG.screen -side top -fill x
	set N 1
	while {$N <= $LEN} {
		eval .$TAG.screen create text 7 [expr $Y + ($N) * $R] -text  \"$N = \" -font \{Helvetica 7\} -anchor w
		eval .$TAG.screen create text 25 [expr $Y + ($N) * $R] -text  \"0\" -font \{Helvetica 7\} -anchor w -tags $TAG$N -fill red
		incr N
	}
}

proc update {TAG VALUES LEN FAC} {
	set Y 2
	set H 2
	set R 4
	set N 1
	while {$N <= $LEN} {
		set value [expr [lindex $VALUES $N] / $FAC / 2 + 10]
		if {$value < 0} {
			set value 0
		} elseif {$value > 255} {
			set value 255
		}
		eval .$TAG.screen coords $TAG$N 7 [expr $Y + ($N - 1) * $R] $value [expr $Y + ($N - 1) * $R + $H]
#		puts "$TAG -- $TAG$N 7 [expr $Y + ($N - 1) * $R] $value [expr $Y + ($N - 1) * $R + $H]"
		incr N
	}
}

proc update_text {TAG VALUES LEN OFFSET} {
	set N 1
	puts -nonewline "$TAG"
	while {$N <= $LEN} {
		eval .$TAG.screen itemconfigure $TAG$N -text \"[lindex $VALUES [expr $N + $OFFSET]]\"
		puts -nonewline ";[lindex $VALUES [expr $N + $OFFSET]]"
		incr N
	}
	puts ""
}


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 rd_chid {chid} {
	global line
	set msg [read $chid 1]
	if {$msg == "\n"} {
		if {[string match "TEL;*" $line]} {
			update "a.tel" [split $line ";"] 16 2
			update_text "a.teltext1" [split $line ";"] 16 0
			update_text "a.teltext2" [split $line ";"] 16 15
			update_text "a.teladc" [split $line ";"] 4 5
			update_text "a.telfs" [split $line ";"] 8 9

			.frame4.rx_rssi configure -text "RX-RSSI: [lindex [split $line ";"] 3]"
			.frame4.rx_lqi configure -text "RX-LQI: [lindex [split $line ";"] 5]"
			.frame4.rx_batt configure -text "RX-BATT: [expr [lindex [split $line ";"] 6] * 1024 / 100] mV"

		} elseif {[string match "CHV;*" $line]} {
			update "a.channel" [split $line ";"] 8 0.1
		} elseif {[string match "FSV;*" $line]} {
			update "a.failsave" [split $line ";"] 8 0.1
		} elseif {[string match "LQI;*" $line]} {
			update "b.lqi" [split $line ";"] 16 1
		} elseif {[string match "RSI;*" $line]} {
			update "b.rssi" [split $line ";"] 16 1
		} elseif {[string match "SCN;*" $line]} {
			update "b.noise" [split $line ";"] 16 1
		} elseif {[string match "ETC;*" $line]} {
			set line2 [split $line ";"]
			.frame1.tx_rssi configure -text "TX-RSSI: [lindex $line2 2]"
			.frame1.tx_lqi configure -text "TX-LQI: [lindex $line2 3]"
			.frame2.txid configure -text "TX-TXID: [lindex $line2 1]"
			.frame2.tx_channels configure -text "TX-Channels: [lindex $line2 4]"
			.frame2.seq configure -text "Sequence: [lindex $line2 4]"
		}
		set line ""
	} else {
		set line "$line$msg"
	}
}


#wm geometry . 320x250
wm title . "RCOPEN24 TX-Tool"

#frame .frame3
#pack .frame3 -side top -fill x -expand yes
#
#	button .frame3.bind -text "Bind" -command {
#		send_cmd "b"
#	}
#	pack .frame3.bind -side left -fill x -expand yes
#
#	button .frame3.falisave -text "set Failsave" -command {
#		send_cmd "f"
#	}
#	pack .frame3.falisave -side left -fill x -expand yes
#
#	button .frame3.update -text "Update" -command {
#		send_cmd "c"
#	}
#	pack .frame3.update -side left -fill x -expand yes
#
#	button .frame3.reset -text "Reset" -command {
#		send_cmd "r"
#	}
#	pack .frame3.reset -side left -fill x -expand yes

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

	label .frame1.tx_rssi -text "TX-RSSI: 0"
	pack .frame1.tx_rssi -side left -fill none -expand yes

	label .frame1.tx_lqi -text "TX-LQI: 0"
	pack .frame1.tx_lqi -side left -fill none -expand yes

	label .frame1.tx_batt -text "TX-BATT: 0 mV"
	pack .frame1.tx_batt -side left -fill none -expand yes

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

	label .frame4.rx_rssi -text "RX-RSSI: 0"
	pack .frame4.rx_rssi -side left -fill none -expand yes

	label .frame4.rx_lqi -text "RX-LQI: 0"
	pack .frame4.rx_lqi -side left -fill none -expand yes

	label .frame4.rx_batt -text "RX-BATT: 0 mV"
	pack .frame4.rx_batt -side left -fill none -expand yes


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

	label .frame2.txid -text "TXID: 0"
	pack .frame2.txid -side left -fill none -expand yes

	label .frame2.tx_channels -text "Setup-TXID: 0"
	pack .frame2.tx_channels -side left -fill none -expand yes

	label .frame2.seq -text "Sequence: 0"
	pack .frame2.seq -side left -fill none -expand yes

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

	draw "TELEDATA's" "a.tel" 16
	draw "Channel's" "a.channel" 8
	text_draw "TELE1" "a.teltext1" 16
	text_draw "TELE2" "a.teltext2" 16
	text_draw "ADC's" "a.teladc" 4
	text_draw "Failsave's" "a.telfs" 8

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: tx-tool COMPORT [BAUDRATE]"
	exit 1
}

fileevent $Serial readable [list rd_chid $Serial]
#puts -nonewline $Serial "c"






