#!/usr/bin/wish
#
#

global line
set line ""

set FRONT_COLOR "green"
set FRONT_COLOR2 "darkgreen"

proc draw_compas_rec {CANVAS CANVAS_W CANVAS_H COMPASS TARGET} {
	global FRONT_COLOR
	global FRONT_COLOR2

	set x [expr $CANVAS_W / 2]
	$CANVAS delete compass
	$CANVAS create rec 1 1 $CANVAS_W 45 -fill black -tags compass
	$CANVAS create line $x 5 $x 25 -fill red -tags compass
	set y 7
	set v 5
	$CANVAS create polygon $x $y [expr {$x + $v}] [expr {$y - $v}] [expr {$x - $v}] [expr {$y - $v}] -fill green -tags compass
	set y 25
	$CANVAS create polygon $x $y [expr {$x + $v}] [expr {$y + $v}] [expr {$x - $v}] [expr {$y + $v}] -fill green -tags compass
	for {set N [expr $COMPASS - $CANVAS_W / 2]} {$N <= [expr $COMPASS + $CANVAS_W / 2]} {set N [expr $N + 1]} {
		set OFFSET [expr $N - $COMPASS + $CANVAS_W / 2]
		set N2 $N
		if {$N2 >= 360} {
			set N2 [expr $N2 - 360]
		} elseif {$N2 < 0} {
			set N2 [expr $N2 + 360]
		}
		if {[expr $N2 % 90] == 0} {
			$CANVAS create line $OFFSET 23 $OFFSET 30 -fill $FRONT_COLOR -tags compass
			if {$N2 == 0 || $N2 == 360} {
				$CANVAS create text $OFFSET 15 -text "N" -tags compass_text -fill $FRONT_COLOR -tags compass
			} elseif {$N2 == 90} {
				$CANVAS create text $OFFSET 15 -text "E" -tags compass_text -fill $FRONT_COLOR -tags compass
			} elseif {$N2 == 180} {
				$CANVAS create text $OFFSET 15 -text "S" -tags compass_text -fill $FRONT_COLOR -tags compass
			} elseif {$N2 == 270} {
				$CANVAS create text $OFFSET 15 -text "W" -tags compass_text -fill $FRONT_COLOR -tags compass
			}
		} elseif {[expr $N2 % 45] == 0} {
			$CANVAS create line $OFFSET 23 $OFFSET 30 -fill $FRONT_COLOR -tags compass
			$CANVAS create text $OFFSET 15 -text "$N2" -tags compass_text -fill $FRONT_COLOR -tags compass
		} elseif {[expr $N2 % 15] == 0} {
			$CANVAS create line $OFFSET 25 $OFFSET 30 -fill $FRONT_COLOR -tags compass
		} elseif {[expr $N2 % 5] == 0} {
			$CANVAS create line $OFFSET 27 $OFFSET 30 -fill $FRONT_COLOR2 -tags compass
		}
	}
	$CANVAS create text $x 39 -text "$COMPASS" -tags compass_text -fill $FRONT_COLOR -tags compass
	set x [expr $TARGET - $COMPASS + $CANVAS_W / 2]
	set y 7
	$CANVAS create polygon $x $y [expr {$x + $v}] [expr {$y - $v}] [expr {$x - $v}] [expr {$y - $v}] -outline green -tags compass
}

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 FRONT_COLOR
	global FRONT_COLOR2
	global line
	set msg [read $chid 1]
	if {$msg == "\n"} {
		puts "$line"
		if {[string match "ADC;*" $line]} {
#			update2 "a.adc" [split $line ";"] 4 8 0
#			draw_batt_bar .c.screen3 40 160 [expr [lindex [split $line ";"] 1] * 80 / 1024] 75
		} elseif {[string match "CHV;*" $line]} {

			.c.r.screen12 delete channel
			for {set N 0} {$N < 8} {set N [expr $N + 1]} {
				set LEVEL [expr [lindex [split $line ";"] [expr $N + 1]]]
				.c.r.screen12 create rec 18 [expr $N * 5 + 3] [expr 18 + $LEVEL * 35 / 4098] [expr $N * 5 + 8] -fill green -tags channel
			}
			.c.r.screen12 create line 18 0 18 45 -fill $FRONT_COLOR2 -tags channel

		} elseif {[string match "FSV;*" $line]} {
#			update2 "a.failsave" [split $line ";"] 8 16 2048
		} elseif {[string match "LQI;*" $line]} {
			.c.l.screen12 delete lqi
			for {set N 0} {$N < 16} {set N [expr $N + 1]} {
				set LEVEL [expr [lindex [split $line ";"] [expr $N + 1]]]
				.c.l.screen12 create line [expr $N * 2 + 4] [expr 45 - $LEVEL / 7] [expr $N * 2 + 4] [expr 45 - $LEVEL / 7 + 2] -fill green -tags lqi
			}
		} elseif {[string match "RSI;*" $line]} {
			.c.l.screen12 delete rssi
			for {set N 0} {$N < 16} {set N [expr $N + 1]} {
				set LEVEL [expr [lindex [split $line ";"] [expr $N + 1]]]
				.c.l.screen12 create line [expr $N * 2 + 3] 45 [expr $N * 2 + 3] [expr 45 - $LEVEL] -fill $FRONT_COLOR2 -tags rssi
			}
		} elseif {[string match "SCN;*" $line]} {
			.c.l.screen12 delete noise
			for {set N 0} {$N < 16} {set N [expr $N + 1]} {
				set LEVEL [expr [lindex [split $line ";"] [expr $N + 1]]]
				.c.l.screen12 create line [expr $N * 2 + 4] 45 [expr $N * 2 + 4] [expr 45 - $LEVEL] -fill red -tags noise
			}
		} elseif {[string match "MAG;*" $line]} {
			set COMPASS [expr [lindex [split $line ";"] 1] / 10.0]
			set SIZE 80
			set X 80
			set Y 80

			.c.h.screen4 delete tilt
			.c.h.screen4 create arc [expr $X - $SIZE] [expr $Y - $SIZE] [expr $X + $SIZE] [expr $Y + $SIZE] -start [expr -$COMPASS + 90] -extent 0 -width 3 -outline $FRONT_COLOR -tags tilt
			.c.h.screen4 create arc [expr $X - $SIZE + 7] [expr $Y - $SIZE + 7] [expr $X + $SIZE - 7] [expr $Y + $SIZE - 7] -start [expr -230 + 90] -extent 0 -width 5 -outline red -tags tilt
			.c.h.screen4 create oval [expr $X - $SIZE + 13] [expr $Y - $SIZE + 13] [expr $X + $SIZE - 13] [expr $Y + $SIZE - 13] -outline $FRONT_COLOR -fill black -tags tilt
			.c.h.screen4 create line [expr $X - $SIZE] [expr $Y] [expr $X + $SIZE] [expr $Y] -width 1 -fill $FRONT_COLOR2 -dash ".-" -tags tilt
			.c.h.screen4 create line [expr $X] [expr $Y - $SIZE] [expr $X] [expr $Y + $SIZE] -width 1 -fill $FRONT_COLOR2 -dash ".-" -tags tilt
			for {set N -30} {$N <= 30} {set N [expr $N + 10]} {
				.c.h.screen4 create line [expr $X - $SIZE + 45] [expr $Y + $N] [expr $X - 35] [expr $Y + $N] -width 1 -fill $FRONT_COLOR2 -tags tilt
				.c.h.screen4 create line [expr $X + 35] [expr $Y + $N] [expr $X + $SIZE - 45] [expr $Y + $N] -width 1 -fill $FRONT_COLOR2 -tags tilt
				.c.h.screen4 create text [expr $X - $SIZE + 30] [expr $Y + $N] -text "$N" -fill $FRONT_COLOR2 -tags tilt
				.c.h.screen4 create text [expr $X + $SIZE - 30] [expr $Y + $N] -text "$N" -fill $FRONT_COLOR2 -tags tilt
			}
			for {set N -30} {$N <= 30} {set N [expr $N + 5]} {
				.c.h.screen4 create line [expr $X - $SIZE + 55] [expr $Y + $N] [expr $X - 35] [expr $Y + $N] -width 1 -fill $FRONT_COLOR2 -tags tilt
				.c.h.screen4 create line [expr $X + 35] [expr $Y + $N] [expr $X + $SIZE - 55] [expr $Y + $N] -width 1 -fill $FRONT_COLOR2 -tags tilt
			}

			set COMPASS 0

			set Y [expr $Y + $COMPASS - 180]
			.c.h.screen4 create arc [expr $X - 15] [expr $Y - 15] [expr $X + 15] [expr $Y + 15] -start [expr $COMPASS] -extent 0 -fill yellow -width 1 -outline $FRONT_COLOR -tags tilt
			.c.h.screen4 create arc [expr $X - 15] [expr $Y - 15] [expr $X + 15] [expr $Y + 15] -start [expr $COMPASS + 180.0] -extent 0 -fill yellow -width 1 -outline $FRONT_COLOR -tags tilt
			.c.h.screen4 create oval [expr $X - 5] [expr $Y - 5] [expr $X + 5] [expr $Y + 5] -fill green -outline $FRONT_COLOR -tags tilt
			.c.h.screen4 create arc [expr $X - 5] [expr $Y - 5] [expr $X + 5] [expr $Y + 5] -start [expr $COMPASS + 180.0] -extent 180 -fill yellow -width 1 -outline $FRONT_COLOR -tags tilt

			set COMPASS [expr [lindex [split $line ";"] 1] / 10]
			draw_compas_rec .c.h.screen2 160 40 $COMPASS 230




		} elseif {[string match "GPS;*" $line]} {
#			puts [expr [expr [lindex [split $line ";"] 1] * 10000.0 + [lindex [split $line ";"] 2]] / 10000.0]
#			puts [expr [expr [lindex [split $line ";"] 3] * 10000.0 + [lindex [split $line ";"] 4]] / 10000.0]
			set SPEED [expr [expr [lindex [split $line ";"] 5] * 100.0 + [lindex [split $line ";"] 6]] / 100.0]
			set ALT [expr [expr [lindex [split $line ";"] 9] * 100.0 + [lindex [split $line ";"] 10]] / 100.0]
			.c.r.screen3 delete alti
			.c.r.screen3 create rec 5 [expr 150 - $ALT / 2] 37 [expr 150 - $ALT / 2 + 2] -fill red -tags alti
			.c.l.screen1 delete speed
			.c.l.screen1 create rec 5 [expr 150 - $SPEED] 37 [expr 150 - $SPEED + 2] -fill red -tags speed


		} elseif {[string match "AHR;*" $line]} {
			set COMPASS [expr [lindex [split $line ";"] 3] / 100.0]
			set SIZE 80
			set X 80
			set Y 80

			.c.h.screen4 delete tilt
			.c.h.screen4 create arc [expr $X - $SIZE] [expr $Y - $SIZE] [expr $X + $SIZE] [expr $Y + $SIZE] -start [expr -$COMPASS + 90] -extent 0 -width 3 -outline $FRONT_COLOR -tags tilt
			.c.h.screen4 create arc [expr $X - $SIZE + 7] [expr $Y - $SIZE + 7] [expr $X + $SIZE - 7] [expr $Y + $SIZE - 7] -start [expr -230 + 90] -extent 0 -width 5 -outline red -tags tilt
			.c.h.screen4 create oval [expr $X - $SIZE + 13] [expr $Y - $SIZE + 13] [expr $X + $SIZE - 13] [expr $Y + $SIZE - 13] -outline $FRONT_COLOR -fill black -tags tilt
			.c.h.screen4 create line [expr $X - $SIZE] [expr $Y] [expr $X + $SIZE] [expr $Y] -width 1 -fill $FRONT_COLOR2 -dash ".-" -tags tilt
			.c.h.screen4 create line [expr $X] [expr $Y - $SIZE] [expr $X] [expr $Y + $SIZE] -width 1 -fill $FRONT_COLOR2 -dash ".-" -tags tilt


			for {set N -45} {$N <= 45} {set N [expr $N + 15]} {
				.c.h.screen4 create line [expr $X - 10] [expr $Y + $N] [expr $X - 7] [expr $Y + $N] -width 1 -fill $FRONT_COLOR2 -tags tilt
				.c.h.screen4 create line [expr $X + 10] [expr $Y + $N] [expr $X + 7] [expr $Y + $N] -width 1 -fill $FRONT_COLOR2 -tags tilt
				if {$N < 0} {
					.c.h.screen4 create text [expr $X - 30] [expr $Y + $N] -text "[expr $N * -1]" -fill $FRONT_COLOR2 -tags tilt
					.c.h.screen4 create text [expr $X + 30] [expr $Y + $N] -text "[expr $N * -1]" -fill $FRONT_COLOR2 -tags tilt
				} else {
					.c.h.screen4 create text [expr $X - 30] [expr $Y + $N] -text "$N" -fill $FRONT_COLOR2 -tags tilt
					.c.h.screen4 create text [expr $X + 30] [expr $Y + $N] -text "$N" -fill $FRONT_COLOR2 -tags tilt
				}
			}

			set N [expr [lindex [split $line ";"] 1] / 100]
			set Y [expr $Y +  [lindex [split $line ";"] 2] / 100]

			.c.h.screen4 create line [expr $X - $SIZE / 3] [expr $Y] [expr $X - $SIZE / 4 - 2] [expr $Y] -width 1 -fill red -tags tilt
			.c.h.screen4 create line [expr $X + $SIZE / 3] [expr $Y] [expr $X + $SIZE / 4 + 2] [expr $Y] -width 1 -fill red -tags tilt

			.c.h.screen4 create arc [expr $X - $SIZE / 4] [expr $Y - $SIZE / 4] [expr $X + $SIZE / 4] [expr $Y + $SIZE / 4] -start [expr -$N] -extent 0 -width 1 -outline red -tags tilt
			.c.h.screen4 create arc [expr $X - $SIZE / 4] [expr $Y - $SIZE / 4] [expr $X + $SIZE / 4] [expr $Y + $SIZE / 4] -start [expr -$N - 180] -extent 0 -width 1 -outline red -tags tilt

			.c.h.screen4 create arc [expr $X - $SIZE / 5] [expr $Y - $SIZE / 5] [expr $X + $SIZE / 5] [expr $Y + $SIZE / 5] -start [expr -$N + 90] -extent 0 -width 1 -outline red -tags tilt

			.c.h.screen4 create oval [expr $X - $SIZE / 10] [expr $Y - $SIZE / 10] [expr $X + $SIZE / 10] [expr $Y + $SIZE / 10] -outline black -fill black -tags tilt




			set COMPASS 0

			set Y [expr $Y + $COMPASS - 180]
			.c.h.screen4 create arc [expr $X - 15] [expr $Y - 15] [expr $X + 15] [expr $Y + 15] -start [expr $COMPASS] -extent 0 -fill yellow -width 1 -outline $FRONT_COLOR -tags tilt
			.c.h.screen4 create arc [expr $X - 15] [expr $Y - 15] [expr $X + 15] [expr $Y + 15] -start [expr $COMPASS + 180.0] -extent 0 -fill yellow -width 1 -outline $FRONT_COLOR -tags tilt
			.c.h.screen4 create oval [expr $X - 5] [expr $Y - 5] [expr $X + 5] [expr $Y + 5] -fill green -outline $FRONT_COLOR -tags tilt
			.c.h.screen4 create arc [expr $X - 5] [expr $Y - 5] [expr $X + 5] [expr $Y + 5] -start [expr $COMPASS + 180.0] -extent 180 -fill yellow -width 1 -outline $FRONT_COLOR -tags tilt

			set COMPASS [expr [lindex [split $line ";"] 3] / 100]
			draw_compas_rec .c.h.screen2 160 40 $COMPASS 230



		} elseif {[string match "ETC;*" $line]} {
#			set line2 [split $line ";"]
#			.frame1.last_error configure -text "Last-Error: [lindex $line2 1]"
#			.frame1.quality_errors configure -text "Quality-Errors: [lindex $line2 2]/[lindex $line2 3] ([lindex $line2 4]%)"
#			.frame2.txid configure -text "TXID: [lindex $line2 5]"
#			.frame2.setup_txid configure -text "Setup-TXID: [lindex $line2 6]"
#			.frame2.seq configure -text "Sequence: [lindex $line2 7]"
		}
		set line ""
	} else {
		set line "$line$msg"
	}
}


