3 # liberator:hostside> ssh bessar 'cd things/trains-bessar/hostside && ./stopgap-controller' | ./gui-displayer -
12 set loco [lindex $locos $locoix]
15 set ch(speeddirnevery) 30
20 set pointprobs {0 0x020 0x080 0x0e0 0x100}
22 set pointabs 1 ;# 0 or 1
26 set pointprob [lindex $pointprobs $pointprobix]
36 set pq {} ;# unset: cdu charged and waiting
39 set askspeeds {1 30 50 80 100 126}
40 #set speeddirn ffff80c3fbcced7f
41 #set speeddirn_fixed {speed126 2 80 0}
42 set speeddirn_fixed {}
44 # unset pointpos($point)
45 # unset segdetect($seg) ;# unset: shown D0; {}: shown D1; or: after id, D1->0
47 set funcsr1 {0x061 0x020 0x000 0x040 0x060}
48 set funcsl1 {0x182 0x080 0x000 0x100 0x180}
49 set funcsr2 {0x021 0x020 0x000}
50 set funcsl2 {0x042 0x040 0x000}
51 set funcsr4 {0x020 0x000}
52 set funcsl4 {0x040 0x000}
60 global watchdog polarity segdetect
65 if {[info exists watchdog]} { gui "P 1" }
67 foreach seg [array names segdetect] {
79 set b [binary format H* $m]
90 global errorInfo errorCode
91 puts stderr "$m\n$errorCode\n$errorInfo"
101 fconfigure $p -blocking yes
109 catch { after cancel $watchdog; unset watchdog }
113 fileevent $p readable {}
116 proc gui_polarity {} {
119 switch -exact $pname {
120 l { lappend 0 X1 X3 X5 X7 X9; lappend 0 X2 X4 X6 X8 X10 }
121 x { lappend 1 X1 X3 X5 X7 X9; lappend 0 X2 X4 X6 X8 X10 }
122 y { lappend 0 X1 X3 X5 X7 X9; lappend 1 X2 X4 X6 X8 X10 }
125 foreach seg [set $v] {
131 proc polarity {newpname} {
133 debug "polarising $newpname"
134 if {![string compare $pname $newpname]} return
135 tellpic $polmsg($newpname)
140 proc pt_now {how point pos xtra} {
141 set msg a0[lindex $point $pos]
142 debug "$how point $point pos=$pos msg=$msg$xtra"
143 gui "M [lindex $point 2] [expr {!$pos}]"
146 proc pt_must {point newpos} {
147 upvar #0 pointpos($point) pos
149 if {[info exists pos] && $pos == $newpos} return
151 if {[info exists pq]} {
152 lappend pq [list $point $pos]
153 debug "queue point $point pos=$pos l=[llength $pq]"
156 pt_now immed $point $pos {}
159 proc pt_ifthenmust {ifpoint ifposwant thenpoint thenpos} {
160 upvar #0 pointpos($ifpoint) ifpos
161 if {![info exists ifpos] || $ifpos != $ifposwant} return
162 pt_must $thenpoint $thenpos
165 proc badwatchdog {} {
167 puts "watchdog - oh well"
168 if {![info exists pq]} { set pq {} }
175 set pq [lrange $pq 1 end]
176 pt_now nowdo [lindex $v 0] [lindex $v 1] " l=[llength $pq]"
185 set c [read $rand 1]; if {![string length $c]} { error "eof on rand" }
190 proc pt_maybe {point oneisright} {
191 upvar #0 pointpos($point) oldpos
192 global lastptchosen pointprob pointabs
193 if {![string compare $point $lastptchosen]} return
194 set lastptchosen $point
196 set pos [expr {$x < $pointprob ? 1 : 0}]
198 debug "chose point $point $pos (abs x=$x prob=$pointprob)"
199 set pos [expr {!$pos}]
200 } elseif {[info exists oldpos] && !$oldpos} {
201 debug "chose point $point $pos (0-> x=$x prob=$pointprob)"
203 set pos [expr {!$pos}]
204 debug "chose point $point $pos (1-> x=$x prob=$pointprob)"
210 upvar #0 segdetect($seg) segd
211 if {![info exists segd]} {
212 debug "segment $seg = already"
213 } elseif {[string length $segd]} {
214 debug "segment $seg = pending already"
216 debug "segment $seg = soon"
217 set segd [after 100 s0t $seg]
221 upvar #0 segdetect($seg) segd
222 debug "segment $seg = now"
227 upvar #0 segdetect($seg) segd
228 if {![info exists segd]} {
230 debug "segment $seg ! (overwrites =)"
231 } elseif {[string length $segd]} {
232 debug "segment $seg ! (cancels =)"
235 debug "segment $seg ! already"
242 proc pm_maydetect {d seg} {
263 #proc pm_nodetect {seg} {
265 # if {![string compare $seg [lindex $segsasgot 1]]} {
266 # set segsasgot [list [lindex $segsasgot 1] [lindex $segsasgot 0]]
270 proc pm_detect {seg} {
271 global segs pname segsasgot
272 if {[string compare $seg [lindex $segsasgot 1]]} {
273 set segsasgot [list [lindex $segsasgot 1] $seg]
275 if {[lsearch -exact $segs $seg] < 0} {
278 debug "pm_detect $seg ($segsasgot) ($segs) $pname$seg"
279 # if {[lsearch -exact {
280 # 06 09 0a 04 02 07 14 20
283 # } $seg] < 0} return
284 switch -exact $pname$seg {
285 l16 - l1c - l08 - l0b { polarity y }
286 l10 - l1a - l03 - l05 { polarity x }
287 x07 - x04 - x0a { polarity l }
288 x16 - x1c - x14 - x0b { polarity y }
289 y06 - y04 - y0a { polarity l }
290 y20 - y10 - y1a - y05 { polarity x }
293 04 - 0a { pt_must "00 01 X7" 1; pt_must "40 41 X8" 1 }
294 05 { pt_must "00 01 X7" 0 }
295 0b { pt_must "40 41 X8" 0 }
296 16 - 1c { pt_must "02 03 A5" 0 }
297 1a - 10 { pt_must "42 43 A6" 0 }
298 14 { pt_ifthenmust "02 03 A5" 1 "42 43 A6" 1 }
299 20 { pt_ifthenmust "42 43 A6" 1 "02 03 A5" 1 }
301 switch -exact [join $segs -] {
302 02-07 { pt_maybe "02 03 A5" 1 }
303 07-02 { pt_maybe "00 01 X7" 0 }
304 09-06 { pt_maybe "42 43 A6" 0 }
305 06-09 { pt_maybe "40 41 X8" 1 }
310 # global nmrawhich speeddirn funcs
312 # for {set i 0} {$i < $m} {incr i} {
313 # tellpic_q [lindex [list $speeddirn $funcs] $nmrawhich]
314 # set nmrawhich [expr {!$nmrawhich}]
319 global watchdog testonly speeddirn funcs nmradiv
320 catch { after cancel $watchdog }
321 set watchdog [after 50 watchdog]
322 tellpic_q 9808 ;# 128ms
323 if {[incr nmradiv] > 35} {
324 tellpic_q $speeddirn$funcs
330 debug "got hello, starting up"
344 switch -glob [lindex $m 0] {
345 01 - 02 { tellnmra $m }
346 09 { fp $m; pm_hello }
347 07 { puts "short circuit"; exit 1 }
348 0d { fp $m; badwatchdog }
349 28 { fp $m; pm_charged }
350 9[0-7] { fp $m; pm_maydetect 0 $v }
351 9? { fp $m; pm_maydetect 1 $v }
352 0a - [234567]? { puts "pic debug $m" }
353 * { fp $m; fail "pic unknown $m" }
357 proc onreadp_test {} {
358 if {![gets stdin m]} { return }
366 if {![string length $c]} {
367 if {[eof $p]} { error "eof on device" }
371 if {![info exists rand]} {
376 if {[regexp {^[0-7]} $x]} {
377 if {![regexp {^x} $m]} {
385 proc newspeeddirn {} {
386 global loco askspeedix
387 set maxspeed [expr {$askspeedix == -1 ? 126.0 : 50.0}]
391 round(($b1 * $b1) / 65535.0 * ($maxspeed - $minspeed) + $minspeed)
394 set dirn [expr {$b2 / 128}]
396 debug "speeddirn b1=$b1 speed=$speed b2=$b2 dirn=$dirn"
397 return "speed126 $loco $speed $dirn"
400 proc funcs_removebits {lr headent} {
402 set funcsval [format 0x%x [expr {$funcsval & ~$headent}]]
404 proc funcs_addbits {lr list} {
406 set headent [lindex $list 0]
410 set rand 0x[randbyte]0
411 set add [expr {$add & $rand}]
412 set val [expr {$val | $add}]
413 debug "funcs $lr v=$funcsval add=$add new=$val rand=$rand ($list)"
415 set val [expr {$val | $add}]
416 debug "funcs $lr v=$funcsval add=$add new=$val ($list)"
421 proc funcsnmralist {} {
423 return "funcs5to8 $loco $funcsval"
429 upvar #0 funcs${lr}${loco} list
430 set now [lindex $list 0]
431 funcs_removebits $lr $now
432 funcs_addbits $lr $list
434 return [funcsnmralist]
437 proc nmrachange {thing argstring} {
439 set bin [eval exec ./adhoc-test -s/dev/stdout $argstring]
440 binary scan $bin H* x
441 debug "changed $thing=$x ($argstring)"
445 proc maybechange {thing force} {
447 upvar #0 ${thing}_fixed fixed
448 if {![info exists fixed]} {
450 debug "maybechange $thing forced ..."
452 set rb 0x[randbyte][randbyte]
455 1.0 / (($ch(${thing}every) - $ch(minint)*0.001) * $ch(scale))
457 debug "maybechange $thing rb=$rb no"
460 debug "maybechange $thing rb=$rb yes ..."
464 debug "fixed $thing $fixed"
466 if {![llength $l]} { return 0 }
474 catch { after cancel $chwa }
475 if {[maybechange speeddirn 0] + [maybechange funcs 0]} {
476 set interval $ch(minint)
480 set chwa [after $interval changewhat]
484 if {[gets stdin l] < 0} {
487 fail "stopgap-controller got eof, quitting"
488 fileevent stdin readable {}
496 global port p testonly stateshowpipe
497 fconfigure stdout -buffering none
499 set p [open $port {RDWR NONBLOCK} 0]
501 exec stty -F $port min 1 time 0 -istrip -ocrnl -onlcr -onocr -opost \
502 -ctlecho -echo -echoe -echok -echonl -iexten -isig \
504 9600 clocal cread crtscts -hup -parenb cs8 -cstopb \
505 -ixoff bs0 cr0 ff0 nl0 -ofill -olcuc
507 fconfigure $p -encoding binary -translation binary \
508 -blocking false -buffering none
510 fileevent $p readable onreadp
511 fconfigure stdin -blocking false
512 fileevent stdin readable onreadcmd
513 set stateshowpipe [open /tmp/train-state w]
514 fconfigure $stateshowpipe -buffering none
517 fconfigure stdin -blocking false
518 fileevent stdin readable onreadp_test
519 set stateshowpipe [open /dev/null w]
522 after 250 setup_complete
525 proc setup_complete {} {
528 set rand [open /dev/urandom {RDONLY} 0]
529 fconfigure $rand -encoding binary -translation binary
534 # for keyboard control
536 proc updownfromlist {wholelistv ixv updown} {
537 upvar #0 $wholelistv wholelist
539 set ll [llength $wholelist]
540 if {![info exists ix]} {
543 int($ll * 0.5 - 0.5 + 0.5 * $updown)
548 if {$ix < 0} { set ix 0 }
549 if {$ix >= $ll} { set ix [expr {$ll - 1}] }
551 set val [lindex $wholelist $ix]
552 debug "updownfromlist ix:$old->$ix /$ll $val ($wholelist)"
556 proc ask_speed {updown} {
557 global speeddirn_fixed loco askspeedix
558 if {$askspeedix < 0} { unset askspeedix }
559 set speed [updownfromlist askspeeds askspeedix $updown]
560 set speeddirn_fixed [list speed126 $loco $speed 0]
561 maybechange speeddirn 1
564 proc ask_randspeed {} {
565 global speeddirn_fixed askspeedix
566 set askspeedix [expr {$askspeedix == -1 ? -2 : -1}]
567 catch { unset speeddirn_fixed }
568 maybechange speeddirn 1
573 set loco [updownfromlist locos locoix 1]
576 proc ask_funcs {lr} {
578 upvar #0 funcs${lr}${loco} list
579 set now [lindex $list 0]
580 funcs_removebits $lr $now
581 set list [concat [lrange $list 1 end] $now]
582 funcs_addbits $lr $list
583 nmrachange funcs [funcsnmralist]
586 proc ask_pointprob {updown} {
588 set pointprob [updownfromlist pointprobs pointprobix $updown]
591 proc ask_pointrelabs {} {
593 set pointabs [expr {!$pointabs}]
597 global loco stateshowpipe pointprob pointabs askspeedix
598 upvar #0 funcsr$loco fr
599 upvar #0 funcsl$loco fl
600 puts -nonewline $stateshowpipe [format \
601 "\nL$loco P%03x%s F%03x S%s" \
602 $pointprob [lindex {R A} $pointabs] \
603 [expr {[lindex $fr 0] | [lindex $fl 0]}] \