Python script to make connecting to the Circuit Python REPL quick and easy!

I’m a HUGE fan of CircuitPython, it’s really my go to for all hardware hacking and I’ll use it as much as possible, prefering to buy CircuitPython compatible devices rather than ones that run straight up Arduino. One of the reasons for that is because the development process is similar to writing code for other projects, that is I can use my favourite text editor (Sublime) and the Mac Terminal to see output and errors via the REPL.

My biggest frustration with this is actually connecting to the device, it’s a bit of a chore. Adafruit have an awesome guide but in short you have to list all the devices that match /dev/tty.* then figure out which one is your device, then connect to it via Screen on a specific port. It’s a lot for my tired old sleep deprived brain to remember.

So… I used the few chunks of grey matter that I still have functioning and wrote a little Python script that makes connecting as simple as typing REPL and then a number.

Heres the code (simple huh?):


import glob
import os

devices = glob.glob("/dev/tty.*")

# clear the terminal
os.system('cls' if os.name == 'nt' else 'clear')

i=0;

print("=====================================")
print("  Select your device (or 0 to quit)")
print("=====================================\n")

for device in devices:
    i+=1
    print("  " + str(i) + ". " + device.replace("/dev/tty.",""))

choice = input("\nDevice: ")

if(int(choice) and choice >=1 and choice <= len(devices)):
    command = "screen " + devices[choice-1] + " 115200"
    os.system(command)
    exit()
else:
    exit()  

Just copy and paste this into the text editor of your choosing, and save it in your home directory as repl.py then open Terminal.app and launch the script by typing python repl.py

Ah… I promised it was as simple as typing repl and typing a number right?

Ok, but it requires a little bit more work up front. You just have to create an alias for “repl” to the command python repl.py. Doing this is well documented on the old internet but just in case your Google-fu is failing you, theres a nice guide to creating aliases over on WPBeaches that makes everything super clear.

Tada! Type repl and connect away!

I know this is only a minor timesaver, but if you spend a lot of time hacking hardware like I do then the seconds you save could add up to whole minutes over the course of your life time.

Enjoy!

Update December 2022 – Take a look at Tio!

Leave a Reply

Your email address will not be published. Required fields are marked *