Updated and addition of more tests for ws2812b control

Former-commit-id: 57078dac71173b2cb203f644e4158e179161bf17
This commit is contained in:
T. van der Zwan 2014-01-18 22:22:10 +00:00
parent 78209f3b92
commit d2665aebd4
4 changed files with 269 additions and 10 deletions

View File

@ -66,5 +66,6 @@ if(NOT APPLE AND UNIX)
include_directories(/usr/include)
add_executable(test_uartHighSpeed TestUartHighSpeed.cpp)
add_executable(test_nonInvWs2812b TestNonInvWs2812b.cpp)
add_executable(test_nonUniformWs2812b TestNonUniformWs2812b.cpp)
add_executable(test_nonInvWs2812b TestNonInvWs2812b.cpp)
endif()

View File

@ -4,6 +4,11 @@
#include <vector>
#include <iostream>
#include <unistd.h> //Used for UART
#include <fcntl.h> //Used for UART
#include <termios.h> //Used for UART
#include <sys/ioctl.h>
std::vector<uint8_t> encode(const std::vector<uint8_t> & data);
void split(const uint8_t byte, uint8_t & out1, uint8_t & out2);
uint8_t encode(const bool bit1, const bool bit2, const bool bit3);
@ -23,10 +28,41 @@ void print(uint8_t byte)
}
}
void printClockSignal(const std::vector<uint8_t> & signal)
{
bool prevBit = true;
bool nextBit = true;
for (uint8_t byte : signal)
{
for (int i=-1; i<9; ++i)
{
if (i == -1) // Start bit
nextBit = false;
else if (i == 8) // Stop bit
nextBit = true;
else
nextBit = byte & (1 << i);
if (!prevBit && nextBit)
{
std::cout << ' ';
}
if (nextBit)
std::cout << '1';
else
std::cout << '0';
prevBit = nextBit;
}
}
}
int main()
{
std::vector<uint8_t> data(3, 0x55);
const std::vector<uint8_t> data(9, 0xff);
std::vector<uint8_t> encData = encode(data);
for (uint8_t encByte : encData)
@ -36,7 +72,45 @@ int main()
std::cout << " 1";
}
std::cout << std::endl;
printClockSignal(encData);
std::cout << std::endl;
//OPEN THE UART
// int uart0_filestream = open("/dev/ttyAMA0", O_WRONLY | O_NOCTTY | O_NDELAY);
int uart0_filestream = open("/dev/ttyUSB0", O_WRONLY | O_NOCTTY | O_NDELAY);
if (uart0_filestream == -1)
{
//ERROR - CAN'T OPEN SERIAL PORT
printf("Error - Unable to open UART. Ensure it is not in use by another application\n");
return -1;
}
// Configure the port
struct termios options;
tcgetattr(uart0_filestream, &options);
options.c_cflag = B4000000 | CS8 | CLOCAL;
options.c_iflag = IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;
tcflush(uart0_filestream, TCIFLUSH);
tcsetattr(uart0_filestream, TCSANOW, &options);
char c = getchar();
const int breakLength_ms = 1;
encData = std::vector<uint8_t>(128, 0x10);
write(uart0_filestream, encData.data(), encData.size());
tcsendbreak(uart0_filestream, breakLength_ms);
//tcdrain(uart0_filestream);
// res = write(uart0_filestream, encData.data(), encData.size());
// (void)res;
close(uart0_filestream);
return 0;
}
@ -96,19 +170,16 @@ std::vector<uint8_t> encode(const std::vector<uint8_t> & data)
previousByte = nextByte;
}
result.push_back(previousByte);
return result;
}
void split(const uint8_t byte, uint8_t & out1, uint8_t & out2)
{
print(byte); std::cout << " => ";
print(out2); std::cout << " => ";
out1 &= ~0x0F;
out1 |= (byte & 0x0F) << 4;
// out2 &= ~0xF0;
print(out2); std::cout << " => ";
out2 = (byte & 0xF0) >> 4;
print(out2); std::cout << std::endl;
}
uint8_t encode(const bool bit1, const bool bit2, const bool bit3)

