-
Notifications
You must be signed in to change notification settings - Fork 2
/
LEDStripDriver.cpp
144 lines (130 loc) · 4.23 KB
/
LEDStripDriver.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
#include <Arduino.h>
#include "LEDStripDriver.h"
/**************************************************************************/
/*!
@brief class constructor
@param din the data pin to use
@param cin the clock pin to use
*/
/**************************************************************************/
LEDStripDriver::LEDStripDriver(uint8_t din, uint8_t cin) : _din(din), _cin(cin), _delay(20) {
pinMode(din, OUTPUT);
pinMode(cin, OUTPUT);
}
/**************************************************************************/
/*!
@brief class constructor
@param din the data pin to use
@param cin the clock pin to use
@param delay the clock delay in microseconds
*/
/**************************************************************************/
LEDStripDriver::LEDStripDriver(uint8_t din, uint8_t cin, uint16_t delay) : _din(din), _cin(cin), _delay(delay) {
pinMode(din, OUTPUT);
pinMode(cin, OUTPUT);
}
/**************************************************************************/
/*!
@brief set color from r,g,b parameter
@param red amount of red color [0-255]
@param green amount of green color [0-255]
@param blue amount of blue color [0-255]
*/
/**************************************************************************/
void LEDStripDriver::setColor(uint8_t red, uint8_t green, uint8_t blue) {
uint32_t dx = 0;
dx |= (uint32_t)0x03 << 30;
dx |= (uint32_t)getColorCode(blue) << 28;
dx |= (uint32_t)getColorCode(green) << 26;
dx |= (uint32_t)getColorCode(red) << 24;
dx |= (uint32_t)blue << 16;
dx |= (uint32_t)green << 8;
dx |= red;
senddata(dx);
}
/**************************************************************************/
/*!
@brief set color from hex parameter
@param hex hex number [0x000000-0xFFFFFF]
*/
/**************************************************************************/
void LEDStripDriver::setColor(uint32_t hex) {
uint8_t red = (hex >> 16) & 0xFF;
uint8_t green = (hex >> 8) & 0xFF;
uint8_t blue = (hex) & 0xFF;
setColor(red, green, blue);
}
/**************************************************************************/
/*!
@brief set color from hex string
@param str string of hex color, e.g. "#00FF00"
*/
/**************************************************************************/
void LEDStripDriver::setColor(String str) {
uint32_t hex = (uint32_t)strtol(&str[1], NULL, 16);
setColor(hex);
}
/**************************************************************************/
/*!
@brief set color to off
*/
/**************************************************************************/
void LEDStripDriver::setColor() {
setColor(0, 0, 0);
}
/**************************************************************************/
/*!
@brief send data over data pin
@param dx data to transmitt
*/
/**************************************************************************/
void LEDStripDriver::senddata(uint32_t dx) {
sendzero();
for (uint8_t i = 0; i < 32; i++) {
if ((dx & 0x80000000) != 0) {
digitalWrite(_din, HIGH);
} else {
digitalWrite(_din, LOW);
}
dx <<= 1;
sendclock();
}
sendzero();
}
/**************************************************************************/
/*!
@brief send a sequence of zeros
*/
/**************************************************************************/
void LEDStripDriver::sendzero() {
for (uint8_t i = 0; i < 32; i++) {
digitalWrite(_din, LOW);
sendclock();
}
}
/**************************************************************************/
/*!
@brief send one clock impulse
*/
/**************************************************************************/
void LEDStripDriver::sendclock() {
digitalWrite(_cin, LOW);
delayMicroseconds(_delay);
digitalWrite(_cin, HIGH);
delayMicroseconds(_delay);
}
/**************************************************************************/
/*!
@brief convert color to code
*/
/**************************************************************************/
uint8_t LEDStripDriver::getColorCode(uint8_t color) {
uint8_t tmp = 0;
if ((color & 0x80) == 0) {
tmp |= 0x02;
}
if ((color & 0x40) == 0) {
tmp |= 0x01;
}
return tmp;
}