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

#include <Machine.h>

#include <OutputPin.h>

#include <MachineOK.h>



void

     Machine::handshakeControl(

          char*          commandPtr,

          String*          msgPtr ) {


     char* data     = commandPtr + 1;


     switch ( *commandPtr ) {

     

          case SET_ALARM :

          configureAlarm(

               data,

               msgPtr );

          break;


          case SET_WATCHDOG :

          configureWatchdog(

               data,

               msgPtr );

          break;


          case SET_MACHINE_OK :

          configureMachineOK(

               data,

               msgPtr );

          break;


          default :

          *msgPtr

               +=     *commandPtr

               +     "E Invalid Machine Command - Ignored"; }; };



void

     Machine::configureAlarm(

          char*          data,

          String*          msgPtr ) {


     *msgPtr

          +=     String( HANDSHAKE_CONTROL )

          +     String( SET_ALARM );

  

     if ( alarmPin )

          delete alarmPin;


     alarmPin          = new OutputPin( data, msgPtr );

     if ( alarmPin->valid() ) {

          alarmPin->setPin( false );


          *msgPtr          += " Alarm pin ";

          alarmPin->fmtPin( msgPtr ); }

     else {

          *msgPtr          += " No Alarm";

          delete alarmPin;

          alarmPin    = NULL; }; };


void

     Machine::configureWatchdog(

          char*          data,

          String*          msgPtr ) {


     *msgPtr

          +=     String( HANDSHAKE_CONTROL )

          +     String( SET_WATCHDOG );

  

     if ( watchdogPin )

          delete watchdogPin;


     watchdogPin          = new OutputPin( data, msgPtr );

     if ( watchdogPin->valid() ) {

          watchdogPin->setPin( true );


          *msgPtr          += " Watchdog pin ";

          watchdogPin->fmtPin( msgPtr ); }

     else {

          *msgPtr          += " No Watchdog";

          delete watchdogPin;

          watchdogPin    = NULL; }; };



void

     Machine::configureMachineOK(

          char*          data,

          String*          msgPtr ) {


     *msgPtr

          +=     String( HANDSHAKE_CONTROL )

          +     String( SET_MACHINE_OK );


     if ( machineOK )

          delete machineOK;


     machineOK     = new MachineOK();

     if ( ! machineOK->validate( data, msgPtr ) ) {

          delete machineOK;

          machineOK   = NULL;

          *msgPtr          += " No Machine OK input"; }; };