chiark / gitweb /
new FOR_TRA etc. macros; initial hooks for resolver
[trains.git] / hostside / startup.c
index 25b67505db8d707952b7f26c5263a66b0d844731..b2822094848828e2b39541e88a3150d00a083732 100644 (file)
@@ -49,6 +49,8 @@ static void initial_ping(void) {
 void sta_startup(void) { sta_goto(Sta_Flush); }
 
 static void sta_goto(StartupState new_state) {
+  SEG_IV;
+  
   toev_stop(&sta_toev);
   sta_toev.callback= timedout_onward;
   sta_toev.duration= -1;
@@ -78,8 +80,17 @@ static void sta_goto(StartupState new_state) {
   case Sta_Ping:                                                   break;
   case Sta_Fault:                                                  break;
   case Sta_Settling:                        enco_pic_off(&piob);   break;
-  case Sta_Resolving:                       enco_pic_on(&piob);    break;
-  case Sta_Run:   persist_install(); retransmit_start();           break;
+  case Sta_Resolving:
+    FOR_SEG seg->res_detect= 0;
+    enco_pic_on(&piob);
+    break;
+  case Sta_Run:
+    if (resolve_failed)
+      /* in this case, we get stuck - user has to power cycle the layout */
+      return;
+    persist_install();
+    retransmit_start();
+    break;
   }
   if (piob.l) serial_transmit(&piob);
 
@@ -190,13 +201,23 @@ void on_pic_aaargh(const PicInsnInfo *pii, const PicInsn *pi, int objnum)
   { abort(); }
 
 void on_pic_detect1(const PicInsnInfo *pii, const PicInsn *pi, int segn) {
+  Segment *seg;
   if (segn >= NUM_SEGMENTS) die("PIC sent detect @#%d out of range",segn);
-  if (sta_state < Sta_Run) {
+
+  seg= &segments[segn];
+  
+  switch (sta_state) {
+  case Sta_Flush:
+  case Sta_Off:
+  case Sta_Ping:
+  case Sta_Fault:
+  case Sta_Settling:
     oprintf(UPO, "warning fixme ignored non-Run detection @%s\n",
            info_segments[segn].pname);
-    return;
+    break;
+  case Sta_Resolving:  seg->res_detect= 1;             break;
+  case Sta_Run:        safety_notify_detection(seg);   break;
   }
-  safety_notify_detection(&segments[segn]);
 }
 
 /*---------- fixme move these to where they want to go ----------*/