startSOS method

Future<void> startSOS()

Starts SOS mode using Morse code pattern (... --- ...). Uses standard Morse code timing:

  • Dot (short): 200ms on, 200ms off
  • Dash (long): 600ms on, 200ms off
  • Letter space: 600ms off
  • Word space: 1400ms off

Implementation

Future<void> startSOS() async {
  _cancelAllTimers();

  // Morse code timing (in milliseconds)
  const dotDuration = 200;
  const dashDuration = 600;
  const intraCharSpace = 200;
  const letterSpace = 600;
  const wordSpace = 1400;

  // SOS pattern: ... --- ...
  final pattern = [
    // S (...)
    dotDuration, intraCharSpace,
    dotDuration, intraCharSpace,
    dotDuration, letterSpace,
    // O (---)
    dashDuration, intraCharSpace,
    dashDuration, intraCharSpace,
    dashDuration, letterSpace,
    // S (...)
    dotDuration, intraCharSpace,
    dotDuration, intraCharSpace,
    dotDuration, wordSpace,
  ];

  int patternIndex = 0;
  bool isTorchOn = false;

  Future<void> executePattern() async {
    if (patternIndex >= pattern.length) {
      patternIndex = 0; // Repeat the pattern
    }

    final duration = pattern[patternIndex];
    patternIndex++;

    if (isTorchOn) {
      await TorchFlashlight.disableTorchFlashlight().catchError(
        (e) => debugPrint('Error disabling torch: $e'),
      );
      isTorchOn = false;
    } else {
      await TorchFlashlight.enableTorchFlashlight().catchError(
        (e) => debugPrint('Error enabling torch: $e'),
      );
      isTorchOn = true;
    }

    _sosTimer = Timer(Duration(milliseconds: duration), executePattern);
  }

  // Start the pattern
  executePattern();
  _isEnabled = true;
}