-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPCA9685.cpp
131 lines (107 loc) · 2.75 KB
/
PCA9685.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "PCA9685.h"
#include <cmath>
#include <unistd.h>
#include <jetgpio.h>
PCA9685::PCA9685(int pi, int bus, uint8_t address, int channel)
{
this->pi = pi;
this->bus = bus;
this->address = address;
handle = i2cOpen(bus, 0);
WriteReg(MODE1, AI | ALLCALL);
WriteReg(MODE2, OCH | OUTDRV);
usleep(5);
uint8_t mode = ReadReg(MODE1);
WriteReg(MODE1, mode & ~SLEEP);
usleep(5);
SetDutyCycle(-1, 0);
SetFrequency(50);
}
PCA9685::~PCA9685()
{
}
float PCA9685::GetFrequency()
{
return frequency;
}
bool PCA9685::SetFrequency(double frequency)
{
int prescale = int(round(25000000.0 / (4096.0 * frequency)) - 1);
if (prescale < 3)
{
prescale = 3;
}
else if (prescale > 255)
{
prescale = 255;
}
int mode = ReadReg(MODE1);
WriteReg(MODE1, (mode & ~SLEEP) | SLEEP);
WriteReg(PRESCALE, prescale);
WriteReg(MODE1, mode);
usleep(5);
WriteReg(MODE1, mode | RESTART);
this->frequency = (25000000.0 / 4096.0) / (prescale + 1);
pulseWidth = (1000000.0 / this->frequency);
i2cWriteByteData(handle, this->address, MODE2, SUBADR3);
return true;
}
bool PCA9685::SetDutyCycle(int channel, int usec)
{
if(500 <= usec && usec <= 2500)
{
//unsigned int pulseWidthValue = static_cast<unsigned int>(usec * 4096.0 / (1000000.0 / frequency));
unsigned int pulseWidthValue = (usec * 4096.0 / (1000000.0 / frequency));
int result = i2cWriteWordData(handle, this->address, LED0_ON_L + 4 * channel, 0);
result = i2cWriteWordData(handle, this->address, LED0_OFF_L + 4 * channel, pulseWidthValue);
}
return true;
}
bool PCA9685::SetDutyCyclePercent(int channel, float percent)
{
if (0 <= percent && percent <= 1)
{
SetDutyCycle(channel, 500 + 2000*percent);
}
//int steps = int(round(percent * (4096.0 / 100.0)));
//int on, off;
//if (steps < 0)
//{
// on = 0;
// off = 4096;
//}
//else if (steps > 4095)
//{
// on = 4096;
// off = 0;
//}
//else {
// on = 0;
// off = steps;
//}
//
//if (channel >= 0 && channel <= 15)
//{
// char buf[] = { on & 0xFF, on >> 8, off & 0xFF, off >> 8 };
// i2c_write_i2c_block_data(pi, handle, LED0_ON_L + 4 * channel, buf, 4);
// return true;
//}
//return false;
}
bool PCA9685::SetPulseWidth(int channel, float width)
{
return SetDutyCycle(channel, (width / pulseWidth) * 100.0);
}
bool PCA9685::Cancel()
{
SetDutyCycle(-1, 0);
i2cClose(handle);
}
int PCA9685::WriteReg(int reg, uint8_t byte)
{
return i2cWriteByteData(handle, this->address, reg, byte);
}
uint8_t PCA9685::ReadReg(int reg)
{
return i2cReadByteData(handle, this->address, reg);
}