7 set ch(speeddirnevery) 15
16 set pq {} ;# unset: cdu charged and waiting
19 # unset pointpos($point)
20 # unset segdetect($seg) ;# unset: shown D0; {}: shown D1; or: after id, D1->0
27 global watchdog polarity segdetect
31 if {[info exists watchdog]} { gui "P 1" }
32 if {![regexp {^90} $polarity]} { gui_polarity }
33 foreach seg [array names segdetect] {
45 set b [binary format H* $m]
56 global errorInfo errorCode
57 puts stderr "$m\n$errorCode\n$errorInfo"
67 fconfigure $p -blocking yes
75 catch { after cancel $watchdog; unset watchdog }
79 fileevent $p readable {}
82 proc gui_polarity {} {
101 debug "polarising $m"
103 if {[string compare $m $polarity]} {
108 proc polarity_l {} { polarity 908000 }
109 proc polarity_x {} { polarity 97ff7f }
111 proc pt_now {how point pos xtra} {
112 set msg a0[lindex $point $pos]
113 debug "$how point $point pos=$pos msg=$msg$xtra"
114 gui "M [lindex $point 2] [expr {!$pos}]"
117 proc pt_must {point newpos} {
118 upvar #0 pointpos($point) pos
120 if {[info exists pos] && $pos == $newpos} return
122 if {[info exists pq]} {
123 lappend pq [list $point $pos]
124 debug "queue point $point pos=$pos l=[llength $pq]"
127 pt_now immed $point $pos {}
135 set pq [lrange $pq 1 end]
136 pt_now nowdo [lindex $v 0] [lindex $v 1] " l=[llength $pq]"
145 set c [read $rand 1]; if {![string length $c]} { error "eof on rand" }
150 proc pt_maybe {point} {
152 if {[info exists always]} {
156 set pos [expr [regexp {^[89a-f]} $x] ? 1 : 0]
157 debug "chose point $point pos=$pos (x=$x)"
163 upvar #0 segdetect($seg) segd
164 if {![info exists segd]} {
165 debug "segment $seg = already"
166 } elseif {[string length $segd]} {
167 debug "segment $seg = pending already"
169 debug "segment $seg = soon"
170 set segd [after 100 s0t $seg]
174 upvar #0 segdetect($seg) segd
175 debug "segment $seg = now"
180 upvar #0 segdetect($seg) segd
181 if {![info exists segd]} {
182 debug "segment $seg ! (overwrites =)"
183 } elseif {[string length $segd]} {
184 debug "segment $seg ! (cancels =)"
187 debug "segment $seg ! already"
194 proc pm_maydetect {d seg} {
215 proc pm_detect {seg} {
218 07 - 06 { polarity_l }
219 16 - 1c - 1a - 10 - 03 - 05 - 08 - 0b { polarity_x }
222 14 - 20 { pt_must "02 03 A5" 1; pt_must "42 43 A6" 1 }
223 04 - 0a { pt_must "00 01 X7" 1; pt_must "40 41 X8" 1 }
224 03 - 05 { pt_must "00 01 X7" 0 }
225 08 - 0b { pt_must "40 41 X8" 0 }
226 16 - 1c { pt_must "02 03 A5" 0 }
227 1a - 10 { pt_must "42 43 A6" 0 }
229 if {[lsearch -exact $segs $seg] < 0} {
230 set segs [list [lindex $segs end] $seg]
232 switch -exact [join $segs -] {
233 07-02 { pt_maybe "00 01 X7" }
234 02-07 { pt_maybe "02 03 A5" }
235 06-09 { pt_maybe "40 41 X8" }
236 09-06 { pt_maybe "42 43 A6" }
241 global watchdog testonly speeddirn funcs
242 catch { after cancel $watchdog }
243 set watchdog [after 50 watchdog]
244 tellpic_q 9808$speeddirn$funcs ;# 128ms
248 debug "got hello, starting up"
256 switch -glob [lindex $m 0] {
259 9[0-7] { pm_maydetect 0 [lindex $m 1] }
260 9? { pm_detect [lindex $m 1]; pm_maydetect 1 [lindex $m 1] }
261 0a - [234567]? { puts "pic debug $m" }
262 * { fail "pic unknown $m" }
266 proc onreadp_test {} {
267 if {![gets stdin m]} { return }
275 if {![string llength $c]} {
276 if {[eof $p]} { error "eof on device" }
281 if {[regexp {^[89a-f]} $x]} {
282 if {![regexp {^x} $m]} {
290 proc newspeeddirn {} {
292 set speed [expr {($b1 * $b1) / 516}]
294 set dirn [expr {$b2 / 128}]
295 debug "speeddirn b1=$b1 speed=$speed b2=$b2 dirn=$dirn"
296 return "speed126 2 $speed $dirn"
301 set value [expr {($b3 & 127) * 16}]
302 debug "funcs b3=$b3 value=[format %x $value]"
303 return "funcs5to8 2 $value"
306 proc maybechange {thing} {
308 set rb 0x[randbyte][randbyte]
311 1.0 / (($ch(${thing}every) - $ch(minint)*0.001) * $ch(scale))
313 debug "maybechange $thing rb=$rb no"
316 debug "maybechange $thing rb=$rb yes ..."
318 set bin [eval exec ./hostside-old -s/dev/stdout $l]
319 binary scan $bin H* x
320 debug "changed $thing=$x"
327 if {[maybechange speeddirn] || [maybechange funcs]} {
328 set interval $ch(minint)
332 after $interval changewhat
336 global port p rand testonly
338 set p [open $port {RDWR NONBLOCK} 0]
340 exec stty -F $port min 1 time 0 -istrip -ocrnl -onlcr -onocr -opost \
341 -ctlecho -echo -echoe -echok -echonl -iexten -isig \
343 9600 clocal cread crtscts -hup -parenb cs8 -cstopb \
344 -ixoff bs0 cr0 ff0 nl0 -ofill -olcuc
346 fconfigure $p -encoding binary -translation binary \
347 -blocking false -buffering none
349 fileevent $p readable onreadp
352 fconfigure stdin -blocking false
353 fileevent stdin readable onreadp_test
356 set rand [open /dev/urandom {RDONLY} 0]
357 fconfigure $rand -encoding binary -translation binary