forked from arduino/ArduinoCore-mbed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRGBled.cpp
167 lines (138 loc) · 3.9 KB
/
RGBled.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* 12/23/2017 Copyright Tlera Corporation
*
* Created by Kris Winer
*
This basic sketch is to operate the IS31FL3194 3-channel led driver
http://www.issi.com/WW/pdf/IS31FL3194.pdf
The sketch uses default SDA/SCL pins on the Ladybug development board
but should work with almost any Arduino-based board.
Library may be used freely and without limit only with attribution.
*/
#include <mbed.h>
#include <I2C.h>
#include "RGBled.h"
#include "Nicla_System.h"
void RGBled::begin()
{
reset();
init();
powerUp();
}
void RGBled::end()
{
powerDown();
}
void RGBled::setColor(RGBColors color)
{
if(color == off) {
_blue = 0x00;
_green = 0x00;
_red = 0x00;
}
if(color == green) {
_blue = 0x00;
_green = 0xFF;
_red = 0x00;
}
if(color == blue) {
_blue = 0xFF;
_green = 0x00;
_red = 0x00;
}
if(color == red) {
_blue = 0x00;
_green = 0x00;
_red = 0xFF;
}
if(color == cyan) {
_blue = 0x20;
_green = 0x20;
_red = 0x00;
}
if(color == magenta) {
_blue = 0x20;
_green = 0x00;
_red = 0x20;
}
if(color == yellow) {
_blue = 0x00;
_green = 0x20;
_red = 0x20;
}
setColor(_red, _green, _blue);
}
void RGBled::setColorBlue(uint8_t blue) {
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT1, blue >> scale_factor);
writeByte(IS31FL3194_ADDRESS, IS31FL3194_COLOR_UPDATE, 0xC5);
}
void RGBled::setColorRed(uint8_t red) {
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT3, red >> scale_factor);
writeByte(IS31FL3194_ADDRESS, IS31FL3194_COLOR_UPDATE, 0xC5);
}
void RGBled::setColorGreen(uint8_t green) {
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT2, green >> scale_factor);
writeByte(IS31FL3194_ADDRESS, IS31FL3194_COLOR_UPDATE, 0xC5);
}
void RGBled::setColor(uint8_t red, uint8_t green, uint8_t blue)
{
// set rgb led current
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT1, blue >> scale_factor); //maximum current
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT2, green >> scale_factor);
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT3, red >> scale_factor);
writeByte(IS31FL3194_ADDRESS, IS31FL3194_COLOR_UPDATE, 0xC5); // write to color update register for changes to take effect
}
// Read the Chip ID register, this is a good test of communication
uint8_t RGBled::getChipID()
{
uint8_t c = readByte(IS31FL3194_ADDRESS, IS31FL3194_PRODUCT_ID); // Read PRODUCT_ID register for IS31FL3194
return c;
}
void RGBled::reset()
{
writeByte(IS31FL3194_ADDRESS, IS31FL3194_RESET, 0xC5);
}
void RGBled::powerDown()
{
uint8_t d = readByte(IS31FL3194_ADDRESS, IS31FL3194_OP_CONFIG);
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OP_CONFIG, d & ~(0x01)); //clear bit 0 to shut down
}
void RGBled::powerUp()
{
uint8_t d = readByte(IS31FL3194_ADDRESS, IS31FL3194_OP_CONFIG);
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OP_CONFIG, d | 0x01); //set bit 0 to enable
}
void RGBled::init()// configure rgb led function
{
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OP_CONFIG, 0x01); // normal operation in current mode
writeByte(IS31FL3194_ADDRESS, IS31FL3194_OUT_CONFIG, 0x07); // enable all three ouputs
writeByte(IS31FL3194_ADDRESS, IS31FL3194_CURRENT_BAND, 0x00); // 10 mA max current
writeByte(IS31FL3194_ADDRESS, IS31FL3194_HOLD_FUNCTION, 0x00); // hold function disable
}
void RGBled::ledBlink(RGBColors color, uint32_t duration)
{
setColor(color);
delay(duration);
setColor(off);
}
void RGBled::writeByte(uint8_t address, uint8_t subAddress, uint8_t data)
{
nicla::i2c_mutex.lock();
Wire1.beginTransmission(address);
Wire1.write(subAddress);
Wire1.write(data);
Wire1.endTransmission();
nicla::i2c_mutex.unlock();
}
uint8_t RGBled::readByte(uint8_t address, uint8_t subAddress)
{
nicla::i2c_mutex.lock();
//char response = 0xFF;
Wire1.beginTransmission(address);
Wire1.write(subAddress);
Wire1.endTransmission(false);
Wire1.requestFrom(address, 1);
while(!Wire1.available()) {}
uint8_t ret = Wire1.read();
nicla::i2c_mutex.unlock();
return ret;
}