Skip to content

Instantly share code, notes, and snippets.

View gallaugher's full-sized avatar

Gallaugher gallaugher

View GitHub Profile
@gallaugher
gallaugher / PreviewContainer.swift
Created February 14, 2024 10:04
PreviewContainer.swift
import Foundation
import SwiftData
class PreviewContainer {
let container: ModelContainer! // register the container
init(_ types: [any PersistentModel.Type], isStoredInMemoryOnly: Bool = true) {
let schema = Schema(types) // should be able to pass in any schema & have them registered
let config = ModelConfiguration(isStoredInMemoryOnly: isStoredInMemoryOnly)
self.container = try! ModelContainer(for: schema, configurations: [config])
// countriesAndFlags is a dictionary of country names : flag emojis
// countries is a String array of country names in alphabetical order
struct Constants {
static let countriesAndFlags = ["Afghanistan": "🇦🇫",
"Åland Islands": "🇦🇽",
"Albania": "🇦🇱",
"Algeria": "🇩🇿",
"American Samoa": "🇦🇸",
@gallaugher
gallaugher / code.py
Created April 5, 2023 15:33
Playback from Adafruit IO - button sound crashes on third interrupted playback
# MQTT With Color Picker - Adafruit I0
import board, time, neopixel
import os, ssl, socketpool, wifi
import adafruit_minimqtt.adafruit_minimqtt as MQTT
# import lines for audio & mp3 playing
from audiopwmio import PWMAudioOut as AudioOut
from audiomp3 import MP3Decoder
# setup the speaker
audio = AudioOut(board.GP16) # assuming tip of speaker attached to GP16
@gallaugher
gallaugher / mount_sd.py
Created March 3, 2023 00:37
mount_sd.py
# mount_sd.py
import board, busio, sdcardio, storage
# setup pins for SPI
sck = board.GP10 # yellow
si = board.GP11 # blue
so = board.GP12 # green
cs = board.GP13 # yellow
spi = busio.SPI(sck, si, so)
sdcard = sdcardio.SDCard(spi, cs)
@gallaugher
gallaugher / code.py
Created March 3, 2023 00:26
DJ Board with SD Card
# Idea based on original code by @todbot
# Modified (probably poorly) by Prof. John Gallaugher to include
# This version tested with CircuitPython v.8
import board, adafruit_mpr121, time, audiomixer, audiocore
import mount_sd
from audiocore import WaveFile
from audiopwmio import PWMAudioOut as AudioOut
# Setup i2c on the Pico w/STEMMA_QT wired as shown in the diagram - GP4 (SDA/Blue), GP5 (SCL/Yellow, Power 3.3v(Out) Red
i2c = board.STEMMA_I2C() # Same for any board w/built-in STEMMA_QT port
@gallaugher
gallaugher / mp3-beats.py
Created February 23, 2023 15:31
Trying to play some mp3 files using audiomixer in CircuitPython.
# audiomixer_demo.py -- show how to fade up and down playing loops
import time, board, audiocore, audiomixer, adafruit_mpr121, digitalio
from audiomp3 import MP3Decoder
# import PWMAudioOut & name it AudioOut
from audiopwmio import PWMAudioOut as AudioOut
# set up I2C
i2c = board.STEMMA_I2C()
@gallaugher
gallaugher / LoginView.swift
Created November 14, 2022 16:02
LoginView for Prof. G's Firebase Chapter from https://bit.ly/prof-g-swiftui
// This code assumes Asset catalog has an image file named "logo"
// also assumes that the first View returned after successful login is called "ListView"
// After pasting this, be sure you update the "App" file so that it loads
// LoginView as the first View.
import SwiftUI
import Firebase
struct LoginView: View {
enum Field {
case email, password
@gallaugher
gallaugher / mqtt-test.py
Created November 19, 2021 16:59
Testing mosquitto
# paho MQTT test
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("$SYS/#")
@gallaugher
gallaugher / simple_sound.py
Last active October 30, 2021 15:19
simple_sound.py
# Sound
import time, board, digitalio
from audiocore import WaveFile
try:
from audioio import AudioOut
except ImportError:
try:
from audiopwmio import PWMAudioOut as AudioOut
except ImportError:
@gallaugher
gallaugher / python_colors.py
Created October 25, 2021 12:58
CircuitPython Colors from the adafruit_led_animation library
from adafruit_led_animation.color import (
AMBER, #(255, 100, 0)
AQUA, # (50, 255, 255)
BLACK, #OFF (0, 0, 0)
BLUE, # (0, 0, 255)
CYAN, # (0, 255, 255)
GOLD, # (255, 222, 30)
GREEN, # (0, 255, 0)
JADE, # (0, 255, 40)
MAGENTA, #(255, 0, 20)