mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Modification and additions to test working of ws2812b
Former-commit-id: 2a7eb3cb67c85a4a4042bd0f1a6ea4d58792b1c2
This commit is contained in:
@@ -49,6 +49,8 @@ int main()
|
||||
requiredTiming(400, 850, 150, 5); // Zero
|
||||
requiredTiming(800, 450, 150, 5); // One
|
||||
|
||||
requiredTiming(650, 600, 150, 5); // One
|
||||
|
||||
// 4bits
|
||||
requiredTiming(400, 850, 150, 4); // Zero
|
||||
requiredTiming(800, 450, 150, 4); // One
|
||||
|
@@ -62,7 +62,9 @@ int testSerialPortLib()
|
||||
continue;
|
||||
}
|
||||
|
||||
rs232Port.flushOutput();
|
||||
rs232Port.write(data);
|
||||
rs232Port.flush();
|
||||
|
||||
data.clear();
|
||||
for (int i=0; i<9; ++i)
|
||||
|
@@ -12,8 +12,80 @@
|
||||
#include <csignal>
|
||||
#include <cstdint>
|
||||
#include <bitset>
|
||||
#include <vector>
|
||||
|
||||
#include <QElapsedTimer>
|
||||
#include <pthread.h>
|
||||
#include <sched.h>
|
||||
|
||||
void set_realtime_priority() {
|
||||
int ret;
|
||||
|
||||
// We'll operate on the currently running thread.
|
||||
pthread_t this_thread = pthread_self();
|
||||
// struct sched_param is used to store the scheduling priority
|
||||
struct sched_param params;
|
||||
// We'll set the priority to the maximum.
|
||||
params.sched_priority = sched_get_priority_max(SCHED_FIFO);
|
||||
std::cout << "Trying to set thread realtime prio = " << params.sched_priority << std::endl;
|
||||
|
||||
// Attempt to set thread real-time priority to the SCHED_FIFO policy
|
||||
ret = pthread_setschedparam(this_thread, SCHED_FIFO, ¶ms);
|
||||
if (ret != 0) {
|
||||
// Print the error
|
||||
std::cout << "Unsuccessful in setting thread realtime prio (erno=" << ret << ")" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Now verify the change in thread priority
|
||||
int policy = 0;
|
||||
ret = pthread_getschedparam(this_thread, &policy, ¶ms);
|
||||
if (ret != 0) {
|
||||
std::cout << "Couldn't retrieve real-time scheduling paramers" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Check the correct policy was applied
|
||||
if(policy != SCHED_FIFO) {
|
||||
std::cout << "Scheduling is NOT SCHED_FIFO!" << std::endl;
|
||||
} else {
|
||||
std::cout << "SCHED_FIFO OK" << std::endl;
|
||||
}
|
||||
|
||||
// Print thread scheduling priority
|
||||
std::cout << "Thread priority is " << params.sched_priority << std::endl;
|
||||
}
|
||||
|
||||
|
||||
struct ColorSignal
|
||||
{
|
||||
uint8_t green_1;
|
||||
uint8_t green_2;
|
||||
uint8_t green_3;
|
||||
uint8_t green_4;
|
||||
|
||||
uint8_t red_1;
|
||||
uint8_t red_2;
|
||||
uint8_t red_3;
|
||||
uint8_t red_4;
|
||||
|
||||
uint8_t blue_1;
|
||||
uint8_t blue_2;
|
||||
uint8_t blue_3;
|
||||
uint8_t blue_4;
|
||||
};
|
||||
|
||||
static ColorSignal RED_Signal = { 0xCE, 0xCE, 0xCE, 0xCE,
|
||||
0xCE, 0x8C, 0x8C, 0x8C,
|
||||
0xCE, 0xCE, 0xCE, 0xCE };
|
||||
static ColorSignal GREEN_Signal = { 0xCE, 0x8C, 0x8C, 0x8C,
|
||||
0xCE, 0xCE, 0xCE, 0xCE,
|
||||
0xCE, 0xCE, 0xCE, 0xCE };
|
||||
static ColorSignal BLUE_Signal = { 0xCE, 0xCE, 0xCE, 0xCE,
|
||||
0xCE, 0xCE, 0xCE, 0xCE,
|
||||
0xCE, 0x8C, 0x8C, 0x8C};
|
||||
static ColorSignal BLACK_Signal = { 0xCE, 0xCE, 0xCE, 0xCE,
|
||||
0xCE, 0xCE, 0xCE, 0xCE,
|
||||
0xCE, 0xCE, 0xCE, 0xCE};
|
||||
|
||||
static volatile bool _running;
|
||||
|
||||
@@ -46,7 +118,7 @@ int main()
|
||||
// immediately with a failure status if the output can't be written immediately.
|
||||
//
|
||||
// O_NOCTTY - When set and path identifies a terminal device, open() shall not cause the terminal device to become the controlling terminal for the process.
|
||||
uart0_filestream = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY); //Open in non blocking read/write mode
|
||||
uart0_filestream = open("/dev/ttyAMA0", O_WRONLY | O_NOCTTY | O_NDELAY); //Open in non blocking read/write mode
|
||||
if (uart0_filestream == -1)
|
||||
{
|
||||
//ERROR - CAN'T OPEN SERIAL PORT
|
||||
@@ -67,17 +139,18 @@ int main()
|
||||
// PARODD - Odd parity (else even)
|
||||
struct termios options;
|
||||
tcgetattr(uart0_filestream, &options);
|
||||
options.c_cflag = B4000000 | CS8 | CLOCAL | CREAD; //<Set baud rate
|
||||
options.c_cflag = B4000000 | CS8 | CLOCAL; //<Set baud rate
|
||||
options.c_iflag = IGNPAR;
|
||||
options.c_oflag = 0;
|
||||
options.c_lflag = 0;
|
||||
tcflush(uart0_filestream, TCIFLUSH);
|
||||
cfmakeraw(&options);
|
||||
|
||||
std::cout << "options.c_cflag = " << options.c_cflag << std::endl;
|
||||
std::cout << "options.c_iflag = " << options.c_iflag << std::endl;
|
||||
std::cout << "options.c_oflag = " << options.c_oflag << std::endl;
|
||||
std::cout << "options.c_lflag = " << options.c_lflag << std::endl;
|
||||
|
||||
tcflush(uart0_filestream, TCIFLUSH);
|
||||
tcsetattr(uart0_filestream, TCSANOW, &options);
|
||||
// Let's verify configured options
|
||||
tcgetattr(uart0_filestream, &options);
|
||||
@@ -128,47 +201,9 @@ int main()
|
||||
}
|
||||
|
||||
//----- TX BYTES -----
|
||||
uint8_t tx_buffer[3*3*8*4];
|
||||
uint8_t *p_tx_buffer;
|
||||
|
||||
// for (int i=0; i<3; ++i)
|
||||
// {
|
||||
// Writing 0xFF, 0x00, 0x00
|
||||
// *p_tx_buffer++ = 0x8C;
|
||||
// *p_tx_buffer++ = 0x8C;
|
||||
// *p_tx_buffer++ = 0x8C;
|
||||
// *p_tx_buffer++ = 0x8C;
|
||||
|
||||
std::default_random_engine generator;
|
||||
std::uniform_int_distribution<int> distribution(1,2);
|
||||
|
||||
p_tx_buffer = &tx_buffer[0];
|
||||
for (int i=0; i<9; ++i)
|
||||
{
|
||||
int coinFlip = distribution(generator);
|
||||
if (coinFlip == 1)
|
||||
{
|
||||
*p_tx_buffer++ = 0xCE;
|
||||
*p_tx_buffer++ = 0xCE;
|
||||
*p_tx_buffer++ = 0xCE;
|
||||
*p_tx_buffer++ = 0xCE;
|
||||
}
|
||||
else
|
||||
{
|
||||
*p_tx_buffer++ = 0x8C;
|
||||
*p_tx_buffer++ = 0x8C;
|
||||
*p_tx_buffer++ = 0x8C;
|
||||
*p_tx_buffer++ = 0x8C;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Binary stream: [";
|
||||
for (unsigned char* txIt=&(tx_buffer[0]); txIt!=p_tx_buffer; ++txIt)
|
||||
{
|
||||
std::cout << " 1 " << (std::bitset<8>) (*txIt) << " 0 ";
|
||||
}
|
||||
std::cout << "]" << std::endl;
|
||||
std::vector<ColorSignal> signalData(10, RED_Signal);
|
||||
|
||||
int loopCnt = 0;
|
||||
std::cout << "Type 'c' to continue, 'q' or 'x' to quit: ";
|
||||
while (_running)
|
||||
{
|
||||
@@ -182,38 +217,28 @@ int main()
|
||||
continue;
|
||||
}
|
||||
|
||||
int count = write(uart0_filestream, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0])); //Filestream, bytes to write, number of bytes to write
|
||||
if (count < 0)
|
||||
set_realtime_priority();
|
||||
for (int iRun=0; iRun<10; ++iRun)
|
||||
{
|
||||
std::cerr << "UART TX error" << std::endl;
|
||||
// tcflush(uart0_filestream, TCOFLUSH);
|
||||
write(uart0_filestream, signalData.data(), signalData.size()*sizeof(ColorSignal));
|
||||
tcdrain(uart0_filestream);
|
||||
|
||||
//----- CLOSE THE UART -----
|
||||
close(uart0_filestream);
|
||||
return -1;
|
||||
}
|
||||
std::cout << "Writing " << count << " bytes to uart" << std::endl;
|
||||
usleep(100000);
|
||||
++loopCnt;
|
||||
|
||||
if (loopCnt%3 == 2)
|
||||
signalData = std::vector<ColorSignal>(10, GREEN_Signal);
|
||||
else if(loopCnt%3 == 1)
|
||||
signalData = std::vector<ColorSignal>(10, BLUE_Signal);
|
||||
else if(loopCnt%3 == 0)
|
||||
signalData = std::vector<ColorSignal>(10, RED_Signal);
|
||||
|
||||
p_tx_buffer = &tx_buffer[0];
|
||||
for (int i=0; i<9; ++i)
|
||||
{
|
||||
int coinFlip = distribution(generator);
|
||||
if (coinFlip == 1)
|
||||
{
|
||||
*p_tx_buffer++ = 0xCE;
|
||||
*p_tx_buffer++ = 0xCE;
|
||||
*p_tx_buffer++ = 0xCE;
|
||||
*p_tx_buffer++ = 0xCE;
|
||||
}
|
||||
else
|
||||
{
|
||||
*p_tx_buffer++ = 0x8C;
|
||||
*p_tx_buffer++ = 0x8C;
|
||||
*p_tx_buffer++ = 0x8C;
|
||||
*p_tx_buffer++ = 0x8C;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
signalData = std::vector<ColorSignal>(50, BLACK_Signal);
|
||||
write(uart0_filestream, signalData.data(), signalData.size()*sizeof(ColorSignal));
|
||||
//----- CLOSE THE UART -----
|
||||
close(uart0_filestream);
|
||||
|
||||
|
Reference in New Issue
Block a user