Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
529c14c
Added #pragma once for content-streamer.h
sshcrack Mar 29, 2024
ddb1486
added cmakelists.txt
sshcrack Apr 2, 2024
35da71b
Merge pull request #1 from hzeller/master
sshcrack Nov 10, 2024
6e68634
Merge pull request #2 from hzeller/master
sshcrack Feb 3, 2025
91121a2
remove idea stuff and add install to cmake
sshcrack Feb 4, 2025
facde6c
add mock
sshcrack Feb 10, 2025
9d082f8
update install loc
sshcrack Feb 15, 2025
aaafa1d
try clearing memory on destroy
sshcrack Feb 27, 2025
e07ea67
fix memory leak in CIEMapColor
sshcrack Feb 27, 2025
576bf40
Merge pull request #3 from hzeller/master
sshcrack Feb 27, 2025
80469a5
remove install from CMakeLists.txt
sshcrack Feb 27, 2025
59163ce
lower cpp standard
sshcrack Feb 27, 2025
0c731fc
forgot to lower cpp standard for target compile features
sshcrack Feb 27, 2025
b7b215f
optimize allocations in example 'CopyImageToCanvas'
sshcrack Feb 27, 2025
c7b4023
add cmake lists
sshcrack Feb 28, 2025
d77349f
remove mock rpi from definitions
sshcrack Feb 28, 2025
55384d7
add mock to cmakelists
sshcrack Feb 28, 2025
173205a
adding emulator
sshcrack Mar 3, 2025
d62cfd0
fix guard issues
sshcrack Mar 3, 2025
2f55661
before emulator refactor
sshcrack Mar 3, 2025
0a1c139
also start refresh thread for matrix-factory
sshcrack Mar 3, 2025
68278b7
fix crash on larger emulation windows
sshcrack Mar 3, 2025
68f75d0
disable emulator by default
sshcrack Mar 3, 2025
e55da94
remove emulator demo from cmakelists
sshcrack Mar 3, 2025
a05c2fa
Merge branch 'master' into mock-rpi
sshcrack Mar 3, 2025
27122d4
fix segmentation fault when --led-emulator is set via command line
sshcrack Mar 3, 2025
63a05b5
fix sigfault for width and height on emulator frame buffer
sshcrack Mar 3, 2025
0df827f
add serialize / deserialize methods to emulator
sshcrack Mar 3, 2025
dd5f5ff
undo changes to example
sshcrack Mar 3, 2025
898abff
merge into master
sshcrack Mar 7, 2025
ee01731
Merge pull request #4 from sshcrack/mock-rpi
sshcrack Mar 7, 2025
9cda73b
Revert "merge into master"
sshcrack Mar 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
*.so
.envrc
.direnv
build
15 changes: 15 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"files.associations": {
"*.scss": "scss",
"functional": "cpp",
"chrono": "cpp",
"*.old": "cpp",
"atomic": "cpp",
"*.tcc": "cpp",
"iomanip": "cpp",
"istream": "cpp",
"ostream": "cpp",
"format": "cpp"
},
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools"
}
62 changes: 62 additions & 0 deletions CMakeLists.txt
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this file for now, don't know about the other pull request for CMake

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
cmake_minimum_required(VERSION 3.5.0)
project(rpi-rgb-led-matrix VERSION 0.1.0)

set(CMAKE_CXX_STANDARD 14)

# Option to enable/disable emulator functionality
option(ENABLE_EMULATOR "Enable LED matrix emulator functionality" OFF)

# Define core library source files
set(MATRIX_CORE_SOURCES
lib/gpio.cc
lib/led-matrix.cc
lib/framebuffer.cc
lib/thread.cc
lib/pixel-mapper.cc
lib/multiplex-mappers.cc
lib/hardware-mapping.c
lib/content-streamer.cc
lib/bdf-font.cc
lib/graphics.cc
lib/options-initialize.cc
lib/led-matrix-c.cc
lib/matrix-factory.cc
)

# Set up emulator if enabled
set(MATRIX_SOURCES ${MATRIX_CORE_SOURCES})

if(ENABLE_EMULATOR)
# Add emulator source
list(APPEND MATRIX_SOURCES lib/emulator.cc)

message(STATUS "Building with emulator support")
else()
message(STATUS "Building without emulator support")
endif()

add_library(${PROJECT_NAME} SHARED ${MATRIX_SOURCES})

# Main Target Compile
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_14)

# Include directories
target_include_directories(${PROJECT_NAME}
PRIVATE
# where the library itself will look for its internal headers
${CMAKE_CURRENT_SOURCE_DIR}/lib
PUBLIC
# where top-level project will look for the library's public headers
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
# where external projects will look for the library's public headers
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)

