8 set ch(speeddirnevery) 15
23 set pq {} ;# unset: cdu charged and waiting
25 set speeddirn_fixed {speed126 2 60 0}
27 # unset pointpos($point)
28 # unset segdetect($seg) ;# unset: shown D0; {}: shown D1; or: after id, D1->0
35 global watchdog polarity segdetect
39 if {[info exists watchdog]} { gui "P 1" }
40 # gui_polarity 0x$polarity
41 foreach seg [array names segdetect] {
53 set b [binary format H* $m]
64 global errorInfo errorCode
65 puts stderr "$m\n$errorCode\n$errorInfo"
75 fconfigure $p -blocking yes
83 catch { after cancel $watchdog; unset watchdog }
87 fileevent $p readable {}
90 proc gui_polarity {diff} {
92 if {$diff & 0x06} { lappend l X10 X9 }
93 if {$diff & 0x09} { lappend l X8 X1 X2 X3 X4 X5 X6 X7 }
99 proc polarity {newpname} {
101 debug "polarising $newpname"
102 if {![string compare $pname $newpname]} return
103 tellpic $polmsg($newpname)
108 proc pt_now {how point pos xtra} {
109 set msg a0[lindex $point $pos]
110 debug "$how point $point pos=$pos msg=$msg$xtra"
111 gui "M [lindex $point 2] [expr {!$pos}]"
114 proc pt_must {point newpos} {
115 upvar #0 pointpos($point) pos
117 if {[info exists pos] && $pos == $newpos} return
119 if {[info exists pq]} {
120 lappend pq [list $point $pos]
121 debug "queue point $point pos=$pos l=[llength $pq]"
124 pt_now immed $point $pos {}
132 set pq [lrange $pq 1 end]
133 pt_now nowdo [lindex $v 0] [lindex $v 1] " l=[llength $pq]"
142 set c [read $rand 1]; if {![string length $c]} { error "eof on rand" }
147 proc pt_maybe {point oneisright} {
149 if {[info exists always]} {
153 set pos [expr [regexp {^[89a-f]} $x] ? 1 : 0]
154 debug "chose point $point pos=$pos (x=$x)"
160 upvar #0 segdetect($seg) segd
161 if {![info exists segd]} {
162 debug "segment $seg = already"
163 } elseif {[string length $segd]} {
164 debug "segment $seg = pending already"
166 debug "segment $seg = soon"
167 set segd [after 100 s0t $seg]
171 upvar #0 segdetect($seg) segd
172 debug "segment $seg = now"
177 upvar #0 segdetect($seg) segd
178 if {![info exists segd]} {
179 debug "segment $seg ! (overwrites =)"
180 } elseif {[string length $segd]} {
181 debug "segment $seg ! (cancels =)"
184 debug "segment $seg ! already"
191 proc pm_maydetect {d seg} {
212 proc pm_detect {seg} {
213 global segs pname segsasgot
214 if {[string compare $seg [lindex $segsasgot 1]]} {
215 set segsasgot [list [lindex $segsasgot 1] $seg]
217 if {[lsearch -exact $segs $seg] < 0} {
220 debug "pm_detect $seg ($segsasgot) ($segs) $pname$seg"
221 switch -exact $pname$seg {
222 l16 - l1c - l08 - l0b { polarity y }
223 l10 - l1a - l03 - l05 { polarity x }
224 x07 - x04 - x0a { polarity l }
225 x16 - x1c - x14 - x0b { polarity y }
226 y06 - y04 - y0a { polarity l }
227 y20 - y10 - y1a - y05 { polarity x }
230 04 - 0a { pt_must "00 01 X7" 1; pt_must "40 41 X8" 1 }
231 05 { pt_must "00 01 X7" 0 }
232 0b { pt_must "40 41 X8" 0 }
233 16 - 1c { pt_must "02 03 A5" 0 }
234 1a - 10 { pt_must "42 43 A6" 0 }
236 switch -exact [join $segs -] {
237 14-20 { pt_must "42 43 A6" 1 }
238 20-14 { pt_must "02 03 A5" 1 }
240 switch -exact [join $segs -] {
241 07-02 { pt_maybe "00 01 X7" 0 }
242 02-07 { pt_maybe "02 03 A5" 1 }
243 06-09 { pt_maybe "40 41 X8" 1 }
244 09-06 { pt_maybe "42 43 A6" 0 }
249 global nmrawhich speeddirn funcs
251 for {set i 0} {$i < $m} {incr i} {
252 tellpic_q [lindex [list $speeddirn $funcs] $nmrawhich]
253 set nmrawhich [expr {!$nmrawhich}]
258 global watchdog testonly
259 catch { after cancel $watchdog }
260 set watchdog [after 50 watchdog]
261 tellpic_q 9808 ;# 128ms
265 debug "got hello, starting up"
278 switch -glob [lindex $m 0] {
279 01 - 02 { tellnmra $m }
280 09 { fp $m; pm_hello }
281 07 { puts "short circuit"; exit 1 }
282 28 { fp $m; pm_charged }
283 9[0-7] { fp $m; pm_maydetect 0 [lindex $m 1] }
284 9? { fp $m; pm_detect [lindex $m 1]; pm_maydetect 1 [lindex $m 1] }
285 0a - [234567]? { puts "pic debug $m" }
286 * { fp $m; fail "pic unknown $m" }
290 proc onreadp_test {} {
291 if {![gets stdin m]} { return }
299 if {![string length $c]} {
300 if {[eof $p]} { error "eof on device" }
305 if {[regexp {^[0-7]} $x]} {
306 if {![regexp {^x} $m]} {
314 proc newspeeddirn {} {
316 set speed [expr {($b1 * $b1) / 516}]
318 set dirn [expr {$b2 / 128}]
319 debug "speeddirn b1=$b1 speed=$speed b2=$b2 dirn=$dirn"
320 return "speed126 2 $speed $dirn"
325 set value [expr {($b3 & 127) * 16}]
326 debug "funcs b3=$b3 value=[format %x $value]"
327 return "funcs5to8 2 $value"
330 proc maybechange {thing} {
332 upvar #0 ${thing}_fixed fixed
333 if {![info exists fixed]} {
334 set rb 0x[randbyte][randbyte]
337 1.0 / (($ch(${thing}every) - $ch(minint)*0.001) * $ch(scale))
339 debug "maybechange $thing rb=$rb no"
342 debug "maybechange $thing rb=$rb yes ..."
345 debug "fixed $thing $fixed"
348 set bin [eval exec ./hostside-old -s/dev/stdout $l]
349 binary scan $bin H* x
350 debug "changed $thing=$x"
357 catch { after cancel $chwa }
358 if {[maybechange speeddirn] || [maybechange funcs]} {
359 set interval $ch(minint)
363 set chwa [after $interval changewhat]
367 global port p rand testonly
369 set p [open $port {RDWR NONBLOCK} 0]
371 exec stty -F $port min 1 time 0 -istrip -ocrnl -onlcr -onocr -opost \
372 -ctlecho -echo -echoe -echok -echonl -iexten -isig \
374 9600 clocal cread crtscts -hup -parenb cs8 -cstopb \
375 -ixoff bs0 cr0 ff0 nl0 -ofill -olcuc
377 fconfigure $p -encoding binary -translation binary \
378 -blocking false -buffering none
380 fileevent $p readable onreadp
383 fconfigure stdin -blocking false
384 fileevent stdin readable onreadp_test
387 set rand [open /dev/urandom {RDONLY} 0]
388 fconfigure $rand -encoding binary -translation binary