wm geometry . 240x320
wm title . "RCOPEN24 RX-Tool"

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

	frame .c.l
	pack .c.l -side left -fill x

		canvas .c.l.screen12 -relief raised -width 35 -height 45
		pack .c.l.screen12 -side top
		.c.l.screen12 create rec 1 1 35 45 -fill black

		canvas .c.l.screen1 -relief raised -width 35 -height 160
		pack .c.l.screen1 -side top
		.c.l.screen1 create rec 1 1 35 160 -fill black
		for {set N 0} {$N < 150} {set N [expr $N + 5]} {
			.c.l.screen1 create line 30 [expr 150 - $N] 35 [expr 150 - $N] -fill $FRONT_COLOR2
		}
		for {set N 0} {$N < 150} {set N [expr $N + 20]} {
			.c.l.screen1 create line 25 [expr 150 - $N] 35 [expr 150 - $N] -fill $FRONT_COLOR2
			.c.l.screen1 create text 10 [expr 150 - $N] -text "$N" -tags compass_text -fill $FRONT_COLOR
		}

	frame .c.h
	pack .c.h -side left -fill x

		canvas .c.h.screen2 -relief raised -width 160 -height 45
		pack .c.h.screen2 -side top

		canvas .c.h.screen4 -relief raised -width 160 -height 160
		pack .c.h.screen4 -side top

		set SIZE 80
		set X 80
		set Y 80
		.c.h.screen4 create rectangle [expr $X - $SIZE] [expr $Y - $SIZE] [expr $X + $SIZE] [expr $Y + $SIZE] -fill black
		.c.h.screen4 create oval [expr $X - $SIZE] [expr $Y - $SIZE] [expr $X + $SIZE] [expr $Y + $SIZE] -outline $FRONT_COLOR
		for {set N 0} {$N <= 360} {set N [expr $N + 5]} {
			.c.h.screen4 create arc [expr $X - $SIZE] [expr $Y - $SIZE] [expr $X + $SIZE] [expr $Y + $SIZE] -start $N -extent 0 -fill yellow -width 1 -outline $FRONT_COLOR2
		}
		.c.h.screen4 create oval [expr $X - $SIZE + 5] [expr $Y - $SIZE + 5] [expr $X + $SIZE - 5] [expr $Y + $SIZE - 5] -outline black -fill black
		for {set N 0} {$N <= 360} {set N [expr $N + 15]} {
			.c.h.screen4 create arc [expr $X - $SIZE] [expr $Y - $SIZE] [expr $X + $SIZE] [expr $Y + $SIZE] -start $N -extent 0 -fill red -width 1 -outline $FRONT_COLOR
		}
		.c.h.screen4 create oval [expr $X - $SIZE + 7] [expr $Y - $SIZE + 7] [expr $X + $SIZE - 7] [expr $Y + $SIZE - 7] -outline black -fill black


	frame .c.r
	pack .c.r -side left -fill x

		canvas .c.r.screen12 -relief raised -width 35 -height 45
		pack .c.r.screen12 -side top
		.c.r.screen12 create rec 1 1 35 45 -fill black

		canvas .c.r.screen3 -relief raised -width 35 -height 160
		pack .c.r.screen3 -side top
		.c.r.screen3 create rec 1 1 35 160 -fill black
		for {set N 0} {$N < 150} {set N [expr $N + 5]} {
			.c.r.screen3 create line 1 [expr 150 - $N] 5 [expr 150 - $N] -fill $FRONT_COLOR2
		}
		for {set N 0} {$N < 150} {set N [expr $N + 20]} {
			.c.r.screen3 create line 1 [expr 150 - $N] 10 [expr 150 - $N] -fill $FRONT_COLOR2
			.c.r.screen3 create text 25 [expr 150 - $N] -text "[expr $N * 2]" -tags compass_text -fill $FRONT_COLOR
		}

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

	canvas .b.screen13 -relief raised -width 234 -height 110
	pack .b.screen13 -side left
	.b.screen13 create rec 1 1 234 110 -fill black




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]
puts -nonewline $Serial "c"






