diff --git a/edit_settings.py b/edit_settings.py new file mode 100644 index 0000000..9bcace3 --- /dev/null +++ b/edit_settings.py @@ -0,0 +1,165 @@ +#!/usr/bin/env python3 +""" +Settings editor - copy from device, edit locally, upload back +Run with: python3 edit_settings.py +""" + +import json +import subprocess +import os +import tempfile + +def copy_settings_from_device(): + """Copy settings.json from the MicroPython device""" + print("Copying settings from device...") + try: + # Use mpremote to copy settings.json to local temp file + result = subprocess.run(['mpremote', 'cp', ':/settings.json', 'settings.json'], + capture_output=True, text=True) + if result.returncode == 0: + print("Settings copied successfully") + return True + else: + print(f"Failed to copy settings: {result.stderr}") + return False + except Exception as e: + print(f"Error copying settings: {e}") + return False + +def load_local_settings(): + """Load settings from local file""" + try: + with open('settings.json', 'r') as f: + return json.load(f) + except FileNotFoundError: + print("No local settings file found") + return {} + except Exception as e: + print(f"Error loading settings: {e}") + return {} + +def save_local_settings(settings): + """Save settings to local file""" + try: + with open('settings.json', 'w') as f: + json.dump(settings, f, indent=2) + print("Settings saved locally") + return True + except Exception as e: + print(f"Error saving settings: {e}") + return False + +def upload_settings_to_device(): + """Upload settings.json to the MicroPython device""" + print("Uploading settings to device...") + try: + result = subprocess.run(['mpremote', 'cp', 'settings.json', ':/settings.json'], + capture_output=True, text=True) + if result.returncode == 0: + print("Settings uploaded successfully") + return True + else: + print(f"Failed to upload settings: {result.stderr}") + return False + except Exception as e: + print(f"Error uploading settings: {e}") + return False + +def edit_settings_interactive(settings): + """Interactive settings editor""" + print("\nLED Bar Settings Editor") + print("=======================") + + # Display current settings + current_pin = settings.get("led_pin", 10) + current_leds = settings.get("num_leds", 119) + current_name = settings.get("name", "104") + current_color_order = settings.get("color_order", "rgb") + + print(f"\nCurrent settings:") + print(f" 1. LED Pin: {current_pin}") + print(f" 2. Number of LEDs: {current_leds}") + print(f" 3. Device Name: {current_name}") + print(f" 4. Color Order: {current_color_order}") + + print(f"\nEnter new values (press Enter to keep current):") + + # LED Pin + new_pin = input(f"LED Pin [{current_pin}]: ").strip() + if new_pin: + try: + settings["led_pin"] = int(new_pin) + except ValueError: + print("Invalid pin number, keeping current value.") + + # Number of LEDs + new_leds = input(f"Number of LEDs [{current_leds}]: ").strip() + if new_leds: + try: + settings["num_leds"] = int(new_leds) + except ValueError: + print("Invalid LED count, keeping current value.") + + # Device Name + new_name = input(f"Device Name [{current_name}]: ").strip() + if new_name: + settings["name"] = new_name + + # Color Order + print(f"Color Order options: rgb, rbg") + new_color_order = input(f"Color Order [{current_color_order}]: ").strip().lower() + if new_color_order in ['rgb', 'rbg']: + settings["color_order"] = new_color_order + elif new_color_order: + print("Invalid color order, keeping current value.") + + return settings + +def main(): + print("LED Bar Settings Editor") + print("=======================") + + # Step 1: Copy settings from device + if not copy_settings_from_device(): + print("Creating default settings...") + default_settings = { + "led_pin": 10, + "num_leds": 119, + "color_order": "rgb", + "name": "104" + } + save_local_settings(default_settings) + + # Step 2: Load and edit settings + settings = load_local_settings() + if not settings: + print("No settings to edit") + return + + # Step 3: Interactive editing + edited_settings = edit_settings_interactive(settings.copy()) + + # Step 4: Save locally + if edited_settings != settings: + save_local_settings(edited_settings) + + # Step 5: Upload to device + upload_choice = input("\nUpload settings to device? (y/n): ").strip().lower() + if upload_choice in ['y', 'yes']: + if upload_settings_to_device(): + print("\nSettings updated successfully!") + print("Restart the device to apply changes.") + else: + print("\nFailed to upload settings.") + else: + print("\nSettings saved locally but not uploaded.") + else: + print("\nNo changes made.") + +if __name__ == "__main__": + main() + + + + +