3 # liberator:hostside> ssh bessar 'cd things/trains-bessar/hostside && ./stopgap-controller' | ./gui-displayer -
10 set ch(speeddirnevery) 30
27 set pq {} ;# unset: cdu charged and waiting
29 #set speeddirn ffff80c3fbcced7f
30 #set speeddirn_fixed {speed126 2 80 0}
31 set speeddirn_fixed {}
33 # unset pointpos($point)
34 # unset segdetect($seg) ;# unset: shown D0; {}: shown D1; or: after id, D1->0
41 global watchdog polarity segdetect
45 if {[info exists watchdog]} { gui "P 1" }
47 foreach seg [array names segdetect] {
59 set b [binary format H* $m]
70 global errorInfo errorCode
71 puts stderr "$m\n$errorCode\n$errorInfo"
81 fconfigure $p -blocking yes
89 catch { after cancel $watchdog; unset watchdog }
93 fileevent $p readable {}
96 proc gui_polarity {} {
99 switch -exact $pname {
100 l { lappend 0 X1 X3 X5 X7 X9; lappend 0 X2 X4 X6 X8 X10 }
101 x { lappend 1 X1 X3 X5 X7 X9; lappend 0 X2 X4 X6 X8 X10 }
102 y { lappend 0 X1 X3 X5 X7 X9; lappend 1 X2 X4 X6 X8 X10 }
105 foreach seg [set $v] {
111 proc polarity {newpname} {
113 debug "polarising $newpname"
114 if {![string compare $pname $newpname]} return
115 tellpic $polmsg($newpname)
120 proc pt_now {how point pos xtra} {
121 set msg a0[lindex $point $pos]
122 debug "$how point $point pos=$pos msg=$msg$xtra"
123 gui "M [lindex $point 2] [expr {!$pos}]"
126 proc pt_must {point newpos} {
127 upvar #0 pointpos($point) pos
129 if {[info exists pos] && $pos == $newpos} return
131 if {[info exists pq]} {
132 lappend pq [list $point $pos]
133 debug "queue point $point pos=$pos l=[llength $pq]"
136 pt_now immed $point $pos {}
139 proc pt_ifthenmust {ifpoint ifposwant thenpoint thenpos} {
140 upvar #0 pointpos($ifpoint) ifpos
141 if {![info exists ifpos] || $ifpos != $ifposwant} return
142 pt_must $thenpoint $thenpos
149 set pq [lrange $pq 1 end]
150 pt_now nowdo [lindex $v 0] [lindex $v 1] " l=[llength $pq]"
159 set c [read $rand 1]; if {![string length $c]} { error "eof on rand" }
164 proc pt_maybe {point oneisright} {
165 global always lastptchosen
166 if {[info exists always]} {
169 if {![string compare $point $lastptchosen]} return
170 set lastptchosen $point
172 set pos [expr [regexp {^[89a-f]} $x] ? 1 : 0]
173 debug "chose point $point pos=$pos (x=$x)"
179 upvar #0 segdetect($seg) segd
180 if {![info exists segd]} {
181 debug "segment $seg = already"
182 } elseif {[string length $segd]} {
183 debug "segment $seg = pending already"
185 debug "segment $seg = soon"
186 set segd [after 100 s0t $seg]
190 upvar #0 segdetect($seg) segd
191 debug "segment $seg = now"
196 upvar #0 segdetect($seg) segd
197 if {![info exists segd]} {
199 debug "segment $seg ! (overwrites =)"
200 } elseif {[string length $segd]} {
201 debug "segment $seg ! (cancels =)"
204 debug "segment $seg ! already"
211 proc pm_maydetect {d seg} {
232 #proc pm_nodetect {seg} {
234 # if {![string compare $seg [lindex $segsasgot 1]]} {
235 # set segsasgot [list [lindex $segsasgot 1] [lindex $segsasgot 0]]
239 proc pm_detect {seg} {
240 global segs pname segsasgot
241 if {[string compare $seg [lindex $segsasgot 1]]} {
242 set segsasgot [list [lindex $segsasgot 1] $seg]
244 if {[lsearch -exact $segs $seg] < 0} {
247 debug "pm_detect $seg ($segsasgot) ($segs) $pname$seg"
248 # if {[lsearch -exact {
249 # 06 09 0a 04 02 07 14 20
252 # } $seg] < 0} return
253 switch -exact $pname$seg {
254 l16 - l1c - l08 - l0b { polarity y }
255 l10 - l1a - l03 - l05 { polarity x }
256 x07 - x04 - x0a { polarity l }
257 x16 - x1c - x14 - x0b { polarity y }
258 y06 - y04 - y0a { polarity l }
259 y20 - y10 - y1a - y05 { polarity x }
262 04 - 0a { pt_must "00 01 X7" 1; pt_must "40 41 X8" 1 }
263 05 { pt_must "00 01 X7" 0 }
264 0b { pt_must "40 41 X8" 0 }
265 16 - 1c { pt_must "02 03 A5" 0 }
266 1a - 10 { pt_must "42 43 A6" 0 }
267 14 { pt_ifthenmust "02 03 A5" 1 "42 43 A6" 1 }
268 20 { pt_ifthenmust "42 43 A6" 1 "02 03 A5" 1 }
270 switch -exact [join $segs -] {
271 02-07 { pt_maybe "02 03 A5" 1 }
272 07-02 { pt_maybe "00 01 X7" 0 }
273 09-06 { pt_maybe "42 43 A6" 0 }
274 06-09 { pt_maybe "40 41 X8" 1 }
279 # global nmrawhich speeddirn funcs
281 # for {set i 0} {$i < $m} {incr i} {
282 # tellpic_q [lindex [list $speeddirn $funcs] $nmrawhich]
283 # set nmrawhich [expr {!$nmrawhich}]
288 global watchdog testonly speeddirn funcs nmradiv
289 catch { after cancel $watchdog }
290 set watchdog [after 50 watchdog]
291 tellpic_q 9808 ;# 128ms
292 if {[incr nmradiv] > 35} {
293 tellpic_q $speeddirn$funcs
299 debug "got hello, starting up"
313 switch -glob [lindex $m 0] {
314 01 - 02 { tellnmra $m }
315 09 { fp $m; pm_hello }
316 07 { puts "short circuit"; exit 1 }
317 28 { fp $m; pm_charged }
318 9[0-7] { fp $m; pm_maydetect 0 $v }
319 9? { fp $m; pm_maydetect 1 $v }
320 0a - [234567]? { puts "pic debug $m" }
321 * { fp $m; fail "pic unknown $m" }
325 proc onreadp_test {} {
326 if {![gets stdin m]} { return }
334 if {![string length $c]} {
335 if {[eof $p]} { error "eof on device" }
339 if {![info exists rand]} {
344 if {[regexp {^[0-7]} $x]} {
345 if {![regexp {^x} $m]} {
353 proc newspeeddirn {} {
355 set speed [expr {round(($b1 * $b1) / 65535.0 * 100.0 + 26.0)}]
357 set dirn [expr {$b2 / 128}]
359 debug "speeddirn b1=$b1 speed=$speed b2=$b2 dirn=$dirn"
360 return "speed126 2 $speed $dirn"
365 set value [expr {($b3 & 127) * 16}]
366 debug "funcs b3=$b3 value=[format %x $value]"
367 return "funcs5to8 2 $value"
370 proc maybechange {thing} {
372 upvar #0 ${thing}_fixed fixed
373 if {![info exists fixed]} {
374 set rb 0x[randbyte][randbyte]
377 1.0 / (($ch(${thing}every) - $ch(minint)*0.001) * $ch(scale))
379 debug "maybechange $thing rb=$rb no"
382 debug "maybechange $thing rb=$rb yes ..."
385 debug "fixed $thing $fixed"
387 if {![llength $l]} { return 0 }
389 set bin [eval exec ./hostside-old -s/dev/stdout $l]
390 binary scan $bin H* x
391 debug "changed $thing=$x"
398 catch { after cancel $chwa }
399 if {[maybechange speeddirn] || [maybechange funcs]} {
400 set interval $ch(minint)
404 set chwa [after $interval changewhat]
408 if {[gets stdin l] < 0} {
410 puts stderr "stopgap-controller got eof, quitting"
420 global port p testonly
422 set p [open $port {RDWR NONBLOCK} 0]
424 exec stty -F $port min 1 time 0 -istrip -ocrnl -onlcr -onocr -opost \
425 -ctlecho -echo -echoe -echok -echonl -iexten -isig \
427 9600 clocal cread crtscts -hup -parenb cs8 -cstopb \
428 -ixoff bs0 cr0 ff0 nl0 -ofill -olcuc
430 fconfigure $p -encoding binary -translation binary \
431 -blocking false -buffering none
433 fileevent $p readable onreadp
434 fconfigure stdin -blocking false
435 fileevent stdin readable onreadcmd
438 fconfigure stdin -blocking false
439 fileevent stdin readable onreadp_test
442 after 250 setup_complete
445 proc setup_complete {} {
447 set rand [open /dev/urandom {RDONLY} 0]
448 fconfigure $rand -encoding binary -translation binary
454 # for keyboard control
457 global speeddirn_fixed; set speeddirn_fixed {speed126 2 126 0}
460 global speeddirn_fixed; set speeddirn_fixed {speed126 2 126 10}
462 proc ask_randspeed {} {
463 global speeddirn_fixed; catch { unset speeddirn_fixed }