APM:Libraries
NavioLED_I2C.cpp
Go to the documentation of this file.
1 /*
2  NavioLED 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 #include "NavioLED_I2C.h"
19 
20 #include <AP_HAL/AP_HAL.h>
21 
22 #define PCA9685_ADDRESS 0x40
23 #define PCA9685_PWM 0x6
24 
25 extern const AP_HAL::HAL& hal;
26 
28 {
30 
31  if (!_dev) {
32  return false;
33  }
34 
36 
37  return true;
38 }
39 
40 // set_rgb - set color as a combination of red, green and blue values
41 bool NavioLED_I2C::hw_set_rgb(uint8_t red, uint8_t green, uint8_t blue)
42 {
43  rgb.r = red;
44  rgb.g = green;
45  rgb.b = blue;
46  _need_update = true;
47  return true;
48 }
49 
51 {
52  if (!_need_update) {
53  return;
54  }
55  _need_update = false;
56 
57  uint16_t red_adjusted = rgb.r * 0x10;
58  uint16_t green_adjusted = rgb.g * 0x10;
59  uint16_t blue_adjusted = rgb.b * 0x10;
60 
61  uint8_t blue_channel_lsb = blue_adjusted & 0xFF;
62  uint8_t blue_channel_msb = blue_adjusted >> 8;
63 
64  uint8_t green_channel_lsb = green_adjusted & 0xFF;
65  uint8_t green_channel_msb = green_adjusted >> 8;
66 
67  uint8_t red_channel_lsb = red_adjusted & 0xFF;
68  uint8_t red_channel_msb = red_adjusted >> 8;
69 
70 
71  uint8_t transaction[] = {PCA9685_PWM, 0x00, 0x00, blue_channel_lsb, blue_channel_msb,
72  0x00, 0x00, green_channel_lsb, green_channel_msb,
73  0x00, 0x00, red_channel_lsb, red_channel_msb};
74 
75  _dev->transfer(transaction, sizeof(transaction), nullptr, 0);
76 }
virtual Device::PeriodicHandle register_periodic_callback(uint32_t period_usec, Device::PeriodicCb) override=0
struct NavioLED_I2C::@156 rgb
void _timer(void)
bool hw_init(void) override
bool hw_set_rgb(uint8_t r, uint8_t g, uint8_t b) override
AP_HAL::OwnPtr< AP_HAL::I2CDevice > _dev
Definition: NavioLED_I2C.h:29
virtual bool transfer(const uint8_t *send, uint32_t send_len, uint8_t *recv, uint32_t recv_len) override=0
AP_HAL::I2CDeviceManager * i2c_mgr
Definition: HAL.h:106
bool _need_update
Definition: NavioLED_I2C.h:34
#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