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" }
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]} {
193 debug "segment $seg ! (overwrites =)"
194 } elseif {[string length $segd]} {
195 debug "segment $seg ! (cancels =)"
198 debug "segment $seg ! already"
205 proc pm_maydetect {d seg} {
226 proc pm_detect {seg} {
227 global segs pname segsasgot
228 if {[string compare $seg [lindex $segsasgot 1]]} {
229 set segsasgot [list [lindex $segsasgot 1] $seg]
231 if {[lsearch -exact $segs $seg] < 0} {
234 debug "pm_detect $seg ($segsasgot) ($segs) $pname$seg"
235 switch -exact $pname$seg {
236 l16 - l1c - l08 - l0b { polarity y }
237 l10 - l1a - l03 - l05 { polarity x }
238 x07 - x04 - x0a { polarity l }
239 x16 - x1c - x14 - x0b { polarity y }
240 y06 - y04 - y0a { polarity l }
241 y20 - y10 - y1a - y05 { polarity x }
244 04 - 0a { pt_must "00 01 X7" 1; pt_must "40 41 X8" 1 }
245 05 { pt_must "00 01 X7" 0 }
246 0b { pt_must "40 41 X8" 0 }
247 16 - 1c { pt_must "02 03 A5" 0 }
248 1a - 10 { pt_must "42 43 A6" 0 }
249 14 { pt_ifthenmust "02 03 A5" 1 "42 43 A6" 1 }
250 20 { pt_ifthenmust "42 43 A6" 1 "02 03 A5" 1 }
252 switch -exact [join $segs -] {
253 02-07 { pt_maybe "00 01 X7" 0 }
254 07-02 { pt_maybe "02 03 A5" 1 }
255 09-06 { pt_maybe "40 41 X8" 1 }
256 06-09 { pt_maybe "42 43 A6" 0 }
261 global nmrawhich speeddirn funcs
263 for {set i 0} {$i < $m} {incr i} {
264 tellpic_q [lindex [list $speeddirn $funcs] $nmrawhich]
265 set nmrawhich [expr {!$nmrawhich}]
270 global watchdog testonly
271 catch { after cancel $watchdog }
272 set watchdog [after 50 watchdog]
273 tellpic_q 9808 ;# 128ms
277 debug "got hello, starting up"
290 switch -glob [lindex $m 0] {
291 01 - 02 { tellnmra $m }
292 09 { fp $m; pm_hello }
293 07 { puts "short circuit"; exit 1 }
294 28 { fp $m; pm_charged }
295 9[0-7] { fp $m; pm_maydetect 0 [lindex $m 1] }
296 9? { fp $m; pm_detect [lindex $m 1]; pm_maydetect 1 [lindex $m 1] }
297 0a - [234567]? { puts "pic debug $m" }
298 * { fp $m; fail "pic unknown $m" }
302 proc onreadp_test {} {
303 if {![gets stdin m]} { return }
311 if {![string length $c]} {
312 if {[eof $p]} { error "eof on device" }
317 if {[regexp {^[0-7]} $x]} {
318 if {![regexp {^x} $m]} {
326 proc newspeeddirn {} {
328 set speed [expr {($b1 * $b1) / 516}]
330 set dirn [expr {$b2 / 128}]
331 debug "speeddirn b1=$b1 speed=$speed b2=$b2 dirn=$dirn"
332 return "speed126 2 $speed $dirn"
337 set value [expr {($b3 & 127) * 16}]
338 debug "funcs b3=$b3 value=[format %x $value]"
339 return "funcs5to8 2 $value"
342 proc maybechange {thing} {
344 upvar #0 ${thing}_fixed fixed
345 if {![info exists fixed]} {
346 set rb 0x[randbyte][randbyte]
349 1.0 / (($ch(${thing}every) - $ch(minint)*0.001) * $ch(scale))
351 debug "maybechange $thing rb=$rb no"
354 debug "maybechange $thing rb=$rb yes ..."
357 debug "fixed $thing $fixed"
360 set bin [eval exec ./hostside-old -s/dev/stdout $l]
361 binary scan $bin H* x
362 debug "changed $thing=$x"
369 catch { after cancel $chwa }
370 if {[maybechange speeddirn] || [maybechange funcs]} {
371 set interval $ch(minint)
375 set chwa [after $interval changewhat]
379 global port p rand testonly
381 set p [open $port {RDWR NONBLOCK} 0]
383 exec stty -F $port min 1 time 0 -istrip -ocrnl -onlcr -onocr -opost \
384 -ctlecho -echo -echoe -echok -echonl -iexten -isig \
386 9600 clocal cread crtscts -hup -parenb cs8 -cstopb \
387 -ixoff bs0 cr0 ff0 nl0 -ofill -olcuc
389 fconfigure $p -encoding binary -translation binary \
390 -blocking false -buffering none
392 fileevent $p readable onreadp
395 fconfigure stdin -blocking false
396 fileevent stdin readable onreadp_test
399 set rand [open /dev/urandom {RDONLY} 0]
400 fconfigure $rand -encoding binary -translation binary