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

#include <Timers.h>

#include <ADC.h>

#include <Spindle.h>


// derived classes

#include <AnalogPin.h>

#include <InputPin.h>

#include     <LimitSwitch.h>

#include <OutputPin.h>

#include     <PwmOutputPin.h>


#include "inc/hw_adc.h"

#include "driverlib/adc.h"

#include <inc/hw_gpio.h>



GpioPin::GpioPin(

     char*          data,

     String*          msgPtr ) {


     gpioBase          = 0;

     portC               = 'A';

     pin                    = 0;

     pinSelectMask     = 0;


     char header          = 'X';

     activeMode          = 'h';

     driveCurrent     = '3'; // 8ma


     parseLength          = 0;

     sscanf( data,

          " %c%c%c%c%c%ln",

          &header,

          &portC,

          &pin,

          &activeMode,

          &driveCurrent,

          &parseLength );


     if ( header != 'P' )

          return;


     pin                    -= '0';

     mask               = 1 << pin;

     if ( ! mask ) {

          *msgPtr          += "\nE Invalid pin number ";

          *msgPtr          += String( data );

          return; };


     uint8_t baseOffset     = portC - 'A';

     baseStruct               = NULL;


     if ( baseOffset < BaseTableLength ) {

          baseStruct     = &GPIOBaseVector[ baseOffset ];

          gpioBase     = baseStruct->base; };


     if ( ! gpioBase ) {

          *msgPtr          += "\nE Invalid port ";

          *msgPtr          += String( data );

          return; };


     ROM_SysCtlPeripheralEnable( baseStruct->sysCtl ); // Enable GPIO port


     pinSelectMask     =

               gpioBase

          +     GPIO_O_DATA

          +     ( mask << 2 );  // address bit map


     activeLow     = activeMode == 'l';


     Timers::checkPinConflict( this ); };



GpioPin::~GpioPin( void ) {

#ifdef DebugDelete

     Serial.println( " delete GpioPin" );

#endif


     if ( valid() )

          ROM_GPIOPinTypeGPIOInput( gpioBase, mask ); };


uint16_t

     GpioPin::id() {


     return

          portC << 8 + pin; };


bool

     GpioPin::valid() {


     return

          pinSelectMask != 0; };


bool

     GpioPin::readPin() {

     

     return

          false; };