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->SetPinType(DIGITAL);
15 }
16 
17 void PortServo::begin(int inPin)
18 {
19  CHECKPIN(Arduino_to_GPIO_pin(inPin), this->GetPinType(), "PortServo::begin");
20  this->pin = inPin;
21 }
22 
23 void PortServo::beginByAccessory(int inStartingPosition)
24 {
25 #ifdef ACCESSORIES_DEBUG_MODE
26  Serial.print(F(" PortServo "));
27  Serial.print(this->pin);
28  Serial.print(F(" beginByAccessory() "));
29  Serial.print(F("Starting pos: "));
30  Serial.println(inStartingPosition, DEC);
31 #endif
32 
33  if (!this->servo.attached())
34  this->servo.attach(this->pin);
35  this->servo.write(inStartingPosition);
36  this->servo.detach();
37 }
38 
39 void PortServo::MovePosition(unsigned long inDuration, int inEndPosition)
40 {
41 #ifdef ACCESSORIES_DEBUG_MODE
42  Serial.print(F(" PortServo "));
43  Serial.print(this->pin);
44  Serial.print(F(" MovePosition() "));
45  if (inDuration != 0)
46  {
47  Serial.print(F("for "));
48  Serial.print(inDuration);
49  Serial.print(F("ms "));
50  }
51  Serial.print(F("to angle "));
52  Serial.println(inEndPosition);
53 #endif
54  if (!this->servo.attached())
55  this->servo.attach(this->pin);
56  this->servo.write(inEndPosition);
57 }
58 
60 {
61  if (!this->servo.attached())
62  this->servo.attach(this->pin);
63  return this->servo.read();
64 }
65 
66 #ifdef ACCESSORIES_PRINT_ACCESSORIES
68 {
69  Serial.print(F("[PortServo pin:"));
70  Serial.print(this->pin);
71  Serial.print(F("]"));
72 }
73 #endif
74 #endif
void printPort()
Definition: PortServo.cpp:67
int GetPosition()
Definition: PortServo.cpp:59
void MovePosition(unsigned long inDuration, int inEndPosition)
Definition: PortServo.cpp:39
Servo servo
Definition: PortServo.hpp:19
void beginByAccessory(int inStartingPosition)
Definition: PortServo.cpp:23
PIN_TYPE GetPinType() const
Definition: Port.hpp:74
virtual void begin()
Definition: Port.hpp:114
void SetPinType(PIN_TYPE inType)
Definition: Port.hpp:81
Definition: Port.hpp:61