Line data Source code
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 : 10 : This program is distributed in the hope that it will be useful, 11 : but WITHOUT ANY WARRANTY; without even the implied warranty of 12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 : GNU General Public License for more details. 14 : 15 : You should have received a copy of the GNU General Public License 16 : along with this program. If not, see <http://www.gnu.org/licenses/>. 17 : */ 18 : 19 : /* LED driver for TCA62724FMG */ 20 : 21 : #include "ToshibaLED_I2C.h" 22 : 23 : #if AP_NOTIFY_TOSHIBALED_ENABLED 24 : 25 : #include <utility> 26 : 27 : #include <AP_HAL/AP_HAL.h> 28 : 29 : extern const AP_HAL::HAL& hal; 30 : 31 : #define TOSHIBA_LED_BRIGHT 0xFF // full brightness 32 : #define TOSHIBA_LED_MEDIUM 0x80 // medium brightness 33 : #define TOSHIBA_LED_DIM 0x11 // dim 34 : #define TOSHIBA_LED_OFF 0x00 // off 35 : 36 : #define TOSHIBA_LED_I2C_ADDR 0x55 // default I2C bus address 37 : 38 : #define TOSHIBA_LED_PWM0 0x01 // pwm0 register 39 : #define TOSHIBA_LED_PWM1 0x02 // pwm1 register 40 : #define TOSHIBA_LED_PWM2 0x03 // pwm2 register 41 : #define TOSHIBA_LED_ENABLE 0x04 // enable register 42 : 43 4188 : ToshibaLED_I2C::ToshibaLED_I2C(uint8_t bus) 44 : : RGBLed(TOSHIBA_LED_OFF, TOSHIBA_LED_BRIGHT, TOSHIBA_LED_MEDIUM, TOSHIBA_LED_DIM) 45 4188 : , _bus(bus) 46 : { 47 4188 : } 48 : 49 4188 : bool ToshibaLED_I2C::init(void) 50 : { 51 : // first look for led on external bus 52 4188 : _dev = hal.i2c_mgr->get_device_ptr(_bus, TOSHIBA_LED_I2C_ADDR); 53 4188 : if (!_dev) { 54 0 : return false; 55 : } 56 4188 : WITH_SEMAPHORE(_dev->get_semaphore()); 57 : 58 4188 : _dev->set_retries(10); 59 : 60 : // enable the led 61 4188 : bool ret = _dev->write_register(TOSHIBA_LED_ENABLE, 0x03); 62 4188 : if (!ret) { 63 2094 : return false; 64 : } 65 : 66 : // update the red, green and blue values to zero 67 2094 : uint8_t val[4] = { TOSHIBA_LED_PWM0, _led_off, _led_off, _led_off }; 68 2094 : ret = _dev->transfer(val, sizeof(val), nullptr, 0); 69 : 70 2094 : _dev->set_retries(1); 71 : 72 2094 : if (ret) { 73 2094 : _dev->register_periodic_callback(20000, FUNCTOR_BIND_MEMBER(&ToshibaLED_I2C::_timer, void)); 74 : } 75 : 76 2094 : return ret; 77 4188 : } 78 : 79 : // set_rgb - set color as a combination of red, green and blue values 80 363999 : bool ToshibaLED_I2C::hw_set_rgb(uint8_t red, uint8_t green, uint8_t blue) 81 : { 82 363999 : rgb = {red, green, blue}; 83 363999 : _need_update = true; 84 363999 : return true; 85 : } 86 : 87 7790512 : void ToshibaLED_I2C::_timer(void) 88 : { 89 7790512 : if (!_need_update) { 90 7426531 : return; 91 : } 92 363981 : _need_update = false; 93 : 94 : /* 4-bit for each color */ 95 363981 : uint8_t val[4] = { TOSHIBA_LED_PWM0, (uint8_t)(rgb.b >> 4), 96 363981 : (uint8_t)(rgb.g / 16), (uint8_t)(rgb.r / 16) }; 97 : 98 363981 : _dev->transfer(val, sizeof(val), nullptr, 0); 99 : } 100 : 101 : #endif // AP_NOTIFY_TOSHIBALED_ENABLED