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