3 # liberator:hostside> ssh bessar 'cd things/trains-bessar/hostside && ./stopgap-controller' | ./gui-displayer -
11 set ch(speeddirnevery) 30
17 # 0 1 (settings) M0 M1 (manual, settings) unset (random) M (manual)
30 set pq {} ;# unset: cdu charged and waiting
32 #set speeddirn ffff80c3fbcced7f
33 #set speeddirn_fixed {speed126 2 80 0}
34 set speeddirn_fixed {}
36 # unset pointpos($point)
37 # unset segdetect($seg) ;# unset: shown D0; {}: shown D1; or: after id, D1->0
44 global watchdog polarity segdetect
49 if {[info exists watchdog]} { gui "P 1" }
51 foreach seg [array names segdetect] {
63 set b [binary format H* $m]
74 global errorInfo errorCode
75 puts stderr "$m\n$errorCode\n$errorInfo"
85 fconfigure $p -blocking yes
93 catch { after cancel $watchdog; unset watchdog }
97 fileevent $p readable {}
100 proc gui_polarity {} {
103 switch -exact $pname {
104 l { lappend 0 X1 X3 X5 X7 X9; lappend 0 X2 X4 X6 X8 X10 }
105 x { lappend 1 X1 X3 X5 X7 X9; lappend 0 X2 X4 X6 X8 X10 }
106 y { lappend 0 X1 X3 X5 X7 X9; lappend 1 X2 X4 X6 X8 X10 }
109 foreach seg [set $v] {
115 proc polarity {newpname} {
117 debug "polarising $newpname"
118 if {![string compare $pname $newpname]} return
119 tellpic $polmsg($newpname)
124 proc pt_now {how point pos xtra} {
125 set msg a0[lindex $point $pos]
126 debug "$how point $point pos=$pos msg=$msg$xtra"
127 gui "M [lindex $point 2] [expr {!$pos}]"
130 proc pt_must {point newpos} {
131 upvar #0 pointpos($point) pos
133 if {[info exists pos] && $pos == $newpos} return
135 if {[info exists pq]} {
136 lappend pq [list $point $pos]
137 debug "queue point $point pos=$pos l=[llength $pq]"
140 pt_now immed $point $pos {}
143 proc pt_ifthenmust {ifpoint ifposwant thenpoint thenpos} {
144 upvar #0 pointpos($ifpoint) ifpos
145 if {![info exists ifpos] || $ifpos != $ifposwant} return
146 pt_must $thenpoint $thenpos
149 proc badwatchdog {} {
151 puts "watchdog - oh well"
152 if {![info exists pq]} { set pq {} }
159 set pq [lrange $pq 1 end]
160 pt_now nowdo [lindex $v 0] [lindex $v 1] " l=[llength $pq]"
169 set c [read $rand 1]; if {![string length $c]} { error "eof on rand" }
174 proc pt_maybe {point oneisright} {
175 global pointasked lastptchosen
176 if {![info exists pointasked]} {
177 if {![string compare $point $lastptchosen]} return
178 set lastptchosen $point
180 set pos [expr [regexp {^[89a-f]} $x] ? 1 : 0]
181 debug "chose point $point pos=$pos (x=$x)"
182 } elseif {[regexp {^M([01])$} $pointasked dummy pos]} {
183 if {[lsearch -exact {40 02} [lindex $point 0]] >= 0} {
184 set pos [expr {!$pos}]
185 debug "chose point $point pos=$pos manual-rl"
187 debug "chose point $point pos=$pos manual-lr"
190 } elseif {![string compare $pointasked M]} {
191 debug "leave point $point pos=$pos manual"
194 debug "fixed point $point pos=$pos"
201 upvar #0 segdetect($seg) segd
202 if {![info exists segd]} {
203 debug "segment $seg = already"
204 } elseif {[string length $segd]} {
205 debug "segment $seg = pending already"
207 debug "segment $seg = soon"
208 set segd [after 100 s0t $seg]
212 upvar #0 segdetect($seg) segd
213 debug "segment $seg = now"
218 upvar #0 segdetect($seg) segd
219 if {![info exists segd]} {
221 debug "segment $seg ! (overwrites =)"
222 } elseif {[string length $segd]} {
223 debug "segment $seg ! (cancels =)"
226 debug "segment $seg ! already"
233 proc pm_maydetect {d seg} {
254 #proc pm_nodetect {seg} {
256 # if {![string compare $seg [lindex $segsasgot 1]]} {
257 # set segsasgot [list [lindex $segsasgot 1] [lindex $segsasgot 0]]
261 proc pm_detect {seg} {
262 global segs pname segsasgot
263 if {[string compare $seg [lindex $segsasgot 1]]} {
264 set segsasgot [list [lindex $segsasgot 1] $seg]
266 if {[lsearch -exact $segs $seg] < 0} {
269 debug "pm_detect $seg ($segsasgot) ($segs) $pname$seg"
270 # if {[lsearch -exact {
271 # 06 09 0a 04 02 07 14 20
274 # } $seg] < 0} return
275 switch -exact $pname$seg {
276 l16 - l1c - l08 - l0b { polarity y }
277 l10 - l1a - l03 - l05 { polarity x }
278 x07 - x04 - x0a { polarity l }
279 x16 - x1c - x14 - x0b { polarity y }
280 y06 - y04 - y0a { polarity l }
281 y20 - y10 - y1a - y05 { polarity x }
284 04 - 0a { pt_must "00 01 X7" 1; pt_must "40 41 X8" 1 }
285 05 { pt_must "00 01 X7" 0 }
286 0b { pt_must "40 41 X8" 0 }
287 16 - 1c { pt_must "02 03 A5" 0 }
288 1a - 10 { pt_must "42 43 A6" 0 }
289 14 { pt_ifthenmust "02 03 A5" 1 "42 43 A6" 1 }
290 20 { pt_ifthenmust "42 43 A6" 1 "02 03 A5" 1 }
292 switch -exact [join $segs -] {
293 02-07 { pt_maybe "02 03 A5" 1 }
294 07-02 { pt_maybe "00 01 X7" 0 }
295 09-06 { pt_maybe "42 43 A6" 0 }
296 06-09 { pt_maybe "40 41 X8" 1 }
301 # global nmrawhich speeddirn funcs
303 # for {set i 0} {$i < $m} {incr i} {
304 # tellpic_q [lindex [list $speeddirn $funcs] $nmrawhich]
305 # set nmrawhich [expr {!$nmrawhich}]
310 global watchdog testonly speeddirn funcs nmradiv
311 catch { after cancel $watchdog }
312 set watchdog [after 50 watchdog]
313 tellpic_q 9808 ;# 128ms
314 if {[incr nmradiv] > 35} {
315 tellpic_q $speeddirn$funcs
321 debug "got hello, starting up"
335 switch -glob [lindex $m 0] {
336 01 - 02 { tellnmra $m }
337 09 { fp $m; pm_hello }
338 07 { puts "short circuit"; exit 1 }
339 0d { fp $m; badwatchdog }
340 28 { fp $m; pm_charged }
341 9[0-7] { fp $m; pm_maydetect 0 $v }
342 9? { fp $m; pm_maydetect 1 $v }
343 0a - [234567]? { puts "pic debug $m" }
344 * { fp $m; fail "pic unknown $m" }
348 proc onreadp_test {} {
349 if {![gets stdin m]} { return }
357 if {![string length $c]} {
358 if {[eof $p]} { error "eof on device" }
362 if {![info exists rand]} {
367 if {[regexp {^[0-7]} $x]} {
368 if {![regexp {^x} $m]} {
376 proc newspeeddirn {} {
378 set speed [expr {round(($b1 * $b1) / 65535.0 * 100.0 + 26.0)}]
380 set dirn [expr {$b2 / 128}]
382 debug "speeddirn b1=$b1 speed=$speed b2=$b2 dirn=$dirn"
383 return "speed126 2 $speed $dirn"
388 set value [expr {($b3 & 127) * 16}]
389 debug "funcs b3=$b3 value=[format %x $value]"
390 return "funcs5to8 2 $value"
393 proc maybechange {thing} {
395 upvar #0 ${thing}_fixed fixed
396 if {![info exists fixed]} {
397 set rb 0x[randbyte][randbyte]
400 1.0 / (($ch(${thing}every) - $ch(minint)*0.001) * $ch(scale))
402 debug "maybechange $thing rb=$rb no"
405 debug "maybechange $thing rb=$rb yes ..."
408 debug "fixed $thing $fixed"
410 if {![llength $l]} { return 0 }
412 set bin [eval exec ./hostside-old -s/dev/stdout $l]
413 binary scan $bin H* x
414 debug "changed $thing=$x"
421 catch { after cancel $chwa }
422 if {[maybechange speeddirn] || [maybechange funcs]} {
423 set interval $ch(minint)
427 set chwa [after $interval changewhat]
431 if {[gets stdin l] < 0} {
434 fail "stopgap-controller got eof, quitting"
435 fileevent stdin readable {}
443 global port p testonly
445 set p [open $port {RDWR NONBLOCK} 0]
447 exec stty -F $port min 1 time 0 -istrip -ocrnl -onlcr -onocr -opost \
448 -ctlecho -echo -echoe -echok -echonl -iexten -isig \
450 9600 clocal cread crtscts -hup -parenb cs8 -cstopb \
451 -ixoff bs0 cr0 ff0 nl0 -ofill -olcuc
453 fconfigure $p -encoding binary -translation binary \
454 -blocking false -buffering none
456 fileevent $p readable onreadp
457 fconfigure stdin -blocking false
458 fileevent stdin readable onreadcmd
461 fconfigure stdin -blocking false
462 fileevent stdin readable onreadp_test
465 after 250 setup_complete
468 proc setup_complete {} {
470 set rand [open /dev/urandom {RDONLY} 0]
471 fconfigure $rand -encoding binary -translation binary
477 # for keyboard control
480 global speeddirn_fixed; set speeddirn_fixed {speed126 2 126 0}
483 global speeddirn_fixed; set speeddirn_fixed {speed126 2 10 0}
485 proc ask_randspeed {} {
486 global speeddirn_fixed; catch { unset speeddirn_fixed }
489 proc ask_manual {rightp} { global pointasked; set pointasked M$rightp }
490 proc ask_figureeightt {rightp} { global pointasked; set pointasked 0 }
491 proc ask_loop {rightp} { global pointasked; set pointasked 1 }
492 proc ask_randpath {rightp} { global pointasked; catch { unset pointasked } }