forked from PaulStoffregen/PWMServo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPWMServo.cpp
75 lines (60 loc) · 2.03 KB
/
PWMServo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "Arduino.h"
#include "PWMServo.h"
/*
PWMServo.cpp - Hardware Servo Timer Library
http://arduiniana.org/libraries/pwmservo/
Author: Jim Studt, [email protected]
Copyright (c) 2007 David A. Mellis. All right reserved.
renamed to PWMServo by Mikal Hart
ported to other chips by Paul Stoffregen
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define NO_ANGLE (0xff)
#if defined(__arm__) && defined(TEENSYDUINO)
uint32_t PWMServo::attachedpins[(NUM_DIGITAL_PINS+31)/32]; // 1 bit per digital pin
PWMServo::PWMServo() : pin(255) {}
uint8_t PWMServo::attach(int pinArg, uint16_t min, uint16_t max)
{
if (pinArg < 0 || pinArg >= NUM_DIGITAL_PINS) return 0;
if (!digitalPinHasPWM(pinArg)) return 0;
pin = pinArg;
min_us = min;
max_us = max;
analogWriteFrequency(pin, 300);
digitalWrite(pin, LOW);
pinMode(pin, OUTPUT);
attachedpins[pin >> 5] |= (1 << (pin & 31));
return 1;
}
void PWMServo::writeMicroseconds(float us)
{
if (us<min_us) { us = min_us; }
if (us>max_us) { us = max_us; }
float duty = us/(1e6/300.0)*65535;
#if TEENSYDUINO >= 137
noInterrupts();
uint32_t oldres = analogWriteResolution(16);
analogWrite(pin, duty);
// restore resolution
analogWriteResolution(oldres);
interrupts();
#endif
}
/*
uint8_t PWMServo::attached()
{
if (pin >= NUM_DIGITAL_PINS) return 0;
return (attachedpins[pin >> 5] & (1 << (pin & 31))) ? 1 : 0;
}
*/
#endif