/*
Script to launch screens to display compounds
sudo ./screen_sensors --led-no-hardware-pulse 12.5 8.4 3.2 445
ou
sudo /var/www/moduleair_pro_4g/matrix/screen_sensors 12.5 8.4 3.2 445

Pour compiler:
g++ -Iinclude  -Llib screen_sensors.cc -o screen_sensors -lrgbmatrix
*/

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

#include <unistd.h>
#include <string.h>

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

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;            /* Corresponding flag: --led-row-addr-type */
    defaults.show_refresh_rate = true;
    defaults.brightness = 100;
    defaults.panel_type = "FM6126A";                /* Corresponding flag: --led-panel-type */
    defaults.disable_hardware_pulsing = false;                       // Flag: --led-hardware-pulse
    
    rgb_matrix::Color myCYAN(0, 255, 255);   // myCYAN
    rgb_matrix::Color myWHITE(255, 255, 255);   // Blue color for the second line
    rgb_matrix::Color myYELLOW(255, 255, 0);   // Blue color for the second line

    rgb_matrix::Color bg_color(0, 0, 0);   // Background color (black)
    
    int letter_spacing = 0;

    Canvas *canvas = RGBMatrix::CreateFromFlags(&argc, &argv, &defaults);
    if (canvas == NULL)
        return 1;

    // Load font
    rgb_matrix::Font font1;
    if (!font1.LoadFont("/var/www/moduleair_pro_4g/matrix/fonts/6x9.bdf")) return 1;
    rgb_matrix::Font font2;
    if (!font2.LoadFont("/var/www/moduleair_pro_4g/matrix/fonts/9x18B.bdf")) return 1;

    // **********
    // SCREEN 1 : PM10
    // **********

    // Define text for each line
    const char *line1 = "PM10 µg/m³";
    const char *line2 = argv[1];  // Take second argument as line2 text
    const char *line3 = "DEGRADE";

    int x = 0;
    int y1 = font1.baseline()-1;  // baseline =  line on which most characters rest (for 6x9.bdf its 7)
    rgb_matrix::DrawText(canvas, font1, x, y1, myCYAN, &bg_color, line1, letter_spacing);

    int y2 = y1 + font2.baseline();  // Second line position, just below the first line
    rgb_matrix::DrawText(canvas, font2, x, y2, myWHITE, &bg_color, line2, letter_spacing);

    int y3 = y1 + y2 + 4;  // baseline =  line on which most characters rest (for 6x9.bdf its 7)
    rgb_matrix::DrawText(canvas, font1, x, y3, myYELLOW, &bg_color, line3, letter_spacing);

    // **********
    // SCREEN 2 : PM2.5
    // **********

    const char *line4 = "PM2.5 µg/m³";
    const char *line5 = argv[2];  // Take third argument
    const char *line6 = "DEGRADE";
    x = 64;
    rgb_matrix::DrawText(canvas, font1, x, y1, myCYAN, &bg_color, line4, letter_spacing);
    rgb_matrix::DrawText(canvas, font2, x, y2, myWHITE, &bg_color, line5, letter_spacing);
    rgb_matrix::DrawText(canvas, font1, x, y3, myYELLOW, &bg_color, line6, letter_spacing);


    // **********
    // SCREEN 3 : PM1
    // **********
    const char *line7 = "PM2.5 µg/m³";
    const char *line8 = argv[3];  // Take third argument
    const char *line9 = "DEGRADE";
    x = 0;
    y1=y1+32;
    y2=y2+32;
    y3=y3+32;
    rgb_matrix::DrawText(canvas, font1, x, y1, myCYAN, &bg_color, line7, letter_spacing);
    rgb_matrix::DrawText(canvas, font2, x, y2, myWHITE, &bg_color, line8, letter_spacing);
    rgb_matrix::DrawText(canvas, font1, x, y3, myYELLOW, &bg_color, line9, letter_spacing);

    // **********
    // SCREEN 4 : CO2
    // **********
    const char *line10 = "CO₂ ppm";
    const char *line11 = argv[4];  // Take third argument
    const char *line12 = "DEGRADE";
    x = 64;
    rgb_matrix::DrawText(canvas, font1, x, y1, myCYAN, &bg_color, line10, letter_spacing);
    rgb_matrix::DrawText(canvas, font2, x, y2, myWHITE, &bg_color, line11, letter_spacing);
    rgb_matrix::DrawText(canvas, font1, x, y3, myYELLOW, &bg_color, line12, letter_spacing);

    usleep(30000000); // Display for 30 seconds

    // Clean up
    canvas->Clear();
    delete canvas;
    return 0;
}