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

#include <AnalogPin.h>


MotorVoltage*     MotorVoltage::motorVoltage     = NULL;


void

     MotorVoltage::New(

          char*          data,

          String*          msgPtr ) {

     

     if ( motorVoltage )

          delete motorVoltage;

     

     motorVoltage = new MotorVoltage(

          data,

          msgPtr );

     

     if ( ! motorVoltage->valid() ) {

          delete motorVoltage;

          motorVoltage = NULL; };     };

     

bool

     MotorVoltage::Low() {

     

     return

          motorVoltage

     &&     motorVoltage->low(); };



MotorVoltage::MotorVoltage(

     char*          data,

     String*          msgPtr ) {


     if ( pvddPin )

          delete pvddPin;

     

     char pvddPinString     [8];

     thresholdPct     = 0;

     maxValue          = 0;


     sscanf( data,

          " %s %lu",

          pvddPinString,

          &thresholdPct );

     

     *msgPtr          += "\n pvdd";

     pvddPin          = new AnalogPin(

          pvddPinString,

          msgPtr );

     if ( ! pvddPin->valid() ) {

          delete pvddPin;

          pvddPin = NULL; };

          

     *msgPtr          += " threshold % ";

     *msgPtr          += String( thresholdPct ); };


bool

     MotorVoltage::valid() {

     

     return

          pvddPin

     &&     thresholdPct; };


bool

     MotorVoltage::low() {

     

     uint32_t analogValue = pvddPin->analogValue();

     if ( analogValue > maxValue )

          maxValue     = analogValue;


     return

          analogValue < ( maxValue * thresholdPct / 100 ); };