2024-08-08 16:14:12 -06:00
|
|
|
#include <iostream>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
2024-08-08 16:22:48 -06:00
|
|
|
#include <GL/glew.h>
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include "imgui.h"
|
|
|
|
#include "imgui_impl_glfw.h"
|
|
|
|
#include "imgui_impl_opengl3.h"
|
2024-08-08 22:56:58 -06:00
|
|
|
#include <atomic>
|
2024-08-08 23:56:59 -06:00
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <fstream>
|
2024-08-08 16:14:12 -06:00
|
|
|
|
2024-08-09 00:33:15 -06:00
|
|
|
#include "tinyfiledialogs.h"
|
|
|
|
|
2024-08-08 23:10:01 -06:00
|
|
|
|
2024-08-08 16:14:12 -06:00
|
|
|
void generateRandomColors(unsigned char& r, unsigned char& g, unsigned char& b);
|
2024-08-08 16:22:48 -06:00
|
|
|
void sendDMXData(HANDLE hSerial, unsigned char* data, int length);
|
|
|
|
void setupImGui(GLFWwindow* window);
|
|
|
|
void renderImGui();
|
2024-08-08 16:14:12 -06:00
|
|
|
|
2024-08-08 23:10:01 -06:00
|
|
|
|
|
|
|
// ui timeline stuff
|
|
|
|
std::vector<std::tuple<float, unsigned char, unsigned char, unsigned char>> markers; // Time, R, G, B
|
|
|
|
float timeline_position = 0.0f;
|
|
|
|
bool is_playing = false;
|
|
|
|
float playback_start_time = 0.0f;
|
|
|
|
float current_time = 0.0f;
|
|
|
|
float timeline_duration = 10.0f; // 10 seconds by default
|
2024-08-08 23:24:29 -06:00
|
|
|
float bpm = 120.0f; // Default BPM
|
|
|
|
float zoom_level = 1.0f; // Zoom level
|
|
|
|
float pan_offset = 0.0f; // Pan offset
|
2024-08-08 23:10:01 -06:00
|
|
|
|
|
|
|
|
2024-08-08 16:22:48 -06:00
|
|
|
const int num_channels = 512;
|
|
|
|
unsigned char buffer[num_channels] = { 0 };
|
2024-08-08 16:14:12 -06:00
|
|
|
|
2024-08-08 16:22:48 -06:00
|
|
|
GLFWwindow* initOpenGL();
|
|
|
|
|
|
|
|
HANDLE hSerial;
|
2024-08-08 22:56:58 -06:00
|
|
|
std::atomic<bool> running(true);
|
|
|
|
|
|
|
|
void dmxThreadFunc() {
|
|
|
|
while (running) {
|
2024-08-08 23:10:01 -06:00
|
|
|
// 25ms refresh rate
|
2024-08-08 22:56:58 -06:00
|
|
|
sendDMXData(hSerial, buffer, num_channels);
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(25));
|
|
|
|
}
|
|
|
|
}
|
2024-08-08 16:14:12 -06:00
|
|
|
|
|
|
|
int main() {
|
2024-08-08 16:22:48 -06:00
|
|
|
hSerial = CreateFileA("\\\\.\\COM3", GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
|
2024-08-08 16:14:12 -06:00
|
|
|
if (hSerial == INVALID_HANDLE_VALUE) {
|
|
|
|
std::cerr << "Error opening COM3 port" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
DCB dcbSerialParams = { 0 };
|
|
|
|
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
|
|
|
|
if (!GetCommState(hSerial, &dcbSerialParams)) {
|
|
|
|
std::cerr << "Error getting COM3 state" << std::endl;
|
|
|
|
CloseHandle(hSerial);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-08-08 23:10:01 -06:00
|
|
|
dcbSerialParams.BaudRate = 250000;
|
2024-08-08 16:14:12 -06:00
|
|
|
dcbSerialParams.ByteSize = 8;
|
2024-08-08 23:10:01 -06:00
|
|
|
dcbSerialParams.StopBits = TWOSTOPBITS; // <-- don't forget about this
|
2024-08-08 16:14:12 -06:00
|
|
|
dcbSerialParams.Parity = NOPARITY;
|
|
|
|
|
|
|
|
if (!SetCommState(hSerial, &dcbSerialParams)) {
|
|
|
|
std::cerr << "Error setting COM3 state" << std::endl;
|
|
|
|
CloseHandle(hSerial);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-08-08 16:22:48 -06:00
|
|
|
GLFWwindow* window = initOpenGL();
|
|
|
|
if (!window) {
|
|
|
|
CloseHandle(hSerial);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
setupImGui(window);
|
2024-08-08 22:56:58 -06:00
|
|
|
std::thread dmxThread(dmxThreadFunc);
|
|
|
|
|
2024-08-08 16:22:48 -06:00
|
|
|
while (!glfwWindowShouldClose(window)) {
|
|
|
|
glfwPollEvents();
|
2024-08-08 16:14:12 -06:00
|
|
|
|
2024-08-08 16:22:48 -06:00
|
|
|
ImGui_ImplOpenGL3_NewFrame();
|
|
|
|
ImGui_ImplGlfw_NewFrame();
|
|
|
|
ImGui::NewFrame();
|
2024-08-08 16:14:12 -06:00
|
|
|
|
2024-08-08 16:22:48 -06:00
|
|
|
renderImGui();
|
2024-08-08 16:14:12 -06:00
|
|
|
|
2024-08-08 16:22:48 -06:00
|
|
|
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());
|
|
|
|
|
|
|
|
glfwSwapBuffers(window);
|
2024-08-08 16:14:12 -06:00
|
|
|
}
|
|
|
|
|
2024-08-08 22:56:58 -06:00
|
|
|
running = false;
|
|
|
|
dmxThread.join();
|
|
|
|
|
2024-08-08 16:22:48 -06:00
|
|
|
ImGui_ImplOpenGL3_Shutdown();
|
|
|
|
ImGui_ImplGlfw_Shutdown();
|
|
|
|
ImGui::DestroyContext();
|
|
|
|
glfwDestroyWindow(window);
|
|
|
|
glfwTerminate();
|
2024-08-08 16:14:12 -06:00
|
|
|
CloseHandle(hSerial);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void generateRandomColors(unsigned char& r, unsigned char& g, unsigned char& b) {
|
|
|
|
r = rand() % 256;
|
|
|
|
g = rand() % 256;
|
|
|
|
b = rand() % 256;
|
2024-08-08 16:22:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void sendDMXData(HANDLE hSerial, unsigned char* data, int length) {
|
2024-08-08 23:10:01 -06:00
|
|
|
// break condition
|
2024-08-08 16:22:48 -06:00
|
|
|
EscapeCommFunction(hSerial, SETBREAK);
|
2024-08-08 23:10:01 -06:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
2024-08-08 16:22:48 -06:00
|
|
|
EscapeCommFunction(hSerial, CLRBREAK);
|
2024-08-08 23:10:01 -06:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1)); // this is the mab
|
2024-08-08 16:22:48 -06:00
|
|
|
|
|
|
|
DWORD bytesWritten;
|
|
|
|
unsigned char startCode = 0;
|
|
|
|
WriteFile(hSerial, &startCode, 1, &bytesWritten, NULL);
|
|
|
|
WriteFile(hSerial, data, length, &bytesWritten, NULL);
|
|
|
|
}
|
|
|
|
|
2024-08-08 23:56:59 -06:00
|
|
|
// Function to export markers to a JSON file
|
|
|
|
void exportMarkersToFile(const std::string& filename) {
|
|
|
|
nlohmann::json j;
|
|
|
|
|
|
|
|
for (const auto& marker : markers) {
|
|
|
|
j.push_back({
|
|
|
|
{"time", std::get<0>(marker)},
|
|
|
|
{"r", std::get<1>(marker)},
|
|
|
|
{"g", std::get<2>(marker)},
|
|
|
|
{"b", std::get<3>(marker)}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ofstream file(filename);
|
|
|
|
if (file.is_open()) {
|
|
|
|
file << j.dump(4); // Pretty print with indentation of 4 spaces
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
std::cerr << "Could not open file for writing: " << filename << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function to load markers from a JSON file
|
|
|
|
void loadMarkersFromFile(const std::string& filename) {
|
|
|
|
nlohmann::json j;
|
|
|
|
std::ifstream file(filename);
|
|
|
|
|
|
|
|
if (file.is_open()) {
|
|
|
|
file >> j;
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
markers.clear();
|
|
|
|
for (const auto& element : j) {
|
|
|
|
markers.emplace_back(
|
|
|
|
element["time"].get<float>(),
|
|
|
|
element["r"].get<unsigned char>(),
|
|
|
|
element["g"].get<unsigned char>(),
|
|
|
|
element["b"].get<unsigned char>()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
std::cerr << "Could not open file for reading: " << filename << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-09 00:33:15 -06:00
|
|
|
|
2024-08-08 16:22:48 -06:00
|
|
|
GLFWwindow* initOpenGL() {
|
|
|
|
if (!glfwInit()) {
|
|
|
|
std::cerr << "Failed to initialize GLFW" << std::endl;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLFWwindow* window = glfwCreateWindow(1280, 720, "DMX Controller", NULL, NULL);
|
|
|
|
if (!window) {
|
|
|
|
std::cerr << "Failed to create GLFW window" << std::endl;
|
|
|
|
glfwTerminate();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
glfwMakeContextCurrent(window);
|
2024-08-08 23:10:01 -06:00
|
|
|
glfwSwapInterval(1); // I think this is vsync??
|
2024-08-08 16:22:48 -06:00
|
|
|
|
|
|
|
if (glewInit() != GLEW_OK) {
|
|
|
|
std::cerr << "Failed to initialize GLEW" << std::endl;
|
|
|
|
glfwDestroyWindow(window);
|
|
|
|
glfwTerminate();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setupImGui(GLFWwindow* window) {
|
|
|
|
IMGUI_CHECKVERSION();
|
|
|
|
ImGui::CreateContext();
|
|
|
|
ImGuiIO& io = ImGui::GetIO(); (void)io;
|
|
|
|
|
|
|
|
ImGui::StyleColorsDark();
|
|
|
|
|
|
|
|
ImGui_ImplGlfw_InitForOpenGL(window, true);
|
|
|
|
ImGui_ImplOpenGL3_Init("#version 130");
|
|
|
|
}
|
|
|
|
|
|
|
|
void renderImGui() {
|
2024-08-08 23:24:29 -06:00
|
|
|
// Calculate time per beat based on BPM
|
|
|
|
float beat_interval = 60.0f / bpm;
|
|
|
|
|
2024-08-08 22:56:58 -06:00
|
|
|
// Temporary variables to hold the slider values
|
|
|
|
int channel1 = buffer[0];
|
|
|
|
int channel2 = buffer[1];
|
|
|
|
int channel3 = buffer[2];
|
|
|
|
|
2024-08-08 16:22:48 -06:00
|
|
|
// Create a window
|
|
|
|
ImGui::Begin("DMX Controller");
|
|
|
|
|
2024-08-09 00:33:15 -06:00
|
|
|
// Create buttons to export and load markers
|
2024-08-08 23:56:59 -06:00
|
|
|
if (ImGui::Button("Export Lightshow")) {
|
2024-08-09 00:33:15 -06:00
|
|
|
|
|
|
|
const char* filterPatterns[2] = { "*.ls", "*.json" };
|
|
|
|
const char* saveFileName = tinyfd_saveFileDialog(
|
|
|
|
"Save As", // Dialog title
|
|
|
|
"lightshow.ls", // Default file name
|
|
|
|
2, // Number of filter patterns
|
|
|
|
filterPatterns, // Filter patterns
|
|
|
|
NULL // Single filter description (optional)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (saveFileName) {
|
|
|
|
std::cout << "File saved as: " << saveFileName << std::endl;
|
|
|
|
exportMarkersToFile(saveFileName);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
std::cout << "Save operation was canceled." << std::endl;
|
|
|
|
}
|
2024-08-08 23:56:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::SameLine();
|
|
|
|
|
|
|
|
if (ImGui::Button("Load Lightshow")) {
|
2024-08-09 00:33:15 -06:00
|
|
|
const char* filterPatterns[2] = { "*.ls", "*.json" };
|
|
|
|
std::string filename = tinyfd_openFileDialog("Select a file", "", 2, filterPatterns, NULL, 0);
|
|
|
|
if (!filename.empty()) {
|
|
|
|
loadMarkersFromFile(filename);
|
|
|
|
}
|
2024-08-08 23:56:59 -06:00
|
|
|
}
|
|
|
|
|
2024-08-09 00:33:15 -06:00
|
|
|
|
2024-08-08 16:22:48 -06:00
|
|
|
// Create sliders for the first three DMX channels
|
2024-08-08 22:56:58 -06:00
|
|
|
ImGui::SliderInt("Channel 1", &channel1, 0, 255);
|
|
|
|
ImGui::SliderInt("Channel 2", &channel2, 0, 255);
|
|
|
|
ImGui::SliderInt("Channel 3", &channel3, 0, 255);
|
|
|
|
|
|
|
|
// Assign the slider values back to the DMX buffer
|
|
|
|
buffer[0] = static_cast<unsigned char>(channel1);
|
|
|
|
buffer[1] = static_cast<unsigned char>(channel2);
|
|
|
|
buffer[2] = static_cast<unsigned char>(channel3);
|
2024-08-08 16:22:48 -06:00
|
|
|
|
2024-08-08 23:24:29 -06:00
|
|
|
// Allow user to set the total duration of the timeline and BPM
|
2024-08-08 23:10:01 -06:00
|
|
|
ImGui::SliderFloat("Timeline Duration (seconds)", &timeline_duration, 1.0f, 300.0f);
|
2024-08-08 23:24:29 -06:00
|
|
|
ImGui::SliderFloat("BPM", &bpm, 30.0f, 240.0f);
|
2024-08-08 23:10:01 -06:00
|
|
|
|
2024-08-08 23:24:29 -06:00
|
|
|
// Play/Pause button
|
2024-08-08 23:10:01 -06:00
|
|
|
if (ImGui::Button(is_playing ? "Pause" : "Play")) {
|
|
|
|
is_playing = !is_playing;
|
|
|
|
if (is_playing) {
|
2024-08-08 23:36:30 -06:00
|
|
|
playback_start_time = static_cast<float>(glfwGetTime()) - timeline_position;
|
2024-08-08 23:10:01 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-08 23:24:29 -06:00
|
|
|
// Time Slider (Timeline Position)
|
|
|
|
ImGui::SliderFloat("Timeline Position", &timeline_position, 0.0f, timeline_duration, "%.2f sec");
|
2024-08-08 23:10:01 -06:00
|
|
|
|
2024-08-08 23:24:29 -06:00
|
|
|
// Custom timeline rendering with grid lines
|
|
|
|
ImGui::Text("Timeline");
|
|
|
|
ImGui::PushID("CustomTimeline");
|
|
|
|
ImGui::BeginChild("##Timeline", ImVec2(ImGui::GetContentRegionAvail().x, 100), true);
|
|
|
|
|
|
|
|
// Draw grid lines for beats
|
|
|
|
ImDrawList* draw_list = ImGui::GetWindowDrawList();
|
|
|
|
ImVec2 cursor_pos = ImGui::GetCursorScreenPos();
|
|
|
|
float timeline_width = ImGui::GetContentRegionAvail().x;
|
|
|
|
|
|
|
|
for (int i = 0; i <= static_cast<int>(timeline_duration / beat_interval); ++i) {
|
|
|
|
float beat_position = i * beat_interval * zoom_level - pan_offset;
|
|
|
|
float x_position = cursor_pos.x + (beat_position / timeline_duration) * timeline_width;
|
|
|
|
|
|
|
|
if (x_position >= cursor_pos.x && x_position <= cursor_pos.x + timeline_width) {
|
|
|
|
// Draw major beat line
|
|
|
|
if (i % 4 == 0) {
|
|
|
|
draw_list->AddLine(ImVec2(x_position, cursor_pos.y), ImVec2(x_position, cursor_pos.y + 100), IM_COL32(255, 0, 0, 255), 2.0f);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Draw minor beat line
|
|
|
|
draw_list->AddLine(ImVec2(x_position, cursor_pos.y), ImVec2(x_position, cursor_pos.y + 100), IM_COL32(255, 255, 255, 255), 1.0f);
|
2024-08-08 23:10:01 -06:00
|
|
|
}
|
|
|
|
}
|
2024-08-08 23:24:29 -06:00
|
|
|
}
|
2024-08-08 23:10:01 -06:00
|
|
|
|
2024-08-08 23:46:20 -06:00
|
|
|
// Draw the current timeline position marker
|
|
|
|
float position_marker = (timeline_position * zoom_level - pan_offset) / timeline_duration * timeline_width;
|
|
|
|
draw_list->AddLine(ImVec2(cursor_pos.x + position_marker, cursor_pos.y), ImVec2(cursor_pos.x + position_marker, cursor_pos.y + 100), IM_COL32(0, 0, 255, 255), 2.0f);
|
|
|
|
|
2024-08-08 23:24:29 -06:00
|
|
|
// Allow adding markers with snapping to grid
|
|
|
|
if (ImGui::Button("Add Marker")) {
|
|
|
|
float snap_position = std::round(timeline_position / beat_interval) * beat_interval;
|
|
|
|
markers.emplace_back(snap_position / timeline_duration, buffer[0], buffer[1], buffer[2]);
|
2024-08-08 23:10:01 -06:00
|
|
|
}
|
|
|
|
|
2024-08-08 23:24:29 -06:00
|
|
|
// Draw markers on the timeline
|
|
|
|
for (const auto& marker : markers) {
|
|
|
|
float marker_position = std::get<0>(marker) * timeline_duration * zoom_level - pan_offset;
|
|
|
|
float x_position = cursor_pos.x + (marker_position / timeline_duration) * timeline_width;
|
|
|
|
|
|
|
|
if (x_position >= cursor_pos.x && x_position <= cursor_pos.x + timeline_width) {
|
|
|
|
draw_list->AddRectFilled(ImVec2(x_position - 2.0f, cursor_pos.y), ImVec2(x_position + 2.0f, cursor_pos.y + 100), IM_COL32(0, 255, 0, 255));
|
|
|
|
}
|
2024-08-08 23:10:01 -06:00
|
|
|
}
|
|
|
|
|
2024-08-08 23:24:29 -06:00
|
|
|
ImGui::EndChild();
|
|
|
|
ImGui::PopID();
|
|
|
|
|
2024-08-08 23:36:30 -06:00
|
|
|
// Playback logic: Update timeline position based on playback time
|
|
|
|
if (is_playing) {
|
|
|
|
current_time = static_cast<float>(glfwGetTime()) - playback_start_time;
|
|
|
|
timeline_position = current_time;
|
|
|
|
|
|
|
|
// Trigger markers during playback
|
|
|
|
for (const auto& marker : markers) {
|
|
|
|
float marker_time = std::get<0>(marker) * timeline_duration;
|
|
|
|
if (current_time >= marker_time && current_time < marker_time + 0.025f) { // Slight tolerance for timing precision
|
|
|
|
buffer[0] = std::get<1>(marker);
|
|
|
|
buffer[1] = std::get<2>(marker);
|
|
|
|
buffer[2] = std::get<3>(marker);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop playback when the timeline reaches the end
|
|
|
|
if (timeline_position >= timeline_duration) {
|
|
|
|
is_playing = false;
|
|
|
|
timeline_position = timeline_duration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Display added markers with their RGB values and time
|
|
|
|
for (const auto& marker : markers) {
|
|
|
|
float marker_time = std::get<0>(marker) * timeline_duration;
|
|
|
|
ImGui::Text("Marker at %.2f sec: R=%d, G=%d, B=%d",
|
|
|
|
marker_time,
|
|
|
|
std::get<1>(marker),
|
|
|
|
std::get<2>(marker),
|
|
|
|
std::get<3>(marker));
|
|
|
|
}
|
|
|
|
|
2024-08-08 23:24:29 -06:00
|
|
|
// Place Zoom and Pan sliders below the timeline to avoid overlap
|
|
|
|
ImGui::SliderFloat("Zoom", &zoom_level, 0.1f, 10.0f);
|
|
|
|
ImGui::SliderFloat("Pan", &pan_offset, 0.0f, timeline_duration * zoom_level);
|
|
|
|
|
2024-08-08 16:22:48 -06:00
|
|
|
ImGui::End();
|
2024-08-08 23:10:01 -06:00
|
|
|
}
|