// © RiceMotion ( Robert Carl Rice ) 2012-2016 - All rights reserved


// This software makes use of tools and libraries obtained from open source projects or released for

// use by relevant hardware manufactures. However, this software is NOT a part of any open source project.

// It is released only on a "need to know" basis for beta testers of the "RiceCNC Interpolation Engine".

// Recipents of this source code must respect the confidential nature of this software and prevent any

// distribution that could result in counterfeit copies of the "RiceCNC Interpolation Engine".


// © RiceMotion ( Robert Carl Rice ) 2012-2016 - All rights reserved

#include <Pause.h>

#include <Machine.h>

#include <SysTick.h>

#include <Spindle.h>

#include <Coolants.h>

#include <MachineCommands.h>


// derived classes

#include     <AbortCommand.h>

#include     <DoorAjarCommand.h>

#include     <LimitCommand.h>

#include     <MachineNotOkCommand.h>

#include     <StallCommand.h>


const char Pause::myName[] =

     "Pause";


const char*

     Pause::name() {

     

     return

          myName; };


Pause::Pause() {

     pauseState               = PauseINIT;

     continueExecution     = false;

};


bool     // return true when completed

     Pause::execute(

          Machine* machine ) {


     uint32_t     elapsedTime;

     Coolant*     coolant;

     

     switch ( pauseState ) {


          case PauseINIT :


          if ( continueExecution ) {

               return

                    true; };


          // collect current states

          spindleRunning     = false;

          if ( machine->spindle )

               spindleRunning     = machine->spindle->running();


          for (     int i = 0;

                    i < 2;

                    i++ ) {

               coolantRunning[ i ]          = false;

               coolant                         = machine->coolants[ i ];

               if ( coolant )

                    coolantRunning[ i ]          = coolant->on; };


          pauseState     = PauseWAIT_FOR_IDLE;

          break;



          case PauseWAIT_FOR_IDLE :


          if ( continueExecution ) {

               return

                    true; };


          if ( machine->isSeekingPosition() ) // Wait for axes to go idle

               break;


          if ( spindleRunning ) {

               machine->spindle->setSpindleOn( false );

               startTime     = SysTick::microSecondTimer;

               pauseState     = PauseWAIT_FOR_SPINDLE_STOP;

               break; };


          pauseState     = PauseWAIT_FOR_CONTINUATION;     

          break;



          case PauseWAIT_FOR_SPINDLE_STOP :


          if ( continueExecution ) {

               pauseState     = PauseWAIT_FOR_CONTINUATION;

               break; };


          elapsedTime          = SysTick::microSecondTimer - startTime;

          if ( elapsedTime >= machine->spindle->offDelayMs * 1000 ) {


               for (     int i = 0;

                         i < 2;

                         i++ ) {


                    if ( coolantRunning[ i ] ) {

                         coolant     = machine->coolants[ i ];

                         coolant->setCoolantOn( false ); }; };


               pauseState          = PauseWAIT_FOR_CONTINUATION; };

          break;



          case PauseWAIT_FOR_CONTINUATION :

          if ( continueExecution ) {

               for (     int i = 0;

                         i < 2;

                         i++ ) {


                    if ( coolantRunning[ i ] ) {

                         coolant     = machine->coolants[ 0 ];

                         coolant->setCoolantOn( true ); }; };

                    

               if ( spindleRunning ) {

                    machine->spindle->setSpindleOn(     true );

                    startTime     = SysTick::microSecondTimer;

                    pauseState     = PauseWAIT_FOR_SPINDLE_START; }


               else

                    return

                         true; };

          break;



          case PauseWAIT_FOR_SPINDLE_START :


          elapsedTime     = SysTick::microSecondTimer - startTime;

          if ( elapsedTime >= machine->spindle->onDelayMs * 1000 )

               return

                    true;

          break; };


     return

          false; };


void

     Pause::continueInterpolation(

          Machine*     machine ) {


     continueExecution     = true; };


void

     Pause::reportInterpolationStatus(

          Machine*     machine,

          String*          msgPtr ) {


     *msgPtr  += INTERPOLATOR_PAUSED; };