# Export ENABLE_EMULATOR define to consumers of the library
if(ENABLE_EMULATOR)
target_compile_definitions(${PROJECT_NAME} PUBLIC ENABLE_EMULATOR)

# Find SDL2 for the emulator
find_package(SDL2 REQUIRED)
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2)
endif()
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@ RGB_LIBDIR=./lib
RGB_LIBRARY_NAME=rgbmatrix
RGB_LIBRARY=$(RGB_LIBDIR)/lib$(RGB_LIBRARY_NAME).a

# Enable emulator support? (0=no, 1=yes)
ENABLE_EMULATOR?=0

# Some language bindings.
PYTHON_LIB_DIR=bindings/python
CSHARP_LIB_DIR=bindings/c\#

all : $(RGB_LIBRARY)
$(MAKE) -C examples-api-use
$(MAKE) -C examples
ifeq ($(ENABLE_EMULATOR), 1)
$(MAKE) -C examples ENABLE_EMULATOR=1 emulator-demo
endif

$(RGB_LIBRARY): FORCE
$(MAKE) -C $(RGB_LIBDIR)
$(MAKE) -C examples-api-use
$(MAKE) -C $(RGB_LIBDIR) ENABLE_EMULATOR=$(ENABLE_EMULATOR)

clean:
$(MAKE) -C lib clean
$(MAKE) -C utils clean
$(MAKE) -C examples-api-use clean
$(MAKE) -C examples clean
$(MAKE) -C $(PYTHON_LIB_DIR) clean

build-csharp:
Expand Down
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
emulator-demo
66 changes: 66 additions & 0 deletions examples/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Makefile for the emulator-demo program

# Location of the RGB Matrix Library
RGB_LIB_DISTRIBUTION=..
RGB_INCDIR=$(RGB_LIB_DISTRIBUTION)/include
RGB_LIBDIR=$(RGB_LIB_DISTRIBUTION)/lib

# Compiler options
CC = g++
CFLAGS = -Wall -O3 -g -Wextra -Wno-unused-parameter
CXXFLAGS = $(CFLAGS)
LDFLAGS = -L$(RGB_LIBDIR)
LDLIBS = -lrgbmatrix -lrt -lm -lpthread

# Check if ENABLE_EMULATOR is defined
ifdef ENABLE_EMULATOR
CFLAGS += -DENABLE_EMULATOR
LDLIBS += -lSDL2
EMULATOR_TARGETS = emulator-demo
EMULATOR_OBJECTS = emulator-demo.o
EMULATOR_DESC = --led-emulator-scale=12 --led-emulator-refresh=60
else
EMULATOR_TARGETS =
EMULATOR_OBJECTS =
endif

# If you're using a Raspberry Pi, uncomment to use hardware
# Otherwise stick with emulator mode
#HARDWARE_DESC=--led-cols=64 --led-rows=32 --led-chain=1
#HARDWARE_DESC+= --led-parallel=1 --led-slowdown-gpio=4
# Add any other flags you might need

# Default build target
all : $(EMULATOR_TARGETS)

# Only build emulator-demo with SDL2 support when ENABLE_EMULATOR is defined
emulator-demo: emulator-demo.o
$(CC) $(CXXFLAGS) emulator-demo.o $(LDFLAGS) -o $@ $(LDLIBS)

# Rule for building object files
%.o : %.cc
$(CC) -I$(RGB_INCDIR) $(CXXFLAGS) -c -o $@ $<

# Install SDL2 dependency (for Ubuntu/Debian)
install-deps:
sudo apt-get update
sudo apt-get install -y libsdl2-dev

# Clean up built files
clean:
rm -f $(EMULATOR_TARGETS) $(EMULATOR_OBJECTS)

# Run the demo using the emulator (only if ENABLE_EMULATOR is defined)
ifdef ENABLE_EMULATOR
run: emulator-demo
./emulator-demo $(EMULATOR_DESC)
else
run:
@echo "Emulator not enabled. Build with ENABLE_EMULATOR=1 to use the emulator."
endif

# Run the demo on hardware (if you have the physical matrix)
run-hw: emulator-demo
./emulator-demo $(HARDWARE_DESC) --led-no-emulator

.PHONY: all clean install-deps run run-hw
174 changes: 174 additions & 0 deletions examples/emulator-demo.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
// Copyright (C) 2023 Hendrik
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation version 2.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://gnu.org/licenses/gpl-2.0.txt>

#ifdef ENABLE_EMULATOR

