Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View RobCranfill's full-sized avatar

Rob Cranfill RobCranfill

View GitHub Profile
@RobCranfill
RobCranfill / neopixel_test.py
Created February 29, 2024 18:03
My standard "Hello, world" for a micro-controller with a NeoPixel on it.
# Based on code from Adafruit Industries
# SPDX-License-Identifier: MIT
"""Rob's CircuitPython blink example for built-in NeoPixel LED"""
import time
import board
import neopixel
import random
k = 20 # number of steps up or down
@RobCranfill
RobCranfill / lcd_util.py
Created November 30, 2023 18:05
Turn Adafruit miniPiTFT backlight on/off
# For the Adafruit miniPiTFT 3.3" LCD display: turn the backlight on/off (usually off).
# robcranfill
import board
import digitalio
import sys
def set_backlight_state(backlight_on: bool) -> None:
backlight_ = digitalio.DigitalInOut(board.D22)
backlight_.switch_to_output()
@RobCranfill
RobCranfill / no_auto_reload.md
Last active March 31, 2024 00:18
Turn off automatic reload of the code in CircuitPython
# To make a CP device stop auto-reloading code,

<code>
import supervisor
supervisor.runtime.autoreload = False  # CirPy 8 and above
print("supervisor.runtime.autoreload = False")

@RobCranfill
RobCranfill / bootloader.py
Last active September 6, 2023 00:20
Put CircuitPython device into bootloader mode; useful on my QTPy that I broke the BOOTSEL button on!
import microcontroller
microcontroller.on_next_reset(microcontroller.RunMode.BOOTLOADER)
microcontroller.reset()
@RobCranfill
RobCranfill / test_pcm5102.py
Last active July 26, 2023 01:09
Output stereo audio via a RPi Pico and a PCM5102 board
# Test outputting stereo audio via a PCM5102 board
# cran, based on:
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
import board
import audiocore, audiobusio, audiomixer, synthio
# Pico
@RobCranfill
RobCranfill / .bashrc
Created July 7, 2023 05:28
A bash function to mount a CircuitPython device to a WSL mount
# put this in .bashrc - or .bash_aliases?
function mountcirc() {
# mount drive $1 to /mnt/CIRCUITPY
sudo mount -t drvfs $1 /mnt/CIRCUITPY/
echo "Mounted $1 to /mnt/CIRCUITPY"
ls -l /mnt/CIRCUITPY
}
@RobCranfill
RobCranfill / runcode.py
Created February 25, 2023 00:36
Run code on a micropython device.
# to see the name of the file(s) on the device:
import os
os.listdir()
# to run the code
# (ok actually you would do this in a separate step)
exec(open('code.py').read())
@RobCranfill
RobCranfill / repo.sh
Created January 6, 2023 17:35
Back up and document work from a circuitpython thumb drive
#!/bin/bash
# copy work files from thumbdrive to a folder here
# params: {new folder name}
NEWFOLDER="$1"
mkdir "$NEWFOLDER"
cp /mnt/f/*py "$NEWFOLDER"
ls -Al /mnt/f/lib >>"$NEWFOLDER"/liblist.text
tree /mnt/f/ >>"$NEWFOLDER"/dirtree.text
@RobCranfill
RobCranfill / picoGetLine.c
Created December 3, 2022 22:32
getLine for RPi Pico, in C (based on https://github.com/ambotaku/pico-getLine)
// Adapted from https://github.com/ambotaku/pico-getLine
// Trivial code change to allow compiling with plain C on Pico.
//
const uint startLineLength = 8; // the linebuffer will automatically grow for longer lines
const char eof = 255; // EOF in stdio.h -is -1, but getchar returns int 255 to avoid blocking
/*
* read a line of any length from stdio (grows)
*
* @param fullDuplex input will echo on entry (terminal mode) when false
* @param linebreak "\r" may be needed for terminals
@RobCranfill
RobCranfill / display-8x8.py
Created November 25, 2022 20:42
Display a scrolling message on an Adafruit 8x8 LED matrix. For the Adafruit RP2040 Feather board (or any CircuitPython board?).
# Display a scrolling message on an Adafruit 8x8 LED matrix.
# For the Adafruit RP2040 Feather board (or any CircuitPython board?).
#
import board
from adafruit_ht16k33.matrix import Matrix8x8
import time
import font # This is my data for a simple 8x8 font, found in this directory.
# For the given string, create the big list of bit values (columns), left to right.
# TODO: This might display faster if we didn't pack these bits, but just