Added sliders (incorrect numbers and lagy) but the sliders work
This commit is contained in:
parent
2c8d0b8150
commit
ecaa2d17fc
163
main.cpp
163
main.cpp
@ -2,26 +2,31 @@
|
|||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <GL/glew.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
#include "imgui.h"
|
||||||
|
#include "imgui_impl_glfw.h"
|
||||||
|
#include "imgui_impl_opengl3.h"
|
||||||
|
|
||||||
|
// Forward declarations
|
||||||
void generateRandomColors(unsigned char& r, unsigned char& g, unsigned char& b);
|
void generateRandomColors(unsigned char& r, unsigned char& g, unsigned char& b);
|
||||||
|
void sendDMXData(HANDLE hSerial, unsigned char* data, int length);
|
||||||
|
void setupImGui(GLFWwindow* window);
|
||||||
|
void renderImGui();
|
||||||
|
|
||||||
void sendDMXData(HANDLE hSerial, unsigned char* data, int length) {
|
// DMX data buffer
|
||||||
// Start with a DMX break condition
|
const int num_channels = 512;
|
||||||
EscapeCommFunction(hSerial, SETBREAK);
|
unsigned char buffer[num_channels] = { 0 };
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(1)); // Break duration
|
|
||||||
EscapeCommFunction(hSerial, CLRBREAK);
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(1)); // Mark-after-break duration
|
|
||||||
|
|
||||||
// Write the start code followed by the DMX data
|
// Function to initialize OpenGL and create a window
|
||||||
DWORD bytesWritten;
|
GLFWwindow* initOpenGL();
|
||||||
unsigned char startCode = 0;
|
|
||||||
WriteFile(hSerial, &startCode, 1, &bytesWritten, NULL);
|
// Serial port handle
|
||||||
WriteFile(hSerial, data, length, &bytesWritten, NULL);
|
HANDLE hSerial;
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// Open the serial port
|
// Open the serial port
|
||||||
HANDLE hSerial = CreateFileA("\\\\.\\COM3", GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
|
hSerial = CreateFileA("\\\\.\\COM3", GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
|
||||||
if (hSerial == INVALID_HANDLE_VALUE) {
|
if (hSerial == INVALID_HANDLE_VALUE) {
|
||||||
std::cerr << "Error opening COM3 port" << std::endl;
|
std::cerr << "Error opening COM3 port" << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
@ -61,24 +66,54 @@ int main() {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
srand(static_cast<unsigned int>(time(nullptr)));
|
// Initialize OpenGL and create a window
|
||||||
|
GLFWwindow* window = initOpenGL();
|
||||||
// DMX data (512 channels, all set to 255)
|
if (!window) {
|
||||||
const int num_channels = 512;
|
CloseHandle(hSerial);
|
||||||
unsigned char buffer[num_channels];
|
return 1;
|
||||||
memset(buffer, 255, sizeof(buffer)); // Fill the buffer with 255
|
|
||||||
|
|
||||||
// Send DMX data in an infinite loop
|
|
||||||
while (true) {
|
|
||||||
// Generate and send random colors for channels 1, 2, and 3
|
|
||||||
generateRandomColors(buffer[0], buffer[1], buffer[2]);
|
|
||||||
|
|
||||||
// Send DMX data with random colors
|
|
||||||
sendDMXData(hSerial, buffer, num_channels);
|
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(25)); // DMX refresh rate
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close the serial port
|
// Setup ImGui
|
||||||
|
setupImGui(window);
|
||||||
|
|
||||||
|
// Main loop
|
||||||
|
while (!glfwWindowShouldClose(window)) {
|
||||||
|
// Poll events
|
||||||
|
glfwPollEvents();
|
||||||
|
|
||||||
|
// Start the ImGui frame
|
||||||
|
ImGui_ImplOpenGL3_NewFrame();
|
||||||
|
ImGui_ImplGlfw_NewFrame();
|
||||||
|
ImGui::NewFrame();
|
||||||
|
|
||||||
|
// Render ImGui components
|
||||||
|
renderImGui();
|
||||||
|
|
||||||
|
// Update DMX data with values from sliders
|
||||||
|
sendDMXData(hSerial, buffer, num_channels);
|
||||||
|
|
||||||
|
// Rendering
|
||||||
|
ImGui::Render();
|
||||||
|
int display_w, display_h;
|
||||||
|
glfwGetFramebufferSize(window, &display_w, &display_h);
|
||||||
|
glViewport(0, 0, display_w, display_h);
|
||||||
|
glClearColor(0.45f, 0.55f, 0.60f, 1.00f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||||
|
|
||||||
|
// Swap buffers
|
||||||
|
glfwSwapBuffers(window);
|
||||||
|
|
||||||
|
// Simulate DMX refresh rate
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(25));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cleanup
|
||||||
|
ImGui_ImplOpenGL3_Shutdown();
|
||||||
|
ImGui_ImplGlfw_Shutdown();
|
||||||
|
ImGui::DestroyContext();
|
||||||
|
glfwDestroyWindow(window);
|
||||||
|
glfwTerminate();
|
||||||
CloseHandle(hSerial);
|
CloseHandle(hSerial);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@ -89,3 +124,73 @@ void generateRandomColors(unsigned char& r, unsigned char& g, unsigned char& b)
|
|||||||
g = rand() % 256;
|
g = rand() % 256;
|
||||||
b = rand() % 256;
|
b = rand() % 256;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)); // Break duration
|
||||||
|
EscapeCommFunction(hSerial, CLRBREAK);
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(1)); // Mark-after-break duration
|
||||||
|
|
||||||
|
// Write the start code followed by the DMX data
|
||||||
|
DWORD bytesWritten;
|
||||||
|
unsigned char startCode = 0;
|
||||||
|
WriteFile(hSerial, &startCode, 1, &bytesWritten, NULL);
|
||||||
|
WriteFile(hSerial, data, length, &bytesWritten, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
GLFWwindow* initOpenGL() {
|
||||||
|
// Initialize GLFW
|
||||||
|
if (!glfwInit()) {
|
||||||
|
std::cerr << "Failed to initialize GLFW" << std::endl;
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a windowed mode window and its OpenGL context
|
||||||
|
GLFWwindow* window = glfwCreateWindow(1280, 720, "DMX Controller", NULL, NULL);
|
||||||
|
if (!window) {
|
||||||
|
std::cerr << "Failed to create GLFW window" << std::endl;
|
||||||
|
glfwTerminate();
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make the window's context current
|
||||||
|
glfwMakeContextCurrent(window);
|
||||||
|
glfwSwapInterval(1); // Enable vsync
|
||||||
|
|
||||||
|
// Initialize GLEW
|
||||||
|
if (glewInit() != GLEW_OK) {
|
||||||
|
std::cerr << "Failed to initialize GLEW" << std::endl;
|
||||||
|
glfwDestroyWindow(window);
|
||||||
|
glfwTerminate();
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return window;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setupImGui(GLFWwindow* window) {
|
||||||
|
// Setup ImGui context
|
||||||
|
IMGUI_CHECKVERSION();
|
||||||
|
ImGui::CreateContext();
|
||||||
|
ImGuiIO& io = ImGui::GetIO(); (void)io;
|
||||||
|
|
||||||
|
// Setup ImGui style
|
||||||
|
ImGui::StyleColorsDark();
|
||||||
|
|
||||||
|
// Setup Platform/Renderer bindings
|
||||||
|
ImGui_ImplGlfw_InitForOpenGL(window, true);
|
||||||
|
ImGui_ImplOpenGL3_Init("#version 130");
|
||||||
|
}
|
||||||
|
|
||||||
|
void renderImGui() {
|
||||||
|
// Create a window
|
||||||
|
ImGui::Begin("DMX Controller");
|
||||||
|
|
||||||
|
// Create sliders for the first three DMX channels
|
||||||
|
ImGui::SliderInt("Channel 1", (int*)&buffer[0], 0, 255);
|
||||||
|
ImGui::SliderInt("Channel 2", (int*)&buffer[1], 0, 255);
|
||||||
|
ImGui::SliderInt("Channel 3", (int*)&buffer[2], 0, 255);
|
||||||
|
|
||||||
|
ImGui::End();
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user