// © 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 <SlaveModeCommand.h>

#include <Machine.h>

#include <MachineCommands.h>

#include <SysTick.h>

#include <MainCommands.h>

#include <InterpolationTimer.h>

#include <StepCounter.h>

#include <Axis.h>


const char SlaveModeCommand::myName[] =

     "SlaveMode";


const char*

     SlaveModeCommand::name() {


     return

          myName; };


SlaveModeCommand::SlaveModeCommand(

     char*               data,

     Axis*               aFirstMachineAxis,

     String*               msgPtr ) {


     firstMachineAxis     = aFirstMachineAxis;


     aborting          = false;

     starting          = true;

     nextCommand          = NULL; };


SlaveModeCommand::~SlaveModeCommand() {};


void

     SlaveModeCommand::abort(

          Machine* machine ) {


     aborting     = true; };



bool // Called on interpolation ISR

     SlaveModeCommand::execute(

          Machine* machine ) {


     if ( starting ) {

          // read the counts to clear them

          Axis* axisPtr  = firstMachineAxis;

          while ( axisPtr ) {

               StepCounter* slaveStepCounter     = axisPtr->slaveStepCounter;

               if ( slaveStepCounter )

                    slaveStepCounter->readScaledCount();


               axisPtr   = axisPtr->nextAxis; };


          starting     = false; };


     if ( aborting )

          return

               true;


     Axis* axisPtr  = firstMachineAxis;

     while ( axisPtr ) {

          axisPtr->addSlaveInput();

          axisPtr   = axisPtr->nextAxis; };


     return     // User must abort to exit

          false; };


void

     SlaveModeCommand::reportInterpolationStatus(

          Machine*     machine,

          String*          msgPtr ) {


     *msgPtr          += INTERPOLATOR_SLAVE; };


void

     SlaveModeCommand::report(

          Machine*     machine,

          String*          msgPtr ) {


     *msgPtr          += " Exit Slave Mode"; };