Add external configuration file system

Breaking Changes:
- Configuration now via config.json instead of editing Python code
- Remove all hardcoded paths (no more /home/berwn)
- Separate system config (config.json) from runtime config (user_config.json)

Features:
- config.example.json with all configurable options
- GPIO pin and state configuration
- Audio device index configuration
- Customizable paths (relative or absolute)
- Web port and upload size settings
- No code editing required for deployment

Configuration Structure:
- gpio: hook_pin, hook_pressed_state
- audio: device_index, chunk_size, channels, sample_rate, max_record_seconds
- paths: base_dir, recordings_dir, sounds_dir
- web: port, max_upload_size_mb
- system: active_greeting, default_volume

Script automatically:
- Checks for config.json on startup
- Provides helpful error if missing
- Uses relative paths by default
- Loads test_complete.py config from same file

Updated Documentation:
- Complete configuration guide in README
- Setup instructions without hardcoded paths
- Troubleshooting for config errors
- Device index discovery command

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-10-24 14:48:43 +07:00
parent 1657a242fd
commit ef0373e60b
5 changed files with 196 additions and 74 deletions

View File

@@ -8,10 +8,28 @@ import wave
import numpy as np
import time
import os
import json
import sys
# HiFiBerry device index (from your system)
HIFIBERRY_INDEX = 1
SAMPLE_RATE = 44100
# Load configuration
def load_config():
"""Load audio device configuration"""
config_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config.json')
if not os.path.exists(config_path):
print("Config file not found. Using default device index 1")
return {"audio": {"device_index": 1, "sample_rate": 44100}}
try:
with open(config_path, 'r') as f:
return json.load(f)
except:
print("Error reading config. Using defaults.")
return {"audio": {"device_index": 1, "sample_rate": 44100}}
CONFIG = load_config()
HIFIBERRY_INDEX = CONFIG['audio']['device_index']
SAMPLE_RATE = CONFIG['audio']['sample_rate']
def test_playback():
"""Test speaker output"""