#!/bin/bash
# Buzzer Test Script for reTerminal DM4
# Tests various buzzer patterns and functions

BUZZER_PATH='/sys/class/leds/usr-buzzer/brightness'

echo "=========================================="
echo "  reTerminal DM4 Buzzer Test Script"
echo "=========================================="
echo ""

# Function to play a beep
beep() {
    local duration=${1:-0.2}
    echo 1 | sudo tee $BUZZER_PATH > /dev/null 2>&1
    sleep $duration
    echo 0 | sudo tee $BUZZER_PATH > /dev/null 2>&1
}

# Function to blink buzzer
blink() {
    local count=${1:-3}
    local on_time=${2:-0.1}
    local off_time=${3:-0.1}
    
    for i in $(seq 1 $count); do
        echo 1 | sudo tee $BUZZER_PATH > /dev/null 2>&1
        sleep $on_time
        echo 0 | sudo tee $BUZZER_PATH > /dev/null 2>&1
        sleep $off_time
    done
}

# Test 1: Single beep
echo "Test 1: Single beep (0.2s)"
beep 0.2
sleep 0.5

# Test 2: Double beep
echo "Test 2: Double beep"
blink 2 0.1 0.1
sleep 0.5

# Test 3: Triple beep
echo "Test 3: Triple beep"
blink 3 0.1 0.1
sleep 0.5

# Test 4: Long beep
echo "Test 4: Long beep (0.5s)"
beep 0.5
sleep 0.5

# Test 5: Rapid beeps
echo "Test 5: Rapid beeps (5x)"
blink 5 0.05 0.05
sleep 0.5

# Test 6: Slow beeps
echo "Test 6: Slow beeps (3x)"
blink 3 0.3 0.3
sleep 0.5

# Test 7: Success pattern (2 short)
echo "Test 7: Success pattern"
blink 2 0.1 0.1
sleep 0.5

# Test 8: Error pattern (3 fast)
echo "Test 8: Error pattern"
blink 3 0.05 0.05
sleep 0.5

# Ensure buzzer is off
echo 0 | sudo tee $BUZZER_PATH > /dev/null 2>&1

echo ""
echo "=========================================="
echo "  Buzzer test complete!"
echo "=========================================="
echo ""
echo "Current buzzer status: $(cat $BUZZER_PATH) (0=OFF, 1=ON)"
