NCP5623.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. NCP5623 I2C LED 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 "NCP5623.h"
  17. #include <utility>
  18. #include <AP_HAL/AP_HAL.h>
  19. extern const AP_HAL::HAL& hal;
  20. #define NCP5623_LED_BRIGHT 0x1f // full brightness
  21. #define NCP5623_LED_MEDIUM 0x18 // medium brightness
  22. #define NCP5623_LED_DIM 0x0f // dim
  23. #define NCP5623_LED_OFF 0x00 // off
  24. #define NCP5623_LED_I2C_ADDR 0x38 // default I2C bus address
  25. #define NCP5623_C_LED_I2C_ADDR 0x39 // default I2C bus address for the NCP5623C
  26. #define NCP5623_LED_PWM0 0x40 // pwm0 register
  27. #define NCP5623_LED_PWM1 0x60 // pwm1 register
  28. #define NCP5623_LED_PWM2 0x80 // pwm2 register
  29. #define NCP5623_LED_ENABLE 0x20 // enable register
  30. NCP5623::NCP5623(uint8_t bus)
  31. : RGBLed(NCP5623_LED_OFF, NCP5623_LED_BRIGHT, NCP5623_LED_MEDIUM, NCP5623_LED_DIM)
  32. , _bus(bus)
  33. {
  34. }
  35. bool NCP5623::write(uint8_t reg, uint8_t data)
  36. {
  37. uint8_t msg[1] = { 0x00 };
  38. msg[0] = ((reg & 0xe0) | (data & 0x1f));
  39. bool ret = _dev->transfer(msg, 1, nullptr, 0);
  40. return ret;
  41. }
  42. bool NCP5623::write_pwm(uint8_t _rgb[3])
  43. {
  44. uint8_t reg = NCP5623_LED_PWM0;
  45. for (uint8_t i=0; i<3; i++) {
  46. if (!write(reg+i*0x20, _rgb[i])) {
  47. return false;
  48. }
  49. }
  50. return true;
  51. }
  52. bool NCP5623::hw_init(void)
  53. {
  54. uint8_t addrs[] = { NCP5623_LED_I2C_ADDR, NCP5623_C_LED_I2C_ADDR };
  55. for (uint8_t i=0; i<ARRAY_SIZE(addrs); i++) {
  56. // first look for led on external bus
  57. _dev = std::move(hal.i2c_mgr->get_device(_bus, addrs[i]));
  58. if (!_dev) {
  59. continue;
  60. }
  61. _dev->get_semaphore()->take_blocking();
  62. _dev->set_retries(10);
  63. // enable the led
  64. bool ret = write(NCP5623_LED_ENABLE, 0x1f);
  65. if (!ret) {
  66. _dev->get_semaphore()->give();
  67. continue;
  68. }
  69. // update the red, green and blue values to zero
  70. uint8_t off[3] = { _led_off, _led_off, _led_off };
  71. ret = write_pwm(off);
  72. _dev->set_retries(1);
  73. // give back i2c semaphore
  74. _dev->get_semaphore()->give();
  75. if (ret) {
  76. _dev->register_periodic_callback(20000, FUNCTOR_BIND_MEMBER(&NCP5623::_timer, void));
  77. }
  78. return true;
  79. }
  80. return false;
  81. }
  82. // set_rgb - set color as a combination of red, green and blue values
  83. bool NCP5623::hw_set_rgb(uint8_t red, uint8_t green, uint8_t blue)
  84. {
  85. rgb[0] = red;
  86. rgb[1] = green;
  87. rgb[2] = blue;
  88. _need_update = true;
  89. return true;
  90. }
  91. void NCP5623::_timer(void)
  92. {
  93. if (!_need_update) {
  94. return;
  95. }
  96. _need_update = false;
  97. write_pwm(rgb);
  98. }