Skip to content

Instantly share code, notes, and snippets.

@monkeymademe
Last active August 3, 2023 22:13
Show Gist options
  • Save monkeymademe/8ce6014c00be908e7c0dba9a48e97af6 to your computer and use it in GitHub Desktop.
Save monkeymademe/8ce6014c00be908e7c0dba9a48e97af6 to your computer and use it in GitHub Desktop.
import os
import time
import shutil
import argparse
# Create the argument parser
parser = argparse.ArgumentParser(description='Pico Firmware Flasher')
parser.add_argument('uf2_file_path', metavar='UF2_FILE', type=str, help='Path to the UF2 file')
parser.add_argument('-d', '--drive_letter', type=str, default='D:', help='Drive letter of the Pico (default: D:)')
# Parse the command-line arguments
args = parser.parse_args()
# Set the UF2 file path and drive letter
uf2_file_path = args.uf2_file_path
drive_letter = args.drive_letter
# Print the initial message
print("Press Ctrl+C to exit - Warning: Exiting while flashing a Pico could damage the Pico.")
# Loop indefinitely
while True:
# Check if the Pico drive is available
if os.path.exists(drive_letter):
print("Pico detected - Attempting to write firmware")
try:
# Copy the UF2 file to the Pico drive
shutil.copy2(uf2_file_path, drive_letter)
# Print a message indicating that the firmware has been installed
print("Firmware installed - Rebooting Pico")
except PermissionError:
print("Permission Error: Unable to copy firmware to the Pico. Please reconnect the Pico and try again.")
except FileNotFoundError:
print("Error: UF2 file not found. Please check the file location.")
# Wait for 5 seconds before checking again
time.sleep(5)
@monkeymademe
Copy link
Author

Fixed a typo (time.sleep(5'') line 37 should not have the apostrophes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment