/*
 ____  _        _    _   _ _  __
| __ )| |      / \  | \ | | |/ /
|  _ \| |     / _ \ |  \| | ' / 
| |_) | |___ / ___ \| |\  | . \ 
|____/|_____/_/   \_\_| \_|_|\_\

Simple blank screen (black) for LED matrix
Used when user wants to turn off the display

Pour compiler:
g++ -I/var/www/moduleair_pro_4g/matrix/include -L/var/www/moduleair_pro_4g/matrix/lib /var/www/moduleair_pro_4g/matrix/screenSensors/blank_screen.cc -o /var/www/moduleair_pro_4g/matrix/screenSensors/blank_screen -lrgbmatrix

Pour lancer:
sudo /var/www/moduleair_pro_4g/matrix/screenSensors/blank_screen
*/

#include "led-matrix.h"
#include "graphics.h"
#include <unistd.h>
#include <iostream>
#include <signal.h>
#include <atomic>

using rgb_matrix::RGBMatrix;
using rgb_matrix::Canvas;

std::atomic<bool> running(true);

void signal_handler(int signum) {
    running = false;
}

int main(int argc, char *argv[]) {
    std::cout << "Blank Screen Display started" << std::endl;

    // Handle signals for graceful exit
    signal(SIGINT, signal_handler);
    signal(SIGTERM, signal_handler);

    // Initialize LED matrix
    std::cout << "Initializing LED matrix..." << std::endl;
    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 = false;
    defaults.brightness = 100;
    defaults.pwm_bits = 1;
    defaults.panel_type = "FM6126A";
    defaults.disable_hardware_pulsing = false;

    rgb_matrix::RuntimeOptions runtime_opt;
    runtime_opt.gpio_slowdown = 4;
    runtime_opt.daemon = 0;
    runtime_opt.drop_privileges = 0;

    Canvas *canvas = RGBMatrix::CreateFromOptions(defaults, runtime_opt);
    if (canvas == NULL) {
        std::cerr << "Error creating LED matrix canvas" << std::endl;
        return 1;
    }
    std::cout << "LED matrix initialized successfully" << std::endl;

    // Clear the screen (turn all pixels black)
    canvas->Clear();
    std::cout << "Screen cleared - all pixels black" << std::endl;

    // Main loop - just keep the screen blank
    std::cout << "Blank screen active - press Ctrl+C to exit" << std::endl;
    while (running) {
        // Ensure screen stays clear
        canvas->Clear();
        
        // Sleep for 5 seconds before next clear
        for (int i = 0; i < 5 && running; i++) {
            sleep(1);
        }
    }

    // Clean up
    std::cout << "Program terminating, cleaning up..." << std::endl;
    canvas->Clear();
    delete canvas;
    std::cout << "Blank Screen Display terminated" << std::endl;
    return 0;
}