# Buzzer Control - reTerminal DM4

## Quick Reference

**Device Path:** `/sys/class/leds/usr-buzzer/brightness`

### Basic Commands

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

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

# Check Status
cat /sys/class/leds/usr-buzzer/brightness  # 0=OFF, 1/255=ON
```

### Single Beep

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

---

## Test Scripts

### Bash Script

**Location on device:** `/home/pi/buzzer/test_buzzer.sh`

**Run:**
```bash
ssh guard "/home/pi/buzzer/test_buzzer.sh"
# or
/home/pi/buzzer/test_buzzer.sh
```

**Tests included:**
- Single beep
- Double beep
- Triple beep
- Long beep
- Rapid beeps
- Slow beeps
- Success pattern
- Error pattern

### Python Script

**Location on device:** `/home/pi/buzzer/test_buzzer.py`

**Run:**
```bash
ssh guard "python3 /home/pi/buzzer/test_buzzer.py"
# or
python3 /home/pi/buzzer/test_buzzer.py
```

**Same tests as bash script, with Python implementation**

---

## Python Control

```python
import subprocess
import time

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

def buzzer_on():
    subprocess.run(['sudo', 'tee', BUZZER], input='1', text=True)

def buzzer_off():
    subprocess.run(['sudo', 'tee', BUZZER], input='0', text=True)

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

# Usage
beep(0.2)  # Short beep
beep(0.5)  # Long beep
```

---

## Common Patterns

### Success Alert (2 short beeps)
```bash
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
```

### Error Alert (3 fast beeps)
```bash
for i in 1 2 3; 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
```

### Notification (1 short beep)
```bash
echo 1 | sudo tee /sys/class/leds/usr-buzzer/brightness
sleep 0.2
echo 0 | sudo tee /sys/class/leds/usr-buzzer/brightness
```

---

## Troubleshooting

**Buzzer not working?**
1. Check device: `ls -la /sys/class/leds/usr-buzzer/`
2. Test manually: `echo 1 | sudo tee /sys/class/leds/usr-buzzer/brightness`
3. Verify permissions: Requires `sudo`

**No sound?**
- Check if buzzer is physically present
- Try longer duration: `sleep 1` instead of `sleep 0.2`
- Check kernel: `dmesg | grep -i buzzer`

---

## Notes

- **Requires:** `sudo` privileges
- **Type:** Simple on/off (no volume/frequency control)
- **Location:** Bottom right corner of screen
- **Use:** Alerts, notifications, system events

---

## Related Files

- Test scripts: `/home/pi/buzzer/test_buzzer.sh` (bash), `/home/pi/buzzer/test_buzzer.py` (Python)
- Detailed guide: `BUZZER-CONTROL-SIMPLE.md`
- Flask API: `FLASK-BUZZER-CONTROL.md`
