PCA9685LED_I2C.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. PCA9685LED I2C driver
  3. */
  4. /*
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "PCA9685LED_I2C.h"
  17. #include <AP_HAL/AP_HAL.h>
  18. #define NAVIO_LED_BRIGHT 0x0 // full brightness
  19. #define NAVIO_LED_MEDIUM 0x7F // medium brightness
  20. #define NAVIO_LED_DIM 0x4F // dim brightness
  21. #define NAVIO_LED_OFF 0xFF // off
  22. #define PCA9685_ADDRESS 0x40
  23. #define PCA9685_MODE1 0x00
  24. #define PCA9685_PWM 0x6
  25. #define PCA9685_MODE_SLEEP (1 << 4)
  26. #define PCA9685_MODE_AUTO_INCREMENT (1 << 5)
  27. extern const AP_HAL::HAL& hal;
  28. PCA9685LED_I2C::PCA9685LED_I2C() :
  29. RGBLed(NAVIO_LED_OFF, NAVIO_LED_BRIGHT, NAVIO_LED_MEDIUM, NAVIO_LED_DIM)
  30. {
  31. }
  32. bool PCA9685LED_I2C::hw_init()
  33. {
  34. _dev = hal.i2c_mgr->get_device(1, PCA9685_ADDRESS);
  35. if (!_dev) {
  36. return false;
  37. }
  38. _dev->get_semaphore()->take_blocking();
  39. _dev->set_retries(5);
  40. // read the current mode1 configuration
  41. uint8_t mode1 = 0;
  42. if (!_dev->read_registers(PCA9685_MODE1, &mode1, sizeof(mode1))) {
  43. _dev->get_semaphore()->give();
  44. return false;
  45. }
  46. // bring the device out of sleep, and enable auto register increment
  47. uint8_t new_mode1 = (mode1 | PCA9685_MODE_AUTO_INCREMENT) & ~PCA9685_MODE_SLEEP;
  48. const uint8_t config[2] = {PCA9685_MODE1, new_mode1};
  49. if (!_dev->transfer(config, sizeof(config), nullptr, 0)) {
  50. _dev->get_semaphore()->give();
  51. return false;
  52. }
  53. _dev->set_retries(1);
  54. _dev->get_semaphore()->give();
  55. _dev->register_periodic_callback(20000, FUNCTOR_BIND_MEMBER(&PCA9685LED_I2C::_timer, void));
  56. return true;
  57. }
  58. // set_rgb - set color as a combination of red, green and blue values
  59. bool PCA9685LED_I2C::hw_set_rgb(uint8_t red, uint8_t green, uint8_t blue)
  60. {
  61. rgb.r = red;
  62. rgb.g = green;
  63. rgb.b = blue;
  64. _need_update = true;
  65. return true;
  66. }
  67. void PCA9685LED_I2C::_timer(void)
  68. {
  69. if (!_need_update) {
  70. return;
  71. }
  72. _need_update = false;
  73. uint16_t red_adjusted = rgb.r * 0x10;
  74. uint16_t green_adjusted = rgb.g * 0x10;
  75. uint16_t blue_adjusted = rgb.b * 0x10;
  76. uint8_t blue_channel_lsb = blue_adjusted & 0xFF;
  77. uint8_t blue_channel_msb = blue_adjusted >> 8;
  78. uint8_t green_channel_lsb = green_adjusted & 0xFF;
  79. uint8_t green_channel_msb = green_adjusted >> 8;
  80. uint8_t red_channel_lsb = red_adjusted & 0xFF;
  81. uint8_t red_channel_msb = red_adjusted >> 8;
  82. uint8_t transaction[] = {PCA9685_PWM, 0x00, 0x00, blue_channel_lsb, blue_channel_msb,
  83. 0x00, 0x00, green_channel_lsb, green_channel_msb,
  84. 0x00, 0x00, red_channel_lsb, red_channel_msb};
  85. _dev->transfer(transaction, sizeof(transaction), nullptr, 0);
  86. }