5b500be5df0e2b28a8d4e0a11e5eeeee0ca40208
[contrib/upm.git] / src / servo / servo.h
1 /*
2  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25
26 #include <string>
27 #include <maa/pwm.h>
28
29 namespace upm {
30
31 #define MIN_PULSE_WIDTH      600
32 #define MAX_PULSE_WIDTH      2500
33 #define MAX_PERIOD           7968
34
35 #define HIGH                  1
36 #define LOW                   0
37
38 /**
39  * @brief Base class for other servo components
40  *
41  * PMOD pins for MAX44000PMB1 board
42  *
43  */
44 class Servo {
45     public:
46         /**
47          * Instanciates a servo object
48          *
49          * @param pin servo pin number
50          */
51         Servo (int pin);
52
53         /**
54          * Servo object destructor.
55          */
56         ~Servo();
57
58         /**
59          * Set the of the servo engine.
60          *
61          * X = between (MIN_PULSE_WIDTH , MAX_PULSE_WIDTH)
62          *
63          * X usec
64          * _______
65          *        |_______________________________________
66          *                      20000 usec
67          *
68          * Max period can be only 7968750(nses) which is ~8(msec)
69          * so the servo will not work as expected.
70          *
71          * @param angle number between 0 and 180
72          */
73         maa_result_t setAngle (int angle);
74
75         /**
76          * Return name of the component
77          */
78         std::string name()
79         {
80             return m_name;
81         }
82     protected:
83         int calcPulseTraveling (int value);
84
85         std::string         m_name;
86         int                 m_servoPin;
87         float               m_maxAngle;
88         maa_pwm_context     m_pwmServoContext;
89         // maa_gpio_context    m_servoPinCtx;
90 };
91
92 }