-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add RPI hardware emulator #1765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sshcrack
wants to merge
32
commits into
hzeller:master
Choose a base branch
from
sshcrack:mock-rpi
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
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 ddb1486
added cmakelists.txt
sshcrack 35da71b
Merge pull request #1 from hzeller/master
sshcrack 6e68634
Merge pull request #2 from hzeller/master
sshcrack 91121a2
remove idea stuff and add install to cmake
sshcrack facde6c
add mock
sshcrack 9d082f8
update install loc
sshcrack aaafa1d
try clearing memory on destroy
sshcrack e07ea67
fix memory leak in CIEMapColor
sshcrack 576bf40
Merge pull request #3 from hzeller/master
sshcrack 80469a5
remove install from CMakeLists.txt
sshcrack 59163ce
lower cpp standard
sshcrack 0c731fc
forgot to lower cpp standard for target compile features
sshcrack b7b215f
optimize allocations in example 'CopyImageToCanvas'
sshcrack c7b4023
add cmake lists
sshcrack d77349f
remove mock rpi from definitions
sshcrack 55384d7
add mock to cmakelists
sshcrack 173205a
adding emulator
sshcrack d62cfd0
fix guard issues
sshcrack 2f55661
before emulator refactor
sshcrack 0a1c139
also start refresh thread for matrix-factory
sshcrack 68278b7
fix crash on larger emulation windows
sshcrack 68f75d0
disable emulator by default
sshcrack e55da94
remove emulator demo from cmakelists
sshcrack a05c2fa
Merge branch 'master' into mock-rpi
sshcrack 27122d4
fix segmentation fault when --led-emulator is set via command line
sshcrack 63a05b5
fix sigfault for width and height on emulator frame buffer
sshcrack 0df827f
add serialize / deserialize methods to emulator
sshcrack dd5f5ff
undo changes to example
sshcrack 898abff
merge into master
sshcrack ee01731
Merge pull request #4 from sshcrack/mock-rpi
sshcrack 9cda73b
Revert "merge into master"
sshcrack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ | |
| *.so | ||
| .envrc | ||
| .direnv | ||
| build | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| emulator-demo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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