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