1 | /*
|
---|
2 | * Copyright (C) 2012 Paulo Marques (pjp.marques@gmail.com)
|
---|
3 | *
|
---|
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of
|
---|
5 | * this software and associated documentation files (the "Software"), to deal in
|
---|
6 | * the Software without restriction, including without limitation the rights to
|
---|
7 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
---|
8 | * the Software, and to permit persons to whom the Software is furnished to do so,
|
---|
9 | * subject to the following conditions:
|
---|
10 | *
|
---|
11 | * The above copyright notice and this permission notice shall be included in all
|
---|
12 | * copies or substantial portions of the Software.
|
---|
13 | *
|
---|
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
---|
16 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
---|
17 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
---|
18 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
---|
19 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /* Information about the P9813 protocol obtained from:
|
---|
23 | * http://www.seeedstudio.com/wiki/index.php?title=Twig_-_Chainable_RGB_LED
|
---|
24 | *
|
---|
25 | * HSB to RGB routine adapted from:
|
---|
26 | * http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
|
---|
27 | */
|
---|
28 |
|
---|
29 |
|
---|
30 | // --------------------------------------------------------------------------------------
|
---|
31 |
|
---|
32 | #include "ChainableLED.h"
|
---|
33 |
|
---|
34 | // Forward declaration
|
---|
35 | float hue2rgb(float p, float q, float t);
|
---|
36 |
|
---|
37 | // --------------------------------------------------------------------------------------
|
---|
38 |
|
---|
39 | ChainableLED::ChainableLED(byte clk_pin, byte data_pin, byte number_of_leds) :
|
---|
40 | _clk_pin(clk_pin), _data_pin(data_pin), _num_leds(number_of_leds)
|
---|
41 | {
|
---|
42 | _led_state = (byte*) calloc(_num_leds*3, sizeof(byte));
|
---|
43 | }
|
---|
44 |
|
---|
45 | ChainableLED::~ChainableLED()
|
---|
46 | {
|
---|
47 | free(_led_state);
|
---|
48 | }
|
---|
49 |
|
---|
50 | // --------------------------------------------------------------------------------------
|
---|
51 |
|
---|
52 | void ChainableLED::init()
|
---|
53 | {
|
---|
54 | pinMode(_clk_pin, OUTPUT);
|
---|
55 | pinMode(_data_pin, OUTPUT);
|
---|
56 |
|
---|
57 | for (byte i=0; i<_num_leds; i++)
|
---|
58 | setColorRGB(i, 0, 0, 0);
|
---|
59 | }
|
---|
60 |
|
---|
61 | void ChainableLED::clk(void)
|
---|
62 | {
|
---|
63 | digitalWrite(_clk_pin, LOW);
|
---|
64 | delayMicroseconds(_CLK_PULSE_DELAY);
|
---|
65 | digitalWrite(_clk_pin, HIGH);
|
---|
66 | delayMicroseconds(_CLK_PULSE_DELAY);
|
---|
67 | }
|
---|
68 |
|
---|
69 | void ChainableLED::sendByte(byte b)
|
---|
70 | {
|
---|
71 | // Send one bit at a time, starting with the MSB
|
---|
72 | for (byte i=0; i<8; i++)
|
---|
73 | {
|
---|
74 | // If MSB is 1, write one and clock it, else write 0 and clock
|
---|
75 | if ((b & 0x80) != 0)
|
---|
76 | digitalWrite(_data_pin, HIGH);
|
---|
77 | else
|
---|
78 | digitalWrite(_data_pin, LOW);
|
---|
79 | clk();
|
---|
80 |
|
---|
81 | // Advance to the next bit to send
|
---|
82 | b <<= 1;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | void ChainableLED::sendColor(byte red, byte green, byte blue)
|
---|
87 | {
|
---|
88 | // Start by sending a byte with the format "1 1 /B7 /B6 /G7 /G6 /R7 /R6"
|
---|
89 | byte prefix = 0b11000000;
|
---|
90 | if ((blue & 0x80) == 0) prefix|= 0b00100000;
|
---|
91 | if ((blue & 0x40) == 0) prefix|= 0b00010000;
|
---|
92 | if ((green & 0x80) == 0) prefix|= 0b00001000;
|
---|
93 | if ((green & 0x40) == 0) prefix|= 0b00000100;
|
---|
94 | if ((red & 0x80) == 0) prefix|= 0b00000010;
|
---|
95 | if ((red & 0x40) == 0) prefix|= 0b00000001;
|
---|
96 | sendByte(prefix);
|
---|
97 |
|
---|
98 | // Now must send the 3 colors
|
---|
99 | sendByte(blue);
|
---|
100 | sendByte(green);
|
---|
101 | sendByte(red);
|
---|
102 | }
|
---|
103 |
|
---|
104 | void ChainableLED::setColorRGB(byte led, byte red, byte green, byte blue)
|
---|
105 | {
|
---|
106 | // Send data frame prefix (32x "0")
|
---|
107 | sendByte(0x00);
|
---|
108 | sendByte(0x00);
|
---|
109 | sendByte(0x00);
|
---|
110 | sendByte(0x00);
|
---|
111 |
|
---|
112 | // Send color data for each one of the leds
|
---|
113 | for (byte i=0; i<_num_leds; i++)
|
---|
114 | {
|
---|
115 | if (i == led)
|
---|
116 | {
|
---|
117 | _led_state[i*3 + _CL_RED] = red;
|
---|
118 | _led_state[i*3 + _CL_GREEN] = green;
|
---|
119 | _led_state[i*3 + _CL_BLUE] = blue;
|
---|
120 | }
|
---|
121 |
|
---|
122 | sendColor(_led_state[i*3 + _CL_RED],
|
---|
123 | _led_state[i*3 + _CL_GREEN],
|
---|
124 | _led_state[i*3 + _CL_BLUE]);
|
---|
125 | }
|
---|
126 |
|
---|
127 | // Terminate data frame (32x "0")
|
---|
128 | sendByte(0x00);
|
---|
129 | sendByte(0x00);
|
---|
130 | sendByte(0x00);
|
---|
131 | sendByte(0x00);
|
---|
132 | }
|
---|
133 |
|
---|
134 | void ChainableLED::setColorHSB(byte led, float hue, float saturation, float brightness)
|
---|
135 | {
|
---|
136 | float r, g, b;
|
---|
137 |
|
---|
138 | constrain(hue, 0.0, 1.0);
|
---|
139 | constrain(saturation, 0.0, 1.0);
|
---|
140 | constrain(brightness, 0.0, 1.0);
|
---|
141 |
|
---|
142 | if(saturation == 0.0)
|
---|
143 | {
|
---|
144 | r = g = b = brightness;
|
---|
145 | }
|
---|
146 | else
|
---|
147 | {
|
---|
148 | float q = brightness < 0.5 ?
|
---|
149 | brightness * (1.0 + saturation) : brightness + saturation - brightness * saturation;
|
---|
150 | float p = 2.0 * brightness - q;
|
---|
151 | r = hue2rgb(p, q, hue + 1.0/3.0);
|
---|
152 | g = hue2rgb(p, q, hue);
|
---|
153 | b = hue2rgb(p, q, hue - 1.0/3.0);
|
---|
154 | }
|
---|
155 |
|
---|
156 | setColorRGB(led, (byte)(255.0*r), (byte)(255.0*g), (byte)(255.0*b));
|
---|
157 | }
|
---|
158 |
|
---|
159 | // --------------------------------------------------------------------------------------
|
---|
160 |
|
---|
161 | float hue2rgb(float p, float q, float t)
|
---|
162 | {
|
---|
163 | if (t < 0.0)
|
---|
164 | t += 1.0;
|
---|
165 | if(t > 1.0)
|
---|
166 | t -= 1.0;
|
---|
167 | if(t < 1.0/6.0)
|
---|
168 | return p + (q - p) * 6.0 * t;
|
---|
169 | if(t < 1.0/2.0)
|
---|
170 | return q;
|
---|
171 | if(t < 2.0/3.0)
|
---|
172 | return p + (q - p) * (2.0/3.0 - t) * 6.0;
|
---|
173 |
|
---|
174 | return p;
|
---|
175 | }
|
---|