forked from PaulPetring/Pentatube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPentatube.cpp
98 lines (80 loc) · 1.85 KB
/
Pentatube.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
#include "./Pentatube.h"
void Pentatube::show(int duration) {
unsigned long start = millis();
while (start + duration > millis())
{
byte r = 0;
byte g = 0;
byte b = 0;
uint8_t rgb[3];
for (int i = 0; i <= 7; i++) {
r = r << 1;
g = g << 1;
b = b << 1;
//TODO more even distribution
if (_colors[i][0] > _c) {
r++;
}
if (_colors[i][1] > _c) {
g++;
}
if (_colors[i][2] > _c) {
b++;
}
}
rgb[0] = b;
rgb[1] = g;
rgb[2] = r;
SPI.transfer(rgb, 3);
if (_c++ > 255) {
_c = 0;
}
}
}
Pentatube::Pentatube() {
Pentatube(4, 18, 5, 23);
}
Pentatube::Pentatube(int SRCLR, int SRCLK, int RCLK, int SERIN) {
_SRCLR = SRCLR;
_SRCLK = SRCLK;
_RCLK = RCLK;
_SERIN = SERIN;
}
Pentatube::~Pentatube() {
//bla
}
void Pentatube::begin() {
pinMode (_SRCLR, OUTPUT);
//pinMode (RCLK, OUTPUT);
SPI.begin(_SRCLK, 24, _SERIN, _RCLK);
SPI.setDataMode(SPI_MODE0);
SPI.setBitOrder(MSBFIRST);
SPI.setHwCs(true);
SPI.setFrequency(79999999); //80MhZ
//slower SPI transfer by a devider
//SPI.setClockDivider(SPI_CLOCK_DIV128);
digitalWrite (_SRCLR, HIGH);
}
void Pentatube::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b) {
_colors[n][0] = r;
_colors[n][1] = g;
_colors[n][2] = b;
}
void Pentatube::setPixelColor(uint16_t n, uint32_t c) {
_colors[n][0] = (uint8_t)(c >> 16),
_colors[n][1] = (uint8_t)(c >> 8),
_colors[n][2] = (uint8_t)c;
}
uint32_t Pentatube::Color(uint8_t r, uint8_t g, uint8_t b) {
return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
}
int Pentatube::numPixels() {
return 8;
}
void Pentatube::clear() {
for (uint16_t i = 0; i < Pentatube::numPixels(); i++) {
_colors[i][0] = 0;
_colors[i][1] = 0;
_colors[i][2] = 0;
}
}