View File

@ -0,0 +1,186 @@
// STL includes
#include <cstdint>
#include <vector>
#include <iostream>
#include <unistd.h> //Used for UART
#include <fcntl.h> //Used for UART
#include <termios.h> //Used for UART
#include <sys/ioctl.h>
std::vector<uint8_t> encode(const std::vector<uint8_t> & data);
uint8_t encode(const bool bit1, const bool bit2, const bool bit3);
void printClockSignal(const std::vector<uint8_t> & signal)
{
bool prevBit = true;
bool nextBit = true;
for (uint8_t byte : signal)
{
for (int i=-1; i<9; ++i)
{
if (i == -1) // Start bit
nextBit = true;
else if (i == 8) // Stop bit
nextBit = false;
else
nextBit = ~byte & (1 << i);
if (!prevBit && nextBit)
{
std::cout << ' ';
}
if (nextBit)
std::cout << '1';
else
std::cout << '0';
prevBit = nextBit;
}
}
}
int main()
{
const std::vector<uint8_t> white{0xff, 0xff, 0xff};
const std::vector<uint8_t> green{0xff, 0x00, 0x00};
const std::vector<uint8_t> red {0x00, 0xff, 0x00};
const std::vector<uint8_t> blue {0x00, 0x00, 0xff};
const std::vector<uint8_t> cyan {0xff, 0x00, 0xff};
const std::vector<uint8_t> mix {0x55, 0x55, 0x55};
const std::vector<uint8_t> black{0x00, 0x00, 0x00};
const std::vector<uint8_t> gray{0x01, 0x01, 0x01};
// printClockSignal(encode(mix));std::cout << std::endl;
//OPEN THE UART
// int uart0_filestream = open("/dev/ttyAMA0", O_WRONLY | O_NOCTTY | O_NDELAY);
int uart0_filestream = open("/dev/ttyUSB0", O_WRONLY | O_NOCTTY | O_NDELAY);
if (uart0_filestream == -1)
{
//ERROR - CAN'T OPEN SERIAL PORT
printf("Error - Unable to open UART. Ensure it is not in use by another application\n");
return -1;
}
// Configure the port
struct termios options;
tcgetattr(uart0_filestream, &options);
options.c_cflag = B2500000 | CS8 | CLOCAL;
options.c_iflag = IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;
tcflush(uart0_filestream, TCIFLUSH);
tcsetattr(uart0_filestream, TCSANOW, &options);
{
getchar();
const std::vector<uint8_t> encGreenData = encode(green);
const std::vector<uint8_t> encBlueData = encode(blue);
const std::vector<uint8_t> encRedData = encode(red);
const std::vector<uint8_t> encGrayData = encode(gray);
const std::vector<uint8_t> encBlackData = encode(black);
//std::cout << "Writing GREEN ("; printClockSignal(encode(green)); std::cout << ")" << std::endl;
const std::vector<uint8_t> garbage {0x0f};
write(uart0_filestream, garbage.data(), garbage.size());
write(uart0_filestream, encGreenData.data(), encGreenData.size());
write(uart0_filestream, encRedData.data(), encRedData.size());
write(uart0_filestream, encBlueData.data(), encBlueData.size());
write(uart0_filestream, encGrayData.data(), encGrayData.size());
write(uart0_filestream, encBlackData.data(), encBlackData.size());
}
{
getchar();
const std::vector<uint8_t> encData = encode(white);
std::cout << "Writing WHITE ("; printClockSignal(encode(white)); std::cout << ")" << std::endl;
const std::vector<uint8_t> garbage {0x0f};
write(uart0_filestream, garbage.data(), garbage.size());
write(uart0_filestream, encData.data(), encData.size());
}
{
getchar();
const std::vector<uint8_t> encData = encode(green);
std::cout << "Writing GREEN ("; printClockSignal(encode(green)); std::cout << ")" << std::endl;
write(uart0_filestream, encData.data(), encData.size());
}
{
getchar();
const std::vector<uint8_t> encData = encode(red);
std::cout << "Writing RED ("; printClockSignal(encode(red)); std::cout << ")" << std::endl;
write(uart0_filestream, encData.data(), encData.size());
}
{
getchar();
const std::vector<uint8_t> encData = encode(blue);
std::cout << "Writing BLUE ("; printClockSignal(encode(blue)); std::cout << ")" << std::endl;
write(uart0_filestream, encData.data(), encData.size());
}
{
getchar();
const std::vector<uint8_t> encData = encode(cyan);
std::cout << "Writing CYAN? ("; printClockSignal(encode(cyan)); std::cout << ")" << std::endl;
write(uart0_filestream, encData.data(), encData.size());
}
{
getchar();
const std::vector<uint8_t> encData = encode(mix);
std::cout << "Writing MIX ("; printClockSignal(encode(mix)); std::cout << ")" << std::endl;
write(uart0_filestream, encData.data(), encData.size());
}
{
getchar();
const std::vector<uint8_t> encData = encode(black);
std::cout << "Writing BLACK ("; printClockSignal(encode(black)); std::cout << ")" << std::endl;
write(uart0_filestream, encData.data(), encData.size());
write(uart0_filestream, encData.data(), encData.size());
write(uart0_filestream, encData.data(), encData.size());
write(uart0_filestream, encData.data(), encData.size());
}
close(uart0_filestream);
return 0;
}
std::vector<uint8_t> encode(const std::vector<uint8_t> & data)
{
std::vector<uint8_t> result;
for (size_t iByte=0; iByte<data.size(); iByte+=3)
{
const uint8_t byte1 = data[iByte];
const uint8_t byte2 = data[iByte+1];
const uint8_t byte3 = data[iByte+2];
result.push_back(encode(byte1 & 0x80, byte1 & 0x40, byte1 & 0x20));
result.push_back(encode(byte1 & 0x10, byte1 & 0x08, byte1 & 0x04));
result.push_back(encode(byte1 & 0x02, byte1 & 0x01, byte2 & 0x80));
result.push_back(encode(byte2 & 0x40, byte2 & 0x20, byte2 & 0x10));
result.push_back(encode(byte2 & 0x08, byte2 & 0x04, byte2 & 0x02));
result.push_back(encode(byte2 & 0x01, byte3 & 0x80, byte3 & 0x40));
result.push_back(encode(byte3 & 0x20, byte3 & 0x10, byte3 & 0x08));
result.push_back(encode(byte3 & 0x04, byte3 & 0x02, byte3 & 0x01));
}
return result;
}
uint8_t encode(const bool bit1, const bool bit2, const bool bit3)
{
uint8_t result = 0x44; // 0100 0100
if (bit1)
result |= 0x01; // 0000 0001
if (bit2)
result |= 0x18; // 0001 1000
if (bit3)
result |= 0x80; // 1000 0000
return ~result;
}

View File

@ -261,7 +261,8 @@ uint8_t bit3Encode(const bool bit_1, const bool bit_2, const bool bit_3);
void test3bitsEncoding()
{
//OPEN THE UART
int uart0_filestream = open("/dev/ttyAMA0", O_WRONLY | O_NOCTTY | O_NDELAY);
// int uart0_filestream = open("/dev/ttyAMA0", O_WRONLY | O_NOCTTY | O_NDELAY);
int uart0_filestream = open("/dev/ttyUSB0", O_WRONLY | O_NOCTTY | O_NDELAY);
if (uart0_filestream == -1)
{
//ERROR - CAN'T OPEN SERIAL PORT