chiark / gitweb /
fixes from bessar fettling session
[trains.git] / hostside / stopgap-controller
index 99f351636555b53f2a7a05298ee6ece4bbea2b29..0c4f3342c2e3846e6b58429e665cecfc22a77c45 100755 (executable)
@@ -5,6 +5,7 @@
 set testonly 0
 #set testonly 1
 set port /dev/ttya0
+#set port /dev/ttyS0
 
 set ch(funcsevery) 10
 set ch(speeddirnevery) 30
@@ -25,9 +26,10 @@ set nmradiv 0
 set segs {xx yy}
 set segsasgot {xx yy}
 set pq {} ;# unset: cdu charged and waiting
-#set speeddirn ff7f
-set speeddirn ffff80c3fbcced7f
+set speeddirn ff7f
+#set speeddirn ffff80c3fbcced7f
 #set speeddirn_fixed {speed126 2 80 0}
+set speeddirn_fixed {}
 set funcs ff7f
 # unset pointpos($point)
 # unset segdetect($seg) ;# unset: shown D0; {}: shown D1; or: after id, D1->0
@@ -41,6 +43,7 @@ proc gui_init {} {
     gui "M A2 0"
     gui "M A5 0 J"
     gui "M A6 0 J"
+    gui "EOE"
     if {[info exists watchdog]} { gui "P 1" }
     gui_polarity
     foreach seg [array names segdetect] {
@@ -79,7 +82,7 @@ proc fail_now {} {
     debug "failing now"
     fconfigure $p -blocking yes
     gui "P 0"
-    tellpic 20
+    tellpic 10
     exit 1
 }
 
@@ -88,7 +91,7 @@ proc fail {m} {
     catch { after cancel $watchdog; unset watchdog }
     puts "failing $m"
     tellpic 9801 ;# 16ms
-    after 2000 fail_now
+    after 1000 fail_now
     fileevent $p readable {}
 }
 
@@ -141,6 +144,12 @@ proc pt_ifthenmust {ifpoint ifposwant thenpoint thenpos} {
     pt_must $thenpoint $thenpos
 }
 
+proc badwatchdog {} {
+    global pq
+    puts "watchdog - oh well"
+    if {![info exists pq]} { set pq {} }
+}
+
 proc pm_charged {} {
     global pq
     if {[llength $pq]} {
@@ -296,7 +305,7 @@ proc watchdog {} {
 
 proc pm_hello {} {
     debug "got hello, starting up"
-    tellpic 21
+    tellpic 11
     gui "P 1"
     watchdog
     changewhat
@@ -313,6 +322,7 @@ proc frompic {m} {
        01 - 02 { tellnmra $m }
        09 { fp $m; pm_hello }
        07 { puts "short circuit"; exit 1 }
+       0d { fp $m; badwatchdog }
        28 { fp $m; pm_charged }
        9[0-7] { fp $m; pm_maydetect 0 $v }
        9? { fp $m; pm_maydetect 1 $v }
@@ -327,7 +337,7 @@ proc onreadp_test {} {
 }
 
 proc onreadp {} {
-    global p m
+    global p m rand
     while 1 {
        set c [read $p 1]
        if {![string length $c]} {
@@ -335,6 +345,10 @@ proc onreadp {} {
            return
        }
        binary scan $c H* x
+       if {![info exists rand]} {
+           fp ...$x
+           return
+       }
        lappend m $x
        if {[regexp {^[0-7]} $x]} {
            if {![regexp {^x} $m]} {
@@ -379,6 +393,7 @@ proc maybechange {thing} {
     } else {
        debug "fixed $thing $fixed"
        set l $fixed
+       if {![llength $l]} { return 0 }
     }
     set bin [eval exec ./hostside-old -s/dev/stdout $l]
     binary scan $bin H* x
@@ -398,8 +413,20 @@ proc changewhat {} {
     set chwa [after $interval changewhat]
 }
 
+proc onreadcmd {} {
+    if {[gets stdin l] < 0} {
+       if {[eof stdin]} {
+           puts "GUI exit 0"
+           fail "stopgap-controller got eof, quitting"
+           fileevent stdin readable {}
+       }
+       return
+    }
+    eval $l
+}
+
 proc setup {} {
-    global port p rand testonly
+    global port p testonly
     if {!$testonly} {
        set p [open $port {RDWR NONBLOCK} 0]
     
@@ -413,15 +440,37 @@ proc setup {} {
                -blocking false -buffering none
 
        fileevent $p readable onreadp
+       fconfigure stdin -blocking false
+       fileevent stdin readable onreadcmd
     } else {
        set p stdin
        fconfigure stdin -blocking false
        fileevent stdin readable onreadp_test
     }
 
+    after 250 setup_complete
+}
+
+proc setup_complete {} {
+    global rand
     set rand [open /dev/urandom {RDONLY} 0]
     fconfigure $rand -encoding binary -translation binary
+    tellpic 0a
+}
+
+
+#----------
+# for keyboard control
+
+proc ask_fast {} {
+    global speeddirn_fixed; set speeddirn_fixed {speed126 2 126 0}
+}
+proc ask_slow {} {
+    global speeddirn_fixed; set speeddirn_fixed {speed126 2 10 0}
 }
+proc ask_randspeed {} {
+    global speeddirn_fixed; catch { unset speeddirn_fixed }
+}    
 
 setup
 gui_init