1+ /* *****************************************************************************
2+ * The MIT License
3+ *
4+ * Copyright (c) 2010, LeafLabs, LLC.
5+ *
6+ * Permission is hereby granted, free of charge, to any person
7+ * obtaining a copy of this software and associated documentation
8+ * files (the "Software"), to deal in the Software without
9+ * restriction, including without limitation the rights to use, copy,
10+ * modify, merge, publish, distribute, sublicense, and/or sell copies
11+ * of the Software, and to permit persons to whom the Software is
12+ * furnished to do so, subject to the following conditions:
13+ *
14+ * The above copyright notice and this permission notice shall be
15+ * included in all copies or substantial portions of the Software.
16+ *
17+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+ * SOFTWARE.
25+ *****************************************************************************/
26+
27+ /*
28+ * @copyright Copyright (c) 2025-2026 Infineon Technologies AG
29+ */
30+
31+ #ifndef __SERVO_TIMERS_H__
32+ #define __SERVO_TIMERS_H__
33+
34+ #include < Arduino.h>
35+
36+ #define MAX_PWM_SERVOS 28
37+ #define ALLOWED_PINS {0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , 26 , 27 , 28 , 29 , 30 }
38+
39+
40+ #define MIN_ANGLE 0 // the minimal angle in degree
41+ #define MAX_ANGLE 180 // the maximal angle in degree
42+ #define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo in microseconds
43+ #define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo in microseconds
44+
45+ #define MIN_PULSE_CHECK 500 // border with below = angle and above = pulse width
46+ #define REFRESH_FREQUENCY 50u // the refresh frequency on analog pins
47+ #define REFRESH_TIME 20.0 // the PWM refresh frequency for the servo motor
48+ #define DUTYCYCLE_STEPS 65536.0 / REFRESH_TIME // the number of duty cycle steps during one refresh period
49+ #define RESOLUTION 16 // the resolution of the adc during analog write
50+
51+ #define INVALID_SERVO 255 // flag indicating an invalid servo index
52+
53+ /* * Class for interfacing with RC servomotors. */
54+ class Servo
55+ {
56+ public:
57+ /* *
58+ * @brief Construct a new Servo instance.
59+ *
60+ * The new instance will not be attached to any pin, but only PWM capable pins will run.
61+ * see pin list above.
62+ */
63+ Servo ();
64+
65+ /* *
66+ * @brief Associate this instance with a servomotor whose input is
67+ * connected to pin.
68+ *
69+ * If this instance is already attached to a pin, it will be
70+ * detached before being attached to the new pin.
71+ * If the pin is not allowed for running PWM or the max number of
72+ * PWM channels on the PSOC6 board is reached it will return
73+ * with an INVALID_SERVO, otherwise with the servoIndex number.
74+ *
75+ * @param pin Pin connected to the servo pulse wave input. This
76+ * pin must be capable of PWM output.
77+ *
78+ * @param min If this value is below MIN_PULSE_CHECK it will be associated
79+ * with an angle in degree. Otherwise it will be the minimum
80+ * pulse width.
81+ * min as an angle must be between MIN_ANGLE < angle < MAX_ANGLE
82+ * with default as MIN_ANGLE
83+ * min as a pulse width must be between MIN_PULSE_WIDTH < pwm < MAX_PULSE_WIDTH
84+ * with a default as MIN_PULSE_WIDTH
85+ *
86+ * @param max If this value is below MIN_PULSE_CHECK it will be associated
87+ * with an angle in degree. Otherwise it will be the maximum
88+ * pulse width.
89+ * max as an angle must be between MIN_ANGLE < angle < MAX_ANGLE
90+ * with default as MAX_ANGLE
91+ * max as a pulse width must be between MIN_PULSE_WIDTH < pwm < MAX_PULSE_WIDTH
92+ * with a default as MAX_PULSE_WIDTH
93+ *
94+ * @return servoIndex number or INVALID_SERVO = 255 in case of an error
95+ */
96+ uint8_t attach (uint8_t pin, uint16_t min = MIN_ANGLE, uint16_t max = MAX_ANGLE);
97+
98+
99+ /* *
100+ * @brief Stop driving the servo pulse train.
101+ *
102+ * If not currently attached to a motor, this function has no effect.
103+ *
104+ * @return true if this call did anything, false otherwise.
105+ */
106+ void detach ();
107+
108+ /* *
109+ * @brief Set the servomotor target angle by recalculating the duty cycle
110+ * for PSOC6 PWM settings.
111+ *
112+ * @param value Target angle, in degrees. If the target angle is
113+ * outside the range specified at attach(), it
114+ * will be clamped to lie in that range.
115+ *
116+ * @see Servo::attach()
117+ */
118+ void write (int value);
119+
120+ /* *
121+ * @brief Set the pulse width, in microseconds by recalculating it for the
122+ * PSOC6 PWM settings. It also calculates the angle from the pwm value.
123+ *
124+ * @param value Pulse width to send to the servomotor, in
125+ * microseconds. If outside of the range
126+ * specified at attach() time, it is clamped to
127+ * lie in that range.
128+ *
129+ * @see Servo::attach()
130+ */
131+ void writeMicroseconds (int value);
132+
133+ /* *
134+ * returns the current value in degree as an angle between 0 and 189 degrees
135+ *
136+ * @see Servo::attach()
137+ */
138+ int read () const { return uint16_t (this ->_deg ); }
139+
140+ /* *
141+ * returns the current pwm value in microseconds.
142+ *
143+ * @see Servo::attach()
144+ */
145+ int readMicroseconds () const { return uint16_t (this ->_pwm ); }
146+
147+ /* *
148+ * @brief Check if this instance is attached to a servo.
149+ * @return true if this instance is attached to a servo, false otherwise.
150+ * @see Servo::attachedPin()
151+ */
152+ bool attached () const { return this ->_isActive ; }
153+
154+ private:
155+ uint16_t _minPW; // the initial minPulseWidth, if not set than MIN_PULSE_WIDTH
156+ uint16_t _maxPW; // the initial maxPulseWidth, if not set than MAX_PULSE_WIDTH
157+ int16_t _minAngle; // the initial minAngle, if not set than MIN_ANGLE
158+ int16_t _maxAngle; // the initial maxAngle, if not set than MAX_ANGLE
159+ int16_t _pin; // attached arduino pin number
160+ double _deg; // actual angle in degree
161+ double _pwm; // actual pwm signal in microseconds
162+ uint8_t _isActive; // true if this pin is active, otherwise false
163+
164+ uint8_t servoIndex; // the actual number of Servos attached to this library
165+
166+
167+ };
168+
169+ #endif
0 commit comments