chiark / gitweb /
path-setting
[trains.git] / hostside / stopgap-controller
index 065db6b15b6f101e4a3d8d0613c00d91a423ad18..3b3348f24d2ab92ccfc0a7f2d381ef884f074451 100755 (executable)
@@ -5,14 +5,17 @@
 set testonly 0
 #set testonly 1
 set port /dev/ttya0
+#set port /dev/ttyS0
 
 set ch(funcsevery) 10
 set ch(speeddirnevery) 30
 set ch(scale) 1
 
 set ch(minint) 5000
-# unset always
-# set always 0
+
+#unset pointasked
+# 0 1 (settings) M0 M1 (manual, settings) unset (random) M (manual)
+
 set nmrawhich 0
 set lastptchosen xx
 
@@ -25,9 +28,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 +45,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] {
@@ -88,7 +93,7 @@ proc fail {m} {
     catch { after cancel $watchdog; unset watchdog }
     puts "failing $m"
     tellpic 9801 ;# 16ms
-    after 10000 fail_now
+    after 1000 fail_now
     fileevent $p readable {}
 }
 
@@ -141,6 +146,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]} {
@@ -161,15 +172,27 @@ proc randbyte {} {
 }
 
 proc pt_maybe {point oneisright} {
-    global always lastptchosen
-    if {[info exists always]} {
-       set pos $always
-    } else {
+    global pointasked lastptchosen
+    if {![info exists pointasked]} {
        if {![string compare $point $lastptchosen]} return
        set lastptchosen $point
        set x [randbyte]
        set pos [expr [regexp {^[89a-f]} $x] ? 1 : 0]
        debug "chose point $point pos=$pos (x=$x)"
+    } elseif {[regexp {^M([01])$} $pointasked dummy pos]} {
+       if {[lsearch -exact {40 02} [lindex $point 0]] >= 0} {
+           set pos [expr {!$pos}]
+           debug "chose point $point pos=$pos manual-rl"
+       } else {
+           debug "chose point $point pos=$pos manual-lr"
+       }
+       set pointasked M
+    } elseif {![string compare $pointasked M]} {
+       debug "leave point $point pos=$pos manual"
+       return
+    } else {
+       debug "fixed point $point pos=$pos"
+       set pos $pointasked
     }
     pt_must $point $pos
 }
@@ -313,6 +336,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 }
@@ -383,6 +407,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
@@ -404,7 +429,11 @@ proc changewhat {} {
 
 proc onreadcmd {} {
     if {[gets stdin l] < 0} {
-       if {[eof stdin]} { fileevent stdin readable {} }
+       if {[eof stdin]} {
+           puts "GUI exit 0"
+           fail "stopgap-controller got eof, quitting"
+           fileevent stdin readable {}
+       }
        return
     }
     eval $l
@@ -443,6 +472,25 @@ proc setup_complete {} {
     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 }
+}    
+
+proc ask_manual {rightp} { global pointasked; set pointasked M$rightp }
+proc ask_figureeightt {rightp} { global pointasked; set pointasked 0 }
+proc ask_loop {rightp} { global pointasked; set pointasked 1 }
+proc ask_randpath {rightp} { global pointasked; catch { unset pointasked } }
+
 setup
 gui_init
 vwait end