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

#include <Timers.h>

#include <ADC.h>

#include <Spindle.h>


#include "inc/hw_adc.h"

#include "driverlib/adc.h"

#include <inc/hw_gpio.h>



LimitSwitch::LimitSwitch(

     char*          data,

     String*          msgPtr )


     :     InputPin(

          data,

          msgPtr ) {


     analogAverage     = 0;

     char highChar     = ' ';


     sscanf( data + parseLength + 1,

          " %c",

          &highChar );


     high          = highChar == 'h'; };


LimitSwitch::~LimitSwitch( void ) {

#ifdef DebugDelete

     Serial.println( " delete LimitSwitch" );

#endif

};


void

     LimitSwitch::fmtPin( String* msgPtr ) {


     char data[10];

     snprintf( data, 10,

          " P%c%d%c%c%c",

          portC,

          pin,

          activeMode,

          driveCurrent,

          high ?

               'h'

          :     'l' );


     *msgPtr          += String( data ); };


bool

     LimitSwitch::readPin() {


     // Debounce logic

     analogAverage     -= analogAverage >> 8;

     if ( InputPin::readPin() )

          analogAverage     += 0x100;

     

     return

          analogAverage     >= 0x8000; };