Added sliders (incorrect numbers and lagy) but the sliders work

This commit is contained in:
illyum 2024-08-08 16:22:48 -06:00
parent 2c8d0b8150
commit ecaa2d17fc

165
main.cpp
View File

@ -2,26 +2,31 @@
#include <windows.h>
#include <thread>
#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 sendDMXData(HANDLE hSerial, unsigned char* data, int length);
void setupImGui(GLFWwindow* window);
void renderImGui();
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
// DMX data buffer
const int num_channels = 512;
unsigned char buffer[num_channels] = { 0 };
// 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);
}
// Function to initialize OpenGL and create a window
GLFWwindow* initOpenGL();
// Serial port handle
HANDLE hSerial;
int main() {
// 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) {
std::cerr << "Error opening COM3 port" << std::endl;
return 1;
@ -61,24 +66,54 @@ int main() {
return 1;
}
srand(static_cast<unsigned int>(time(nullptr)));
// DMX data (512 channels, all set to 255)
const int num_channels = 512;
unsigned char buffer[num_channels];
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
// Initialize OpenGL and create a window
GLFWwindow* window = initOpenGL();
if (!window) {
CloseHandle(hSerial);
return 1;
}
// 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);
return 0;
@ -88,4 +123,74 @@ void generateRandomColors(unsigned char& r, unsigned char& g, unsigned char& b)
r = rand() % 256;
g = 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();
}