2013-08-29 00:31:55 -03:00
|
|
|
/*
|
|
|
|
ToshibaLED I2C driver
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2016-07-11 16:44:58 -03:00
|
|
|
|
|
|
|
/* LED driver for TCA62724FMG */
|
|
|
|
|
2016-01-29 00:38:21 -04:00
|
|
|
#include "ToshibaLED_I2C.h"
|
2013-08-29 00:31:55 -03:00
|
|
|
|
2016-07-11 15:04:54 -03:00
|
|
|
#include <utility>
|
|
|
|
|
2015-08-11 03:28:44 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2013-08-29 00:31:55 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2018-06-26 14:05:10 -03:00
|
|
|
#define TOSHIBA_LED_BRIGHT 0xFF // full brightness
|
|
|
|
#define TOSHIBA_LED_MEDIUM 0x80 // medium brightness
|
|
|
|
#define TOSHIBA_LED_DIM 0x11 // dim
|
|
|
|
#define TOSHIBA_LED_OFF 0x00 // off
|
|
|
|
|
2016-04-18 14:02:19 -03:00
|
|
|
#define TOSHIBA_LED_I2C_ADDR 0x55 // default I2C bus address
|
|
|
|
|
2013-08-29 00:31:55 -03:00
|
|
|
#define TOSHIBA_LED_PWM0 0x01 // pwm0 register
|
|
|
|
#define TOSHIBA_LED_PWM1 0x02 // pwm1 register
|
|
|
|
#define TOSHIBA_LED_PWM2 0x03 // pwm2 register
|
|
|
|
#define TOSHIBA_LED_ENABLE 0x04 // enable register
|
|
|
|
|
2017-09-06 07:12:36 -03:00
|
|
|
ToshibaLED_I2C::ToshibaLED_I2C(uint8_t bus)
|
2018-06-26 14:05:10 -03:00
|
|
|
: RGBLed(TOSHIBA_LED_OFF, TOSHIBA_LED_BRIGHT, TOSHIBA_LED_MEDIUM, TOSHIBA_LED_DIM)
|
2017-09-06 07:12:36 -03:00
|
|
|
, _bus(bus)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-03-28 22:44:23 -03:00
|
|
|
bool ToshibaLED_I2C::init(void)
|
2016-04-18 14:02:19 -03:00
|
|
|
{
|
2017-01-26 05:11:37 -04:00
|
|
|
// first look for led on external bus
|
2017-09-06 07:12:36 -03:00
|
|
|
_dev = std::move(hal.i2c_mgr->get_device(_bus, TOSHIBA_LED_I2C_ADDR));
|
2018-10-11 20:35:04 -03:00
|
|
|
if (!_dev) {
|
2013-08-29 00:31:55 -03:00
|
|
|
return false;
|
|
|
|
}
|
2018-10-11 20:35:04 -03:00
|
|
|
WITH_SEMAPHORE(_dev->get_semaphore());
|
2013-08-29 00:31:55 -03:00
|
|
|
|
2017-09-06 07:12:36 -03:00
|
|
|
_dev->set_retries(10);
|
|
|
|
|
2013-08-29 00:31:55 -03:00
|
|
|
// enable the led
|
2016-04-18 14:02:19 -03:00
|
|
|
bool ret = _dev->write_register(TOSHIBA_LED_ENABLE, 0x03);
|
2017-01-26 05:11:37 -04:00
|
|
|
if (!ret) {
|
2017-09-06 07:12:36 -03:00
|
|
|
return false;
|
2017-01-26 05:11:37 -04:00
|
|
|
}
|
|
|
|
|
2013-08-29 00:31:55 -03:00
|
|
|
// update the red, green and blue values to zero
|
2016-04-18 14:02:19 -03:00
|
|
|
uint8_t val[4] = { TOSHIBA_LED_PWM0, _led_off, _led_off, _led_off };
|
2017-09-06 07:12:36 -03:00
|
|
|
ret = _dev->transfer(val, sizeof(val), nullptr, 0);
|
|
|
|
|
|
|
|
_dev->set_retries(1);
|
2013-10-21 05:27:34 -03:00
|
|
|
|
2017-09-06 07:12:36 -03:00
|
|
|
if (ret) {
|
|
|
|
_dev->register_periodic_callback(20000, FUNCTOR_BIND_MEMBER(&ToshibaLED_I2C::_timer, void));
|
|
|
|
}
|
|
|
|
|
2013-08-29 00:31:55 -03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set_rgb - set color as a combination of red, green and blue values
|
|
|
|
bool ToshibaLED_I2C::hw_set_rgb(uint8_t red, uint8_t green, uint8_t blue)
|
|
|
|
{
|
2017-01-26 05:11:37 -04:00
|
|
|
rgb = {red, green, blue};
|
2016-11-04 01:25:52 -03:00
|
|
|
_need_update = true;
|
|
|
|
return true;
|
|
|
|
}
|
2016-07-11 15:04:54 -03:00
|
|
|
|
2017-01-13 15:26:14 -04:00
|
|
|
void ToshibaLED_I2C::_timer(void)
|
2016-11-04 01:25:52 -03:00
|
|
|
{
|
|
|
|
if (!_need_update) {
|
2017-01-13 15:26:14 -04:00
|
|
|
return;
|
2016-11-04 01:25:52 -03:00
|
|
|
}
|
|
|
|
_need_update = false;
|
2017-01-13 15:26:14 -04:00
|
|
|
|
2016-07-11 15:04:54 -03:00
|
|
|
/* 4-bit for each color */
|
2016-11-04 01:25:52 -03:00
|
|
|
uint8_t val[4] = { TOSHIBA_LED_PWM0, (uint8_t)(rgb.b >> 4),
|
|
|
|
(uint8_t)(rgb.g / 16), (uint8_t)(rgb.r / 16) };
|
2016-07-11 15:04:54 -03:00
|
|
|
|
2016-11-04 01:25:52 -03:00
|
|
|
_dev->transfer(val, sizeof(val), nullptr, 0);
|
2013-08-29 00:31:55 -03:00
|
|
|
}
|