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}
32 # unset pointpos($point)
33 # unset segdetect($seg) ;# unset: shown D0; {}: shown D1; or: after id, D1->0
40 global watchdog polarity segdetect
44 if {[info exists watchdog]} { gui "P 1" }
46 foreach seg [array names segdetect] {
58 set b [binary format H* $m]
69 global errorInfo errorCode
70 puts stderr "$m\n$errorCode\n$errorInfo"
80 fconfigure $p -blocking yes
88 catch { after cancel $watchdog; unset watchdog }
92 fileevent $p readable {}
95 proc gui_polarity {} {
98 switch -exact $pname {
99 l { lappend 0 X1 X3 X5 X7 X9; lappend 0 X2 X4 X6 X8 X10 }
100 x { lappend 1 X1 X3 X5 X7 X9; lappend 0 X2 X4 X6 X8 X10 }
101 y { lappend 0 X1 X3 X5 X7 X9; lappend 1 X2 X4 X6 X8 X10 }
104 foreach seg [set $v] {
110 proc polarity {newpname} {
112 debug "polarising $newpname"
113 if {![string compare $pname $newpname]} return
114 tellpic $polmsg($newpname)
119 proc pt_now {how point pos xtra} {
120 set msg a0[lindex $point $pos]
121 debug "$how point $point pos=$pos msg=$msg$xtra"
122 gui "M [lindex $point 2] [expr {!$pos}]"
125 proc pt_must {point newpos} {
126 upvar #0 pointpos($point) pos
128 if {[info exists pos] && $pos == $newpos} return
130 if {[info exists pq]} {
131 lappend pq [list $point $pos]
132 debug "queue point $point pos=$pos l=[llength $pq]"
135 pt_now immed $point $pos {}
138 proc pt_ifthenmust {ifpoint ifposwant thenpoint thenpos} {
139 upvar #0 pointpos($ifpoint) ifpos
140 if {![info exists ifpos] || $ifpos != $ifposwant} return
141 pt_must $thenpoint $thenpos
148 set pq [lrange $pq 1 end]
149 pt_now nowdo [lindex $v 0] [lindex $v 1] " l=[llength $pq]"
158 set c [read $rand 1]; if {![string length $c]} { error "eof on rand" }
163 proc pt_maybe {point oneisright} {
164 global always lastptchosen
165 if {[info exists always]} {
168 if {![string compare $point $lastptchosen]} return
169 set lastptchosen $point
171 set pos [expr [regexp {^[89a-f]} $x] ? 1 : 0]
172 debug "chose point $point pos=$pos (x=$x)"
178 upvar #0 segdetect($seg) segd
179 if {![info exists segd]} {
180 debug "segment $seg = already"
181 } elseif {[string length $segd]} {
182 debug "segment $seg = pending already"
184 debug "segment $seg = soon"
185 set segd [after 100 s0t $seg]
189 upvar #0 segdetect($seg) segd
190 debug "segment $seg = now"
195 upvar #0 segdetect($seg) segd
196 if {![info exists segd]} {
198 debug "segment $seg ! (overwrites =)"
199 } elseif {[string length $segd]} {
200 debug "segment $seg ! (cancels =)"
203 debug "segment $seg ! already"
210 proc pm_maydetect {d seg} {
231 #proc pm_nodetect {seg} {
233 # if {![string compare $seg [lindex $segsasgot 1]]} {
234 # set segsasgot [list [lindex $segsasgot 1] [lindex $segsasgot 0]]
238 proc pm_detect {seg} {
239 global segs pname segsasgot
240 if {[string compare $seg [lindex $segsasgot 1]]} {
241 set segsasgot [list [lindex $segsasgot 1] $seg]
243 if {[lsearch -exact $segs $seg] < 0} {
246 debug "pm_detect $seg ($segsasgot) ($segs) $pname$seg"
247 # if {[lsearch -exact {
248 # 06 09 0a 04 02 07 14 20
251 # } $seg] < 0} return
252 switch -exact $pname$seg {
253 l16 - l1c - l08 - l0b { polarity y }
254 l10 - l1a - l03 - l05 { polarity x }
255 x07 - x04 - x0a { polarity l }
256 x16 - x1c - x14 - x0b { polarity y }
257 y06 - y04 - y0a { polarity l }
258 y20 - y10 - y1a - y05 { polarity x }
261 04 - 0a { pt_must "00 01 X7" 1; pt_must "40 41 X8" 1 }
262 05 { pt_must "00 01 X7" 0 }
263 0b { pt_must "40 41 X8" 0 }
264 16 - 1c { pt_must "02 03 A5" 0 }
265 1a - 10 { pt_must "42 43 A6" 0 }
266 14 { pt_ifthenmust "02 03 A5" 1 "42 43 A6" 1 }
267 20 { pt_ifthenmust "42 43 A6" 1 "02 03 A5" 1 }
269 switch -exact [join $segs -] {
270 02-07 { pt_maybe "02 03 A5" 1 }
271 07-02 { pt_maybe "00 01 X7" 0 }
272 09-06 { pt_maybe "42 43 A6" 0 }
273 06-09 { pt_maybe "40 41 X8" 1 }
278 # global nmrawhich speeddirn funcs
280 # for {set i 0} {$i < $m} {incr i} {
281 # tellpic_q [lindex [list $speeddirn $funcs] $nmrawhich]
282 # set nmrawhich [expr {!$nmrawhich}]
287 global watchdog testonly speeddirn funcs nmradiv
288 catch { after cancel $watchdog }
289 set watchdog [after 50 watchdog]
290 tellpic_q 9808 ;# 128ms
291 if {[incr nmradiv] > 35} {
292 tellpic_q $speeddirn$funcs
298 debug "got hello, starting up"
312 switch -glob [lindex $m 0] {
313 01 - 02 { tellnmra $m }
314 09 { fp $m; pm_hello }
315 07 { puts "short circuit"; exit 1 }
316 28 { fp $m; pm_charged }
317 9[0-7] { fp $m; pm_maydetect 0 $v }
318 9? { fp $m; pm_maydetect 1 $v }
319 0a - [234567]? { puts "pic debug $m" }
320 * { fp $m; fail "pic unknown $m" }
324 proc onreadp_test {} {
325 if {![gets stdin m]} { return }
333 if {![string length $c]} {
334 if {[eof $p]} { error "eof on device" }
338 if {![info exists rand]} {
343 if {[regexp {^[0-7]} $x]} {
344 if {![regexp {^x} $m]} {
352 proc newspeeddirn {} {
354 set speed [expr {round(($b1 * $b1) / 65535.0 * 100.0 + 26.0)}]
356 set dirn [expr {$b2 / 128}]
358 debug "speeddirn b1=$b1 speed=$speed b2=$b2 dirn=$dirn"
359 return "speed126 2 $speed $dirn"
364 set value [expr {($b3 & 127) * 16}]
365 debug "funcs b3=$b3 value=[format %x $value]"
366 return "funcs5to8 2 $value"
369 proc maybechange {thing} {
371 upvar #0 ${thing}_fixed fixed
372 if {![info exists fixed]} {
373 set rb 0x[randbyte][randbyte]
376 1.0 / (($ch(${thing}every) - $ch(minint)*0.001) * $ch(scale))
378 debug "maybechange $thing rb=$rb no"
381 debug "maybechange $thing rb=$rb yes ..."
384 debug "fixed $thing $fixed"
387 set bin [eval exec ./hostside-old -s/dev/stdout $l]
388 binary scan $bin H* x
389 debug "changed $thing=$x"
396 catch { after cancel $chwa }
397 if {[maybechange speeddirn] || [maybechange funcs]} {
398 set interval $ch(minint)
402 set chwa [after $interval changewhat]
406 if {[gets stdin l] < 0} {
407 if {[eof stdin]} { fileevent stdin readable {} }
414 global port p testonly
416 set p [open $port {RDWR NONBLOCK} 0]
418 exec stty -F $port min 1 time 0 -istrip -ocrnl -onlcr -onocr -opost \
419 -ctlecho -echo -echoe -echok -echonl -iexten -isig \
421 9600 clocal cread crtscts -hup -parenb cs8 -cstopb \
422 -ixoff bs0 cr0 ff0 nl0 -ofill -olcuc
424 fconfigure $p -encoding binary -translation binary \
425 -blocking false -buffering none
427 fileevent $p readable onreadp
428 fconfigure stdin -blocking false
429 fileevent stdin readable onreadcmd
432 fconfigure stdin -blocking false
433 fileevent stdin readable onreadp_test
436 after 250 setup_complete
439 proc setup_complete {} {
441 set rand [open /dev/urandom {RDONLY} 0]
442 fconfigure $rand -encoding binary -translation binary