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