#include "matrix-factory.h"
#include "graphics.h"

#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <ostream>
#include <iostream>

using namespace rgb_matrix;

volatile bool interrupt_received = false;
static void InterruptHandler(int signo) {
interrupt_received = true;
}

// Demo that draws a circle bouncing around the screen.
// Shows how the MatrixFactory can be used to create either a real
// LED matrix or an emulator depending on command line flags.
int main(int argc, char *argv[]) {
// Setup signal handlers
signal(SIGTERM, InterruptHandler);
signal(SIGINT, InterruptHandler);

// Set up matrix factory options
MatrixFactory::Options options;

// Set default options
options.led_options.cols = 64;
options.led_options.rows = 64;
options.led_options.chain_length = 2;
options.led_options.parallel = 2;
options.led_options.hardware_mapping = "regular"; // Add this line to ensure hardware mapping is set
options.emulator_options.display_scale = 5; // Larger window for emulator
options.emulator_options.window_title = "LED Matrix Emulator Demo";

// Parse options from command line
if (!MatrixFactory::ParseOptionsFromFlags(&argc, &argv, &options)) {
// Show usage and exit
fprintf(stderr, "Usage: %s [options]\n", argv[0]);
fprintf(stderr, "Options:\n");
MatrixFactory::PrintMatrixFactoryFlags(stderr, options);
return 1;
}

// Create matrix from options (either real or emulated)
RGBMatrixBase *matrix = MatrixFactory::CreateMatrix(options);
if (matrix == NULL) {
fprintf(stderr, "Failed to create matrix\n");
return 1;
}

// Get canvas dimensions
const int width = matrix->width();
const int height = matrix->height();

std::cout << "Canvas width: " << width << ", height: " << height << std::endl << std::flush;

// Create a canvas for double-buffering
FrameCanvas *offscreen = matrix->CreateFrameCanvas();
if (offscreen == NULL) {
fprintf(stderr, "Failed to create offscreen canvas\n");
delete matrix;
return 1;
}

// Setup drawing parameters
float radius = height / 4;
float center_x = width / 2;
float center_y = height / 2;
float velocity_x = 0.5; // pixels per frame
float velocity_y = 0.2; // pixels per frame

// Animation state variables
float x = center_x;
float y = center_y;
uint8_t r = 255, g = 0, b = 0;

printf("Press Ctrl+C to exit\n");

// Main animation loop
while (!interrupt_received) {
// Clear the canvas and draw a circle
offscreen->Fill(0, 0, 0);

// Draw the bouncy circle
DrawCircle(offscreen, x, y, radius, Color(r, g, b));

// Update position
x += velocity_x;
y += velocity_y;

// Bounce off edges with a little elasticity
if (x - radius <= 0) {
x = radius;
velocity_x = -velocity_x * 0.9;

// Change color on bounce
r = rand() % 256;
g = rand() % 256;
b = rand() % 256;
}
if (x + radius >= width - 1) {
x = width - 1 - radius;
velocity_x = -velocity_x * 0.9;

// Change color on bounce
r = rand() % 256;
g = rand() % 256;
b = rand() % 256;
}
if (y - radius <= 0) {
y = radius;
velocity_y = -velocity_y * 0.9;

// Change color on bounce
r = rand() % 256;
g = rand() % 256;
b = rand() % 256;
}
if (y + radius >= height - 1) {
y = height - 1 - radius;
velocity_y = -velocity_y * 0.9;

// Change color on bounce
r = rand() % 256;
g = rand() % 256;
b = rand() % 256;
}

// Swap buffers
offscreen = matrix->SwapOnVSync(offscreen);

// Sleep for a bit to control animation speed
usleep(16000); // ~60 Hz refresh rate
}

// Cleanup
matrix->Clear();
delete matrix;

printf("\nAnimation stopped. Exiting.\n");
return 0;
}

#else
// Compiled without emulator support
#include <stdio.h>

int main(int argc, char *argv[]) {
fprintf(stderr, "This demo requires ENABLE_EMULATOR to be defined.\n");
fprintf(stderr, "Please compile with 'make ENABLE_EMULATOR=1'\n");
return 1;
}
#endif // ENABLE_EMULATOR
2 changes: 1 addition & 1 deletion include/content-streamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
// These abstractions are used in util/led-image-viewer.cc to read and
// write such animations to disk. It is also used in util/video-viewer.cc
// to write a version to disk that then can be played with the led-image-viewer.

#ifndef RPI_CONTENT_STREAMER_H
#define RPI_CONTENT_STREAMER_H

#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
Expand Down
Loading