8 set ch(speeddirnevery) 15
21 set pq {} ;# unset: cdu charged and waiting
23 set speeddirn_fixed {speed126 2 60 0}
25 # unset pointpos($point)
26 # unset segdetect($seg) ;# unset: shown D0; {}: shown D1; or: after id, D1->0
33 global watchdog polarity segdetect
37 if {[info exists watchdog]} { gui "P 1" }
38 gui_polarity 0x$polarity
39 foreach seg [array names segdetect] {
51 set b [binary format H* $m]
62 global errorInfo errorCode
63 puts stderr "$m\n$errorCode\n$errorInfo"
73 fconfigure $p -blocking yes
81 catch { after cancel $watchdog; unset watchdog }
85 fileevent $p readable {}
88 proc gui_polarity {diff} {
90 if {$diff & 0x06} { lappend l X10 X9 }
91 if {$diff & 0x09} { lappend l X8 X1 X2 X3 X4 X5 X6 X7 }
97 proc polarity {newpname m} {
100 if {![string compare $m $polarity]} return
103 gui_polarity [expr "0x$m ^ 0x$polarity"]
106 proc polarity_l {} { polarity l 908000 }
107 proc polarity_p {} { polarity p 97ff79 }
108 proc polarity_x {} { polarity x 97ff7f }
110 proc pt_now {how point pos xtra} {
111 set msg a0[lindex $point $pos]
112 debug "$how point $point pos=$pos msg=$msg$xtra"
113 gui "M [lindex $point 2] [expr {!$pos}]"
116 proc pt_must {point newpos} {
117 upvar #0 pointpos($point) pos
119 if {[info exists pos] && $pos == $newpos} return
121 if {[info exists pq]} {
122 lappend pq [list $point $pos]
123 debug "queue point $point pos=$pos l=[llength $pq]"
126 pt_now immed $point $pos {}
134 set pq [lrange $pq 1 end]
135 pt_now nowdo [lindex $v 0] [lindex $v 1] " l=[llength $pq]"
144 set c [read $rand 1]; if {![string length $c]} { error "eof on rand" }
149 proc pt_maybe {point oneisright} {
151 if {[info exists always]} {
155 set pos [expr [regexp {^[89a-f]} $x] ? 1 : 0]
156 debug "chose point $point pos=$pos (x=$x)"
162 upvar #0 segdetect($seg) segd
163 if {![info exists segd]} {
164 debug "segment $seg = already"
165 } elseif {[string length $segd]} {
166 debug "segment $seg = pending already"
168 debug "segment $seg = soon"
169 set segd [after 100 s0t $seg]
173 upvar #0 segdetect($seg) segd
174 debug "segment $seg = now"
179 upvar #0 segdetect($seg) segd
180 if {![info exists segd]} {
181 debug "segment $seg ! (overwrites =)"
182 } elseif {[string length $segd]} {
183 debug "segment $seg ! (cancels =)"
186 debug "segment $seg ! already"
193 proc pm_maydetect {d seg} {
214 proc pm_detect {seg} {
215 global segs pname segsasgot
216 if {[string compare $seg [lindex $segsasgot 1]]} {
217 set segsasgot [list [lindex $segsasgot 1] $seg]
219 if {[lsearch -exact $segs $seg] < 0} {
222 debug "pm_detect $seg ($segsasgot) ($segs) $pname$seg"
223 switch -exact $pname$seg {
224 p07 - p06 { polarity_l }
225 p02 - p09 { polarity_x }
226 l16 - l1c - l10 - l1a { polarity_p }
227 l03 - l05 - l08 - l0b { polarity_x }
228 x07 - x06 { polarity_l }
229 x14 - x16 - x1c - x20 - x1c - x10 { polarity_p }
232 04 - 0a { pt_must "00 01 X7" 1; pt_must "40 41 X8" 1 }
233 03 - 05 { pt_must "00 01 X7" 0 }
234 08 - 0b { pt_must "40 41 X8" 0 }
235 16 - 1c { pt_must "02 03 A5" 0 }
236 1a - 10 { pt_must "42 43 A6" 0 }
238 switch -exact [join $segs -] {
239 14-20 { pt_must "42 43 A6" 1 }
240 20-14 { pt_must "02 03 A5" 1 }
242 switch -exact [join $segs -] {
243 07-02 { pt_maybe "00 01 X7" 0 }
244 02-07 { pt_maybe "02 03 A5" 1 }
245 06-09 { pt_maybe "40 41 X8" 1 }
246 09-06 { pt_maybe "42 43 A6" 0 }
251 global nmrawhich speeddirn funcs
253 for {set i 0} {$i < $m} {incr i} {
254 tellpic_q [lindex [list $speeddirn $funcs] $nmrawhich]
255 set nmrawhich [expr {!$nmrawhich}]
260 global watchdog testonly
261 catch { after cancel $watchdog }
262 set watchdog [after 50 watchdog]
263 tellpic_q 9808 ;# 128ms
267 debug "got hello, starting up"
280 switch -glob [lindex $m 0] {
281 01 - 02 { tellnmra $m }
282 09 { fp $m; pm_hello }
283 07 { puts "short circuit"; exit 1 }
284 28 { fp $m; pm_charged }
285 9[0-7] { fp $m; pm_maydetect 0 [lindex $m 1] }
286 9? { fp $m; pm_detect [lindex $m 1]; pm_maydetect 1 [lindex $m 1] }
287 0a - [234567]? { puts "pic debug $m" }
288 * { fp $m; fail "pic unknown $m" }
292 proc onreadp_test {} {
293 if {![gets stdin m]} { return }
301 if {![string length $c]} {
302 if {[eof $p]} { error "eof on device" }
307 if {[regexp {^[0-7]} $x]} {
308 if {![regexp {^x} $m]} {
316 proc newspeeddirn {} {
318 set speed [expr {($b1 * $b1) / 516}]
320 set dirn [expr {$b2 / 128}]
321 debug "speeddirn b1=$b1 speed=$speed b2=$b2 dirn=$dirn"
322 return "speed126 2 $speed $dirn"
327 set value [expr {($b3 & 127) * 16}]
328 debug "funcs b3=$b3 value=[format %x $value]"
329 return "funcs5to8 2 $value"
332 proc maybechange {thing} {
334 upvar #0 ${thing}_fixed fixed
335 if {![info exists fixed]} {
336 set rb 0x[randbyte][randbyte]
339 1.0 / (($ch(${thing}every) - $ch(minint)*0.001) * $ch(scale))
341 debug "maybechange $thing rb=$rb no"
344 debug "maybechange $thing rb=$rb yes ..."
347 debug "fixed $thing $fixed"
350 set bin [eval exec ./hostside-old -s/dev/stdout $l]
351 binary scan $bin H* x
352 debug "changed $thing=$x"
359 catch { after cancel $chwa }
360 if {[maybechange speeddirn] || [maybechange funcs]} {
361 set interval $ch(minint)
365 set chwa [after $interval changewhat]
369 global port p rand testonly
371 set p [open $port {RDWR NONBLOCK} 0]
373 exec stty -F $port min 1 time 0 -istrip -ocrnl -onlcr -onocr -opost \
374 -ctlecho -echo -echoe -echok -echonl -iexten -isig \
376 9600 clocal cread crtscts -hup -parenb cs8 -cstopb \
377 -ixoff bs0 cr0 ff0 nl0 -ofill -olcuc
379 fconfigure $p -encoding binary -translation binary \
380 -blocking false -buffering none
382 fileevent $p readable onreadp
385 fconfigure stdin -blocking false
386 fileevent stdin readable onreadp_test
389 set rand [open /dev/urandom {RDONLY} 0]
390 fconfigure $rand -encoding binary -translation binary