136 lines
4.2 KiB
C++
136 lines
4.2 KiB
C++
/*
|
|
|
|
__ __ _ _____ ____ _____ __
|
|
| \/ | / \|_ _| _ \|_ _\ \/ /
|
|
| |\/| | / _ \ | | | |_) || | \ /
|
|
| | | |/ ___ \| | | _ < | | / \
|
|
|_| |_/_/ \_\_| |_| \_\___/_/\_\
|
|
|
|
|
|
Script to display a simple square on the matrix LED
|
|
|
|
Pour compiler:
|
|
g++ -Iinclude -Llib test_forms_infinite.cc -o test_forms_infinite -lrgbmatrix
|
|
|
|
Pour le lancer:
|
|
sudo /var/www/moduleair_pro_4g/matrix/test_forms_infinite
|
|
*/
|
|
|
|
#include "led-matrix.h"
|
|
#include "graphics.h"
|
|
#include <signal.h>
|
|
#include <unistd.h>
|
|
#include <vector>
|
|
#include <cstdlib> // For rand()
|
|
#include <ctime> // For time()
|
|
|
|
using rgb_matrix::RGBMatrix;
|
|
using rgb_matrix::Canvas;
|
|
using rgb_matrix::Color;
|
|
|
|
// Global flag that can be used to exit the loop
|
|
volatile bool running = true;
|
|
|
|
// Signal handler to catch Ctrl+C
|
|
static void InterruptHandler(int signo) {
|
|
running = false;
|
|
}
|
|
|
|
int main(int argc, char *argv[]) {
|
|
RGBMatrix::Options defaults;
|
|
|
|
defaults.hardware_mapping = "moduleair_pinout";
|
|
defaults.rows = 64;
|
|
defaults.cols = 128;
|
|
defaults.chain_length = 1;
|
|
defaults.parallel = 1;
|
|
defaults.row_address_type = 3;
|
|
defaults.show_refresh_rate = true;
|
|
defaults.brightness = 100;
|
|
defaults.pwm_bits = 1;
|
|
defaults.panel_type = "FM6126A";
|
|
defaults.disable_hardware_pulsing = false;
|
|
|
|
// Set up signal handler for clean exit
|
|
signal(SIGTERM, InterruptHandler);
|
|
signal(SIGINT, InterruptHandler);
|
|
|
|
// Seed the random number generator
|
|
srand(time(NULL));
|
|
|
|
Canvas *canvas = RGBMatrix::CreateFromFlags(&argc, &argv, &defaults);
|
|
if (canvas == NULL)
|
|
return 1;
|
|
|
|
// Get canvas dimensions for position constraints
|
|
int canvas_width = canvas->width();
|
|
int canvas_height = canvas->height();
|
|
|
|
// Square properties
|
|
int square_size = 8; // Size of the square (8x8)
|
|
int current_x = 10; // Initial X position
|
|
int current_y = 10; // Initial Y position
|
|
|
|
// Create an array of colors to cycle through
|
|
std::vector<Color> colors = {
|
|
Color(255, 0, 0), // Red
|
|
Color(0, 255, 0), // Green
|
|
Color(0, 0, 255), // Blue
|
|
Color(255, 255, 0), // Yellow
|
|
Color(0, 255, 255), // Cyan
|
|
Color(255, 0, 255), // Magenta
|
|
Color(255, 255, 255), // White
|
|
Color(255, 127, 0), // Orange
|
|
Color(127, 0, 255) // Purple
|
|
};
|
|
|
|
int color_index = 0;
|
|
time_t last_change = time(NULL);
|
|
|
|
// Draw initial square
|
|
Color current_color = colors[color_index];
|
|
for (int x = current_x; x < current_x + square_size; ++x) {
|
|
for (int y = current_y; y < current_y + square_size; ++y) {
|
|
canvas->SetPixel(x, y, current_color.r, current_color.g, current_color.b);
|
|
}
|
|
}
|
|
|
|
// Run indefinitely until interrupted
|
|
while (running) {
|
|
// Check if it's time to change color and position (every 2 seconds)
|
|
time_t now = time(NULL);
|
|
if (now - last_change >= 2) {
|
|
// Clear the previous square
|
|
for (int x = current_x; x < current_x + square_size; ++x) {
|
|
for (int y = current_y; y < current_y + square_size; ++y) {
|
|
canvas->SetPixel(x, y, 0, 0, 0); // Clear to black
|
|
}
|
|
}
|
|
|
|
// Move to the next color
|
|
color_index = (color_index + 1) % colors.size();
|
|
current_color = colors[color_index];
|
|
|
|
// Generate new random position, ensuring the square stays fully on screen
|
|
current_x = rand() % (canvas_width - square_size);
|
|
current_y = rand() % (canvas_height - square_size);
|
|
|
|
// Draw the square with the new color at the new position
|
|
for (int x = current_x; x < current_x + square_size; ++x) {
|
|
for (int y = current_y; y < current_y + square_size; ++y) {
|
|
canvas->SetPixel(x, y, current_color.r, current_color.g, current_color.b);
|
|
}
|
|
}
|
|
|
|
last_change = now;
|
|
}
|
|
|
|
// Small sleep to prevent using 100% CPU
|
|
usleep(100000); // 100ms sleep
|
|
}
|
|
|
|
// Clean up when interrupted
|
|
canvas->Clear();
|
|
delete canvas;
|
|
return 0;
|
|
} |