1
1
#include " Adafruit_PWMServoDriverGroup.h"
2
2
3
3
/* !
4
- * @brief Instantiates new PCA9685 PWM driver chips with user defined
4
+ * @brief Instantiates new PCA9685 PWM driver chips with user defined
5
5
* I2C addresses on a TwoWire interface
6
6
* @param nDrivers Number of PWM driver chips to instantiate
7
- * @param nServosEach Number of servos to allocate on each driver
8
- * @param addr Array of 7-bit I2C addresses to locate the chips, default is 0x40 through 0x44
7
+ * @param nServosEach Number of servos to allocate on each driver
8
+ * @param addr Array of 7-bit I2C addresses to locate the chips, default is
9
+ * 0x40 through 0x44
9
10
*/
10
- Adafruit_PWMServoDriverGroup::Adafruit_PWMServoDriverGroup (const uint8_t nDrivers, const uint8_t nServosEach, const uint8_t *addr) {
11
+ Adafruit_PWMServoDriverGroup::Adafruit_PWMServoDriverGroup (
12
+ const uint8_t nDrivers, const uint8_t nServosEach, const uint8_t *addr) {
11
13
_nDrivers = nDrivers;
12
14
_nServosEach = nServosEach;
13
15
14
- _drivers = (Adafruit_PWMServoDriver **) malloc (nDrivers * sizeof (Adafruit_PWMServoDriver *));
16
+ _drivers = (Adafruit_PWMServoDriver **)malloc (
17
+ nDrivers * sizeof (Adafruit_PWMServoDriver *));
15
18
16
- for (uint8_t i= 0 ; i < _nDrivers; i++) {
19
+ for (uint8_t i = 0 ; i < _nDrivers; i++) {
17
20
_drivers[i] = new Adafruit_PWMServoDriver (addr[i], Wire);
18
21
}
19
22
}
@@ -23,49 +26,54 @@ Adafruit_PWMServoDriverGroup::Adafruit_PWMServoDriverGroup(const uint8_t nDriver
23
26
* TwoWire interface
24
27
* @param nDrivers Number of PWM driver chips to instantiate
25
28
* @param nServosEach Number of servos to allocate on each driver
26
- * @param addr Array of 7-bit I2C addresses to locate the chips, default is 0x40 thorugh 0x44
27
- * @param i2c A reference to a 'TwoWire' object that we'll use to communicate with
29
+ * @param addr Array of 7-bit I2C addresses to locate the chips, default is
30
+ * 0x40 thorugh 0x44
31
+ * @param i2c A reference to a 'TwoWire' object that we'll use to communicate
28
32
*/
29
- Adafruit_PWMServoDriverGroup::Adafruit_PWMServoDriverGroup (const uint8_t nDrivers, const uint8_t nServosEach, const uint8_t *addr, TwoWire &i2c)
30
- {
33
+ Adafruit_PWMServoDriverGroup::Adafruit_PWMServoDriverGroup (
34
+ const uint8_t nDrivers, const uint8_t nServosEach, const uint8_t *addr,
35
+ TwoWire &i2c) {
31
36
_nDrivers = nDrivers;
32
37
_nServosEach = nServosEach;
33
38
34
- _drivers = (Adafruit_PWMServoDriver **) malloc (nDrivers * sizeof (Adafruit_PWMServoDriver *));
39
+ _drivers = (Adafruit_PWMServoDriver **)malloc (
40
+ nDrivers * sizeof (Adafruit_PWMServoDriver *));
35
41
36
- for (uint8_t i= 0 ; i < _nDrivers; i++) {
42
+ for (uint8_t i = 0 ; i < _nDrivers; i++) {
37
43
_drivers[i] = new Adafruit_PWMServoDriver (addr[i], i2c);
38
44
}
39
45
}
40
46
41
47
/* @brief Gets the number of PCA9685 PWM driver chips connected to this class
42
- */
43
- uint8_t Adafruit_PWMServoDriverGroup::getNumDrivers () {
44
- return _nDrivers;
48
+ */
49
+ uint8_t Adafruit_PWMServoDriverGroup::getNumDrivers () {
50
+ return _nDrivers;
45
51
}
46
52
47
53
/* @brief Gets the number of servos associated with each PCA9685 PWM chip
48
- */
54
+ */
49
55
uint8_t Adafruit_PWMServoDriverGroup::getNumServosEach () {
50
56
return _nServosEach;
51
57
}
52
58
53
59
/* @brief Gets the total number of servos associated with this class
54
- */
60
+ */
55
61
uint8_t Adafruit_PWMServoDriverGroup::getNumServos () {
56
62
return _nDrivers * _nServosEach;
57
63
}
58
64
59
65
/* !
60
- * @brief Gets the Adafruit_PWMServoDriver associated with a given servo and
66
+ * @brief Gets the Adafruit_PWMServoDriver associated with a given servo and
61
67
* the local servo number on that device
62
68
* @param num Number of servo in master list
63
69
* @param localId returns the number of the servo as known to the PCA9685 chip
64
70
* @return The Adafruit_PWMServoDriver associated with the requested servo
65
71
*/
66
- Adafruit_PWMServoDriver* Adafruit_PWMServoDriverGroup::getDriver (uint8_t num, uint8_t & localId){
72
+ Adafruit_PWMServoDriver *
73
+ Adafruit_PWMServoDriverGroup::getDriver (uint8_t num, uint8_t &localId) {
67
74
uint8_t driverId = 0 ;
68
- if (num > _nServosEach - 1 ) driverId = num/_nServosEach;
75
+ if (num > _nServosEach - 1 )
76
+ driverId = num / _nServosEach;
69
77
localId = num - (driverId * _nServosEach);
70
78
71
79
return _drivers[driverId];
@@ -79,7 +87,8 @@ Adafruit_PWMServoDriver* Adafruit_PWMServoDriverGroup::getDriver(uint8_t num, ui
79
87
*/
80
88
bool Adafruit_PWMServoDriverGroup::begin (uint8_t prescale) {
81
89
bool status = true ;
82
- for (int i = 0 ; i < _nDrivers; i++) status &= _drivers[i]->begin (prescale);
90
+ for (int i = 0 ; i < _nDrivers; i++)
91
+ status &= _drivers[i]->begin (prescale);
83
92
84
93
return status;
85
94
}
@@ -88,38 +97,43 @@ bool Adafruit_PWMServoDriverGroup::begin(uint8_t prescale) {
88
97
* @brief Sends a reset command to the PCA9685 chips over I2C
89
98
*/
90
99
void Adafruit_PWMServoDriverGroup::reset () {
91
- for (int i = 0 ; i < _nDrivers; i++) _drivers[i]->reset ();
100
+ for (int i = 0 ; i < _nDrivers; i++)
101
+ _drivers[i]->reset ();
92
102
}
93
103
94
104
/* !
95
105
* @brief Puts boards into sleep mode
96
106
*/
97
- void Adafruit_PWMServoDriverGroup::sleep (){
98
- for (int i = 0 ; i < _nDrivers; i++) _drivers[i]->sleep ();
107
+ void Adafruit_PWMServoDriverGroup::sleep () {
108
+ for (int i = 0 ; i < _nDrivers; i++)
109
+ _drivers[i]->sleep ();
99
110
}
100
111
101
112
/* !
102
113
* @brief Wakes boards from sleep
103
114
*/
104
- void Adafruit_PWMServoDriverGroup::wakeup (){
105
- for (int i = 0 ; i < _nDrivers; i++) _drivers[i]->wakeup ();
115
+ void Adafruit_PWMServoDriverGroup::wakeup () {
116
+ for (int i = 0 ; i < _nDrivers; i++)
117
+ _drivers[i]->wakeup ();
106
118
}
107
119
108
120
/* !
109
121
* @brief Sets EXTCLK pin to use the external clock
110
122
* @param prescale
111
123
* Configures the prescale value to be used by the external clock
112
124
*/
113
- void Adafruit_PWMServoDriverGroup::setExtClk (uint8_t prescale){
114
- for (int i = 0 ; i < _nDrivers; i++) _drivers[i]->setExtClk (prescale);
125
+ void Adafruit_PWMServoDriverGroup::setExtClk (uint8_t prescale) {
126
+ for (int i = 0 ; i < _nDrivers; i++)
127
+ _drivers[i]->setExtClk (prescale);
115
128
}
116
129
117
130
/* !
118
131
* @brief Sets the PWM frequency for all chips, up to ~1.6 KHz
119
132
* @param freq Floating point frequency that we will attempt to match
120
133
*/
121
- void Adafruit_PWMServoDriverGroup::setPWMFreq (float freq){
122
- for (int i = 0 ; i < _nDrivers; i++) _drivers[i]->setPWMFreq (freq);
134
+ void Adafruit_PWMServoDriverGroup::setPWMFreq (float freq) {
135
+ for (int i = 0 ; i < _nDrivers; i++)
136
+ _drivers[i]->setPWMFreq (freq);
123
137
}
124
138
125
139
/* !
@@ -129,30 +143,34 @@ void Adafruit_PWMServoDriverGroup::setPWMFreq(float freq){
129
143
* only be driven in open drain mode.
130
144
* @param totempole Totempole if true, open drain if false.
131
145
*/
132
- void Adafruit_PWMServoDriverGroup::setOutputMode (bool totempole){
133
- for (int i = 0 ; i < _nDrivers; i++) _drivers[i]->setOutputMode (totempole);
146
+ void Adafruit_PWMServoDriverGroup::setOutputMode (bool totempole) {
147
+ for (int i = 0 ; i < _nDrivers; i++)
148
+ _drivers[i]->setOutputMode (totempole);
134
149
}
135
150
136
151
/* !
137
152
* @brief Gets the PWM output of one of the PCA9685 pins
138
- * @param num One of the PWM output pins, from 0 to (nDrivers * nServosEach - 1)
153
+ * @param num One of the PWM output pins, from 0 to (nDrivers * nServosEach -
154
+ * 1)
139
155
* @param off If true, returns PWM OFF value, otherwise PWM ON
140
156
* @return requested PWM output value
141
157
*/
142
- uint16_t Adafruit_PWMServoDriverGroup::getPWM (uint8_t num, bool off){
158
+ uint16_t Adafruit_PWMServoDriverGroup::getPWM (uint8_t num, bool off) {
143
159
uint8_t localId = 0 ;
144
160
auto driver = getDriver (num, localId);
145
161
return driver->getPWM (localId, off);
146
162
}
147
163
148
164
/* !
149
165
* @brief Sets the PWM output of one of the PCA9685 pins
150
- * @param num One of the PWM output pins, from 0 to (nDrivers * nServosEach - 1)
166
+ * @param num One of the PWM output pins, from 0 to (nDrivers * nServosEach -
167
+ * 1)
151
168
* @param on At what point in the 4096-part cycle to turn the PWM output ON
152
169
* @param off At what point in the 4096-part cycle to turn the PWM output OFF
153
170
* @return 0 if successful, otherwise 1
154
171
*/
155
- uint8_t Adafruit_PWMServoDriverGroup::setPWM (uint8_t num, uint16_t on, uint16_t off){
172
+ uint8_t Adafruit_PWMServoDriverGroup::setPWM (uint8_t num, uint16_t on,
173
+ uint16_t off) {
156
174
uint8_t localId = 0 ;
157
175
auto driver = getDriver (num, localId);
158
176
return driver->setPWM (localId, on, off);
@@ -163,12 +181,14 @@ uint8_t Adafruit_PWMServoDriverGroup::setPWM(uint8_t num, uint16_t on, uint16_t
163
181
* on/off tick placement and properly handles a zero value as completely off and
164
182
* 4095 as completely on. Optional invert parameter supports inverting the
165
183
* pulse for sinking to ground.
166
- * @param num One of the PWM output pins, from 0 to (nDrivers * nServosEach - 1)
184
+ * @param num One of the PWM output pins, from 0 to (nDrivers * nServosEach -
185
+ * 1)
167
186
* @param val The number of ticks out of 4096 to be active, should be a value
168
187
* from 0 to 4095 inclusive.
169
188
* @param invert If true, inverts the output, defaults to 'false'
170
189
*/
171
- void Adafruit_PWMServoDriverGroup::setPin (uint8_t num, uint16_t val, bool invert){
190
+ void Adafruit_PWMServoDriverGroup::setPin (uint8_t num, uint16_t val,
191
+ bool invert) {
172
192
uint8_t localId = 0 ;
173
193
auto driver = getDriver (num, localId);
174
194
driver->setPin (localId, val, invert);
@@ -178,17 +198,19 @@ void Adafruit_PWMServoDriverGroup::setPin(uint8_t num, uint16_t val, bool invert
178
198
* @brief Reads set Prescale from PCA9685
179
199
* @return prescale value
180
200
*/
181
- uint8_t Adafruit_PWMServoDriverGroup::readPrescale (){
201
+ uint8_t Adafruit_PWMServoDriverGroup::readPrescale () {
182
202
return _drivers[0 ]->readPrescale ();
183
203
}
184
204
185
205
/* !
186
206
* @brief Sets the PWM output of one of the PCA9685 pins based on the input
187
207
* microseconds, output is not precise
188
- * @param num One of the PWM output pins, from 0 to (nDrivers * nServosEach - 1)
208
+ * @param num One of the PWM output pins, from 0 to (nDrivers * nServosEach -
209
+ * 1)
189
210
* @param Microseconds The number of Microseconds to turn the PWM output ON
190
211
*/
191
- void Adafruit_PWMServoDriverGroup::writeMicroseconds (uint8_t num, uint16_t microseconds){
212
+ void Adafruit_PWMServoDriverGroup::writeMicroseconds (uint8_t num,
213
+ uint16_t microseconds) {
192
214
uint8_t localId = 0 ;
193
215
auto driver = getDriver (num, localId);
194
216
@@ -201,7 +223,7 @@ void Adafruit_PWMServoDriverGroup::writeMicroseconds(uint8_t num, uint16_t micro
201
223
* @returns The frequency the PCA9685 thinks it is running at (it cannot
202
224
* introspect)
203
225
*/
204
- uint32_t Adafruit_PWMServoDriverGroup::getOscillatorFrequency (uint8_t id){
226
+ uint32_t Adafruit_PWMServoDriverGroup::getOscillatorFrequency (uint8_t id) {
205
227
return _drivers[id]->getOscillatorFrequency ();
206
228
}
207
229
@@ -210,11 +232,12 @@ uint32_t Adafruit_PWMServoDriverGroup::getOscillatorFrequency(uint8_t id){
210
232
* calculations
211
233
* @param freq The frequency the PCA9685 should use for frequency calculations
212
234
*/
213
- void Adafruit_PWMServoDriverGroup::setOscillatorFrequency (uint32_t freq){
214
- for (int i = 0 ; i < _nDrivers; i++) _drivers[i]->setOscillatorFrequency (freq);
235
+ void Adafruit_PWMServoDriverGroup::setOscillatorFrequency (uint32_t freq) {
236
+ for (int i = 0 ; i < _nDrivers; i++)
237
+ _drivers[i]->setOscillatorFrequency (freq);
215
238
}
216
239
217
- void Adafruit_PWMServoDriverGroup::setOscillatorFrequency (uint8_t id, uint32_t freq){
218
- _drivers[id]->setOscillatorFrequency (freq);
240
+ void Adafruit_PWMServoDriverGroup::setOscillatorFrequency (uint8_t id,
241
+ uint32_t freq) {
242
+ _drivers[id]->setOscillatorFrequency (freq);
219
243
}
220
-
0 commit comments