chiark / gitweb /
bugfixes
[trains.git] / hostside / stopgap-controller
index ace2905b27c5bb1ad6136dc57325dcd424c9a858..12959e7fc414a65e72116cb3ede0de3a6ae62d23 100755 (executable)
@@ -1,16 +1,21 @@
 #!/usr/bin/tclsh8.2
+# used like this:
+# liberator:hostside> ssh bessar 'cd things/trains-bessar/hostside && ./stopgap-controller' | ./gui-displayer -
 
 set testonly 0
 #set testonly 1
 set port /dev/ttya0
+#set port /dev/ttyS0
 
 set ch(funcsevery) 10
-set ch(speeddirnevery) 12
+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
 
@@ -19,11 +24,14 @@ set polmsg(x) 90f802
 set polmsg(y) 90807c
 set pname l
 set m {}
+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_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
@@ -37,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] {
@@ -75,7 +84,7 @@ proc fail_now {} {
     debug "failing now"
     fconfigure $p -blocking yes
     gui "P 0"
-    tellpic 20
+    tellpic 10
     exit 1
 }
 
@@ -84,7 +93,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 {}
 }
 
@@ -137,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]} {
@@ -157,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
 }
@@ -271,24 +298,28 @@ proc pm_detect {seg} {
 }
 
 proc tellnmra {m} {
-    global nmrawhich speeddirn funcs
-    set m 0x$m
-    for {set i 0} {$i < $m} {incr i} {
-       tellpic_q [lindex [list $speeddirn $funcs] $nmrawhich]
-       set nmrawhich [expr {!$nmrawhich}]
-    }
+#    global nmrawhich speeddirn funcs
+#    set m 0x$m
+#    for {set i 0} {$i < $m} {incr i} {
+#      tellpic_q [lindex [list $speeddirn $funcs] $nmrawhich]
+#      set nmrawhich [expr {!$nmrawhich}]
+#    }
 }
 
 proc watchdog {} {
-    global watchdog testonly
+    global watchdog testonly speeddirn funcs nmradiv
     catch { after cancel $watchdog }
     set watchdog [after 50 watchdog]
     tellpic_q 9808 ;# 128ms
+    if {[incr nmradiv] > 35} {
+       tellpic_q $speeddirn$funcs
+       set nmradiv 0
+    }
 }
 
 proc pm_hello {} {
     debug "got hello, starting up"
-    tellpic 21
+    tellpic 11
     gui "P 1"
     watchdog
     changewhat
@@ -305,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 }
@@ -319,7 +351,7 @@ proc onreadp_test {} {
 }
 
 proc onreadp {} {
-    global p m
+    global p m rand
     while 1 {
        set c [read $p 1]
        if {![string length $c]} {
@@ -327,6 +359,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]} {
@@ -342,6 +378,7 @@ proc newspeeddirn {} {
     set speed [expr {round(($b1 * $b1) / 65535.0 * 100.0 + 26.0)}]
     set b2 0x[randbyte]
     set dirn [expr {$b2 / 128}]
+    set dirn 0
     debug "speeddirn b1=$b1 speed=$speed b2=$b2 dirn=$dirn"
     return "speed126 2 $speed $dirn"
 }
@@ -353,23 +390,28 @@ proc newfuncs {} {
     return "funcs5to8 2 $value"
 }
 
-proc maybechange {thing} {
+proc maybechange {thing force} {
     global $thing ch
     upvar #0 ${thing}_fixed fixed
     if {![info exists fixed]} {
-       set rb 0x[randbyte][randbyte]
-       if {
-           $rb / 65536.0 >
-           1.0 / (($ch(${thing}every) - $ch(minint)*0.001) * $ch(scale))
-       } {
-           debug "maybechange $thing rb=$rb no"
-           return 0
+       if {$force} {
+           debug "maybechange $thing forced ..."
+       } else {
+           set rb 0x[randbyte][randbyte]
+           if {
+               $rb / 65536.0 >
+               1.0 / (($ch(${thing}every) - $ch(minint)*0.001) * $ch(scale))
+           } {
+               debug "maybechange $thing rb=$rb no"
+               return 0
+           }
+           debug "maybechange $thing rb=$rb yes ..."
        }
-       debug "maybechange $thing rb=$rb yes ..."
        set l [new$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
@@ -381,7 +423,7 @@ proc maybechange {thing} {
 proc changewhat {} {
     global ch chwa
     catch { after cancel $chwa }
-    if {[maybechange speeddirn] || [maybechange funcs]} {
+    if {[maybechange speeddirn 0] || [maybechange funcs 0]} {
        set interval $ch(minint)
     } else {
        set interval 1000
@@ -389,8 +431,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]
     
@@ -404,16 +458,46 @@ 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_speed_now {l} {
+    global speeddirn_fixed
+    set speeddirn_fixed $l
+    maybechange speeddirn 1
+}
+
+proc ask_fast {} { ask_speed_now {speed126 2 126 0} }
+proc ask_slow {} { ask_speed_now {speed126 2 10 0} }
+proc ask_randspeed {} {
+    global speeddirn_fixed; catch { unset speeddirn_fixed }
+    maybechange speeddirn 1
+}    
+
+proc ask_manual {rightp} { global pointasked; set pointasked M$rightp }
+proc ask_figureeightt {} { global pointasked; set pointasked 0 }
+proc ask_loop {} { global pointasked; set pointasked 1 }
+proc ask_randpath {} { global pointasked; catch { unset pointasked } }
+
 setup
 gui_init
 vwait end