File tree Expand file tree Collapse file tree 3 files changed +39
-0
lines changed Expand file tree Collapse file tree 3 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -361,6 +361,43 @@ void Adafruit_PWMServoDriver::setOscillatorFrequency(uint32_t freq) {
361361 _oscillator_freq = freq;
362362}
363363
364+ /* !
365+ * @brief Sets the PWM output for multiple channels
366+ * @param values Array of PWM values (0-4095) for each channel
367+ * @param length Length of the array, from 1 to 16
368+ * @return 0 if successful, otherwise 1
369+ */
370+ uint8_t Adafruit_PWMServoDriver::setMultiplePWM (uint16_t *values,
371+ uint8_t length) {
372+ if (length > 16 )
373+ length = 16 ;
374+ uint8_t totalBytes = 1 + 4 * length;
375+
376+ uint8_t buffer[totalBytes];
377+ buffer[0 ] = PCA9685_LED0_ON_L;
378+ uint8_t *b = buffer + 1 ;
379+
380+ for (uint8_t i = 0 ; i < length; i++, b += 4 ) {
381+ uint16_t val = values[i] > 4095 ? 4095 : values[i];
382+ b[0 ] = 0 ;
383+ if (val == 0 ) {
384+ b[1 ] = 0 ;
385+ b[2 ] = 0 ;
386+ b[3 ] = MODE1_SLEEP;
387+ } else if (val == 4095 ) {
388+ b[1 ] = MODE1_SLEEP;
389+ b[2 ] = 0 ;
390+ b[3 ] = 0 ;
391+ } else {
392+ b[1 ] = 0 ;
393+ b[2 ] = val & 0xFF ;
394+ b[3 ] = val >> 8 ;
395+ }
396+ }
397+
398+ return i2c_dev->write (buffer, totalBytes) ? 0 : 1 ;
399+ }
400+
364401/* ****************** Low level I2C interface */
365402uint8_t Adafruit_PWMServoDriver::read8 (uint8_t addr) {
366403 uint8_t buffer[1 ] = {addr};
Original file line number Diff line number Diff line change @@ -86,6 +86,7 @@ class Adafruit_PWMServoDriver {
8686 void setOutputMode (bool totempole);
8787 uint16_t getPWM (uint8_t num, bool off = false );
8888 uint8_t setPWM (uint8_t num, uint16_t on, uint16_t off);
89+ uint8_t setMultiplePWM (uint16_t *values, uint8_t length);
8990 void setPin (uint8_t num, uint16_t val, bool invert = false );
9091 uint8_t readPrescale (void );
9192 void writeMicroseconds (uint8_t num, uint16_t Microseconds);
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ setPWMFreq KEYWORD2
1717setOutputMode KEYWORD2
1818getPWM KEYWORD2
1919setPWM KEYWORD2
20+ setMultiplePWM KEYWORD2
2021setPin KEYWORD2
2122readPrescale KEYWORD2
2223writeMicroseconds KEYWORD2
You can’t perform that action at this time.
0 commit comments