]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/arducontrol.cpp
Don't wait for queued accessories if power is off
[r2c2.git] / source / libr2c2 / arducontrol.cpp
index 7f95b631aa119bd77e7698d48c52b94fedd7bb23..98f74338285178ecb73a11e63faa005deea56f6e 100644 (file)
@@ -457,7 +457,7 @@ void ArduControl::tick()
 
 void ArduControl::flush()
 {
-       while(!command_queue.empty() || !accessory_queue.empty())
+       while(!command_queue.empty() || (power && !accessory_queue.empty()))
                tick();
 }
 
@@ -1025,10 +1025,41 @@ void ArduControl::ControlThread::main()
                if(get_work(cmd))
                {
                        bool success = true;
+                       bool resync = false;
                        for(unsigned i=0; (success && i<cmd.repeat_count); ++i)
-                               success = (do_command(cmd)==COMMAND_OK);
+                       {
+                               unsigned result = do_command(cmd, control.command_timeout);
+                               success = (result==COMMAND_OK);
+                               resync = (result==0);
+                       }
+
                        if(success && cmd.tag)
                                control.completed_commands.push(cmd.tag);
+
+                       if(resync)
+                       {
+                               if(control.debug>=1)
+                                       IO::print("Synchronization with ArduControl lost, attempting to recover\n");
+                               for(unsigned i=0; (resync && i<16); ++i)
+                               {
+                                       control.serial.put('\xFF');
+                                       while(IO::poll(control.serial, IO::P_INPUT, control.command_timeout))
+                                               resync = (control.serial.get()!=0xFF);
+                               }
+                               if(resync)
+                               {
+                                       if(control.debug>=1)
+                                               IO::print("Resynchronization failed, giving up\n");
+                                       done = true;
+                               }
+                               else
+                               {
+                                       if(control.debug>=1)
+                                               IO::print("Resynchronization successful\n");
+                                       if(cmd.tag)
+                                               control.command_queue.push(cmd);
+                               }
+                       }
                }
                else
                        Time::sleep(10*Time::msec);