Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit 57078da

Browse files
committed
Updated and addition of more tests for ws2812b control
1 parent 2de5308 commit 57078da

File tree

4 files changed

+269
-10
lines changed

4 files changed

+269
-10
lines changed

test/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,6 @@ if(NOT APPLE AND UNIX)
6666
include_directories(/usr/include)
6767
add_executable(test_uartHighSpeed TestUartHighSpeed.cpp)
6868

69-
add_executable(test_nonInvWs2812b TestNonInvWs2812b.cpp)
69+
add_executable(test_nonUniformWs2812b TestNonUniformWs2812b.cpp)
70+
add_executable(test_nonInvWs2812b TestNonInvWs2812b.cpp)
7071
endif()

test/TestNonInvWs2812b.cpp

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
#include <vector>
55
#include <iostream>
66

7+
#include <unistd.h> //Used for UART
8+
#include <fcntl.h> //Used for UART
9+
#include <termios.h> //Used for UART
10+
#include <sys/ioctl.h>
11+
712
std::vector<uint8_t> encode(const std::vector<uint8_t> & data);
813
void split(const uint8_t byte, uint8_t & out1, uint8_t & out2);
914
uint8_t encode(const bool bit1, const bool bit2, const bool bit3);
@@ -23,10 +28,41 @@ void print(uint8_t byte)
2328
}
2429
}
2530

26-
int main()
31+
void printClockSignal(const std::vector<uint8_t> & signal)
2732
{
28-
std::vector<uint8_t> data(3, 0x55);
33+
bool prevBit = true;
34+
bool nextBit = true;
35+
36+
for (uint8_t byte : signal)
37+
{
2938

39+
for (int i=-1; i<9; ++i)
40+
{
41+
if (i == -1) // Start bit
42+
nextBit = false;
43+
else if (i == 8) // Stop bit
44+
nextBit = true;
45+
else
46+
nextBit = byte & (1 << i);
47+
48+
if (!prevBit && nextBit)
49+
{
50+
std::cout << ' ';
51+
}
52+
53+
if (nextBit)
54+
std::cout << '1';
55+
else
56+
std::cout << '0';
57+
58+
prevBit = nextBit;
59+
}
60+
}
61+
}
62+
63+
int main()
64+
{
65+
const std::vector<uint8_t> data(9, 0xff);
3066
std::vector<uint8_t> encData = encode(data);
3167

3268
for (uint8_t encByte : encData)
@@ -36,7 +72,45 @@ int main()
3672
std::cout << " 1";
3773
}
3874
std::cout << std::endl;
75+
printClockSignal(encData);
76+
std::cout << std::endl;
3977

78+
//OPEN THE UART
79+
// int uart0_filestream = open("/dev/ttyAMA0", O_WRONLY | O_NOCTTY | O_NDELAY);
80+
int uart0_filestream = open("/dev/ttyUSB0", O_WRONLY | O_NOCTTY | O_NDELAY);
81+
if (uart0_filestream == -1)
82+
{
83+
//ERROR - CAN'T OPEN SERIAL PORT
84+
printf("Error - Unable to open UART. Ensure it is not in use by another application\n");
85+
return -1;
86+
}
87+
88+
// Configure the port
89+
struct termios options;
90+
tcgetattr(uart0_filestream, &options);
91+
options.c_cflag = B4000000 | CS8 | CLOCAL;
92+
options.c_iflag = IGNPAR;
93+
options.c_oflag = 0;
94+
options.c_lflag = 0;
95+
96+
tcflush(uart0_filestream, TCIFLUSH);
97+
tcsetattr(uart0_filestream, TCSANOW, &options);
98+
99+
char c = getchar();
100+
101+
const int breakLength_ms = 1;
102+
103+
encData = std::vector<uint8_t>(128, 0x10);
104+
105+
write(uart0_filestream, encData.data(), encData.size());
106+
107+
tcsendbreak(uart0_filestream, breakLength_ms);
108+
109+
//tcdrain(uart0_filestream);
110+
// res = write(uart0_filestream, encData.data(), encData.size());
111+
// (void)res;
112+
113+
close(uart0_filestream);
40114

41115
return 0;
42116
}
@@ -96,19 +170,16 @@ std::vector<uint8_t> encode(const std::vector<uint8_t> & data)
96170
previousByte = nextByte;
97171
}
98172

173+
result.push_back(previousByte);
174+
175+
99176
return result;
100177
}
101178

102179
void split(const uint8_t byte, uint8_t & out1, uint8_t & out2)
103180
{
104-
print(byte); std::cout << " => ";
105-
print(out2); std::cout << " => ";
106-
out1 &= ~0x0F;
107181
out1 |= (byte & 0x0F) << 4;
108-
// out2 &= ~0xF0;
109-
print(out2); std::cout << " => ";
110182
out2 = (byte & 0xF0) >> 4;
111-
print(out2); std::cout << std::endl;
112183
}
113184

