# reTerminal DM4 Buzzer Test Guide

## Overview

The reTerminal DM4 has a built-in buzzer located at the bottom right corner of the screen. The buzzer is controlled via the Linux LED subsystem and can be used for system alerts and notifications.

## Buzzer Device Location

- **Sysfs Path**: `/sys/class/leds/usr-buzzer`
- **Control Method**: LED brightness control (0 = off, 1 = on)
- **Type**: Active buzzer (on/off control)

## Quick Test Methods

### Method 1: Simple On/Off Test

```bash
# Turn buzzer ON
echo 1 | sudo tee /sys/class/leds/usr-buzzer/brightness

# Turn buzzer OFF
echo 0 | sudo tee /sys/class/leds/usr-buzzer/brightness
```

### Method 2: Single Beep

```bash
# Beep for 0.2 seconds
echo 1 | sudo tee /sys/class/leds/usr-buzzer/brightness
sleep 0.2
echo 0 | sudo tee /sys/class/leds/usr-buzzer/brightness
```

### Method 3: Double Beep

```bash
# Double beep pattern
for i in 1 2; do
    echo 1 | sudo tee /sys/class/leds/usr-buzzer/brightness
    sleep 0.1
    echo 0 | sudo tee /sys/class/leds/usr-buzzer/brightness
    sleep 0.1
done
```

### Method 4: Rapid Beeps

```bash
# 5 rapid beeps
for i in {1..5}; do
    echo 1 | sudo tee /sys/class/leds/usr-buzzer/brightness
    sleep 0.05
    echo 0 | sudo tee /sys/class/leds/usr-buzzer/brightness
    sleep 0.05
done
```

## Complete Test Script

A comprehensive test script is available on the device at `/tmp/test_buzzer.sh`. To use it:

```bash
# Run the test script
/tmp/test_buzzer.sh
```

Or create your own script:

```bash
#!/bin/bash
# Buzzer test script for reTerminal DM4

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

echo 'Testing reTerminal DM4 Buzzer'
echo '=============================='
echo ''

# Test 1: Single beep
echo 'Test 1: Single beep (0.2 seconds)'
echo 1 | sudo tee $BUZZER_PATH/brightness > /dev/null
sleep 0.2
echo 0 | sudo tee $BUZZER_PATH/brightness > /dev/null
sleep 0.5

# Test 2: Double beep
echo 'Test 2: Double beep'
for i in 1 2; do
    echo 1 | sudo tee $BUZZER_PATH/brightness > /dev/null
    sleep 0.1
    echo 0 | sudo tee $BUZZER_PATH/brightness > /dev/null
    sleep 0.1
done
sleep 0.5

# Test 3: Long beep
echo 'Test 3: Long beep (0.5 seconds)'
echo 1 | sudo tee $BUZZER_PATH/brightness > /dev/null
sleep 0.5
echo 0 | sudo tee $BUZZER_PATH/brightness > /dev/null
sleep 0.5

# Test 4: Rapid beeps
echo 'Test 4: Rapid beeps (5 beeps)'
for i in {1..5}; do
    echo 1 | sudo tee $BUZZER_PATH/brightness > /dev/null
    sleep 0.05
    echo 0 | sudo tee $BUZZER_PATH/brightness > /dev/null
    sleep 0.05
done

echo ''
echo 'Buzzer test complete!'
```

## Using Timer Trigger (Advanced)

The buzzer can also use the timer trigger for automatic beeping patterns:

```bash
# Set timer trigger
echo timer | sudo tee /sys/class/leds/usr-buzzer/trigger

# Set delay_on and delay_off (in milliseconds)
echo 100 | sudo tee /sys/class/leds/usr-buzzer/delay_on
echo 100 | sudo tee /sys/class/leds/usr-buzzer/delay_off

# Disable timer trigger (return to manual control)
echo none | sudo tee /sys/class/leds/usr-buzzer/trigger
```

## Checking Buzzer Status

```bash
# Check current brightness (0 = off, 1 = on)
cat /sys/class/leds/usr-buzzer/brightness

# Check max brightness
cat /sys/class/leds/usr-buzzer/max_brightness

# Check current trigger
cat /sys/class/leds/usr-buzzer/trigger
```

## Integration Examples

### Python Script

```python
#!/usr/bin/env python3
import time

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

def buzzer_on():
    with open(BUZZER_PATH, 'w') as f:
        f.write('1')

def buzzer_off():
    with open(BUZZER_PATH, 'w') as f:
        f.write('0')

def beep(duration=0.2):
    buzzer_on()
    time.sleep(duration)
    buzzer_off()

# Test
beep(0.2)  # Single beep
time.sleep(0.5)
beep(0.1)  # Short beep
time.sleep(0.1)
beep(0.1)  # Short beep (double beep)
```

### Bash Function

Add to your `~/.bashrc`:

```bash
# Buzzer control function
buzzer() {
    local action=${1:-status}
    local BUZZER='/sys/class/leds/usr-buzzer/brightness'
    
    case $action in
        on)
            echo 1 | sudo tee $BUZZER > /dev/null
            echo "Buzzer ON"
            ;;
        off)
            echo 0 | sudo tee $BUZZER > /dev/null
            echo "Buzzer OFF"
            ;;
        beep)
            local duration=${2:-0.2}
            echo 1 | sudo tee $BUZZER > /dev/null
            sleep $duration
            echo 0 | sudo tee $BUZZER > /dev/null
            ;;
        status)
            local state=$(cat $BUZZER)
            echo "Buzzer is: $([ $state -eq 1 ] && echo 'ON' || echo 'OFF')"
            ;;
        *)
            echo "Usage: buzzer {on|off|beep [duration]|status}"
            ;;
    esac
}
```

Then use:
```bash
buzzer beep 0.2    # Single beep
buzzer on          # Turn on
buzzer off         # Turn off
buzzer status      # Check status
```

## Troubleshooting

### Buzzer Not Working

1. **Check device exists:**
   ```bash
   ls -la /sys/class/leds/usr-buzzer/
   ```

2. **Check permissions:**
   ```bash
   # You need sudo to control the buzzer
   sudo echo 1 > /sys/class/leds/usr-buzzer/brightness
   ```

3. **Check kernel messages:**
   ```bash
   dmesg | grep -i buzzer
   ```

4. **Verify GPIO/device tree:**
   ```bash
   # Check if buzzer is in device tree
   ls /proc/device-tree/ | grep -i buzz
   ```

### No Sound from Buzzer

- The buzzer is a simple active buzzer - it should make a continuous tone when ON
- If no sound, check physical connections (buzzer may be damaged)
- Verify the device is powered on
- Check if buzzer is disabled in device tree or kernel config

## Notes

- **Buzzer is simple**: It can only be ON or OFF - no frequency control
- **Requires sudo**: Writing to sysfs requires root privileges
- **Location**: Bottom right corner of the screen
- **Use case**: System alerts, notifications, error indicators
- **Not for audio**: The buzzer cannot play music or complex sounds - only simple beeps

## Related Documentation

- See `AUDIO-CONFIGURATION-REPORT.md` for complete audio system information
- reTerminal DM4 User Manual for hardware specifications
