GY-63_MS5611/libraries/PCA9635/README.md

216 lines
8.4 KiB
Markdown
Raw Normal View History

2021-01-29 06:31:58 -05:00
[![Arduino CI](https://github.com/RobTillaart/PCA9635/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
2021-12-23 04:41:42 -05:00
[![Arduino-lint](https://github.com/RobTillaart/PCA9635/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/PCA9635/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/PCA9635/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/PCA9635/actions/workflows/jsoncheck.yml)
2021-01-29 06:31:58 -05:00
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/PCA9635/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/PCA9635.svg?maxAge=3600)](https://github.com/RobTillaart/PCA9635/releases)
2020-11-27 05:28:57 -05:00
# PCA9635
2022-01-06 14:19:35 -05:00
Arduino library for PCA9635 I2C 8 bit PWM LED driver, 16 channel.
2020-11-27 05:28:57 -05:00
2021-01-29 06:31:58 -05:00
2021-12-23 04:41:42 -05:00
## Description
2020-11-27 05:28:57 -05:00
This library is to control the I2C PCA9635 PWM extender.
2022-01-06 14:19:35 -05:00
The 16 channels are independently configurable in steps of 1/256.
2021-12-23 04:41:42 -05:00
this allows for better than 1% fine tuning of the duty-cycle
2020-11-27 05:28:57 -05:00
of the PWM signal.
2022-05-16 12:25:10 -04:00
library is related to the 8 channel PCA9634 class.
(these might merge in the future)
2020-11-27 05:28:57 -05:00
2021-01-29 06:31:58 -05:00
## Interface
2021-12-23 04:41:42 -05:00
2021-01-29 06:31:58 -05:00
### Constructor
2020-11-27 05:28:57 -05:00
2021-12-23 04:41:42 -05:00
- **PCA9635(uint8_t deviceAddress, TwoWire \*wire = &Wire)** Constructor with I2C device address,
and optional the Wire interface as parameter.
2022-06-02 06:11:57 -04:00
- **bool begin(uint8_t mode1_mask = PCA9634_MODE1_ALLCALL, uint8_t mode2_mask = PCA9634_MODE2_NONE)**
initializes the library after startup. Optionally setting the MODE1 and MODE2 configuration registers.
See PCA9635.h and datasheet for settings possible.
2022-09-10 14:47:37 -04:00
- **bool begin(int sda, int scl, uint8_t mode1_mask = PCA9635_MODE1_ALLCALL, uint8_t mode2_mask = PCA9635_MODE2_NONE)**
2022-06-02 06:11:57 -04:00
idem, ESP32 ESP8266 only.
- **void configure(uint8_t mode1_mask, uint8_t mode2_mask)**
To configure the library after startup one can set the MODE1 and MODE2 configuration registers.
See PCA9635.h and datasheet for settings possible.
2021-12-23 04:41:42 -05:00
- **bool isConnected()** checks if address is available on I2C bus.
2022-01-06 14:19:35 -05:00
- **uint8_t channelCount()** returns the number of channels = 16.
2021-01-29 06:31:58 -05:00
2020-11-27 05:28:57 -05:00
2021-01-29 06:31:58 -05:00
### LedDriverMode
2020-11-27 05:28:57 -05:00
2022-06-02 06:11:57 -04:00
Configure LED behaviour.
2021-12-23 04:41:42 -05:00
- **uint8_t setLedDriverMode(uint8_t channel, uint8_t mode)** mode is 0..3 See datasheet for full details.
- **uint8_t getLedDriverMode(uint8_t channel)** returns the current mode of the channel.
2020-11-27 05:28:57 -05:00
2022-01-06 14:19:35 -05:00
| LED mode | Value | Description |
|:-------------------|:-----:|:----------------------------------|
| PCA9635_LEDOFF | 0x00 | led is 100% off, default @startup |
| PCA9635_LEDON | 0x01 | led is 100% on. |
| PCA9635_LEDPWM | 0x02 | set LED in PWM mode, 0..255 |
| PCA9635_LEDGRPPWM | 0x03 | add LED to the GRPPWM* |
2020-11-27 05:28:57 -05:00
2021-12-23 04:41:42 -05:00
\* all LEDs in the group GRPPWM can be set to the same PWM value in one set.
This is ideal to trigger e.g. multiple LEDs (servo's) at same time.
2020-11-27 05:28:57 -05:00
2021-01-29 06:31:58 -05:00
### Read and write
2020-11-27 05:28:57 -05:00
2022-06-02 06:11:57 -04:00
Read and write individual values to LED channels.
Requires LEDs' DriverMode of the specific channels to be in PWM mode.
2021-12-23 04:41:42 -05:00
- **uint8_t write1(uint8_t channel, uint8_t value)** writes a single 8 bit PWM value.
2022-06-02 06:11:57 -04:00
- **uint8_t write3(uint8_t channel, uint8_t R, uint8_t G, uint8_t B)**
writes three consecutive PWM registers.
2021-12-23 04:41:42 -05:00
typical use is to write R, G, B values for a full colour LED.
2022-06-02 06:11:57 -04:00
- **uint8_t writeN(uint8_t channel, uint8_t \* array, uint8_t count)**
write count consecutive PWM registers.
2020-11-27 05:28:57 -05:00
May return **PCA9635_ERR_WRITE** if array has too many elements
2021-12-23 04:41:42 -05:00
(including channel as offset).
2022-05-16 12:25:10 -04:00
### Mode registers
2022-06-02 06:11:57 -04:00
Used to configure the PCA963x general behaviour.
2021-12-23 04:41:42 -05:00
- **uint8_t writeMode(uint8_t reg, uint8_t value)** configuration of one of the two configuration registers.
2022-06-02 06:11:57 -04:00
Check datasheet for details.
2021-12-23 04:41:42 -05:00
- **uint8_t readMode(uint8_t reg)** reads back the configured mode,
useful to add or remove a single flag (bit masking).
2022-06-02 06:11:57 -04:00
- **uint8_t setMode1(uint8_t value)** convenience wrapper.
- **uint8_t setMode2(uint8_t value)** convenience wrapper.
- **uint8_t getMode1()** convenience wrapper.
- **uint8_t getMode2()** convenience wrapper.
2022-05-16 12:25:10 -04:00
#### Constants for mode registers
(added 0.3.4)
2022-06-02 06:11:57 -04:00
| Name | Value | Description |
|:------------------------|:-----:|:-----------------------------------|
| PCA9635_MODE1_AUTOINCR2 | 0x80 | Read Only, 0 = disable 1 = enable |
| PCA9635_MODE1_AUTOINCR1 | 0x40 | Read Only, bit1 |
| PCA9635_MODE1_AUTOINCR0 | 0x20 | Read Only, bit0 |
| PCA9635_MODE1_SLEEP | 0x10 | 0 = normal 1 = sleep |
| PCA9635_MODE1_SUB1 | 0x08 | 0 = disable 1 = enable |
| PCA9635_MODE1_SUB2 | 0x04 | 0 = disable 1 = enable |
| PCA9635_MODE1_SUB3 | 0x02 | 0 = disable 1 = enable |
| PCA9635_MODE1_ALLCALL | 0x01 | 0 = disable 1 = enable |
| | | |
| PCA9635_MODE2_BLINK | 0x20 | 0 = dim 1 = blink |
| PCA9635_MODE2_INVERT | 0x10 | 0 = normal 1 = inverted |
| PCA9635_MODE2_STOP | 0x08 | 0 = on STOP 1 = on ACK |
| PCA9635_MODE2_TOTEMPOLE | 0x04 | 0 = open drain 1 = totem-pole |
2022-05-16 12:25:10 -04:00
These constants makes it easier to set modes without using a non descriptive
2022-06-02 06:11:57 -04:00
bit mask. The constants can be merged by OR-ing them together, see snippet:
2022-05-16 12:25:10 -04:00
```cpp
ledArray.writeMode(PCA9635_MODE2, 0b00110100);
// would become
uint8_t mode2_mask = PCA9635_MODE2_BLINK | PCA9635_MODE2_INVERT | PCA9635_MODE2_TOTEMPOLE;
ledArray.writeMode(PCA9635_MODE2, mode2_mask);
// or even
ledArray.setMode2(PCA9635_MODE2_BLINK | PCA9635_MODE2_INVERT | PCA9635_MODE2_TOTEMPOLE);
```
2021-12-23 04:41:42 -05:00
2020-11-27 05:28:57 -05:00
2021-01-29 06:31:58 -05:00
### Group PWM and frequency
2020-11-27 05:28:57 -05:00
2022-06-02 06:11:57 -04:00
Check datasheet for the details.
2021-12-23 04:41:42 -05:00
- **void setGroupPWM(uint8_t value)** sets all channels that are part of the PWM group to value.
- **uint8_t getGroupPWM()** get the current PWM setting of the group.
2022-06-02 06:11:57 -04:00
- **void setGroupFREQ(uint8_t value)** is used for blinking the group of configured LED.
Value goes from 0 to 255 with each step representing an increase of approx. 41 ms.
So 0x00 results in 41 ms blinking period (on AND off) and 0xFF in approx. 10.5 s.
- **uint8_t getGroupFREQ()** returns the set frequency of the PWM group.
2020-11-27 05:28:57 -05:00
2021-12-23 04:41:42 -05:00
### Miscellaneous
2020-11-27 05:28:57 -05:00
2021-12-23 04:41:42 -05:00
- **int lastError()** returns **PCA9635_OK** if all is OK, and other error codes otherwise.
2020-11-27 05:28:57 -05:00
2022-06-02 06:11:57 -04:00
| Error code | Value | Description |
|:------------------|:-----:|:---------------------|
2021-12-23 04:41:42 -05:00
| PCA9635_OK | 0x00 | Everything went well
| PCA9635_ERROR | 0xFF | Generic error
| PCA9635_ERR_WRITE | 0xFE | Tries to write more elements than PWM channels
| PCA9635_ERR_CHAN | 0xFD | Channel out of range
| PCA9635_ERR_MODE | 0xFC | Invalid mode
| PCA9635_ERR_REG | 0xFB | Invalid register
2022-05-16 12:25:10 -04:00
| PCA9635_ERR_I2C | 0xFA | I2C communication error
2020-11-27 05:28:57 -05:00
2021-12-23 04:41:42 -05:00
2022-06-02 06:11:57 -04:00
### SUB CALL and ALL CALL
Please read the datasheet to understand the working of **SUB CALL** and **ALL CALL**.
Since version 0.4.0 there is (experimental) support for the **SUB CALL** and **ALL CALL** functions.
It needs more testing and if there are issues, please report.
AllCall is automatically activated for each device on startup.
2022-09-10 14:47:37 -04:00
2022-06-02 06:11:57 -04:00
#### Description
**SUB CALL** allows one to make groups of PCA9634 devices and control them on group level.
The number of groups one can make depends on free I2C addresses on one I2C bus.
Using multiple I2C buses or multiplexers will even increase the possible number.
Every PCA9634 device can be member of up to three of these groups.
To become member one needs to set the **setSubCallAddress(nr, address)** and enable
it with **enableSubCall(nr)**.
In the same way one can become member of an **ALL CALL** group.
Typically there is only one such group but one can configure more of them by applying different addresses.
2022-09-10 14:47:37 -04:00
2022-06-02 06:11:57 -04:00
#### Interface
The functions to enable all/sub-addresses are straightforward:
- **bool enableSubCall(uint8_t nr)** nr = 1,2,3
- **bool disableSubCall(uint8_t nr)** nr = 1,2,3
- **bool isEnabledSubCall(uint8_t nr)** nr = 1,2,3
- **bool setSubCallAddress(uint8_t nr, uint8_t address)**
- **uint8_t getSubCallAddress(uint8_t nr)**
- **bool enableAllCall()**
- **bool disableAllCall()**
- **bool isEnabledAllCall()**
- **bool setAllCallAddress(uint8_t address)**
- **uint8_t getAllCallAddress()**
2021-12-23 04:41:42 -05:00
## Operation
2020-11-27 05:28:57 -05:00
See examples
2021-12-23 04:41:42 -05:00
## Future
2022-11-19 10:54:49 -05:00
#### must
2021-12-23 04:41:42 -05:00
- improve documentation
2022-11-19 10:54:49 -05:00
#### should
- move code from .h to .cpp
2021-12-23 04:41:42 -05:00
- unit tests
2022-06-02 06:11:57 -04:00
- SUB CALL if possible?
- ALL CALL if possible?
2021-12-23 04:41:42 -05:00
- add examples
2022-11-19 10:54:49 -05:00
#### could
2022-06-02 06:11:57 -04:00
- sync with PCA9634 developments
2022-05-16 12:25:10 -04:00
- merge with PCA9634 and a PCA963X base class if possible
2021-12-23 04:41:42 -05:00