1+ /*
2+
3+ Copyright (c) 2015 Arduino LLC. All rights reserved.
4+ Copyright (c) 2019 SparkFun Electronics
5+
6+ Permission is hereby granted, free of charge, to any person obtaining a copy
7+ of this software and associated documentation files (the "Software"), to deal
8+ in the Software without restriction, including without limitation the rights
9+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+ copies of the Software, and to permit persons to whom the Software is
11+ furnished to do so, subject to the following conditions:
12+
13+ The above copyright notice and this permission notice shall be included in all
14+ copies or substantial portions of the Software.
15+
16+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+ SOFTWARE.
23+ */
24+
25+ #include " Servo.h"
26+ #include " Arduino.h"
27+
28+ // Constructor
29+ Servo::Servo ()
30+ {
31+ }
32+
33+ // Assign global var
34+ void Servo::attach (uint8_t pinNumber)
35+ {
36+ _servoPinNumber = pinNumber;
37+ _servoPadNumber = ap3_gpio_pin2pad (pinNumber);
38+ }
39+
40+ void Servo::write (uint8_t servoPosition)
41+ {
42+ _servoPosition = servoPosition;
43+ if (_servoPosition > 180 )
44+ _servoPosition = 180 ; // Bounds check
45+
46+ uint16_t newServoPosition = map (servoPosition, 0 , 180 , 0 , 1023 );
47+
48+ servoWrite (_servoPadNumber, newServoPosition); // This and the above write should both produce 1.5 ms wide pulses, though using different resolutions
49+ }
50+
51+ void Servo::writeMicroseconds (uint8_t servoPosition)
52+ {
53+
54+ servoWrite (_servoPadNumber, newServoPosition); // This and the above write should both produce 1.5 ms wide pulses, though using different resolutions
55+ }
56+
57+ void Servo::detach (void )
58+ {
59+ _servoPinNumber = NULL ;
60+ _servoPadNumber = NULL ;
61+ pinMode (_servoPinNumber, INPUT); // Will stop PWM output
62+ }
63+
64+ uint8_t Servo::read (void )
65+ {
66+ return (_servoPosition);
67+ }
68+
69+ bool Servo::attached (uint8_t pinNumber)
70+ {
71+ if (_servoPinNumber == pinNumber)
72+ return (true );
73+ return (false );
74+ }
0 commit comments