-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpca9685.h
66 lines (49 loc) · 1.74 KB
/
pca9685.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifndef PCA9685_H
#define PCA9685_H
#include <Wire.h>
/**
* A wrapper class for PCA9685 servo controller.
* It takes a I2C bus (Wire) and an address, and writes pulse width
* to servo at specified index.
*/
class PCA9685 {
private:
// LED index range, total 16 LEDs.
static const uint8_t MIN_LED_INDEX = 0;
static const uint8_t MAX_LED_INDEX = 15;
// PWM number of steps, which is 4096.
static const uint16_t PWM_STEPS = 4096;
// PCA9685 internal clock frequency, which is 25 MHz.
static const long CLOCK_FREQ = 25000000;
// Register addresses.
static const uint8_t MODE1 = 0x00;
static const uint8_t PRESCALE = 0xFE;
static const uint8_t LED0_ON_L = 0x06;
static const uint8_t LED0_ON_H = 0x07;
static const uint8_t LED0_OFF_L = 0x08;
static const uint8_t LED0_OFF_H = 0x09;
// PWM cycle in miliseconds, which usually is 20 ms to 60 ms.
static const uint8_t pwmCycle = 20;
// PWM delay in miliseconds, which should be smaller than pwmCycle.
static const uint8_t pwmDelay = 5;
// Wire pointer.
TwoWire *i2cBus;
// PCA9685 I2C address.
uint16_t address;
// Initialize controller:
// set to sleep, set prescale, set to normal.
void init();
// Calculate prescaler value:
// https://cdn-shop.adafruit.com/datasheets/PCA9685.pdf
uint8_t getPrescaler();
// Write byte value to a register.
void writeRegister(uint8_t reg, uint8_t val);
// Read byte value from a register.
uint8_t readRegister(uint8_t reg);
public:
// Constructor takes &Wire and I2C address.
PCA9685(TwoWire *bus, uint16_t addr);
// Set servo pulse width (miliseconds) at servo index.
void setServo(uint8_t idx, double pw);
};
#endif