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.cpp 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. #include "PCA9685.h"
  20. #define PCA9685_I2C_BASE_ADDRESS (byte)0x40
  21. // Register addresses from data sheet
  22. #define PCA9685_MODE1_REG (byte)0x00
  23. #define PCA9685_MODE2_REG (byte)0x01
  24. #define PCA9685_SUBADR1_REG (byte)0x02
  25. #define PCA9685_SUBADR2_REG (byte)0x03
  26. #define PCA9685_SUBADR3_REG (byte)0x04
  27. #define PCA9685_ALLCALL_REG (byte)0x05
  28. #define PCA9685_LED0_REG (byte)0x06 // Start of LEDx regs, 4B per reg, 2B on phase, 2B off phase, little-endian
  29. #define PCA9685_PRESCALE_REG (byte)0xFE
  30. #define PCA9685_ALLLED_REG (byte)0xFA
  31. // Mode1 register pin layout
  32. #define PCA9685_MODE_RESTART (byte)0x80
  33. #define PCA9685_MODE_EXTCLK (byte)0x40
  34. #define PCA9685_MODE_AUTOINC (byte)0x20
  35. #define PCA9685_MODE_SLEEP (byte)0x10
  36. #define PCA9685_MODE_SUBADR1 (byte)0x08
  37. #define PCA9685_MODE_SUBADR2 (byte)0x04
  38. #define PCA9685_MODE_SUBADR3 (byte)0x02
  39. #define PCA9685_MODE_ALLCALL (byte)0x01
  40. #define PCA9685_SW_RESET (byte)0x06 // Sent to address 0x00 to reset all devices on Wire line
  41. #define PCA9685_PWM_FULL (uint16_t)0x01000 // Special value for full on/full off LEDx modes
  42. // To balance the load out in a weaved fashion, we use this offset table to distribute
  43. // the load on the outputs in a more interleaving fashion than just a simple 16 offset
  44. // per channel. We can set the off cycle value to be lower than the on cycle, which will
  45. // put the high edge across the 0-4095 phase cycle range, which is supported by device.
  46. static uint16_t phaseDistTable[16] = { 0, 2048, 1024, 3072, 512, 3584, 1536, 2560, 256, 3840, 1280, 2304, 3328, 768, 2816, 1792 };
  47. #ifndef PCA9685_ENABLE_SOFTWARE_I2C
  48. PCA9685::PCA9685(TwoWire& i2cWire, PCA9685_PhaseBalancer phaseBalancer) {
  49. _i2cWire = &i2cWire;
  50. #else
  51. PCA9685::PCA9685(PCA9685_PhaseBalancer phaseBalancer) {
  52. #endif
  53. _i2cAddress = 0;
  54. _phaseBalancer = phaseBalancer;
  55. _isProxyAddresser = false;
  56. _lastI2CError = 0;
  57. }
  58. void PCA9685::resetDevices() {
  59. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  60. Serial.println("PCA9685::resetDevices");
  61. #endif
  62. i2cWire_beginTransmission(0x00);
  63. i2cWire_write(PCA9685_SW_RESET);
  64. i2cWire_endTransmission();
  65. delayMicroseconds(10);
  66. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  67. checkForErrors();
  68. #endif
  69. }
  70. void PCA9685::init(byte i2cAddress, byte mode) {
  71. if (_isProxyAddresser) return;
  72. // I2C 7-bit address is B 1 A5 A4 A3 A2 A1 A0
  73. // RW bit added by Arduino core TWI library
  74. _i2cAddress = PCA9685_I2C_BASE_ADDRESS | (i2cAddress & 0x3F);
  75. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  76. Serial.print("PCA9685::init i2cAddress: 0x");
  77. Serial.println(_i2cAddress, HEX);
  78. #endif
  79. writeRegister(PCA9685_MODE1_REG, PCA9685_MODE_RESTART | PCA9685_MODE_AUTOINC);
  80. writeRegister(PCA9685_MODE2_REG, mode);
  81. }
  82. #ifndef PCA9685_EXCLUDE_EXT_FUNC
  83. void PCA9685::initAsProxyAddresser(byte i2cAddress) {
  84. _i2cAddress = i2cAddress & 0xFE;
  85. _isProxyAddresser = true;
  86. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  87. Serial.print("PCA9685::initAsProxyAddresser i2cAddress: 0x");
  88. Serial.println(_i2cAddress, HEX);
  89. #endif
  90. }
  91. #endif
  92. byte PCA9685::getI2CAddress() {
  93. return _i2cAddress;
  94. }
  95. PCA9685_PhaseBalancer PCA9685::getPhaseBalancer() {
  96. return _phaseBalancer;
  97. }
  98. void PCA9685::setPWMFrequency(float pwmFrequency) {
  99. if (pwmFrequency < 0 || _isProxyAddresser) return;
  100. // This equation comes from section 7.3.5 of the datasheet, but the rounding has been
  101. // removed because it isn't needed. Lowest freq is 23.84, highest is 1525.88.
  102. int preScalerVal = (25000000 / (4096 * pwmFrequency)) - 1;
  103. if (preScalerVal > 255) preScalerVal = 255;
  104. if (preScalerVal < 3) preScalerVal = 3;
  105. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  106. Serial.print("PCA9685::setPWMFrequency pwmFrequency: ");
  107. Serial.print(pwmFrequency);
  108. Serial.print(", preScalerVal: 0x");
  109. Serial.println(preScalerVal, HEX);
  110. #endif
  111. // The PRE_SCALE register can only be set when the SLEEP bit of MODE1 register is set to logic 1.
  112. byte mode1Reg = readRegister(PCA9685_MODE1_REG);
  113. writeRegister(PCA9685_MODE1_REG, (mode1Reg = (mode1Reg & ~PCA9685_MODE_RESTART) | PCA9685_MODE_SLEEP));
  114. writeRegister(PCA9685_PRESCALE_REG, (byte)preScalerVal);
  115. // It takes 500us max for the oscillator to be up and running once SLEEP bit has been set to logic 0.
  116. writeRegister(PCA9685_MODE1_REG, (mode1Reg = (mode1Reg & ~PCA9685_MODE_SLEEP) | PCA9685_MODE_RESTART));
  117. delayMicroseconds(500);
  118. }
  119. void PCA9685::setChannelOn(int channel) {
  120. if (channel < 0 || channel > 15) return;
  121. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  122. Serial.println("PCA9685::setChannelOn");
  123. #endif
  124. writeChannelBegin(channel);
  125. writeChannelPWM(PCA9685_PWM_FULL, 0); // time_on = FULL; time_off = 0;
  126. writeChannelEnd();
  127. }
  128. void PCA9685::setChannelOff(int channel) {
  129. if (channel < 0 || channel > 15) return;
  130. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  131. Serial.println("PCA9685::setChannelOff");
  132. #endif
  133. writeChannelBegin(channel);
  134. writeChannelPWM(0, PCA9685_PWM_FULL); // time_on = 0; time_off = FULL;
  135. writeChannelEnd();
  136. }
  137. void PCA9685::setChannelPWM(int channel, uint16_t pwmAmount) {
  138. if (channel < 0 || channel > 15) return;
  139. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  140. Serial.println("PCA9685::setChannelPWM");
  141. #endif
  142. writeChannelBegin(channel);
  143. uint16_t phaseBegin, phaseEnd;
  144. getPhaseCycle(channel, pwmAmount, &phaseBegin, &phaseEnd);
  145. writeChannelPWM(phaseBegin, phaseEnd);
  146. writeChannelEnd();
  147. }
  148. void PCA9685::setChannelsPWM(int begChannel, int numChannels, const uint16_t *pwmAmounts) {
  149. if (begChannel < 0 || begChannel > 15 || numChannels < 0) return;
  150. if (begChannel + numChannels > 16) numChannels -= (begChannel + numChannels) - 16;
  151. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  152. Serial.print("PCA9685::setChannelsPWM numChannels: ");
  153. Serial.println(numChannels);
  154. #endif
  155. // In avr/libraries/Wire.h and avr/libraries/utility/twi.h, _BUFFER_LENGTH controls
  156. // how many channels can be written at once. Therefore, we loop around until all
  157. // channels have been written out into their registers.
  158. while (numChannels > 0) {
  159. writeChannelBegin(begChannel);
  160. int maxChannels = min(numChannels, (_BUFFER_LENGTH - 1) / 4);
  161. while (maxChannels-- > 0) {
  162. uint16_t phaseBegin, phaseEnd;
  163. getPhaseCycle(begChannel++, *pwmAmounts++, &phaseBegin, &phaseEnd);
  164. writeChannelPWM(phaseBegin, phaseEnd);
  165. --numChannels;
  166. }
  167. writeChannelEnd();
  168. if (_lastI2CError) return;
  169. }
  170. }
  171. #ifndef PCA9685_EXCLUDE_EXT_FUNC
  172. void PCA9685::setAllChannelsPWM(uint16_t pwmAmount) {
  173. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  174. Serial.println("PCA9685::setAllChannelsPWM");
  175. #endif
  176. writeChannelBegin(-1); // Special value for ALLLED registers
  177. uint16_t phaseBegin, phaseEnd;
  178. getPhaseCycle(-1, pwmAmount, &phaseBegin, &phaseEnd);
  179. writeChannelPWM(phaseBegin, phaseEnd);
  180. writeChannelEnd();
  181. }
  182. uint16_t PCA9685::getChannelPWM(int channel) {
  183. if (channel < 0 || channel > 15 || _isProxyAddresser) return 0;
  184. byte regAddress = PCA9685_LED0_REG + (channel << 2);
  185. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  186. Serial.print("PCA9685::getChannelPWM channel: ");
  187. Serial.print(channel);
  188. Serial.print(", regAddress: 0x");
  189. Serial.println(regAddress, HEX);
  190. #endif
  191. i2cWire_beginTransmission(_i2cAddress);
  192. i2cWire_write(regAddress);
  193. if (i2cWire_endTransmission()) {
  194. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  195. checkForErrors();
  196. #endif
  197. return 0;
  198. }
  199. int bytesRead = i2cWire_requestFrom((uint8_t)_i2cAddress, (uint8_t)4);
  200. if (bytesRead != 4) {
  201. while (bytesRead-- > 0)
  202. i2cWire_read();
  203. _lastI2CError = 4;
  204. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  205. checkForErrors();
  206. #endif
  207. return 0;
  208. }
  209. uint16_t phaseBegin = (uint16_t)i2cWire_read();
  210. phaseBegin |= (uint16_t)i2cWire_read() << 8;
  211. uint16_t phaseEnd = (uint16_t)i2cWire_read();
  212. phaseEnd |= (uint16_t)i2cWire_read() << 8;
  213. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  214. Serial.print(" PCA9685::getChannelPWM phaseBegin: ");
  215. Serial.print(phaseBegin);
  216. Serial.print(", phaseEnd: ");
  217. Serial.println(phaseEnd);
  218. #endif
  219. // See datasheet section 7.3.3
  220. uint16_t retVal;
  221. if (phaseEnd >= PCA9685_PWM_FULL)
  222. // Full OFF
  223. // Figure 11 Example 4: full OFF takes precedence over full ON
  224. // See also remark after Table 7
  225. retVal = 0;
  226. else if (phaseBegin >= PCA9685_PWM_FULL)
  227. // Full ON
  228. // Figure 9 Example 3
  229. retVal = PCA9685_PWM_FULL;
  230. else if (phaseBegin <= phaseEnd)
  231. // start and finish in same cycle
  232. // Section 7.3.3 example 1
  233. retVal = phaseEnd - phaseBegin;
  234. else
  235. // span cycles
  236. // Section 7.3.3 example 2
  237. retVal = (phaseEnd + PCA9685_PWM_FULL) - phaseBegin;
  238. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  239. Serial.print(" PCA9685::getChannelPWM retVal: ");
  240. Serial.println(retVal);
  241. #endif
  242. return retVal;
  243. }
  244. void PCA9685::enableAllCallAddress(byte i2cAddress) {
  245. if (_isProxyAddresser) return;
  246. i2cAddress &= 0xFE;
  247. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  248. Serial.print("PCA9685::enableAllCallAddress i2cAddress: 0x");
  249. Serial.println(i2cAddress, HEX);
  250. #endif
  251. writeRegister(PCA9685_ALLCALL_REG, i2cAddress);
  252. byte mode1Reg = readRegister(PCA9685_MODE1_REG);
  253. writeRegister(PCA9685_MODE1_REG, (mode1Reg |= PCA9685_MODE_ALLCALL));
  254. }
  255. void PCA9685::enableSub1Address(byte i2cAddress) {
  256. if (_isProxyAddresser) return;
  257. i2cAddress &= 0xFE;
  258. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  259. Serial.print("PCA9685::enableSub1Address i2cAddress: 0x");
  260. Serial.println(i2cAddress, HEX);
  261. #endif
  262. writeRegister(PCA9685_SUBADR1_REG, i2cAddress);
  263. byte mode1Reg = readRegister(PCA9685_MODE1_REG);
  264. writeRegister(PCA9685_MODE1_REG, (mode1Reg |= PCA9685_MODE_SUBADR1));
  265. }
  266. void PCA9685::enableSub2Address(byte i2cAddress) {
  267. if (_isProxyAddresser) return;
  268. i2cAddress &= 0xFE;
  269. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  270. Serial.print("PCA9685::enableSub2Address i2cAddress: 0x");
  271. Serial.println(i2cAddress, HEX);
  272. #endif
  273. writeRegister(PCA9685_SUBADR2_REG, i2cAddress);
  274. byte mode1Reg = readRegister(PCA9685_MODE1_REG);
  275. writeRegister(PCA9685_MODE1_REG, (mode1Reg |= PCA9685_MODE_SUBADR2));
  276. }
  277. void PCA9685::enableSub3Address(byte i2cAddress) {
  278. if (_isProxyAddresser) return;
  279. i2cAddress &= 0xFE;
  280. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  281. Serial.print("PCA9685::enableSub3Address i2cAddress: 0x");
  282. Serial.println(i2cAddress, HEX);
  283. #endif
  284. writeRegister(PCA9685_SUBADR3_REG, i2cAddress);
  285. byte mode1Reg = readRegister(PCA9685_MODE1_REG);
  286. writeRegister(PCA9685_MODE1_REG, (mode1Reg |= PCA9685_MODE_SUBADR3));
  287. }
  288. void PCA9685::disableAllCallAddress() {
  289. if (_isProxyAddresser) return;
  290. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  291. Serial.println("PCA9685::disableAllCallAddress");
  292. #endif
  293. byte mode1Reg = readRegister(PCA9685_MODE1_REG);
  294. writeRegister(PCA9685_MODE1_REG, (mode1Reg &= ~PCA9685_MODE_ALLCALL));
  295. }
  296. void PCA9685::disableSub1Address() {
  297. if (_isProxyAddresser) return;
  298. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  299. Serial.println("PCA9685::disableSub1Address");
  300. #endif
  301. byte mode1Reg = readRegister(PCA9685_MODE1_REG);
  302. writeRegister(PCA9685_MODE1_REG, (mode1Reg &= ~PCA9685_MODE_SUBADR1));
  303. }
  304. void PCA9685::disableSub2Address() {
  305. if (_isProxyAddresser) return;
  306. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  307. Serial.println("PCA9685::disableSub2Address");
  308. #endif
  309. byte mode1Reg = readRegister(PCA9685_MODE1_REG);
  310. writeRegister(PCA9685_MODE1_REG, (mode1Reg &= ~PCA9685_MODE_SUBADR2));
  311. }
  312. void PCA9685::disableSub3Address() {
  313. if (_isProxyAddresser) return;
  314. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  315. Serial.println("PCA9685::disableSub3Address");
  316. #endif
  317. byte mode1Reg = readRegister(PCA9685_MODE1_REG);
  318. writeRegister(PCA9685_MODE1_REG, (mode1Reg &= ~PCA9685_MODE_SUBADR3));
  319. }
  320. void PCA9685::enableExtClockLine() {
  321. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  322. Serial.println("PCA9685::enableExtClockLine");
  323. #endif
  324. // The PRE_SCALE register can only be set when the SLEEP bit of MODE1 register is set to logic 1.
  325. byte mode1Reg = readRegister(PCA9685_MODE1_REG);
  326. writeRegister(PCA9685_MODE1_REG, (mode1Reg = (mode1Reg & ~PCA9685_MODE_RESTART) | PCA9685_MODE_SLEEP));
  327. writeRegister(PCA9685_MODE1_REG, (mode1Reg |= PCA9685_MODE_EXTCLK));
  328. // It takes 500us max for the oscillator to be up and running once SLEEP bit has been set to logic 0.
  329. writeRegister(PCA9685_MODE1_REG, (mode1Reg = (mode1Reg & ~PCA9685_MODE_SLEEP) | PCA9685_MODE_RESTART));
  330. delayMicroseconds(500);
  331. }
  332. #endif
  333. byte PCA9685::getLastI2CError() {
  334. return _lastI2CError;
  335. }
  336. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  337. static const char *textForI2CError(byte errorCode) {
  338. switch (errorCode) {
  339. case 0:
  340. return "Success";
  341. case 1:
  342. return "Data too long to fit in transmit buffer";
  343. case 2:
  344. return "Received NACK on transmit of address";
  345. case 3:
  346. return "Received NACK on transmit of data";
  347. default:
  348. return "Other error";
  349. }
  350. }
  351. void PCA9685::checkForErrors() {
  352. if (_lastI2CError) {
  353. Serial.print(" PCA9685::checkErrors lastI2CError: ");
  354. Serial.print(_lastI2CError);
  355. Serial.print(": ");
  356. Serial.println(textForI2CError(getLastI2CError()));
  357. }
  358. }
  359. #endif
  360. void PCA9685::getPhaseCycle(int channel, uint16_t pwmAmount, uint16_t *phaseBegin, uint16_t *phaseEnd) {
  361. // Set delay
  362. if (channel < 0) {
  363. // All channels
  364. *phaseBegin = 0;
  365. }
  366. else if (_phaseBalancer == PCA9685_PhaseBalancer_Linear) {
  367. // Distribute high phase area over entire phase range to balance load.
  368. *phaseBegin = channel * (4096 / 16);
  369. }
  370. else if (_phaseBalancer == PCA9685_PhaseBalancer_Weaved) {
  371. // Distribute high phase area over entire phase range to balance load.
  372. *phaseBegin = phaseDistTable[channel];
  373. }
  374. else {
  375. *phaseBegin = 0;
  376. }
  377. // See datasheet section 7.3.3
  378. if (pwmAmount == 0) {
  379. // Full OFF => time_off[12] = 1;
  380. *phaseEnd = PCA9685_PWM_FULL;
  381. }
  382. else if (pwmAmount >= PCA9685_PWM_FULL) {
  383. // Full ON => time_on[12] = 1; time_off = ignored;
  384. *phaseBegin |= PCA9685_PWM_FULL;
  385. *phaseEnd = 0;
  386. }
  387. else {
  388. *phaseEnd = *phaseBegin + pwmAmount;
  389. if (*phaseEnd >= PCA9685_PWM_FULL)
  390. *phaseEnd -= PCA9685_PWM_FULL;
  391. }
  392. }
  393. void PCA9685::writeChannelBegin(int channel) {
  394. byte regAddress;
  395. if (channel != -1)
  396. regAddress = PCA9685_LED0_REG + (channel * 0x04);
  397. else
  398. regAddress = PCA9685_ALLLED_REG;
  399. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  400. Serial.print(" PCA9685::writeChannelBegin channel: ");
  401. Serial.print(channel);
  402. Serial.print(", regAddress: 0x");
  403. Serial.println(regAddress, HEX);
  404. #endif
  405. i2cWire_beginTransmission(_i2cAddress);
  406. i2cWire_write(regAddress);
  407. }
  408. void PCA9685::writeChannelPWM(uint16_t phaseBegin, uint16_t phaseEnd) {
  409. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  410. Serial.print(" PCA9685::writeChannelPWM phaseBegin: ");
  411. Serial.print(phaseBegin);
  412. Serial.print(", phaseEnd: ");
  413. Serial.println(phaseEnd);
  414. #endif
  415. i2cWire_write(lowByte(phaseBegin));
  416. i2cWire_write(highByte(phaseBegin));
  417. i2cWire_write(lowByte(phaseEnd));
  418. i2cWire_write(highByte(phaseEnd));
  419. }
  420. void PCA9685::writeChannelEnd() {
  421. i2cWire_endTransmission();
  422. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  423. checkForErrors();
  424. #endif
  425. }
  426. void PCA9685::writeRegister(byte regAddress, byte value) {
  427. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  428. Serial.print(" PCA9685::writeRegister regAddress: 0x");
  429. Serial.print(regAddress, HEX);
  430. Serial.print(", value: 0x");
  431. Serial.println(value, HEX);
  432. #endif
  433. i2cWire_beginTransmission(_i2cAddress);
  434. i2cWire_write(regAddress);
  435. i2cWire_write(value);
  436. i2cWire_endTransmission();
  437. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  438. checkForErrors();
  439. #endif
  440. }
  441. byte PCA9685::readRegister(byte regAddress) {
  442. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  443. Serial.print(" PCA9685::readRegister regAddress: 0x");
  444. Serial.println(regAddress, HEX);
  445. #endif
  446. i2cWire_beginTransmission(_i2cAddress);
  447. i2cWire_write(regAddress);
  448. if (i2cWire_endTransmission()) {
  449. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  450. checkForErrors();
  451. #endif
  452. return 0;
  453. }
  454. int bytesRead = i2cWire_requestFrom((uint8_t)_i2cAddress, (uint8_t)1);
  455. if (bytesRead != 1) {
  456. while (bytesRead-- > 0)
  457. i2cWire_read();
  458. _lastI2CError = 4;
  459. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  460. checkForErrors();
  461. #endif
  462. return 0;
  463. }
  464. byte retVal = i2cWire_read();
  465. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  466. Serial.print(" PCA9685::readRegister retVal: 0x");
  467. Serial.println(retVal, HEX);
  468. #endif
  469. return retVal;
  470. }
  471. #ifdef PCA9685_ENABLE_SOFTWARE_I2C
  472. bool __attribute__((noinline)) i2c_start(uint8_t addr);
  473. void __attribute__((noinline)) i2c_stop(void) asm("ass_i2c_stop");
  474. bool __attribute__((noinline)) i2c_write(uint8_t value) asm("ass_i2c_write");
  475. uint8_t __attribute__((noinline)) i2c_read(bool last);
  476. #endif
  477. void PCA9685::i2cWire_beginTransmission(uint8_t addr) {
  478. _lastI2CError = 0;
  479. #ifndef PCA9685_ENABLE_SOFTWARE_I2C
  480. _i2cWire->beginTransmission(addr);
  481. #else
  482. i2c_start(addr);
  483. #endif
  484. }
  485. uint8_t PCA9685::i2cWire_endTransmission(void) {
  486. #ifndef PCA9685_ENABLE_SOFTWARE_I2C
  487. return (_lastI2CError = _i2cWire->endTransmission());
  488. #else
  489. i2c_stop();
  490. return (_lastI2CError = 0);
  491. #endif
  492. }
  493. uint8_t PCA9685::i2cWire_requestFrom(uint8_t addr, uint8_t len) {
  494. #ifndef PCA9685_ENABLE_SOFTWARE_I2C
  495. return _i2cWire->requestFrom(addr, len);
  496. #else
  497. i2c_start(addr | 0x01);
  498. return (_readBytes = len);
  499. #endif
  500. }
  501. size_t PCA9685::i2cWire_write(uint8_t data) {
  502. #ifndef PCA9685_ENABLE_SOFTWARE_I2C
  503. return _i2cWire->write(data);
  504. #else
  505. return (size_t)i2c_write(data);
  506. #endif
  507. }
  508. uint8_t PCA9685::i2cWire_read(void) {
  509. #ifndef PCA9685_ENABLE_SOFTWARE_I2C
  510. return (uint8_t)(_i2cWire->read() & 0xFF);
  511. #else
  512. if (_readBytes > 1) {
  513. _readByes -= 1;
  514. return (uint8_t)(i2c_read(false) & 0xFF);
  515. }
  516. else {
  517. _readBytes = 0;
  518. return (uint8_t)(i2c_read(true) & 0xFF);
  519. }
  520. #endif
  521. }
  522. #ifdef PCA9685_ENABLE_DEBUG_OUTPUT
  523. void PCA9685::printModuleInfo() {
  524. Serial.println(""); Serial.println(" ~~~ PCA9685 Module Info ~~~");
  525. Serial.println(""); Serial.println("i2c Address:");
  526. Serial.print("0x");
  527. Serial.println(_i2cAddress, HEX);
  528. Serial.println(""); Serial.println("Phase Balancer:");
  529. switch (_phaseBalancer) {
  530. case PCA9685_PhaseBalancer_None:
  531. Serial.println("PCA9685_PhaseBalancer_None"); break;
  532. case PCA9685_PhaseBalancer_Linear:
  533. Serial.println("PCA9685_PhaseBalancer_Linear"); break;
  534. case PCA9685_PhaseBalancer_Weaved:
  535. Serial.println("PCA9685_PhaseBalancer_Weaved"); break;
  536. default:
  537. Serial.println(""); break;
  538. }
  539. if (!_isProxyAddresser) {
  540. Serial.println(""); Serial.println("Proxy Addresser:");
  541. Serial.println("false");
  542. Serial.println(""); Serial.println("Mode1 Register:");
  543. byte mode1Reg = readRegister(PCA9685_MODE1_REG);
  544. Serial.print("0x");
  545. Serial.print(mode1Reg, HEX);
  546. Serial.print(", Bitset:");
  547. if (mode1Reg & PCA9685_MODE_RESTART)
  548. Serial.print(" PCA9685_MODE_RESTART");
  549. if (mode1Reg & PCA9685_MODE_EXTCLK)
  550. Serial.print(" PCA9685_MODE_EXTCLK");
  551. if (mode1Reg & PCA9685_MODE_AUTOINC)
  552. Serial.print(" PCA9685_MODE_AUTOINC");
  553. if (mode1Reg & PCA9685_MODE_SLEEP)
  554. Serial.print(" PCA9685_MODE_SLEEP");
  555. if (mode1Reg & PCA9685_MODE_SUBADR1)
  556. Serial.print(" PCA9685_MODE_SUBADR1");
  557. if (mode1Reg & PCA9685_MODE_SUBADR2)
  558. Serial.print(" PCA9685_MODE_SUBADR2");
  559. if (mode1Reg & PCA9685_MODE_SUBADR3)
  560. Serial.print(" PCA9685_MODE_SUBADR3");
  561. if (mode1Reg & PCA9685_MODE_ALLCALL)
  562. Serial.print(" PCA9685_MODE_ALLCALL");
  563. Serial.println("");
  564. Serial.println(""); Serial.println("Mode2 Register:");
  565. byte mode2Reg = readRegister(PCA9685_MODE2_REG);
  566. Serial.print("0x");
  567. Serial.print(mode2Reg, HEX);
  568. Serial.print(", Bitset:");
  569. if (mode2Reg & PCA9685_MODE_INVRT)
  570. Serial.print(" PCA9685_MODE_INVRT");
  571. if (mode2Reg & PCA9685_MODE_OUTPUT_ONACK)
  572. Serial.print(" PCA9685_MODE_OUTPUT_ONACK");
  573. if (mode2Reg & PCA9685_MODE_OUTPUT_TPOLE)
  574. Serial.print(" PCA9685_MODE_OUTPUT_TPOLE");
  575. if (mode2Reg & PCA9685_MODE_OUTNE_HIGHZ)
  576. Serial.print(" PCA9685_MODE_OUTNE_HIGHZ");
  577. if (mode2Reg & PCA9685_MODE_OUTNE_LOW)
  578. Serial.print(" PCA9685_MODE_OUTNE_LOW");
  579. Serial.println("");
  580. Serial.println(""); Serial.println("SubAddress1 Register:");
  581. byte subAdr1Reg = readRegister(PCA9685_SUBADR1_REG);
  582. Serial.print("0x");
  583. Serial.println(subAdr1Reg, HEX);
  584. Serial.println(""); Serial.println("SubAddress2 Register:");
  585. byte subAdr2Reg = readRegister(PCA9685_SUBADR2_REG);
  586. Serial.print("0x");
  587. Serial.println(subAdr2Reg, HEX);
  588. Serial.println(""); Serial.println("SubAddress3 Register:");
  589. byte subAdr3Reg = readRegister(PCA9685_SUBADR3_REG);
  590. Serial.print("0x");
  591. Serial.println(subAdr3Reg, HEX);
  592. Serial.println(""); Serial.println("AllCall Register:");
  593. byte allCallReg = readRegister(PCA9685_ALLCALL_REG);
  594. Serial.print("0x");
  595. Serial.println(allCallReg, HEX);
  596. }
  597. else {
  598. Serial.println(""); Serial.println("Proxy Addresser:");
  599. Serial.println("true");
  600. }
  601. }
  602. #endif
  603. #ifndef PCA9685_EXCLUDE_SERVO_EVAL
  604. PCA9685_ServoEvaluator::PCA9685_ServoEvaluator(uint16_t n90PWMAmount, uint16_t p90PWMAmount) {
  605. n90PWMAmount = constrain(n90PWMAmount, 0, PCA9685_PWM_FULL);
  606. p90PWMAmount = constrain(p90PWMAmount, n90PWMAmount, PCA9685_PWM_FULL);
  607. _coeff = new float[2];
  608. _isCSpline = false;
  609. _coeff[0] = n90PWMAmount;
  610. _coeff[1] = (p90PWMAmount - n90PWMAmount) / 180.0f;
  611. }
  612. PCA9685_ServoEvaluator::PCA9685_ServoEvaluator(uint16_t n90PWMAmount, uint16_t zeroPWMAmount, uint16_t p90PWMAmount) {
  613. n90PWMAmount = constrain(n90PWMAmount, 0, PCA9685_PWM_FULL);
  614. zeroPWMAmount = constrain(zeroPWMAmount, n90PWMAmount, PCA9685_PWM_FULL);
  615. p90PWMAmount = constrain(p90PWMAmount, zeroPWMAmount, PCA9685_PWM_FULL);
  616. if (p90PWMAmount - zeroPWMAmount != zeroPWMAmount - n90PWMAmount) {
  617. _coeff = new float[8];
  618. _isCSpline = true;
  619. // Cubic spline code adapted from: https://shiftedbits.org/2011/01/30/cubic-spline-interpolation/
  620. /* "THE BEER-WARE LICENSE" (Revision 42): Devin Lane wrote this [part]. As long as you retain
  621. * this notice you can do whatever you want with this stuff. If we meet some day, and you
  622. * think this stuff is worth it, you can buy me a beer in return. */
  623. float x[3] = { 0, 90, 180 };
  624. float y[3] = { (float)n90PWMAmount, (float)zeroPWMAmount, (float)p90PWMAmount };
  625. float c[3], b[2], d[2], h[2], l[1], u[2], a[1], z[2]; // n = 3
  626. h[0] = x[1] - x[0];
  627. u[0] = z[0] = 0;
  628. c[2] = 0;
  629. for (int i = 1; i < 2; ++i) {
  630. h[i] = x[i + 1] - x[i];
  631. l[i - 1] = (2 * (x[i + 1] - x[i - 1])) - h[i - 1] * u[i - 1];
  632. u[i] = h[i] / l[i - 1];
  633. a[i - 1] = (3 / h[i]) * (y[i + 1] - y[i]) - (3 / h[i - 1]) * (y[i] - y[i - 1]);
  634. z[i] = (a[i - 1] - h[i - 1] * z[i - 1]) / l[i - 1];
  635. }
  636. for (int i = 1; i >= 0; --i) {
  637. c[i] = z[i] - u[i] * c[i + 1];
  638. b[i] = (y[i + 1] - y[i]) / h[i] - (h[i] * (c[i + 1] + 2 * c[i])) / 3;
  639. d[i] = (c[i + 1] - c[i]) / (3 * h[i]);
  640. _coeff[4 * i + 0] = y[i]; // a
  641. _coeff[4 * i + 1] = b[i]; // b
  642. _coeff[4 * i + 2] = c[i]; // c
  643. _coeff[4 * i + 3] = d[i]; // d
  644. }
  645. }
  646. else {
  647. _coeff = new float[2];
  648. _isCSpline = false;
  649. _coeff[0] = n90PWMAmount;
  650. _coeff[1] = (p90PWMAmount - n90PWMAmount) / 180.0f;
  651. }
  652. }
  653. PCA9685_ServoEvaluator::~PCA9685_ServoEvaluator() {
  654. if (_coeff) delete[] _coeff;
  655. }
  656. uint16_t PCA9685_ServoEvaluator::pwmForAngle(float angle) {
  657. float retVal;
  658. angle = constrain(angle + 90, 0, 180);
  659. if (!_isCSpline) {
  660. retVal = _coeff[0] + (_coeff[1] * angle);
  661. }
  662. else {
  663. if (angle <= 90) {
  664. retVal = _coeff[0] + (_coeff[1] * angle) + (_coeff[2] * angle * angle) + (_coeff[3] * angle * angle * angle);
  665. }
  666. else {
  667. angle -= 90;
  668. retVal = _coeff[4] + (_coeff[5] * angle) + (_coeff[6] * angle * angle) + (_coeff[7] * angle * angle * angle);
  669. }
  670. }
  671. return (uint16_t)constrain((int)roundf(retVal), 0, PCA9685_PWM_FULL);
  672. };
  673. #endif