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