-
Notifications
You must be signed in to change notification settings - Fork 6
/
TLC59108.cpp
140 lines (118 loc) · 3.58 KB
/
TLC59108.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
132
133
134
135
136
137
138
139
140
/*
* TLC59108: Arduino library to control TI TLC59108/TLC59108F/TLC59208 LED drivers
*
* (C) 2013 Christopher Smith <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
extern "C"
{
#include <inttypes.h>
}
#include "TLC59108.h"
void TLC59108::setDefaultI2C(TwoWire i2c_default)
{
TLC59108::i2c_default = i2c_default;
}
TLC59108::TLC59108(TwoWire i2c, const byte i2c_address): i2c(i2c), addr(i2c_address)
{
}
TLC59108::TLC59108(const byte i2c_address): addr(i2c_address)
{
i2c = Wire;
}
uint8_t TLC59108::setRegister(const uint8_t reg, const uint8_t value)
{
i2c.beginTransmission(addr);
i2c.write(reg);
i2c.write(value);
return i2c.endTransmission();
}
uint8_t TLC59108::setRegisters(const uint8_t startReg, const uint8_t values[], const uint8_t numValues)
{
i2c.beginTransmission(addr);
i2c.write(startReg | AUTO_INCREMENT::ALL);
for(uint8_t i = 0; i < numValues; i++)
i2c.write(values[i]);
return i2c.endTransmission();
}
int TLC59108::readRegister(const uint8_t reg) const
{
i2c.beginTransmission(addr);
i2c.write(reg);
if(!i2c.endTransmission())
return -1;
i2c.requestFrom(addr, (uint8_t) 1);
if(i2c.available())
return i2c.read();
else
return -1;
}
uint8_t TLC59108::readRegisters(uint8_t *dest, const uint8_t startReg, const uint8_t num) const {
Serial.println("in readRegisters");
i2c.beginTransmission(addr);
i2c.write(startReg | AUTO_INCREMENT::ALL);
if(i2c.endTransmission())
return 0;
uint8_t bytesRead = 0;
i2c.requestFrom(addr, num);
while(i2c.available() && (bytesRead < num)) {
(*dest) = (uint8_t) i2c.read();
dest++;
bytesRead++;
}
return bytesRead;
}
bool TLC59108::getAllBrightness(uint8_t dutyCycles[]) const {
return (readRegisters(dutyCycles, REGISTER::PWM0::ADDR, NUM_CHANNELS) == NUM_CHANNELS);
}
uint8_t TLC59108::init(const uint8_t hwResetPin)
{
if(hwResetPin)
{
pinMode(hwResetPin, OUTPUT);
digitalWrite(hwResetPin, LOW);
delay(1);
digitalWrite(hwResetPin, HIGH);
delay(1);
}
return setRegister(REGISTER::MODE1::ADDR, REGISTER::MODE1::ALLCALL);
}
uint8_t TLC59108::setBrightness(const uint8_t pwmChannel, const uint8_t dutyCycle)
{
if(pwmChannel > 7)
return ERROR::EINVAL;
return setRegister(pwmChannel + 2, dutyCycle);
}
uint8_t TLC59108::setLedOutputMode(const uint8_t outputMode)
{
if(outputMode & 0xfc)
return ERROR::EINVAL;
byte regValue = (outputMode << 6) | (outputMode << 4) | (outputMode << 2) | outputMode;
uint8_t retVal = setRegister(REGISTER::LEDOUT0::ADDR, regValue);
retVal &= setRegister(REGISTER::LEDOUT1::ADDR, regValue);
return retVal;
}
uint8_t TLC59108::setAllBrightness(const uint8_t dutyCycle)
{
i2c.beginTransmission(addr);
i2c.write(REGISTER::PWM0::ADDR | AUTO_INCREMENT::IND);
for(uint8_t i=0; i<NUM_CHANNELS; i++)
i2c.write(dutyCycle);
return i2c.endTransmission();
}
uint8_t TLC59108::setAllBrightness(const uint8_t dutyCycles[])
{
return setRegisters(REGISTER::PWM0::ADDR, dutyCycles, NUM_CHANNELS);
}