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

#include <MachineCommands.h>

#include <SysTick.h>

#include <InputPin.h>



bool

     MachineOK::validate( char* data, String* msgPtr ) {


     pin  = new InputPin( data, msgPtr );

     if ( pin->valid() ) {

          pinState        = pin->readPin();

          timeout               = false;

          statusChange     = true;

          lastPulseTime     = SysTick::microSecondTimer;


          *msgPtr               += " Machine OK pin ";

          pin->fmtPin( msgPtr );


          return

               true; };

     delete pin;

     pin    = NULL;


     return

          false; };



bool

     MachineOK::checkMachineNotOk() {


     if ( pinState != pin->readPin() ) {

          pinState          = ! pinState;

          lastPulseTime     = SysTick::microSecondTimer;

          if ( timeout ) {

               timeout               = false;

               statusChange     = true; }; }


     else if ( ( SysTick::microSecondTimer - lastPulseTime ) > 1000000 ) {

          if ( ! timeout ) {

               timeout               = true;

               statusChange     = true; }; };


     return

          timeout; };


void

     MachineOK::reportDigital( String* msgPtr ) {


     if ( statusChange ) {

          char  data[8];

          snprintf( data, 8,

               " %c%d %c",

               pin->portC,

               pin->pin,

               timeout ?

                         '1'     // Alarm

                    :     '0' );

          *msgPtr               += String( data );

          statusChange     = false; }; };