chiark / gitweb /
proper syntax for xset rune
[trains.git] / hostside / stopgap-controller
index 99f351636555b53f2a7a05298ee6ece4bbea2b29..bfed27853760e36d1ab6dcdcb4808425268c2116 100755 (executable)
@@ -5,16 +5,23 @@
 set testonly 0
 #set testonly 1
 set port /dev/ttya0
+#set port /dev/ttyS0
+
+set loco 2
 
 set ch(funcsevery) 10
 set ch(speeddirnevery) 30
 set ch(scale) 1
 
 set ch(minint) 5000
-# unset always
-# set always 0
+
+set pointprobs {0 0x010 0x080 0x0f0 0x100}
+set pointprobix 0
+set pointabs 1 ;# 0 or 1
+
 set nmrawhich 0
 set lastptchosen xx
+set pointprob [lindex $pointprobs $pointprobix]
 
 set polmsg(l) 908000
 set polmsg(x) 90f802
@@ -25,13 +32,22 @@ 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
+#unset askspeedix
+set askspeeds {10 30 50 80 100 126}
+#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
 
+set funcsr1 {0x061 0x020 0x000 0x040 0x060}
+set funcsl1 {0x182 0x080 0x000 0x100 0x180}
+set funcsr2 {0x021 0x020 0x000}
+set funcsl2 {0x042 0x040 0x000}
+set funcsval 0x000
+
 proc gui {m} {
     puts "GUI $m"
 }
@@ -41,6 +57,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 +96,7 @@ proc fail_now {} {
     debug "failing now"
     fconfigure $p -blocking yes
     gui "P 0"
-    tellpic 20
+    tellpic 10
     exit 1
 }
 
@@ -88,7 +105,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 +158,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 +184,20 @@ proc randbyte {} {
 }
 
 proc pt_maybe {point oneisright} {
-    global always lastptchosen
-    if {[info exists always]} {
-       set pos $always
+    upvar #0 pointpos($point) oldpos
+    global lastptchosen pointprob pointabs
+    if {![string compare $point $lastptchosen]} return
+    set lastptchosen $point
+    set x 0x[randbyte]
+    set pos [expr {$x < $pointprob ? 1 : 0}]
+    if {$pointabs} {
+       debug "chose point $point $pos (abs x=$x prob=$pointprob)"
+       set pos [expr {!$pos}]
+    } elseif {[info exists oldpos] && !$oldpos} {
+       debug "chose point $point $pos (0-> x=$x prob=$pointprob)"
     } else {
-       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)"
+       set pos [expr {!$pos}]
+       debug "chose point $point $pos (1-> x=$x prob=$pointprob)"
     }
     pt_must $point $pos
 }
@@ -296,7 +324,7 @@ proc watchdog {} {
 
 proc pm_hello {} {
     debug "got hello, starting up"
-    tellpic 21
+    tellpic 11
     gui "P 1"
     watchdog
     changewhat
@@ -313,6 +341,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 +356,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 +364,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]} {
@@ -346,51 +379,92 @@ proc onreadp {} {
 }
 
 proc newspeeddirn {} {
+    global loco
     set b1 0x[randbyte]
     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"
+    return "speed126 $loco $speed $dirn"
+}
+
+proc funcs_removebits {lr headent} {
+    global funcsval
+    set funcsval [format 0x%x [expr {$funcsval & ~$headent}]]
+}
+proc funcs_addbits {lr list} {
+    global loco funcsval
+    set headent [lindex $list 0]
+    set val $funcsval
+    set add $headent
+    if {$add & 0x02} {
+       set rand 0x[randbyte]0
+       set add [expr {$add & $rand}]
+       set val [expr {$val | $add}]
+       debug "funcs $lr v=$funcsval add=$add new=$val rand=$rand ($list)"
+    } else {
+       set val [expr {$val | $add}]
+       debug "funcs $lr v=$funcsval add=$add new=$val ($list)"
+    }
+    set funcsval $val
+}
+
+proc funcsnmralist {} {
+    global loco funcsval
+    return "funcs5to8 $loco $funcsval"
 }
 
 proc newfuncs {} {
-    set b3 0x[randbyte]
-    set value [expr {($b3 & 127) * 16}]
-    debug "funcs b3=$b3 value=[format %x $value]"
-    return "funcs5to8 2 $value"
+    global loco funcsval
+    foreach lr {l r} {
+       upvar #0 funcs${lr}${loco} list
+       set now [lindex $list 0]
+       funcs_removebits $lr $now
+       funcs_addbits $lr $list
+    }
+    return [funcsnmralist]
+}
+
+proc nmrachange {thing argstring} {
+    global $thing
+    set bin [eval exec ./hostside-old -s/dev/stdout $argstring]
+    binary scan $bin H* x
+    debug "changed $thing=$x ($argstring)"
+    set $thing ff$x
 }
 
-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
-    debug "changed $thing=$x"
-    set $thing ff$x
+    nmrachange $thing $l
     return 1
 }
 
 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
