Accessories
Arduino for motors and lights library.
PortServo.cpp
1 /*************************************************************
2 project: <Accessories>
3 author: <Thierry PARIS>
4 description: <Driver port for a servo on Arduino>
5 *************************************************************/
6 
7 #include "Accessories.h"
8 
9 #ifndef NO_SERVO
10 
12 {
13  this->pin = -1;
14  this->pinType = DIGITAL;
15 }
16 
17 void PortServo::begin(int inPin)
18 {
19  CHECKPIN(Arduino_to_GPIO_pin(inPin), this->pinType, "PortServo::begin");
20  this->pin = inPin;
21  this->servo.attach(this->pin);
22 }
23 
24 void PortServo::beginByAccessory(int inStartingPosition)
25 {
26 #ifdef ACCESSORIES_DEBUG_MODE
27  Serial.print(F(" PortServo "));
28  Serial.print(this->pin);
29  Serial.print(F(" beginByAccessory() "));
30  Serial.print(F("Starting pos: "));
31  Serial.println(inStartingPosition, DEC);
32 #endif
33 
34 #ifdef ACCESSORIES_DEBUG_MODE
35  if (!this->servo.attached())
36  Serial.println(F("Invalid servo beginByAccessory()."));
37 #endif
38 
39  this->servo.write(inStartingPosition);
40 }
41 
42 void PortServo::MovePosition(unsigned long inDuration, int inEndPosition)
43 {
44 #ifdef ACCESSORIES_DEBUG_MODE
45  Serial.print(F(" PortServo "));
46  Serial.print(this->pin);
47  Serial.print(F(" MovePosition() "));
48  if (inDuration != 0)
49  {
50  Serial.print(F("for "));
51  Serial.print(inDuration);
52  Serial.print(F("ms "));
53  }
54  Serial.print(F("to angle "));
55  Serial.println(inEndPosition);
56 #endif
57  this->servo.write(inEndPosition);
58 }
59 
61 {
62  return this->servo.read();
63 }
64 
65 #ifdef ACCESSORIES_PRINT_ACCESSORIES
66 void PortServo::printPort()
67 {
68  Serial.print(F("[PortServo pin:"));
69  Serial.print(this->pin);
70  Serial.print(F("]"));
71 }
72 #endif
73 #endif
int GetPosition()
Definition: PortServo.cpp:60
void MovePosition(unsigned long inDuration, int inEndPosition)
Definition: PortServo.cpp:42
void beginByAccessory(int inStartingPosition)
Definition: PortServo.cpp:24
Servo servo
Definition: PortServo.hpp:19
virtual void begin()
Definition: Port.hpp:99
PIN_TYPE pinType
Definition: Port.hpp:65
Definition: Port.hpp:61