startCustomBlink method

Future<void> startCustomBlink(
  1. BlinkPattern pattern
)

Starts a custom blink pattern. The pattern is a list of durations in milliseconds. The torch alternates between on and off states for each duration. The pattern repeats indefinitely until stopped.

Implementation

Future<void> startCustomBlink(BlinkPattern pattern) async {
  if (pattern.isEmpty) {
    throw ArgumentError('Pattern cannot be empty');
  }

  _cancelAllTimers();

  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;
    }

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

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