@@ -398,8 +472,21 @@ 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 stateshowpipe
+    fconfigure stdout -buffering none
     if {!$testonly} {
        set p [open $port {RDWR NONBLOCK} 0]
     
@@ -413,16 +500,98 @@ proc setup {} {
                -blocking false -buffering none
 
        fileevent $p readable onreadp
+       fconfigure stdin -blocking false
+       fileevent stdin readable onreadcmd
+       set stateshowpipe [open /tmp/train-state w]
+       fconfigure $stateshowpipe -buffering none
     } else {
        set p stdin
        fconfigure stdin -blocking false
        fileevent stdin readable onreadp_test
+       set stateshowpipe [open /dev/null w]
     }
 
+    after 250 setup_complete
+}
+
+proc setup_complete {} {
+    global rand
+    exec xset s off
     set rand [open /dev/urandom {RDONLY} 0]
     fconfigure $rand -encoding binary -translation binary
+    tellpic 0a
+}
+
+#----------
+# for keyboard control
+
+proc updownfromlist {wholelistv ixv updown} {
+    upvar #0 $wholelistv wholelist
+    upvar #0 $ixv ix
+    set ll [llength $wholelist]
+    if {![info exists ix]} {
+       set old ?
+       set ix [expr {
+           int($ll * 0.5 - 0.5 + 0.5 * $updown)
+       }]
+    } else {
+       set old $ix
+       incr ix $updown
+       if {$ix < 0} { set ix 0 }
+       if {$ix >= $ll} { set ix [expr {$ll - 1}] }
+    }
+    set val [lindex $wholelist $ix]
+    debug "updownfromlist ix:$old->$ix /$ll $val ($wholelist)"
+    return $val
+}
+
+proc ask_speed {updown} {
+    global speeddirn_fixed loco
+    set speed [updownfromlist askspeeds askspeedix $updown]
+    set speeddirn_fixed [list speed126 $loco $speed 0]
+    maybechange speeddirn 1
+}
+
+proc ask_randspeed {} {
+    global speeddirn_fixed askspeedix
+    catch { unset speeddirn_fixed }
+    catch { unset askspeedix }
+    maybechange speeddirn 1
+}    
+
+proc ask_funcs {lr} {
+    global loco
+    upvar #0 funcs${lr}${loco} list
+    set now [lindex $list 0]
+    funcs_removebits $lr $now
+    set list [concat [lrange $list 1 end] $now]
+    funcs_addbits $lr $list
+    nmrachange funcs [funcsnmralist]
+}
+
+proc ask_pointprob {updown} {
+    global pointprob
+    set pointprob [updownfromlist pointprobs pointprobix $updown]
+}
+
+proc ask_pointrelabs {} {
+    global pointabs
+    set pointabs [expr {!$pointabs}]
+}
+
+proc ask_show {} {
+    global loco stateshowpipe pointprob pointabs askspeedix
+    upvar #0 funcsr$loco fr
+    upvar #0 funcsl$loco fl
+    if {[info exists askspeedix]} { set spd $askspeedix } { set spd r }
+    puts -nonewline $stateshowpipe [format \
+           "\nL$loco P%03x%s F%03x S%s" \
+           $pointprob [lindex {R A} $pointabs] \
+           [expr {[lindex $fr 0] | [lindex $fl 0]}] \
+           $spd]
 }
 
 setup
 gui_init
+ask_show
 vwait end