Advertisement
thorium90

MyServo.cpp

Oct 9th, 2011
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.09 KB | None | 0 0
  1. /*
  2.    ___|  _)  | _)       _)                         ___|                    
  3.  \___ \   |  |  |   __|  |  |   |  __ `__ \       |       _ \    __|  __ \  
  4.        |  |  |  |  (     |  |   |  |   |   |      |      (   |  |     |   |
  5.  _____/  _| _| _| \___| _| \__,_| _|  _|  _|     \____| \___/  _|     .__/  
  6.                                                                      _|  
  7. Code source libre de droit. Si vous utilisez mon code, merci à vous d'en préciser la source.
  8.  
  9. Date : 09/10/2011
  10. Auteur : Silicium Corp
  11. */
  12.  
  13. #include "MyServo.h"
  14.  
  15. //===============================================================
  16. MyServo::MyServo(int nbStepInit)
  17. {
  18.     nbStep = nbStepInit;
  19.     init();
  20. }
  21. //===============================================================
  22. void MyServo::_attach(int pin)
  23. {
  24.     objectServo.attach(pin);
  25.     myPin = pin;
  26. }
  27. //===============================================================
  28. void MyServo::_detach()
  29. {
  30.     objectServo.detach();
  31. }
  32. //===============================================================
  33. void MyServo::_write(int posit)
  34. {
  35.     if (posit < zero)
  36.     {
  37.         posit = zero;
  38.     }
  39.     else if (posit > final)
  40.     {
  41.         posit = final;
  42.     }
  43.     objectServo.write(posit);
  44. }
  45. //===============================================================
  46. int MyServo::_read()
  47. {
  48.     return(objectServo.read());
  49. }
  50. //===============================================================
  51. boolean MyServo::_attached()
  52. {
  53.     return (objectServo.attached());
  54. }
  55. //===============================================================
  56. void MyServo::init()
  57. {
  58.     setZero(0);
  59.     setFinal(180);
  60.     updateProperty();
  61.     actualStep = 0;
  62. }
  63. //=================================================================
  64. void MyServo::updateProperty()
  65. {
  66.     calculateSpectrum();
  67.     deductionDegree();
  68. }
  69. //===============================================================
  70. void MyServo::calculateSpectrum()
  71. {
  72.     if ( final > zero )
  73.     {
  74.         totalSpectrum = final - zero;
  75.     }
  76.     else if ( zero > final)
  77.     {
  78.         totalSpectrum = zero - final;
  79.     }
  80.     else
  81.     {
  82.         totalSpectrum = 0;
  83.     }
  84. }
  85. //===============================================================
  86. void MyServo::deductionDegree()
  87. {
  88.     if (totalSpectrum < nbStep)
  89.     {
  90.         nbStep = totalSpectrum;
  91.     }
  92.     if (totalSpectrum % nbStep > 0)
  93.     {
  94.         nbDegreeByStep = totalSpectrum / nbStep + 1;
  95.     }
  96.     else
  97.     {
  98.         nbDegreeByStep = totalSpectrum / nbStep;
  99.     }
  100. }
  101. //===============================================================
  102. void MyServo::setZero(int theZero)
  103. {
  104.     if (theZero > -1 && theZero < 181)
  105.     {
  106.         zero = theZero;
  107.     }
  108. }
  109. //===============================================================
  110. void MyServo::setFinal(int theFinal)
  111. {
  112.     if (theFinal > -1 && theFinal < 181)
  113.     {
  114.         final = theFinal;
  115.     }
  116. }
  117. //===============================================================
  118. void MyServo::setSpectrum(int theZero,int theFinal)
  119. {
  120.     if (theZero > -1 && theZero < 181)
  121.     {
  122.         zero = theZero;
  123.     }
  124.     if (theFinal > -1 && theFinal < 181)
  125.     {
  126.         final = theFinal;
  127.     }
  128.     updateProperty();
  129. }
  130. //===============================================================
  131. void MyServo::setNbStep(int numberOfStep)
  132. {
  133.     nbStep = numberOfStep;
  134. }
  135. //===============================================================
  136. void MyServo::plus()
  137. {
  138.     if (actualStep < nbStep)
  139.     {
  140.         actualStep ++;
  141.         _write((actualStep * nbDegreeByStep) + zero);
  142.     }
  143. }
  144. //===============================================================
  145. void MyServo::reachStep(int stepReached)
  146. {
  147.     if (stepReached > -1 && stepReached < nbStep)
  148.     {
  149.         actualStep = stepReached;
  150.         _write((actualStep * nbDegreeByStep) + zero);
  151.     }
  152. }
  153. //===============================================================
  154. void MyServo::less()
  155. {
  156.     if (actualStep > 0)
  157.     {
  158.         actualStep --;
  159.         _write(actualStep * nbDegreeByStep + zero);
  160.     }
  161. }
  162. //===============================================================
  163. void MyServo::low()
  164. {
  165.     actualStep = 0;
  166.     _write(zero);
  167. }
  168. //===============================================================
  169. void MyServo::high()
  170. {
  171.     actualStep = nbStep;
  172.     _write(final);
  173. }
  174. //===============================================================
  175. int MyServo::getZero()
  176. {
  177.     return(zero);
  178. }
  179. //===============================================================
  180. int MyServo::getFinal()
  181. {
  182.     return(final);
  183. }
  184. //===============================================================
  185. int MyServo::getNbStep()
  186. {
  187.     return(nbStep);
  188. }
  189. //===============================================================
  190. int MyServo::getActualStep()
  191. {
  192.     return (actualStep);
  193. }
  194. //===============================================================
  195. int MyServo::getPin()
  196. {
  197.     return (myPin);
  198. }
  199. //===============================================================
  200. int MyServo::getSpectrum()
  201. {
  202.     return (totalSpectrum);
  203. }
  204. //===============================================================
  205. int MyServo::getNbDegreeByStep()
  206. {
  207.     return (nbDegreeByStep);
  208. }
  209.  
  210.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement