DmxShow/app.cpp

119 lines
3.6 KiB
C++
Raw Normal View History

2024-08-08 16:14:12 -06:00
#include <iostream>
#include <windows.h>
#include <thread>
#include <chrono>
#include <csignal>
HANDLE hSerial = INVALID_HANDLE_VALUE;
// Number of channels in the universe
const int num_channels = 512;
bool blackoutMode = false;
void sendDMXData(HANDLE hSerial, unsigned char* data, int length);
void signalHandler(int signum) {
std::cout << "Interrupt signal (" << signum << ") received.\n";
blackoutMode = true;
if (hSerial != INVALID_HANDLE_VALUE) {
std::cout << "Closing serial port...\n";
CloseHandle(hSerial);
}
exit(signum);
}
void sendDMXData(HANDLE hSerial, unsigned char* data, int length) {
// Start with a DMX break condition
EscapeCommFunction(hSerial, SETBREAK);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
EscapeCommFunction(hSerial, CLRBREAK);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
DWORD bytesWritten;
unsigned char startCode = 0;
WriteFile(hSerial, &startCode, 1, &bytesWritten, NULL);
WriteFile(hSerial, data, length, &bytesWritten, NULL);
}
int main() {
// Register the signal handler for SIGINT (Ctrl+C)
std::signal(SIGINT, signalHandler);
// Open the serial port
hSerial = CreateFileA("\\\\.\\COM3", GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (hSerial == INVALID_HANDLE_VALUE) {
std::cerr << "Error opening COM3 port" << std::endl;
return 1;
}
// Configure the serial port
DCB dcbSerialParams = { 0 };
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (!GetCommState(hSerial, &dcbSerialParams)) {
std::cerr << "Error getting COM3 state" << std::endl;
CloseHandle(hSerial);
return 1;
}
dcbSerialParams.BaudRate = 250000; // DMX512 uses 250000 baud
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = TWOSTOPBITS; // DMX512 uses 2 stop bits
dcbSerialParams.Parity = NOPARITY;
if (!SetCommState(hSerial, &dcbSerialParams)) {
std::cerr << "Error setting COM3 state" << std::endl;
CloseHandle(hSerial);
return 1;
}
// Set timeouts
COMMTIMEOUTS timeouts = { 0 };
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if (!SetCommTimeouts(hSerial, &timeouts)) {
std::cerr << "Error setting COM3 timeouts" << std::endl;
CloseHandle(hSerial);
return 1;
}
// DMX data (512 channels, all set to 255)
unsigned char buffer[num_channels];
memset(buffer, 255, sizeof(buffer)); // Fill the buffer with 255
// Send DMX data in an infinite loop
while (!blackoutMode) {
sendDMXData(hSerial, buffer, num_channels);
std::this_thread::sleep_for(std::chrono::milliseconds(25));
// Set random colors for channels 1, 2, and 3 of device 1
std::srand(std::time(0)); // Seed random number generator
buffer[0] = std::rand() % 256; // Random value for channel 1
buffer[1] = std::rand() % 256; // Random value for channel 2
buffer[2] = std::rand() % 256; //
}
// Blackout buffer (512 channels, all set to 0)
unsigned char blackoutBuffer[num_channels];
memset(blackoutBuffer, 0, sizeof(blackoutBuffer)); // Fill the buffer with 0
for (int x = 0; x < 10; x++) {
printf("step: %d\n", x);
sendDMXData(hSerial, blackoutBuffer, num_channels);
std::this_thread::sleep_for(std::chrono::milliseconds(25));
}
printf("Done!\n");
return 0;
}