8 set ch(speeddirnevery) 12
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" }
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 {} {
94 switch -exact $pname {
95 l { lappend 0 X1 X3 X5 X7 X9; lappend 0 X2 X4 X6 X8 X10 }
96 x { lappend 1 X1 X3 X5 X7 X9; lappend 0 X2 X4 X6 X8 X10 }
97 y { lappend 0 X1 X3 X5 X7 X9; lappend 1 X2 X4 X6 X8 X10 }
100 foreach seg [set $v] {
106 proc polarity {newpname} {
108 debug "polarising $newpname"
109 if {![string compare $pname $newpname]} return
110 tellpic $polmsg($newpname)
115 proc pt_now {how point pos xtra} {
116 set msg a0[lindex $point $pos]
117 debug "$how point $point pos=$pos msg=$msg$xtra"
118 gui "M [lindex $point 2] [expr {!$pos}]"
121 proc pt_must {point newpos} {
122 upvar #0 pointpos($point) pos
124 if {[info exists pos] && $pos == $newpos} return
126 if {[info exists pq]} {
127 lappend pq [list $point $pos]
128 debug "queue point $point pos=$pos l=[llength $pq]"
131 pt_now immed $point $pos {}
134 proc pt_ifthenmust {ifpoint ifposwant thenpoint thenpos} {
135 upvar #0 pointpos($ifpoint) ifpos
136 if {![info exists ifpos] || $ifpos != $ifposwant} return
137 pt_must $thenpoint $thenpos
144 set pq [lrange $pq 1 end]
145 pt_now nowdo [lindex $v 0] [lindex $v 1] " l=[llength $pq]"
154 set c [read $rand 1]; if {![string length $c]} { error "eof on rand" }
159 proc pt_maybe {point oneisright} {
160 global always lastptchosen
161 if {[info exists always]} {
164 if {![string compare $point $lastptchosen]} return
165 set lastptchosen $point
167 set pos [expr [regexp {^[89a-f]} $x] ? 1 : 0]
168 debug "chose point $point pos=$pos (x=$x)"
174 upvar #0 segdetect($seg) segd
175 if {![info exists segd]} {
176 debug "segment $seg = already"
177 } elseif {[string length $segd]} {
178 debug "segment $seg = pending already"
180 debug "segment $seg = soon"
181 set segd [after 100 s0t $seg]
185 upvar #0 segdetect($seg) segd
186 debug "segment $seg = now"
191 upvar #0 segdetect($seg) segd
192 if {![info exists segd]} {
194 debug "segment $seg ! (overwrites =)"
195 } elseif {[string length $segd]} {
196 debug "segment $seg ! (cancels =)"
199 debug "segment $seg ! already"
206 proc pm_maydetect {d seg} {
227 #proc pm_nodetect {seg} {
229 # if {![string compare $seg [lindex $segsasgot 1]]} {
230 # set segsasgot [list [lindex $segsasgot 1] [lindex $segsasgot 0]]
234 proc pm_detect {seg} {
235 global segs pname segsasgot
236 if {[string compare $seg [lindex $segsasgot 1]]} {
237 set segsasgot [list [lindex $segsasgot 1] $seg]
239 if {[lsearch -exact $segs $seg] < 0} {
242 debug "pm_detect $seg ($segsasgot) ($segs) $pname$seg"
243 # if {[lsearch -exact {
244 # 06 09 0a 04 02 07 14 20
247 # } $seg] < 0} return
248 switch -exact $pname$seg {
249 l16 - l1c - l08 - l0b { polarity y }
250 l10 - l1a - l03 - l05 { polarity x }
251 x07 - x04 - x0a { polarity l }
252 x16 - x1c - x14 - x0b { polarity y }
253 y06 - y04 - y0a { polarity l }
254 y20 - y10 - y1a - y05 { polarity x }
257 04 - 0a { pt_must "00 01 X7" 1; pt_must "40 41 X8" 1 }
258 05 { pt_must "00 01 X7" 0 }
259 0b { pt_must "40 41 X8" 0 }
260 16 - 1c { pt_must "02 03 A5" 0 }
261 1a - 10 { pt_must "42 43 A6" 0 }
262 14 { pt_ifthenmust "02 03 A5" 1 "42 43 A6" 1 }
263 20 { pt_ifthenmust "42 43 A6" 1 "02 03 A5" 1 }
265 switch -exact [join $segs -] {
266 02-07 { pt_maybe "02 03 A5" 1 }
267 07-02 { pt_maybe "00 01 X7" 0 }
268 09-06 { pt_maybe "42 43 A6" 0 }
269 06-09 { pt_maybe "40 41 X8" 1 }
274 global nmrawhich speeddirn funcs
276 for {set i 0} {$i < $m} {incr i} {
277 tellpic_q [lindex [list $speeddirn $funcs] $nmrawhich]
278 set nmrawhich [expr {!$nmrawhich}]
283 global watchdog testonly
284 catch { after cancel $watchdog }
285 set watchdog [after 50 watchdog]
286 tellpic_q 9808 ;# 128ms
290 debug "got hello, starting up"
304 switch -glob [lindex $m 0] {
305 01 - 02 { tellnmra $m }
306 09 { fp $m; pm_hello }
307 07 { puts "short circuit"; exit 1 }
308 28 { fp $m; pm_charged }
309 9[0-7] { fp $m; pm_maydetect 0 $v }
310 9? { fp $m; pm_maydetect 1 $v }
311 0a - [234567]? { puts "pic debug $m" }
312 * { fp $m; fail "pic unknown $m" }
316 proc onreadp_test {} {
317 if {![gets stdin m]} { return }
325 if {![string length $c]} {
326 if {[eof $p]} { error "eof on device" }
331 if {[regexp {^[0-7]} $x]} {
332 if {![regexp {^x} $m]} {
340 proc newspeeddirn {} {
342 set speed [expr {round(($b1 * $b1) / 65535.0 * 100.0 + 26.0)}]
344 set dirn [expr {$b2 / 128}]
345 debug "speeddirn b1=$b1 speed=$speed b2=$b2 dirn=$dirn"
346 return "speed126 2 $speed $dirn"
351 set value [expr {($b3 & 127) * 16}]
352 debug "funcs b3=$b3 value=[format %x $value]"
353 return "funcs5to8 2 $value"
356 proc maybechange {thing} {
358 upvar #0 ${thing}_fixed fixed
359 if {![info exists fixed]} {
360 set rb 0x[randbyte][randbyte]
363 1.0 / (($ch(${thing}every) - $ch(minint)*0.001) * $ch(scale))
365 debug "maybechange $thing rb=$rb no"
368 debug "maybechange $thing rb=$rb yes ..."
371 debug "fixed $thing $fixed"
374 set bin [eval exec ./hostside-old -s/dev/stdout $l]
375 binary scan $bin H* x
376 debug "changed $thing=$x"
383 catch { after cancel $chwa }
384 if {[maybechange speeddirn] || [maybechange funcs]} {
385 set interval $ch(minint)
389 set chwa [after $interval changewhat]
393 global port p rand testonly
395 set p [open $port {RDWR NONBLOCK} 0]
397 exec stty -F $port min 1 time 0 -istrip -ocrnl -onlcr -onocr -opost \
398 -ctlecho -echo -echoe -echok -echonl -iexten -isig \
400 9600 clocal cread crtscts -hup -parenb cs8 -cstopb \
401 -ixoff bs0 cr0 ff0 nl0 -ofill -olcuc
403 fconfigure $p -encoding binary -translation binary \
404 -blocking false -buffering none
406 fileevent $p readable onreadp
409 fconfigure stdin -blocking false
410 fileevent stdin readable onreadp_test
413 set rand [open /dev/urandom {RDONLY} 0]
414 fconfigure $rand -encoding binary -translation binary