ToshibaLED_I2C.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. ToshibaLED 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. /* LED driver for TCA62724FMG */
  17. #include "ToshibaLED_I2C.h"
  18. #include <utility>
  19. #include <AP_HAL/AP_HAL.h>
  20. extern const AP_HAL::HAL& hal;
  21. #define TOSHIBA_LED_BRIGHT 0xFF // full brightness
  22. #define TOSHIBA_LED_MEDIUM 0x80 // medium brightness
  23. #define TOSHIBA_LED_DIM 0x11 // dim
  24. #define TOSHIBA_LED_OFF 0x00 // off
  25. #define TOSHIBA_LED_I2C_ADDR 0x55 // default I2C bus address
  26. #define TOSHIBA_LED_PWM0 0x01 // pwm0 register
  27. #define TOSHIBA_LED_PWM1 0x02 // pwm1 register
  28. #define TOSHIBA_LED_PWM2 0x03 // pwm2 register
  29. #define TOSHIBA_LED_ENABLE 0x04 // enable register
  30. ToshibaLED_I2C::ToshibaLED_I2C(uint8_t bus)
  31. : RGBLed(TOSHIBA_LED_OFF, TOSHIBA_LED_BRIGHT, TOSHIBA_LED_MEDIUM, TOSHIBA_LED_DIM)
  32. , _bus(bus)
  33. {
  34. }
  35. bool ToshibaLED_I2C::hw_init(void)
  36. {
  37. // first look for led on external bus
  38. _dev = std::move(hal.i2c_mgr->get_device(_bus, TOSHIBA_LED_I2C_ADDR));
  39. if (!_dev) {
  40. return false;
  41. }
  42. WITH_SEMAPHORE(_dev->get_semaphore());
  43. _dev->set_retries(10);
  44. // enable the led
  45. bool ret = _dev->write_register(TOSHIBA_LED_ENABLE, 0x03);
  46. if (!ret) {
  47. return false;
  48. }
  49. // update the red, green and blue values to zero
  50. uint8_t val[4] = { TOSHIBA_LED_PWM0, _led_off, _led_off, _led_off };
  51. ret = _dev->transfer(val, sizeof(val), nullptr, 0);
  52. _dev->set_retries(1);
  53. if (ret) {
  54. _dev->register_periodic_callback(20000, FUNCTOR_BIND_MEMBER(&ToshibaLED_I2C::_timer, void));
  55. }
  56. return ret;
  57. }
  58. // set_rgb - set color as a combination of red, green and blue values
  59. bool ToshibaLED_I2C::hw_set_rgb(uint8_t red, uint8_t green, uint8_t blue)
  60. {
  61. rgb = {red, green, blue};
  62. _need_update = true;
  63. return true;
  64. }
  65. void ToshibaLED_I2C::_timer(void)
  66. {
  67. if (!_need_update) {
  68. return;
  69. }
  70. _need_update = false;
  71. /* 4-bit for each color */
  72. uint8_t val[4] = { TOSHIBA_LED_PWM0, (uint8_t)(rgb.b >> 4),
  73. (uint8_t)(rgb.g / 16), (uint8_t)(rgb.r / 16) };
  74. _dev->transfer(val, sizeof(val), nullptr, 0);
  75. }