6 /*========== basic types etc. ==========*/
8 typedef unsigned short TrainNum;
9 typedef unsigned short SegmentNum;
10 typedef unsigned short LocationNum;
11 typedef short TimeInterval;
12 typedef short Distance;
13 typedef char Speed; /* non-negative, units of 4mm/s */
15 /*========== state of the layout ==========*/
18 SegmentNum foredetect; /* train's detectable part is at most maxinto */
19 Distance maxinto, uncertainty; /* into foredetect but train may be */
20 unsigned /* uncertainty less far advanced */
21 backwards:1; /* train is moving backwards wrt its own front and back */
27 owned:1, /* this track segment is reserved for a train */
28 tr_backwards:1, /* train's motion is (would be) backwards wrt track */
29 pt_sideways:1, /* points are set to `alternative'. (no points?: 0) */
30 pt_moving:1, /* points have been told to change to pt_sideways */
31 cm_autostop:1, /* train should stop on detection */
32 cm_pointchange:1, /* points should change when they can */
33 seg_inverted:1, /* polarity is inverted! */
34 tr_updated:1; /* for use by safety.c:lay_train etc.; otherwise 0 */
35 TimeInterval until_here, /* ) nonnegative; */ /* ) always valid but */
36 until_detect; /* ) 0 if already */ /* ) only meaningful */
37 TrainNum owner; /* ) iff owned */
42 unsigned next_backwards:1;
48 unsigned invertible:1;
49 SegmentLink backwards, forwards, sideways;
55 Distance tail, detectable, head;
59 extern const TrainInfo info_trains[NUM_TRAINS];
60 extern const SegmentInfo info_segments[NUM_SEGMENTS];
63 TrainState trains[NUM_TRAINS];
64 SegmentState segments[NUM_SEGMENTS];
69 /*========== safety.c ==========*/
71 * safety.c is responsible for ensuring that things don't go
72 * physically wrong (eg, collisions, derailments, short circuits,
76 void safety_emergencystop(TranNum);
77 /* Callable directly in response to application command. */
79 void safety_requestspeed(TrainNum tran, long newspeed);
80 /* To be called only by the speed manager, thus indirectly from
82 * Will result in a call to speedmanager_speedchange_notify (and of
83 * course to actual_setspeed. Speed manager must apply accel/decel
84 * curve so that if safety.c agrees the command, the actual speed of
85 * the train changes straight away (at least for decel).
88 void safety_notify_detection(SegmentNum segn);
89 /* To be called by actual.c when new train detection occurs. */
91 /*========== speedmgr.c ==========*/
93 void speedmanager_speedchange_notify(TrainNum tran);
94 /* To be called only by safety.c, whenever speed is actually set.
95 * New speed has already been recorded in State. */
97 /*========== actual.c ==========*/
98 /* actual.c should only be called from safety.c.
99 * It is responsible for communicating with the PICs, including
100 * repeating the NMRA commands and redacting detection information.
103 void actual_setspeed(TrainNum tran);
107 * Entrypoints are: Called from, and as a result of:
108 * actual_setspeed safety.c
109 * actual_emergencystop safety.c
112 /*========== utils.c ==========*/
114 typedef struct TrackLocation TrackLocation;
115 struct TrackLocation { /* transparent, and manipulable by trackloc_... fns */
116 SegmentNum segn; /* current segment */
117 long into; /* distance from start of segment */
118 unsigned backwards:1; /* if 1, into is positive and measured from end */
121 long trackloc_remaininseg(const TrackLocation *tloc);
122 /* Returns dist that tloc can advance before it goes into next segment. */
124 void trackloc_further(TrackLocation *tloc, long *remain_io);
125 /* Advances tloc, decrementing *remain_io, until either
126 * *remain_io becomes zero, or tloc->segn changes. */
128 void trackloc_reverse(TrackLocation *tloc);
129 /* Reverses tloc without changing its actual location. */
131 const SegmentLinkInfo *trackloc_segmentlink_near(const TrackLocation *tloc);
132 const SegmentLinkInfo *trackloc_segmentlink_far(const TrackLocation *tloc);