Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ___| _) | _) _) ___|
- \___ \ | | | __| | | | __ `__ \ | _ \ __| __ \
- | | | | ( | | | | | | | ( | | | |
- _____/ _| _| _| \___| _| \__,_| _| _| _| \____| \___/ _| .__/
- _|
- Code source libre de droit. Si vous utilisez mon code, merci à vous d'en préciser la source.
- Date : 09/10/2011
- Auteur : Silicium Corp
- */
- #include "MyServo.h"
- //===============================================================
- MyServo::MyServo(int nbStepInit)
- {
- nbStep = nbStepInit;
- init();
- }
- //===============================================================
- void MyServo::_attach(int pin)
- {
- objectServo.attach(pin);
- myPin = pin;
- }
- //===============================================================
- void MyServo::_detach()
- {
- objectServo.detach();
- }
- //===============================================================
- void MyServo::_write(int posit)
- {
- if (posit < zero)
- {
- posit = zero;
- }
- else if (posit > final)
- {
- posit = final;
- }
- objectServo.write(posit);
- }
- //===============================================================
- int MyServo::_read()
- {
- return(objectServo.read());
- }
- //===============================================================
- boolean MyServo::_attached()
- {
- return (objectServo.attached());
- }
- //===============================================================
- void MyServo::init()
- {
- setZero(0);
- setFinal(180);
- updateProperty();
- actualStep = 0;
- }
- //=================================================================
- void MyServo::updateProperty()
- {
- calculateSpectrum();
- deductionDegree();
- }
- //===============================================================
- void MyServo::calculateSpectrum()
- {
- if ( final > zero )
- {
- totalSpectrum = final - zero;
- }
- else if ( zero > final)
- {
- totalSpectrum = zero - final;
- }
- else
- {
- totalSpectrum = 0;
- }
- }
- //===============================================================
- void MyServo::deductionDegree()
- {
- if (totalSpectrum < nbStep)
- {
- nbStep = totalSpectrum;
- }
- if (totalSpectrum % nbStep > 0)
- {
- nbDegreeByStep = totalSpectrum / nbStep + 1;
- }
- else
- {
- nbDegreeByStep = totalSpectrum / nbStep;
- }
- }
- //===============================================================
- void MyServo::setZero(int theZero)
- {
- if (theZero > -1 && theZero < 181)
- {
- zero = theZero;
- }
- }
- //===============================================================
- void MyServo::setFinal(int theFinal)
- {
- if (theFinal > -1 && theFinal < 181)
- {
- final = theFinal;
- }
- }
- //===============================================================
- void MyServo::setSpectrum(int theZero,int theFinal)
- {
- if (theZero > -1 && theZero < 181)
- {
- zero = theZero;
- }
- if (theFinal > -1 && theFinal < 181)
- {
- final = theFinal;
- }
- updateProperty();
- }
- //===============================================================
- void MyServo::setNbStep(int numberOfStep)
- {
- nbStep = numberOfStep;
- }
- //===============================================================
- void MyServo::plus()
- {
- if (actualStep < nbStep)
- {
- actualStep ++;
- _write((actualStep * nbDegreeByStep) + zero);
- }
- }
- //===============================================================
- void MyServo::reachStep(int stepReached)
- {
- if (stepReached > -1 && stepReached < nbStep)
- {
- actualStep = stepReached;
- _write((actualStep * nbDegreeByStep) + zero);
- }
- }
- //===============================================================
- void MyServo::less()
- {
- if (actualStep > 0)
- {
- actualStep --;
- _write(actualStep * nbDegreeByStep + zero);
- }
- }
- //===============================================================
- void MyServo::low()
- {
- actualStep = 0;
- _write(zero);
- }
- //===============================================================
- void MyServo::high()
- {
- actualStep = nbStep;
- _write(final);
- }
- //===============================================================
- int MyServo::getZero()
- {
- return(zero);
- }
- //===============================================================
- int MyServo::getFinal()
- {
- return(final);
- }
- //===============================================================
- int MyServo::getNbStep()
- {
- return(nbStep);
- }
- //===============================================================
- int MyServo::getActualStep()
- {
- return (actualStep);
- }
- //===============================================================
- int MyServo::getPin()
- {
- return (myPin);
- }
- //===============================================================
- int MyServo::getSpectrum()
- {
- return (totalSpectrum);
- }
- //===============================================================
- int MyServo::getNbDegreeByStep()
- {
- return (nbDegreeByStep);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement