8 set ch(speeddirnevery) 15
24 set pq {} ;# unset: cdu charged and waiting
26 set speeddirn_fixed {speed126 2 80 0}
28 # unset pointpos($point)
29 # unset segdetect($seg) ;# unset: shown D0; {}: shown D1; or: after id, D1->0
36 global watchdog polarity segdetect
40 if {[info exists watchdog]} { gui "P 1" }
41 # gui_polarity 0x$polarity
42 foreach seg [array names segdetect] {
54 set b [binary format H* $m]
65 global errorInfo errorCode
66 puts stderr "$m\n$errorCode\n$errorInfo"
76 fconfigure $p -blocking yes
84 catch { after cancel $watchdog; unset watchdog }
88 fileevent $p readable {}
91 proc gui_polarity {diff} {
93 if {$diff & 0x06} { lappend l X10 X9 }
94 if {$diff & 0x09} { lappend l X8 X1 X2 X3 X4 X5 X6 X7 }
100 proc polarity {newpname} {
102 debug "polarising $newpname"
103 if {![string compare $pname $newpname]} return
104 tellpic $polmsg($newpname)
109 proc pt_now {how point pos xtra} {
110 set msg a0[lindex $point $pos]
111 debug "$how point $point pos=$pos msg=$msg$xtra"
112 gui "M [lindex $point 2] [expr {!$pos}]"
115 proc pt_must {point newpos} {
116 upvar #0 pointpos($point) pos
118 if {[info exists pos] && $pos == $newpos} return
120 if {[info exists pq]} {
121 lappend pq [list $point $pos]
122 debug "queue point $point pos=$pos l=[llength $pq]"
125 pt_now immed $point $pos {}
128 proc pt_ifthenmust {ifpoint ifposwant thenpoint thenpos} {
129 upvar #0 pointpos($ifpoint) ifpos
130 if {![info exists ifpos] || $ifpos != $ifposwant} return
131 pt_must $thenpoint $thenpos
138 set pq [lrange $pq 1 end]
139 pt_now nowdo [lindex $v 0] [lindex $v 1] " l=[llength $pq]"
148 set c [read $rand 1]; if {![string length $c]} { error "eof on rand" }
153 proc pt_maybe {point oneisright} {
154 global always lastptchosen
155 if {[info exists always]} {
158 if {![string compare $point $lastptchosen]} return
159 set lastptchosen $point
161 set pos [expr [regexp {^[89a-f]} $x] ? 1 : 0]
162 debug "chose point $point pos=$pos (x=$x)"
168 upvar #0 segdetect($seg) segd
169 if {![info exists segd]} {
170 debug "segment $seg = already"
171 } elseif {[string length $segd]} {
172 debug "segment $seg = pending already"
174 debug "segment $seg = soon"
175 set segd [after 100 s0t $seg]
179 upvar #0 segdetect($seg) segd
180 debug "segment $seg = now"
185 upvar #0 segdetect($seg) segd
186 if {![info exists segd]} {
187 debug "segment $seg ! (overwrites =)"
188 } elseif {[string length $segd]} {
189 debug "segment $seg ! (cancels =)"
192 debug "segment $seg ! already"
199 proc pm_maydetect {d seg} {
220 proc pm_detect {seg} {
221 global segs pname segsasgot
222 if {[string compare $seg [lindex $segsasgot 1]]} {
223 set segsasgot [list [lindex $segsasgot 1] $seg]
225 if {[lsearch -exact $segs $seg] < 0} {
228 debug "pm_detect $seg ($segsasgot) ($segs) $pname$seg"
229 switch -exact $pname$seg {
230 l16 - l1c - l08 - l0b { polarity y }
231 l10 - l1a - l03 - l05 { polarity x }
232 x07 - x04 - x0a { polarity l }
233 x16 - x1c - x14 - x0b { polarity y }
234 y06 - y04 - y0a { polarity l }
235 y20 - y10 - y1a - y05 { polarity x }
238 04 - 0a { pt_must "00 01 X7" 1; pt_must "40 41 X8" 1 }
239 05 { pt_must "00 01 X7" 0 }
240 0b { pt_must "40 41 X8" 0 }
241 16 - 1c { pt_must "02 03 A5" 0 }
242 1a - 10 { pt_must "42 43 A6" 0 }
243 14 { pt_ifthenmust "02 03 A5" 1 "42 43 A6" 1 }
244 20 { pt_ifthenmust "42 43 A6" 1 "02 03 A5" 1 }
246 switch -exact [join $segs -] {
247 02-07 { pt_maybe "00 01 X7" 0 }
248 07-02 { pt_maybe "02 03 A5" 1 }
249 09-06 { pt_maybe "40 41 X8" 1 }
250 06-09 { pt_maybe "42 43 A6" 0 }
255 global nmrawhich speeddirn funcs
257 for {set i 0} {$i < $m} {incr i} {
258 tellpic_q [lindex [list $speeddirn $funcs] $nmrawhich]
259 set nmrawhich [expr {!$nmrawhich}]
264 global watchdog testonly
265 catch { after cancel $watchdog }
266 set watchdog [after 50 watchdog]
267 tellpic_q 9808 ;# 128ms
271 debug "got hello, starting up"
284 switch -glob [lindex $m 0] {
285 01 - 02 { tellnmra $m }
286 09 { fp $m; pm_hello }
287 07 { puts "short circuit"; exit 1 }
288 28 { fp $m; pm_charged }
289 9[0-7] { fp $m; pm_maydetect 0 [lindex $m 1] }
290 9? { fp $m; pm_detect [lindex $m 1]; pm_maydetect 1 [lindex $m 1] }
291 0a - [234567]? { puts "pic debug $m" }
292 * { fp $m; fail "pic unknown $m" }
296 proc onreadp_test {} {
297 if {![gets stdin m]} { return }
305 if {![string length $c]} {
306 if {[eof $p]} { error "eof on device" }
311 if {[regexp {^[0-7]} $x]} {
312 if {![regexp {^x} $m]} {
320 proc newspeeddirn {} {
322 set speed [expr {($b1 * $b1) / 516}]
324 set dirn [expr {$b2 / 128}]
325 debug "speeddirn b1=$b1 speed=$speed b2=$b2 dirn=$dirn"
326 return "speed126 2 $speed $dirn"
331 set value [expr {($b3 & 127) * 16}]
332 debug "funcs b3=$b3 value=[format %x $value]"
333 return "funcs5to8 2 $value"
336 proc maybechange {thing} {
338 upvar #0 ${thing}_fixed fixed
339 if {![info exists fixed]} {
340 set rb 0x[randbyte][randbyte]
343 1.0 / (($ch(${thing}every) - $ch(minint)*0.001) * $ch(scale))
345 debug "maybechange $thing rb=$rb no"
348 debug "maybechange $thing rb=$rb yes ..."
351 debug "fixed $thing $fixed"
354 set bin [eval exec ./hostside-old -s/dev/stdout $l]
355 binary scan $bin H* x
356 debug "changed $thing=$x"
363 catch { after cancel $chwa }
364 if {[maybechange speeddirn] || [maybechange funcs]} {
365 set interval $ch(minint)
369 set chwa [after $interval changewhat]
373 global port p rand testonly
375 set p [open $port {RDWR NONBLOCK} 0]
377 exec stty -F $port min 1 time 0 -istrip -ocrnl -onlcr -onocr -opost \
378 -ctlecho -echo -echoe -echok -echonl -iexten -isig \
380 9600 clocal cread crtscts -hup -parenb cs8 -cstopb \
381 -ixoff bs0 cr0 ff0 nl0 -ofill -olcuc
383 fconfigure $p -encoding binary -translation binary \
384 -blocking false -buffering none
386 fileevent $p readable onreadp
389 fconfigure stdin -blocking false
390 fileevent stdin readable onreadp_test
393 set rand [open /dev/urandom {RDONLY} 0]
394 fconfigure $rand -encoding binary -translation binary