114185
uint8_t encode(const bool bit1, const bool bit2, const bool bit3)

test/TestNonUniformWs2812b.cpp

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
2+
// STL includes
3+
#include <cstdint>
4+
#include <vector>
5+
#include <iostream>
6+
7+
#include <unistd.h> //Used for UART
8+
#include <fcntl.h> //Used for UART
9+
#include <termios.h> //Used for UART
10+
#include <sys/ioctl.h>
11+
12+
std::vector<uint8_t> encode(const std::vector<uint8_t> & data);
13+
uint8_t encode(const bool bit1, const bool bit2, const bool bit3);
14+
15+
void printClockSignal(const std::vector<uint8_t> & signal)
16+
{
17+
bool prevBit = true;
18+
bool nextBit = true;
19+
20+
for (uint8_t byte : signal)
21+
{
22+
23+
for (int i=-1; i<9; ++i)
24+
{
25+
if (i == -1) // Start bit
26+
nextBit = true;
27+
else if (i == 8) // Stop bit
28+
nextBit = false;
29+
else
30+
nextBit = ~byte & (1 << i);
31+
32+
if (!prevBit && nextBit)
33+
{
34+
std::cout << ' ';
35+
}
36+
37+
if (nextBit)
38+
std::cout << '1';
39+
else
40+
std::cout << '0';
41+
42+
prevBit = nextBit;
43+
}
44+
}
45+
}
46+
47+
int main()
48+
{
49+
const std::vector<uint8_t> white{0xff, 0xff, 0xff};
50+
const std::vector<uint8_t> green{0xff, 0x00, 0x00};
51+
const std::vector<uint8_t> red {0x00, 0xff, 0x00};
52+
const std::vector<uint8_t> blue {0x00, 0x00, 0xff};
53+
const std::vector<uint8_t> cyan {0xff, 0x00, 0xff};
54+
const std::vector<uint8_t> mix {0x55, 0x55, 0x55};
55+
const std::vector<uint8_t> black{0x00, 0x00, 0x00};
56+
const std::vector<uint8_t> gray{0x01, 0x01, 0x01};
57+
58+
// printClockSignal(encode(mix));std::cout << std::endl;
59+
60+
//OPEN THE UART
61+
// int uart0_filestream = open("/dev/ttyAMA0", O_WRONLY | O_NOCTTY | O_NDELAY);
62+
int uart0_filestream = open("/dev/ttyUSB0", O_WRONLY | O_NOCTTY | O_NDELAY);
63+
if (uart0_filestream == -1)
64+
{
65+
//ERROR - CAN'T OPEN SERIAL PORT
66+
printf("Error - Unable to open UART. Ensure it is not in use by another application\n");
67+
return -1;
68+
}
69+
70+
// Configure the port
71+
struct termios options;
72+
tcgetattr(uart0_filestream, &options);
73+
options.c_cflag = B2500000 | CS8 | CLOCAL;
74+
options.c_iflag = IGNPAR;
75+
options.c_oflag = 0;
76+
options.c_lflag = 0;
77+
78+
tcflush(uart0_filestream, TCIFLUSH);
79+
tcsetattr(uart0_filestream, TCSANOW, &options);
80+
81+
{
82+
getchar();
83+
const std::vector<uint8_t> encGreenData = encode(green);
84+
const std::vector<uint8_t> encBlueData = encode(blue);
85+
const std::vector<uint8_t> encRedData = encode(red);
86+
const std::vector<uint8_t> encGrayData = encode(gray);
87+
const std::vector<uint8_t> encBlackData = encode(black);
88+
89+
//std::cout << "Writing GREEN ("; printClockSignal(encode(green)); std::cout << ")" << std::endl;
90+
const std::vector<uint8_t> garbage {0x0f};
91+
write(uart0_filestream, garbage.data(), garbage.size());
92+
write(uart0_filestream, encGreenData.data(), encGreenData.size());
93+
write(uart0_filestream, encRedData.data(), encRedData.size());
94+
write(uart0_filestream, encBlueData.data(), encBlueData.size());
95+
write(uart0_filestream, encGrayData.data(), encGrayData.size());
96+
write(uart0_filestream, encBlackData.data(), encBlackData.size());
97+
}
98+
{
99+
getchar();
100+
const std::vector<uint8_t> encData = encode(white);
101+
std::cout << "Writing WHITE ("; printClockSignal(encode(white)); std::cout << ")" << std::endl;
102+
const std::vector<uint8_t> garbage {0x0f};
103+
write(uart0_filestream, garbage.data(), garbage.size());
104+
write(uart0_filestream, encData.data(), encData.size());
105+
}
106+
{
107+
getchar();
108+
const std::vector<uint8_t> encData = encode(green);
109+
std::cout << "Writing GREEN ("; printClockSignal(encode(green)); std::cout << ")" << std::endl;
110+
write(uart0_filestream, encData.data(), encData.size());
111+
}
112+
{
113+
getchar();
114+
const std::vector<uint8_t> encData = encode(red);
115+
std::cout << "Writing RED ("; printClockSignal(encode(red)); std::cout << ")" << std::endl;
116+
write(uart0_filestream, encData.data(), encData.size());
117+
}
118+
{
119+
getchar();
120+
const std::vector<uint8_t> encData = encode(blue);
121+
std::cout << "Writing BLUE ("; printClockSignal(encode(blue)); std::cout << ")" << std::endl;
122+
write(uart0_filestream, encData.data(), encData.size());
123+
}
124+
{
125+
getchar();
126+
const std::vector<uint8_t> encData = encode(cyan);
127+
std::cout << "Writing CYAN? ("; printClockSignal(encode(cyan)); std::cout << ")" << std::endl;
128+
write(uart0_filestream, encData.data(), encData.size());
129+
}
130+
{
131+
getchar();
132+
const std::vector<uint8_t> encData = encode(mix);
133+
std::cout << "Writing MIX ("; printClockSignal(encode(mix)); std::cout << ")" << std::endl;
134+
write(uart0_filestream, encData.data(), encData.size());
135+
}
136+
{
137+
getchar();
138+
const std::vector<uint8_t> encData = encode(black);
139+
std::cout << "Writing BLACK ("; printClockSignal(encode(black)); std::cout << ")" << std::endl;
140+
write(uart0_filestream, encData.data(), encData.size());
141+
write(uart0_filestream, encData.data(), encData.size());
142+
write(uart0_filestream, encData.data(), encData.size());
143+
write(uart0_filestream, encData.data(), encData.size());
144+
}
145+
146+
close(uart0_filestream);
147+
148+
return 0;
149+
}
150+
151+
std::vector<uint8_t> encode(const std::vector<uint8_t> & data)
152+
{
153+
std::vector<uint8_t> result;
154+
for (size_t iByte=0; iByte<data.size(); iByte+=3)
155+
{
156+
const uint8_t byte1 = data[iByte];
157+
const uint8_t byte2 = data[iByte+1];
158+
const uint8_t byte3 = data[iByte+2];
159+
160+
result.push_back(encode(byte1 & 0x80, byte1 & 0x40, byte1 & 0x20));
161+
result.push_back(encode(byte1 & 0x10, byte1 & 0x08, byte1 & 0x04));
162+
result.push_back(encode(byte1 & 0x02, byte1 & 0x01, byte2 & 0x80));
163+
result.push_back(encode(byte2 & 0x40, byte2 & 0x20, byte2 & 0x10));
164+
result.push_back(encode(byte2 & 0x08, byte2 & 0x04, byte2 & 0x02));
165+
result.push_back(encode(byte2 & 0x01, byte3 & 0x80, byte3 & 0x40));
166+
result.push_back(encode(byte3 & 0x20, byte3 & 0x10, byte3 & 0x08));
167+
result.push_back(encode(byte3 & 0x04, byte3 & 0x02, byte3 & 0x01));
168+
}
169+
return result;
170+
}
171+
172+
uint8_t encode(const bool bit1, const bool bit2, const bool bit3)
173+
{
174+
uint8_t result = 0x44; // 0100 0100
175+
176+
if (bit1)
177+
result |= 0x01; // 0000 0001
178+
179+
if (bit2)
180+
result |= 0x18; // 0001 1000
181+
182+
if (bit3)
183+
result |= 0x80; // 1000 0000
184+
185+
return ~result;
186+
}

test/TestUartHighSpeed.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ uint8_t bit3Encode(const bool bit_1, const bool bit_2, const bool bit_3);
261261
void test3bitsEncoding()
262262
{
263263
//OPEN THE UART
264-
int uart0_filestream = open("/dev/ttyAMA0", O_WRONLY | O_NOCTTY | O_NDELAY);
264+
// int uart0_filestream = open("/dev/ttyAMA0", O_WRONLY | O_NOCTTY | O_NDELAY);
265+
int uart0_filestream = open("/dev/ttyUSB0", O_WRONLY | O_NOCTTY | O_NDELAY);
265266
if (uart0_filestream == -1)
266267
{
267268
//ERROR - CAN'T OPEN SERIAL PORT

0 commit comments

Comments
 (0)