upm  0.5.1
Sensor/Actuator repository for libmraa (v0.9.1)
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
pca9685.h
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2015 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <string>
27 #include <mraa/i2c.h>
28 #include <mraa/gpio.h>
29 
30 #define PCA9685_I2C_BUS 0
31 #define PCA9685_DEFAULT_I2C_ADDR 0x60
32 // internal oscillator frequency
33 #define PCA9685_INTERNAL_OSC 25000000.0
34 
35 // This is a 'special' LED number, used to refer to the ALL_LED registers
36 // that affect all LED outputs at once.
37 #define PCA9685_ALL_LED 0xff
38 
39 namespace upm {
40 
66  class PCA9685 {
67  public:
68 
72  typedef enum { REG_MODE1 = 0x00,
73  REG_MODE2 = 0x01,
74  REG_I2C_SA1 = 0x02, // I2C subaddress 1
75  REG_I2C_SA2 = 0x03,
76  REG_I2C_SA3 = 0x04,
77  REG_ALLCALL = 0x05, // I2C all call address
78 
79  // LED output PWM control
80  REG_LED0_ON_L = 0x06, // LED0 ON low byte
81  REG_LED0_ON_H = 0x07, // LED0 ON high byte
82  REG_LED0_OFF_L = 0x08, // LED0 OFF low byte
83  REG_LED0_OFF_H = 0x09, // LED0 OFF high byte
84  REG_LED1_ON_L = 0x0a,
85  REG_LED1_ON_H = 0x0b,
86  REG_LED1_OFF_L = 0x0c,
87  REG_LED1_OFF_H = 0x0d,
88  REG_LED2_ON_L = 0x0e,
89  REG_LED2_ON_H = 0x0f,
90  REG_LED2_OFF_L = 0x10,
91  REG_LED2_OFF_H = 0x11,
92  REG_LED3_ON_L = 0x12,
93  REG_LED3_ON_H = 0x13,
94  REG_LED3_OFF_L = 0x14,
95  REG_LED3_OFF_H = 0x15,
96  REG_LED4_ON_L = 0x16,
97  REG_LED4_ON_H = 0x17,
98  REG_LED4_OFF_L = 0x18,
99  REG_LED4_OFF_H = 0x19,
100  REG_LED5_ON_L = 0x1a,
101  REG_LED5_ON_H = 0x1b,
102  REG_LED5_OFF_L = 0x1c,
103  REG_LED5_OFF_H = 0x1d,
104  REG_LED6_ON_L = 0x1e,
105  REG_LED6_ON_H = 0x1f,
106  REG_LED6_OFF_L = 0x20,
107  REG_LED6_OFF_H = 0x21,
108  REG_LED7_ON_L = 0x22,
109  REG_LED7_ON_H = 0x23,
110  REG_LED7_OFF_L = 0x24,
111  REG_LED7_OFF_H = 0x25,
112  REG_LED8_ON_L = 0x26,
113  REG_LED8_ON_H = 0x27,
114  REG_LED8_OFF_L = 0x28,
115  REG_LED8_OFF_H = 0x29,
116  REG_LED9_ON_L = 0x2a,
117  REG_LED9_ON_H = 0x2b,
118  REG_LED9_OFF_L = 0x2c,
119  REG_LED9_OFF_H = 0x2d,
120  REG_LED10_ON_L = 0x2e,
121  REG_LED10_ON_H = 0x2f,
122  REG_LED10_OFF_L = 0x30,
123  REG_LED10_OFF_H = 0x31,
124  REG_LED11_ON_L = 0x32,
125  REG_LED11_ON_H = 0x33,
126  REG_LED11_OFF_L = 0x34,
127  REG_LED11_OFF_H = 0x35,
128  REG_LED12_ON_L = 0x36,
129  REG_LED12_ON_H = 0x37,
130  REG_LED12_OFF_L = 0x38,
131  REG_LED12_OFF_H = 0x39,
132  REG_LED13_ON_L = 0x3a,
133  REG_LED13_ON_H = 0x3b,
134  REG_LED13_OFF_L = 0x3c,
135  REG_LED13_OFF_H = 0x3d,
136  REG_LED14_ON_L = 0x3e,
137  REG_LED14_ON_H = 0x3f,
138  REG_LED14_OFF_L = 0x40,
139  REG_LED14_OFF_H = 0x41,
140  REG_LED15_ON_L = 0x42,
141  REG_LED15_ON_H = 0x43,
142  REG_LED15_OFF_L = 0x44,
143  REG_LED15_OFF_H = 0x45,
144  // 0x46-0xf9 reserved
145 
146  REG_ALL_LED_ON_L = 0xfa, // write all LED ON L
147  REG_ALL_LED_ON_H = 0xfb, // write all LED ON H
148  REG_ALL_LED_OFF_L = 0xfc, // write all LED OFF L
149  REG_ALL_LED_OFF_H = 0xfd, // write all LED OFF H
150  REG_PRESCALE = 0xfe,
151  REG_TESTMODE = 0xff // don't use
152  } PCA9685_REG_T;
153 
157  typedef enum { MODE1_ALL_CALL = 0x01, // all call status
158  MODE1_SUB3 = 0x02, // subcall 3 status
159  MODE1_SUB2 = 0x04, // subcall 2 status
160  MODE1_SUB1 = 0x08, // subcall 1 status
161  MODE1_SLEEP = 0x10, // sleep/normal mode
162  MODE1_AI = 0x20, // auto-increment enable
163  MODE1_EXTCLK = 0x40, // external clock enable
164  MODE1_RESTART = 0x80 // restart status
165  } PCA9685_MODE1_T;
166 
170  typedef enum { MODE2_OUTNE0 = 0x01, // output driver enable bit 0
171  MODE2_OUTNE = 0x02, // output driver enable bit 1
172  MODE2_OUTDRV = 0x04, // output open-drain/totem pole
173  MODE2_OCH = 0x08, // output change on STOP or ACK
174  MODE2_INVRT = 0x10, // output logic state invert
175  MODE2_RESERVE0 = 0x20, // reserved
176  MODE2_RESERVE1 = 0x40, // reserved
177  MODE2_RESERVE2 = 0x80 // reserved
178  } PCA9685_MODE2_T;
179 
186  PCA9685(int bus, uint8_t address = PCA9685_DEFAULT_I2C_ADDR, bool raw = false);
187 
191  ~PCA9685();
192 
200  bool writeByte(uint8_t reg, uint8_t byte);
201 
210  bool writeWord(uint8_t reg, uint16_t word);
211 
218  uint8_t readByte(uint8_t reg);
219 
227  uint16_t readWord(uint8_t reg);
228 
236  bool setModeSleep(bool sleep);
237 
245  bool ledFullOn(uint8_t led, bool val);
246 
255  bool ledFullOff(uint8_t led, bool val);
256 
264  bool ledOnTime(uint8_t led, uint16_t time);
265 
273  bool ledOffTime(uint8_t led, uint16_t time);
274 
283  bool setPrescale(uint8_t prescale);
284 
293  bool setPrescaleFromHz(float hz,
294  float oscFreq=PCA9685_INTERNAL_OSC);
295 
302  void enableRestart(bool enabled) { m_restartEnabled = enabled; };
303 
304  private:
312  bool enableAutoIncrement(bool ai);
313 
314  bool m_restartEnabled;
315  mraa_i2c_context m_i2c;
316  uint8_t m_addr;
317  };
318 }
319 
320 
bool ledOffTime(uint8_t led, uint16_t time)
Definition: pca9685.cxx:250
uint16_t readWord(uint8_t reg)
Definition: pca9685.cxx:113
PCA9685_MODE1_T
Definition: pca9685.h:157
bool ledFullOn(uint8_t led, bool val)
Definition: pca9685.cxx:161
PCA9685_MODE2_T
Definition: pca9685.h:170
void enableRestart(bool enabled)
Definition: pca9685.h:302
bool setPrescaleFromHz(float hz, float oscFreq=PCA9685_INTERNAL_OSC)
Definition: pca9685.cxx:289
uint8_t readByte(uint8_t reg)
Definition: pca9685.cxx:108
bool ledOnTime(uint8_t led, uint16_t time)
Definition: pca9685.cxx:217
bool writeByte(uint8_t reg, uint8_t byte)
Definition: pca9685.cxx:80
API for the PCA9685 16-channel, 12-bit PWM LED Controller.
Definition: pca9685.h:66
bool setPrescale(uint8_t prescale)
Definition: pca9685.cxx:283
bool writeWord(uint8_t reg, uint16_t word)
Definition: pca9685.cxx:94
PCA9685(int bus, uint8_t address=PCA9685_DEFAULT_I2C_ADDR, bool raw=false)
Definition: pca9685.cxx:37
bool ledFullOff(uint8_t led, bool val)
Definition: pca9685.cxx:189
bool setModeSleep(bool sleep)
Definition: pca9685.cxx:118
~PCA9685()
Definition: pca9685.cxx:74
PCA9685_REG_T
Definition: pca9685.h:72