3 # liberator:hostside> ssh bessar 'cd things/trains-bessar/hostside && ./stopgap-controller' | ./gui-displayer -
13 set ch(speeddirnevery) 30
19 # 0 1 (settings) M0 M1 (manual, settings) unset (random) M (manual)
32 set pq {} ;# unset: cdu charged and waiting
35 set askspeeds {10 30 50 80 100 126}
36 #set speeddirn ffff80c3fbcced7f
37 #set speeddirn_fixed {speed126 2 80 0}
38 set speeddirn_fixed {}
40 # unset pointpos($point)
41 # unset segdetect($seg) ;# unset: shown D0; {}: shown D1; or: after id, D1->0
43 set funcsr1 {0x061 0x020 0x000 0x040 0x060}
44 set funcsl1 {0x181 0x080 0x000 0x100 0x180}
45 set funcsr2 {0x021 0x020 0x000}
46 set funcsl2 {0x041 0x040 0x000}
54 global watchdog polarity segdetect
59 if {[info exists watchdog]} { gui "P 1" }
61 foreach seg [array names segdetect] {
73 set b [binary format H* $m]
84 global errorInfo errorCode
85 puts stderr "$m\n$errorCode\n$errorInfo"
95 fconfigure $p -blocking yes
103 catch { after cancel $watchdog; unset watchdog }
107 fileevent $p readable {}
110 proc gui_polarity {} {
113 switch -exact $pname {
114 l { lappend 0 X1 X3 X5 X7 X9; lappend 0 X2 X4 X6 X8 X10 }
115 x { lappend 1 X1 X3 X5 X7 X9; lappend 0 X2 X4 X6 X8 X10 }
116 y { lappend 0 X1 X3 X5 X7 X9; lappend 1 X2 X4 X6 X8 X10 }
119 foreach seg [set $v] {
125 proc polarity {newpname} {
127 debug "polarising $newpname"
128 if {![string compare $pname $newpname]} return
129 tellpic $polmsg($newpname)
134 proc pt_now {how point pos xtra} {
135 set msg a0[lindex $point $pos]
136 debug "$how point $point pos=$pos msg=$msg$xtra"
137 gui "M [lindex $point 2] [expr {!$pos}]"
140 proc pt_must {point newpos} {
141 upvar #0 pointpos($point) pos
143 if {[info exists pos] && $pos == $newpos} return
145 if {[info exists pq]} {
146 lappend pq [list $point $pos]
147 debug "queue point $point pos=$pos l=[llength $pq]"
150 pt_now immed $point $pos {}
153 proc pt_ifthenmust {ifpoint ifposwant thenpoint thenpos} {
154 upvar #0 pointpos($ifpoint) ifpos
155 if {![info exists ifpos] || $ifpos != $ifposwant} return
156 pt_must $thenpoint $thenpos
159 proc badwatchdog {} {
161 puts "watchdog - oh well"
162 if {![info exists pq]} { set pq {} }
169 set pq [lrange $pq 1 end]
170 pt_now nowdo [lindex $v 0] [lindex $v 1] " l=[llength $pq]"
179 set c [read $rand 1]; if {![string length $c]} { error "eof on rand" }
184 proc pt_maybe {point oneisright} {
185 global pointasked lastptchosen
186 if {![info exists pointasked]} {
187 if {![string compare $point $lastptchosen]} return
188 set lastptchosen $point
190 set pos [expr [regexp {^[89a-f]} $x] ? 1 : 0]
191 debug "chose point $point pos=$pos (x=$x)"
192 } elseif {[regexp {^M([01])$} $pointasked dummy pos]} {
193 if {[lsearch -exact {40 02} [lindex $point 0]] >= 0} {
194 set pos [expr {!$pos}]
195 debug "chose point $point pos=$pos manual-rl"
197 debug "chose point $point pos=$pos manual-lr"
200 } elseif {![string compare $pointasked M]} {
201 debug "leave point $point manual"
205 debug "fixed point $point pos=$pos"
211 upvar #0 segdetect($seg) segd
212 if {![info exists segd]} {
213 debug "segment $seg = already"
214 } elseif {[string length $segd]} {
215 debug "segment $seg = pending already"
217 debug "segment $seg = soon"
218 set segd [after 100 s0t $seg]
222 upvar #0 segdetect($seg) segd
223 debug "segment $seg = now"
228 upvar #0 segdetect($seg) segd
229 if {![info exists segd]} {
231 debug "segment $seg ! (overwrites =)"
232 } elseif {[string length $segd]} {
233 debug "segment $seg ! (cancels =)"
236 debug "segment $seg ! already"
243 proc pm_maydetect {d seg} {
264 #proc pm_nodetect {seg} {
266 # if {![string compare $seg [lindex $segsasgot 1]]} {
267 # set segsasgot [list [lindex $segsasgot 1] [lindex $segsasgot 0]]
271 proc pm_detect {seg} {
272 global segs pname segsasgot
273 if {[string compare $seg [lindex $segsasgot 1]]} {
274 set segsasgot [list [lindex $segsasgot 1] $seg]
276 if {[lsearch -exact $segs $seg] < 0} {
279 debug "pm_detect $seg ($segsasgot) ($segs) $pname$seg"
280 # if {[lsearch -exact {
281 # 06 09 0a 04 02 07 14 20
284 # } $seg] < 0} return
285 switch -exact $pname$seg {
286 l16 - l1c - l08 - l0b { polarity y }
287 l10 - l1a - l03 - l05 { polarity x }
288 x07 - x04 - x0a { polarity l }
289 x16 - x1c - x14 - x0b { polarity y }
290 y06 - y04 - y0a { polarity l }
291 y20 - y10 - y1a - y05 { polarity x }
294 04 - 0a { pt_must "00 01 X7" 1; pt_must "40 41 X8" 1 }
295 05 { pt_must "00 01 X7" 0 }
296 0b { pt_must "40 41 X8" 0 }
297 16 - 1c { pt_must "02 03 A5" 0 }
298 1a - 10 { pt_must "42 43 A6" 0 }
299 14 { pt_ifthenmust "02 03 A5" 1 "42 43 A6" 1 }
300 20 { pt_ifthenmust "42 43 A6" 1 "02 03 A5" 1 }
302 switch -exact [join $segs -] {
303 02-07 { pt_maybe "02 03 A5" 1 }
304 07-02 { pt_maybe "00 01 X7" 0 }
305 09-06 { pt_maybe "42 43 A6" 0 }
306 06-09 { pt_maybe "40 41 X8" 1 }
311 # global nmrawhich speeddirn funcs
313 # for {set i 0} {$i < $m} {incr i} {
314 # tellpic_q [lindex [list $speeddirn $funcs] $nmrawhich]
315 # set nmrawhich [expr {!$nmrawhich}]
320 global watchdog testonly speeddirn funcs nmradiv
321 catch { after cancel $watchdog }
322 set watchdog [after 50 watchdog]
323 tellpic_q 9808 ;# 128ms
324 if {[incr nmradiv] > 35} {
325 tellpic_q $speeddirn$funcs
331 debug "got hello, starting up"
345 switch -glob [lindex $m 0] {
346 01 - 02 { tellnmra $m }
347 09 { fp $m; pm_hello }
348 07 { puts "short circuit"; exit 1 }
349 0d { fp $m; badwatchdog }
350 28 { fp $m; pm_charged }
351 9[0-7] { fp $m; pm_maydetect 0 $v }
352 9? { fp $m; pm_maydetect 1 $v }
353 0a - [234567]? { puts "pic debug $m" }
354 * { fp $m; fail "pic unknown $m" }
358 proc onreadp_test {} {
359 if {![gets stdin m]} { return }
367 if {![string length $c]} {
368 if {[eof $p]} { error "eof on device" }
372 if {![info exists rand]} {
377 if {[regexp {^[0-7]} $x]} {
378 if {![regexp {^x} $m]} {
386 proc newspeeddirn {} {
389 set speed [expr {round(($b1 * $b1) / 65535.0 * 100.0 + 26.0)}]
391 set dirn [expr {$b2 / 128}]
393 debug "speeddirn b1=$b1 speed=$speed b2=$b2 dirn=$dirn"
394 return "speed126 $loco $speed $dirn"
397 proc funcs_removebits {lr headent} {
399 set funcsval [format 0x%x [expr {$funcsval & ~$headent}]]
401 proc funcs_addbits {lr list} {
403 set headent [lindex $list 0]
407 set rand 0x[randbyte]0
408 set add [expr {$add & $rand}]
409 set val [expr {$val | $add}]
410 debug "funcs $lr v=$funcsval add=$add new=$val rand=$rand ($list)"
412 set val [expr {$val | $add}]
413 debug "funcs $lr v=$funcsval add=$add new=$val ($list)"
418 proc funcsnmralist {} {
420 return "funcs5to8 $loco $funcsval"
426 upvar #0 funcs${lr}${loco} list
427 set now [lindex $list 0]
428 funcs_removebits $lr $now
429 funcs_addbits $lr $list
431 return [funcsnmralist]
434 proc nmrachange {thing argstring} {
436 set bin [eval exec ./hostside-old -s/dev/stdout $argstring]
437 binary scan $bin H* x
438 debug "changed $thing=$x ($argstring)"
442 proc maybechange {thing force} {
444 upvar #0 ${thing}_fixed fixed
445 if {![info exists fixed]} {
447 debug "maybechange $thing forced ..."
449 set rb 0x[randbyte][randbyte]
452 1.0 / (($ch(${thing}every) - $ch(minint)*0.001) * $ch(scale))
454 debug "maybechange $thing rb=$rb no"
457 debug "maybechange $thing rb=$rb yes ..."
461 debug "fixed $thing $fixed"
463 if {![llength $l]} { return 0 }
471 catch { after cancel $chwa }
472 if {[maybechange speeddirn 0] + [maybechange funcs 0]} {
473 set interval $ch(minint)
477 set chwa [after $interval changewhat]
481 if {[gets stdin l] < 0} {
484 fail "stopgap-controller got eof, quitting"
485 fileevent stdin readable {}
493 global port p testonly
494 fconfigure stdout -buffering none
496 set p [open $port {RDWR NONBLOCK} 0]
498 exec stty -F $port min 1 time 0 -istrip -ocrnl -onlcr -onocr -opost \
499 -ctlecho -echo -echoe -echok -echonl -iexten -isig \
501 9600 clocal cread crtscts -hup -parenb cs8 -cstopb \
502 -ixoff bs0 cr0 ff0 nl0 -ofill -olcuc
504 fconfigure $p -encoding binary -translation binary \
505 -blocking false -buffering none
507 fileevent $p readable onreadp
508 fconfigure stdin -blocking false
509 fileevent stdin readable onreadcmd
512 fconfigure stdin -blocking false
513 fileevent stdin readable onreadp_test
516 after 250 setup_complete
519 proc setup_complete {} {
522 set rand [open /dev/urandom {RDONLY} 0]
523 fconfigure $rand -encoding binary -translation binary
528 # for keyboard control
530 proc ask_speed {updown} {
531 global speeddirn_fixed askspeeds askspeedix loco
532 set ll [llength $askspeeds]
533 if {![info exists askspeedix]} {
534 set askspeedix [expr {
535 int($ll * 0.5 - 0.5 + 0.5 * $updown)
538 incr askspeedix $updown
539 if {$askspeedix < 0} { set askspeedix 0 }
540 if {$askspeedix >= $ll} { set askspeedix [expr {$ll - 1}] }
542 set speed [lindex $askspeeds $askspeedix]
543 set speeddirn_fixed [list speed126 $loco $speed 0]
544 maybechange speeddirn 1
547 proc ask_randspeed {} {
548 global speeddirn_fixed askspeedix
549 catch { unset speeddirn_fixed }
550 catch { unset askspeedix }
551 maybechange speeddirn 1
554 proc ask_funcs {lr} {
556 upvar #0 funcs${lr}${loco} list
557 set now [lindex $list 0]
558 funcs_removebits $lr $now
559 set list [concat [lrange $list 1 end] $now]
560 funcs_addbits $lr $list
561 nmrachange funcs [funcsnmralist]
564 proc ask_figureeight {} { global pointasked; set pointasked 0 }
565 proc ask_loop {} { global pointasked; set pointasked 1 }
566 proc ask_randpath {} { global pointasked; catch { unset pointasked } }