#include <WiFi.h>

// --- Configuration ---
const char* ssid = "YOUR_SSID";         // Your WiFi network SSID
const char* password = "YOUR_PASSWORD"; // Your WiFi network password
const int LED_PIN = 15;                 // GPIO pin for the LED
const int BUTTON_PIN = 4;               // GPIO pin for the button
const int DOT_DURATION = 200;           // Duration of a Morse code dot in milliseconds

// Morse code map for numbers 0-9 and the dot character '.'
// The index in the array corresponds to the digit (0-9).
const char* morseCodes[] = {
  "-----", // 0
  ".----", // 1
  "..---", // 2
  "...--", // 3
  "....-", // 4
  ".....", // 5
  "-....", // 6
  "--...", // 7
  "---..", // 8
  "----.", // 9
  ".-.-.-" // . (period)
};

// --- Variables ---
IPAddress privateIP;
bool buttonPressed = false;

// Function prototypes
void blinkMorse(char character);
void blinkDot();
void blinkDash();
void checkButton();
void signalWifiConnected();

void setup() {
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);
  // Use internal pull-up resistor for the button pin
  pinMode(BUTTON_PIN, INPUT_PULLUP);

  Serial.println("\nConnecting to WiFi...");
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(LED_PIN, HIGH); // Quick blink while trying to connect
    delay(100);
    digitalWrite(LED_PIN, LOW);
    delay(400);
    Serial.print(".");
  }

  Serial.println("\nWiFi connected.");
  privateIP = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(privateIP);

  // Call the new function to signal successful connection
  signalWifiConnected();
}

void loop() {
  checkButton();

  if (buttonPressed) {
    Serial.println("Button pressed. Blinking IP in Morse Code: " + privateIP.toString());
    String ipStr = privateIP.toString();

    for (int i = 0; i < ipStr.length(); i++) {
      blinkMorse(ipStr[i]);
      delay(DOT_DURATION * 3); // Gap between characters (3 units)
    }
    
    buttonPressed = false; // Reset the flag after blinking
    Serial.println("Morse code sequence complete. Waiting for button press.");
    delay(DOT_DURATION * 7); // Gap between words/sequences (7 units)
  }
}

// Function to blink the digit '9' twice when WiFi connects
void signalWifiConnected() {
  // '9' in Morse is "----."
  for (int j = 0; j < 2; j++) {
    blinkMorse('9'); 
    // Short pause between the two '9' signals
    delay(DOT_DURATION * 5); 
  }
  Serial.println("WiFi connection signal complete (9 twice).");
}


void checkButton() {
  // Read the state of the button. It's LOW when pressed due to pull-up resistor.
  if (digitalRead(BUTTON_PIN) == LOW) {
    // Debounce delay
    delay(50); 
    if (digitalRead(BUTTON_PIN) == LOW) {
      buttonPressed = true;
      // Wait until the button is released to prevent multiple triggers
      while(digitalRead(BUTTON_PIN) == LOW); 
    }
  }
}

void blinkMorse(char character) {
  int index = -1;
  if (character >= '0' && character <= '9') {
    index = character - '0';
  } else if (character == '.') {
    index = 10; // Index for the period in the morseCodes array
  } else {
    return; // Ignore other characters
  }

  const char* morse = morseCodes[index];
  for (int i = 0; i < strlen(morse); i++) {
    if (morse[i] == '.') {
      blinkDot();
    } else {
      blinkDash();
    }
    delay(DOT_DURATION); // Gap between dots and dashes in a character (1 unit)
  }
}

void blinkDot() {
  digitalWrite(LED_PIN, HIGH);
  delay(DOT_DURATION); // Dot duration (1 unit)
  digitalWrite(LED_PIN, LOW);
}

void blinkDash() {
  digitalWrite(LED_PIN, HIGH);
  delay(DOT_DURATION * 3); // Dash duration (3 units)
  digitalWrite(LED_PIN, LOW);
}
