APM:Libraries
ToshibaLED_I2C.cpp
Go to the documentation of this file.
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 #include <utility>
24 
25 #include <AP_HAL/AP_HAL.h>
26 
27 extern const AP_HAL::HAL& hal;
28 
29 #define TOSHIBA_LED_I2C_ADDR 0x55 // default I2C bus address
30 
31 #define TOSHIBA_LED_PWM0 0x01 // pwm0 register
32 #define TOSHIBA_LED_PWM1 0x02 // pwm1 register
33 #define TOSHIBA_LED_PWM2 0x03 // pwm2 register
34 #define TOSHIBA_LED_ENABLE 0x04 // enable register
35 
37  : ToshibaLED()
38  , _bus(bus)
39 {
40 }
41 
43 {
44  // first look for led on external bus
45  _dev = std::move(hal.i2c_mgr->get_device(_bus, TOSHIBA_LED_I2C_ADDR));
47  return false;
48  }
49 
50  _dev->set_retries(10);
51 
52  // enable the led
53  bool ret = _dev->write_register(TOSHIBA_LED_ENABLE, 0x03);
54  if (!ret) {
55  _dev->get_semaphore()->give();
56  return false;
57  }
58 
59  // update the red, green and blue values to zero
60  uint8_t val[4] = { TOSHIBA_LED_PWM0, _led_off, _led_off, _led_off };
61  ret = _dev->transfer(val, sizeof(val), nullptr, 0);
62 
63  _dev->set_retries(1);
64 
65  // give back i2c semaphore
66  _dev->get_semaphore()->give();
67 
68  if (ret) {
70  }
71 
72  return ret;
73 }
74 
75 // set_rgb - set color as a combination of red, green and blue values
76 bool ToshibaLED_I2C::hw_set_rgb(uint8_t red, uint8_t green, uint8_t blue)
77 {
78  rgb = {red, green, blue};
79  _need_update = true;
80  return true;
81 }
82 
84 {
85  if (!_need_update) {
86  return;
87  }
88  _need_update = false;
89 
90  /* 4-bit for each color */
91  uint8_t val[4] = { TOSHIBA_LED_PWM0, (uint8_t)(rgb.b >> 4),
92  (uint8_t)(rgb.g / 16), (uint8_t)(rgb.r / 16) };
93 
94  _dev->transfer(val, sizeof(val), nullptr, 0);
95 }
struct ToshibaLED_I2C::@158 rgb
virtual Device::PeriodicHandle register_periodic_callback(uint32_t period_usec, Device::PeriodicCb) override=0
void _timer(void)
virtual void set_retries(uint8_t retries)
Definition: Device.h:260
virtual Semaphore * get_semaphore() override=0
AP_HAL::OwnPtr< AP_HAL::I2CDevice > _dev
#define HAL_SEMAPHORE_BLOCK_FOREVER
Definition: Semaphores.h:5
virtual bool take(uint32_t timeout_ms) WARN_IF_UNUSED=0
uint8_t _led_off
Definition: RGBLed.h:61
#define TOSHIBA_LED_ENABLE
bool hw_set_rgb(uint8_t r, uint8_t g, uint8_t b) override
virtual bool transfer(const uint8_t *send, uint32_t send_len, uint8_t *recv, uint32_t recv_len) override=0
#define TOSHIBA_LED_I2C_ADDR
#define TOSHIBA_LED_PWM0
virtual bool give()=0
const AP_HAL::HAL & hal
-*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
Definition: AC_PID_test.cpp:14
AP_HAL::I2CDeviceManager * i2c_mgr
Definition: HAL.h:106
ToshibaLED_I2C(uint8_t bus)
bool hw_init(void) override
#define FUNCTOR_BIND_MEMBER(func, rettype,...)
Definition: functor.h:31
virtual OwnPtr< AP_HAL::I2CDevice > get_device(uint8_t bus, uint8_t address, uint32_t bus_clock=400000, bool use_smbus=false, uint32_t timeout_ms=4)=0
bool write_register(uint8_t reg, uint8_t val, bool checked=false)
Definition: Device.h:125