ESP8266 Treppenlichtsteuerung mit OTA zum Firmware Upload
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PCA9685.h 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* Arduino Library for the PCA9685 16-Channel PWM Driver Module.
  2. Copyright (c) 2016 NachtRaveVL <nachtravevl@gmail.com>
  3. Copyright (C) 2012 Kasper Skårhøj <kasperskaarhoj@gmail.com>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. Created by Kasper Skårhøj, August 3rd, 2012.
  15. Forked by Vitska, June 18th, 2016.
  16. Forked by NachtRaveVL, July 29th, 2016.
  17. PCA9685-Arduino - Version 1.2.9
  18. */
  19. #ifndef PCA9685_H
  20. #define PCA9685_H
  21. // Library Setup
  22. // Uncomment this define to enable use of the software i2c library (min 4MHz+ processor required).
  23. //#define PCA9685_ENABLE_SOFTWARE_I2C 1 // http://playground.arduino.cc/Main/SoftwareI2CLibrary
  24. // Uncomment this define if wanting to exclude extended functionality from compilation.
  25. //#define PCA9685_EXCLUDE_EXT_FUNC 1
  26. // Uncomment this define if wanting to exclude ServoEvaluator assistant from compilation.
  27. //#define PCA9685_EXCLUDE_SERVO_EVAL 1
  28. // Uncomment this define to enable debug output.
  29. //#define PCA9685_ENABLE_DEBUG_OUTPUT 1
  30. // Servo Control Note
  31. // Many 180 degree controlled digital servos run on a 20ms pulse width (50Hz update
  32. // frequency) based duty cycle, and do not utilize the entire pulse width for their
  33. // -90/+90 degree control. Typically, 2.5% of the 20ms pulse width (0.5ms) is considered
  34. // -90 degrees, and 12.5% of the 20ms pulse width (2.5ms) is considered +90 degrees. This
  35. // roughly translates to raw PCA9685 PWM values of 102 and 512 (out of the 4096 value
  36. // range) for -90 to +90 degree control, but may need to be adjusted to fit your specific
  37. // servo (e.g. some I've tested run ~130 to ~525 for their -90/+90 degree control). Also
  38. // be aware that driving some servos past their -90/+90 degrees of movement can cause a
  39. // little plastic limiter pin to break off and get stuck inside of the gearing, which
  40. // could potentially cause the servo to become jammed. See the PCA9685_ServoEvaluator
  41. // class to assist with calculating PWM values from Servo angle values.
  42. #if defined(ARDUINO) && ARDUINO >= 100
  43. #include <Arduino.h>
  44. #else
  45. #include <WProgram.h>
  46. #endif
  47. #ifndef PCA9685_ENABLE_SOFTWARE_I2C
  48. #include <Wire.h>
  49. #endif
  50. #define _BUFFER_LENGTH 32
  51. #define PCA9685_MODE_INVRT (byte)0x10 // Inverts polarity of channel output signal
  52. #define PCA9685_MODE_OUTPUT_ONACK (byte)0x08 // Channel update happens upon ACK (post-set) rather than on STOP (endTransmission)
  53. #define PCA9685_MODE_OUTPUT_TPOLE (byte)0x04 // Use a totem-pole (push-pull) style output, typical for boards using this chipset
  54. #define PCA9685_MODE_OUTNE_HIGHZ (byte)0x02 // For active low output enable, sets channel output to high-impedance state
  55. #define PCA9685_MODE_OUTNE_LOW (byte)0x01 // Similarly, sets channel output to high if in totem-pole mode, otherwise high-impedance state
  56. #define PCA9685_MIN_CHANNEL 0
  57. #define PCA9685_MAX_CHANNEL 15
  58. #define PCA9685_CHANNEL_COUNT 16
  59. typedef enum {
  60. PCA9685_PhaseBalancer_None = -1, // Disables phase balancing, all high phase areas start at begining of cycle
  61. PCA9685_PhaseBalancer_Linear = 0, // Balances all outputs linearly, 256 steps away from previous output
  62. PCA9685_PhaseBalancer_Weaved, // Balances first few outputs better, steps away from previous shorten towards last output
  63. PCA9685_PhaseBalancer_Count
  64. } PCA9685_PhaseBalancer;
  65. class PCA9685 {
  66. public:
  67. #ifndef PCA9685_ENABLE_SOFTWARE_I2C
  68. // May use a different Wire instance than Wire. Some chipsets, such as Due/Zero/etc.,
  69. // have a Wire1 class instance that uses the SDA1/SCL1 lines instead.
  70. // Supported i2c baud rates are 100kHz, 400kHz, and 1000kHz.
  71. PCA9685(TwoWire& i2cWire = Wire, PCA9685_PhaseBalancer phaseBalancer = PCA9685_PhaseBalancer_Linear);
  72. #else
  73. // Minimum supported i2c baud rate is 100kHz, which means minimum supported processor
  74. // speed is 4MHz+ while running i2c standard mode. For 400kHz i2c baud rate, minimum
  75. // supported processor speed is 16MHz+ while running i2c fast mode.
  76. PCA9685(PCA9685_PhaseBalancer phaseBalancer = PCA9685_PhaseBalancer_Linear);
  77. #endif
  78. // Should be called only once in setup(), before any init()'s, but after Wire.begin().
  79. // Only should be called once on any Wire instance to do a software reset, which
  80. // will affect all devices on that line. This helps when you're constantly rebuilding
  81. // and reuploading to ensure all the devices on that line are reset properly.
  82. void resetDevices();
  83. // Called in setup(). The i2c address here is the value of the A0, A1, A2, A3, A4 and
  84. // A5 pins ONLY, as the class takes care of its internal base address. i2cAddress
  85. // should be a value between 0 and 61, since only 62 boards can be addressed.
  86. void init(byte i2cAddress = 0, byte mode = PCA9685_MODE_OUTPUT_ONACK | PCA9685_MODE_OUTPUT_TPOLE);
  87. #ifndef PCA9685_EXCLUDE_EXT_FUNC
  88. // Called in setup(). Used when instance talks through to AllCall/Sub1-Sub3 instances
  89. // as a proxy object. Using this method will disable any method that performs a read
  90. // or conflicts certain states.
  91. void initAsProxyAddresser(byte i2cAddress = 0xE0);
  92. #endif
  93. byte getI2CAddress();
  94. PCA9685_PhaseBalancer getPhaseBalancer();
  95. // Min: 24Hz, Max: 1526Hz, Default: 200Hz (resolution widens as Hz goes higher)
  96. void setPWMFrequency(float pwmFrequency);
  97. // Turns channel either full on or full off
  98. void setChannelOn(int channel);
  99. void setChannelOff(int channel);
  100. // PWM amounts 0 - 4096, 0 full off, 4096 full on
  101. void setChannelPWM(int channel, uint16_t pwmAmount);
  102. void setChannelsPWM(int begChannel, int numChannels, const uint16_t *pwmAmounts);
  103. #ifndef PCA9685_EXCLUDE_EXT_FUNC
  104. // Sets all channels, but won't distribute phases
  105. void setAllChannelsPWM(uint16_t pwmAmount);
  106. // Returns PWM amounts 0 - 4096, 0 full off, 4096 full on
  107. uint16_t getChannelPWM(int channel);
  108. // Enables multiple talk-through paths via i2c bus (lsb/bit0 must stay 0)
  109. // To use, create a new class instance using initAsSubAddressed() with said address
  110. void enableAllCallAddress(byte i2cAddress = 0xE0);
  111. void enableSub1Address(byte i2cAddress = 0xE2);
  112. void enableSub2Address(byte i2cAddress = 0xE4);
  113. void enableSub3Address(byte i2cAddress = 0xE8);
  114. void disableAllCallAddress();
  115. void disableSub1Address();
  116. void disableSub2Address();
  117. void disableSub3Address();
  118. // Allows external clock line to be utilized (once enabled cannot be disabled)
  119. void enableExtClockLine();
  120. #endif
  121. byte getLastI2CError();
  122. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  123. void printModuleInfo();
  124. void checkForErrors();
  125. #endif
  126. private:
  127. #ifndef PCA9685_ENABLE_SOFTWARE_I2C
  128. TwoWire *_i2cWire; // Wire class instance to use
  129. #endif
  130. byte _i2cAddress; // Module's i2c address
  131. PCA9685_PhaseBalancer _phaseBalancer; // Phase balancer scheme to distribute load
  132. bool _isProxyAddresser; // Instance is a proxy for sub addressing (disables certain functionality)
  133. byte _lastI2CError; // Last i2c error
  134. void getPhaseCycle(int channel, uint16_t pwmAmount, uint16_t *phaseBegin, uint16_t *phaseEnd);
  135. void writeChannelBegin(int channel);
  136. void writeChannelPWM(uint16_t phaseBegin, uint16_t phaseEnd);
  137. void writeChannelEnd();
  138. void writeRegister(byte regAddress, byte value);
  139. byte readRegister(byte regAddress);
  140. #ifdef PCA9685_ENABLE_SOFTWARE_I2C
  141. uint8_t _readBytes;
  142. #endif
  143. void i2cWire_beginTransmission(uint8_t);
  144. uint8_t i2cWire_endTransmission(void);
  145. uint8_t i2cWire_requestFrom(uint8_t, uint8_t);
  146. size_t i2cWire_write(uint8_t);
  147. uint8_t i2cWire_read(void);
  148. };
  149. #ifndef PCA9685_EXCLUDE_SERVO_EVAL
  150. // Class to assist with calculating Servo PWM values from angle values
  151. class PCA9685_ServoEvaluator {
  152. public:
  153. // Uses a linear interpolation method to quickly compute PWM output value. Uses
  154. // default values of 2.5% and 12.5% of phase length for -90/+90.
  155. PCA9685_ServoEvaluator(uint16_t n90PWMAmount = 102, uint16_t p90PWMAmount = 512);
  156. // Uses a cubic spline to interpolate due to an offsetted zero angle that isn't
  157. // exactly between -90/+90. This takes more time to compute, but gives a more
  158. // accurate PWM output value along the entire range.
  159. PCA9685_ServoEvaluator(uint16_t n90PWMAmount, uint16_t zeroPWMAmount, uint16_t p90PWMAmount);
  160. ~PCA9685_ServoEvaluator();
  161. // Returns the PWM value to use given the angle (-90 to +90)
  162. uint16_t pwmForAngle(float angle);
  163. private:
  164. float *_coeff; // a,b,c,d coefficient values
  165. bool _isCSpline; // Cubic spline tracking, for _coeff length
  166. };
  167. #endif
  168. #endif