chiark / gitweb /
works (although we do not know why 02-07 etc. in pm_detect seem to have to be backwards
[trains.git] / hostside / stopgap-controller
1 #!/usr/bin/tclsh8.2
2
3 set testonly 0
4 #set testonly 1
5 set port /dev/ttya0
6
7 set ch(funcsevery) 10
8 set ch(speeddirnevery) 15
9 set ch(scale) 1
10
11 set ch(minint) 5000
12 # unset always
13 # set always 0
14 set nmrawhich 0
15 set lastptchosen xx
16
17 set polmsg(l) 908000
18 set polmsg(x) 90f802
19 set polmsg(y) 90807c
20 set pname l
21 set m {}
22 set segs {xx yy}
23 set segsasgot {xx yy}
24 set pq {} ;# unset: cdu charged and waiting
25 set speeddirn ff7f
26 set speeddirn_fixed {speed126 2 80 0}
27 set funcs ff7f
28 # unset pointpos($point)
29 # unset segdetect($seg) ;# unset: shown D0; {}: shown D1; or: after id, D1->0
30
31 proc gui {m} {
32     puts "GUI $m"
33 }
34
35 proc gui_init {} {
36     global watchdog polarity segdetect
37     gui "M A2 0"
38     gui "M A5 0 J"
39     gui "M A6 0 J"
40     if {[info exists watchdog]} { gui "P 1" }
41 #    gui_polarity 0x$polarity
42     foreach seg [array names segdetect] {
43         gui "D1 $seg"
44     }
45 }
46
47 proc debug {m} {
48     puts $m
49 }
50
51 proc tellpic_q {m} {
52     global p testonly
53     if {$testonly} return
54     set b [binary format H* $m]
55     puts -nonewline $p $b
56 }
57
58 proc tellpic {m} {
59     puts ">> $m"
60     tellpic_q $m
61 }
62
63 proc bgerror {m} {
64     if {[catch {
65         global errorInfo errorCode
66         puts stderr "$m\n$errorCode\n$errorInfo"
67         fail "bgerror $m"
68     } emsg]} {
69         exit 127
70     }
71 }
72
73 proc fail_now {} {
74     global p
75     debug "failing now"
76     fconfigure $p -blocking yes
77     gui "P 0"
78     tellpic 20
79     exit 1
80 }
81
82 proc fail {m} {
83     global watchdog p
84     catch { after cancel $watchdog; unset watchdog }
85     puts "failing $m"
86     tellpic 9801 ;# 16ms
87     after 2000 fail_now
88     fileevent $p readable {}
89 }
90
91 proc gui_polarity {diff} {
92     set l {}
93     if {$diff & 0x06} { lappend l X10 X9 }
94     if {$diff & 0x09} { lappend l X8 X1 X2 X3 X4 X5 X6 X7 }
95     foreach seg $l {
96         gui "R $seg"
97     }
98 }
99
100 proc polarity {newpname} {
101     global pname polmsg
102     debug "polarising $newpname"
103     if {![string compare $pname $newpname]} return
104     tellpic $polmsg($newpname)
105 #    gui_polarity
106     set pname $newpname
107 }
108
109 proc pt_now {how point pos xtra} {
110     set msg a0[lindex $point $pos]
111     debug "$how point $point pos=$pos msg=$msg$xtra"
112     gui "M [lindex $point 2] [expr {!$pos}]"
113     tellpic $msg
114 }
115 proc pt_must {point newpos} {
116     upvar #0 pointpos($point) pos
117     global pq
118     if {[info exists pos] && $pos == $newpos} return
119     set pos $newpos
120     if {[info exists pq]} {
121         lappend pq [list $point $pos]
122         debug "queue point $point pos=$pos l=[llength $pq]"
123         return
124     }
125     pt_now immed $point $pos {}
126     set pq {}
127 }
128 proc pt_ifthenmust {ifpoint ifposwant thenpoint thenpos} {
129     upvar #0 pointpos($ifpoint) ifpos
130     if {![info exists ifpos] || $ifpos != $ifposwant} return
131     pt_must $thenpoint $thenpos
132 }
133
134 proc pm_charged {} {
135     global pq
136     if {[llength $pq]} {
137         set v [lindex $pq 0]
138         set pq [lrange $pq 1 end]
139         pt_now nowdo [lindex $v 0] [lindex $v 1] " l=[llength $pq]"
140     } else {
141         debug "cdu-charged"
142         unset pq
143     }
144 }
145
146 proc randbyte {} {
147     global rand
148     set c [read $rand 1]; if {![string length $c]} { error "eof on rand" }
149     binary scan $c H* x
150     return $x
151 }
152
153 proc pt_maybe {point oneisright} {
154     global always lastptchosen
155     if {[info exists always]} {
156         set pos $always
157     } else {
158         if {![string compare $point $lastptchosen]} return
159         set lastptchosen $point
160         set x [randbyte]
161         set pos [expr [regexp {^[89a-f]} $x] ? 1 : 0]
162         debug "chose point $point pos=$pos (x=$x)"
163     }
164     pt_must $point $pos
165 }
166
167 proc s0 {seg} {
168     upvar #0 segdetect($seg) segd
169     if {![info exists segd]} {
170         debug "segment $seg = already"
171     } elseif {[string length $segd]} {
172         debug "segment $seg = pending already"
173     } else {
174         debug "segment $seg = soon"
175         set segd [after 100 s0t $seg]
176     }
177 }
178 proc s0t {seg} {
179     upvar #0 segdetect($seg) segd
180     debug "segment $seg = now"
181     unset segd
182     gui "D0 $seg"
183 }
184 proc s1 {seg} {
185     upvar #0 segdetect($seg) segd
186     if {![info exists segd]} {
187         debug "segment $seg ! (overwrites =)"
188     } elseif {[string length $segd]} {
189         debug "segment $seg ! (cancels =)"
190         after cancel $segd
191     } else {
192         debug "segment $seg ! already"
193         return
194     }
195     gui "D1 $seg"
196     set segd {}
197 }       
198
199 proc pm_maydetect {d seg} {
200     switch -exact $seg {
201         06 { s$d X10 }
202         09 { s$d X8 }
203         0a { s$d X6 }
204         04 { s$d X5 }
205         02 { s$d X7 }
206         07 { s$d X9 }
207         14 { s$d A5 }
208         20 { s$d A6 }
209         1a { s$d A4 }
210         10 { s$d A2 }
211         03 { s$d X1 }
212         05 { s$d X3 }
213         16 { s$d A3 }
214         1c { s$d A1 }
215         08 { s$d X2 }
216         0b { s$d X4 }
217     }
218 }
219
220 proc pm_detect {seg} {
221     global segs pname segsasgot
222     if {[string compare $seg [lindex $segsasgot 1]]} {
223         set segsasgot [list [lindex $segsasgot 1] $seg]
224     }
225     if {[lsearch -exact $segs $seg] < 0} {
226         set segs $segsasgot
227     }
228     debug "pm_detect $seg ($segsasgot) ($segs) $pname$seg"
229     switch -exact $pname$seg {
230         l16 - l1c - l08 - l0b { polarity y }
231         l10 - l1a - l03 - l05 { polarity x }
232         x07 - x04 - x0a { polarity l }
233         x16 - x1c - x14 - x0b { polarity y }
234         y06 - y04 - y0a { polarity l }
235         y20 - y10 - y1a - y05 { polarity x }
236     }
237     switch -exact $seg {
238         04 - 0a { pt_must "00 01 X7" 1; pt_must "40 41 X8" 1 }
239         05 { pt_must "00 01 X7" 0 }
240         0b { pt_must "40 41 X8" 0 }
241         16 - 1c { pt_must "02 03 A5" 0 }
242         1a - 10 { pt_must "42 43 A6" 0 }
243         14 { pt_ifthenmust "02 03 A5" 1 "42 43 A6" 1 }
244         20 { pt_ifthenmust "42 43 A6" 1 "02 03 A5" 1 }
245     }
246     switch -exact [join $segs -] {
247         02-07 { pt_maybe "00 01 X7" 0 }
248         07-02 { pt_maybe "02 03 A5" 1 }
249         09-06 { pt_maybe "40 41 X8" 1 }
250         06-09 { pt_maybe "42 43 A6" 0 }
251     }
252 }
253
254 proc tellnmra {m} {
255     global nmrawhich speeddirn funcs
256     set m 0x$m
257     for {set i 0} {$i < $m} {incr i} {
258         tellpic_q [lindex [list $speeddirn $funcs] $nmrawhich]
259         set nmrawhich [expr {!$nmrawhich}]
260     }
261 }
262
263 proc watchdog {} {
264     global watchdog testonly
265     catch { after cancel $watchdog }
266     set watchdog [after 50 watchdog]
267     tellpic_q 9808 ;# 128ms
268 }
269
270 proc pm_hello {} {
271     debug "got hello, starting up"
272     tellpic 21
273     gui "P 1"
274     watchdog
275     changewhat
276     tellnmra 01
277 }
278
279 proc fp {m} {
280     debug "<< $m"
281 }
282
283 proc frompic {m} {
284     switch -glob [lindex $m 0] {
285         01 - 02 { tellnmra $m }
286         09 { fp $m; pm_hello }
287         07 { puts "short circuit"; exit 1 }
288         28 { fp $m; pm_charged }
289         9[0-7] { fp $m; pm_maydetect 0 [lindex $m 1] }
290         9? { fp $m; pm_detect [lindex $m 1]; pm_maydetect 1 [lindex $m 1] }
291         0a - [234567]? { puts "pic debug $m" }
292         * { fp $m; fail "pic unknown $m" }
293     }
294 }
295
296 proc onreadp_test {} {
297     if {![gets stdin m]} { return }
298     frompic $m
299 }
300
301 proc onreadp {} {
302     global p m
303     while 1 {
304         set c [read $p 1]
305         if {![string length $c]} {
306             if {[eof $p]} { error "eof on device" }
307             return
308         }
309         binary scan $c H* x
310         lappend m $x
311         if {[regexp {^[0-7]} $x]} {
312             if {![regexp {^x} $m]} {
313                 frompic $m
314             }
315             set m {}
316         }
317     }
318 }
319
320 proc newspeeddirn {} {
321     set b1 0x[randbyte]
322     set speed [expr {($b1 * $b1) / 516}]
323     set b2 0x[randbyte]
324     set dirn [expr {$b2 / 128}]
325     debug "speeddirn b1=$b1 speed=$speed b2=$b2 dirn=$dirn"
326     return "speed126 2 $speed $dirn"
327 }
328
329 proc newfuncs {} {
330     set b3 0x[randbyte]
331     set value [expr {($b3 & 127) * 16}]
332     debug "funcs b3=$b3 value=[format %x $value]"
333     return "funcs5to8 2 $value"
334 }
335
336 proc maybechange {thing} {
337     global $thing ch
338     upvar #0 ${thing}_fixed fixed
339     if {![info exists fixed]} {
340         set rb 0x[randbyte][randbyte]
341         if {
342             $rb / 65536.0 >
343             1.0 / (($ch(${thing}every) - $ch(minint)*0.001) * $ch(scale))
344         } {
345             debug "maybechange $thing rb=$rb no"
346             return 0
347         }
348         debug "maybechange $thing rb=$rb yes ..."
349         set l [new$thing]
350     } else {
351         debug "fixed $thing $fixed"
352         set l $fixed
353     }
354     set bin [eval exec ./hostside-old -s/dev/stdout $l]
355     binary scan $bin H* x
356     debug "changed $thing=$x"
357     set $thing ff$x
358     return 1
359 }
360
361 proc changewhat {} {
362     global ch chwa
363     catch { after cancel $chwa }
364     if {[maybechange speeddirn] || [maybechange funcs]} {
365         set interval $ch(minint)
366     } else {
367         set interval 1000
368     }
369     set chwa [after $interval changewhat]
370 }
371
372 proc setup {} {
373     global port p rand testonly
374     if {!$testonly} {
375         set p [open $port {RDWR NONBLOCK} 0]
376     
377         exec stty -F $port min 1 time 0 -istrip -ocrnl -onlcr -onocr -opost \
378                        -ctlecho -echo -echoe -echok -echonl -iexten -isig \
379                        -icanon -icrnl \
380             9600 clocal cread crtscts -hup -parenb cs8 -cstopb \
381             -ixoff bs0 cr0 ff0 nl0 -ofill -olcuc
382
383         fconfigure $p -encoding binary -translation binary \
384                 -blocking false -buffering none
385
386         fileevent $p readable onreadp
387     } else {
388         set p stdin
389         fconfigure stdin -blocking false
390         fileevent stdin readable onreadp_test
391     }
392
393     set rand [open /dev/urandom {RDONLY} 0]
394     fconfigure $rand -encoding binary -translation binary
395 }
396
397 setup
398 gui_init
399 vwait end