#include "src/all.c"

__declspec(dllexport) void* main(void* p)
{
    // Initialize hardware
    LED_Init();
    BZR_Init();
    SSD_Init();

    // Morse code for A–Z (1–26)
    const char* morse[26] = {
        ".-", "-...", "-.-.", "-..", ".",      // A–E
        "..-.", "--.", "....", "..", ".---",   // F–J
        "-.-", ".-..", "--", "-.", "---",      // K–O
        ".--.", "--.-", ".-.", "...", "-",     // P–T
        "..-", "...-", ".--", "-..-", "-.--", "--.."  // U–Z
    };

    // SOS → numbers
    int message[3] = {19, 15, 19};

    // Timing (microseconds)
    int DOT  = 200000;   // 200 ms
    int DASH = 600000;   // 600 ms
    int SYMBOL_PAUSE = 200000;
    int LETTER_PAUSE = 800000;

    while (1)
    {
        for (int i = 0; i < 3; i++)
        {
            int num = message[i];
            SSD_SetValue(num);   // show number on SSD

            const char* code = morse[num - 1];

            // Play Morse code for this letter
            for (int j = 0; code[j] != '\0'; j++)
            {
                int duration = (code[j] == '.') ? DOT : DASH;

                // LED + buzzer ON
                LED_SetValue(0, 1);
                BZR_SetValue(600, 800, 1);

                wait(duration);

                // LED + buzzer OFF
                LED_SetValue(0, 0);
                BZR_SetValue(0, 0, 0);

                wait(SYMBOL_PAUSE);
            }

            wait(LETTER_PAUSE);
        }
    }

    return 0;
}
