Skip to content

Instantly share code, notes, and snippets.

// x tracks the horizontal position of the paddle
let x = 2
// Plot the paddle on-screen for the first time
led.plot(x, 4)
// Pick a shared radio group between two devices
radio.setGroup(1)
// Pressing A moves the paddle to the left but don't let it go off-screen
radio.setGroup(1)
input.onButtonPressed(Button.AB, function () {
basic.showIcon(IconNames.Duck)
})
input.onGesture(Gesture.Shake, function () {
basic.clearScreen()
radio.sendNumber(0)
music.play(music.builtinPlayableSoundEffect(soundExpression.slide), music.PlaybackMode.UntilDone)
@mudge
mudge / pass-the-heart.js
Created October 29, 2026 18:11
Pass the Heart
let holding = false
let id = 0
let TOTAL = 2
// Setup: secretly do this on the first micro:bit
input.onLogoEvent(TouchButtonEvent.Pressed, function () {
holding = true
})
// Set IDs for each micro:bit
@mudge
mudge / enyaq_soc_to_ohme.yaml
Last active October 4, 2026 16:13
Home Assistant automation to set Ohme charger state of charge from Škoda API. Add this as a new automation, click the three dots and select “Edit in YAML”. It assumes you’ve already added an Ohme and Škoda device and adjust entity names accordingly.
alias: Sync Enyaq SoC to Ohme
triggers:
- trigger: state
entity_id:
- sensor.skoda_enyaq_coupe_battery_percentage
- sensor.ohme_home_pro_vehicle_battery
not_to:
- unknown
- unavailable
conditions:
@mudge
mudge / layout1.txt
Created July 7, 2025 09:56
Kinesis Advantage360 configuration for the foot pedal to press Cmd+Space on macOS
<base>
[pedl]>[hk2]
{hk2}>{-lwin}{spc}{+lwin}
@mudge
mudge / user.rb
Last active November 9, 2024 12:35
Transparently upgrade from bcrypt to Argon2id with Active Record
require "argon2id"
require "bcrypt"
# Schema: User(name: string, password_digest:string)
class User < ApplicationRecord
attr_reader :password
validates :password_digest, presence: true
validates :password, confirmation: true, allow_blank: true
@mudge
mudge / new.html.erb
Last active November 9, 2023 14:51
Using a custom direct upload controller with Rails Active Storage to limit file size before any data is uploaded
<%= form_with model: @post, data: { controller: 'upload', action: 'direct-upload:error@window->upload#error' } do |f| %>
<%= f.file_field(:image, data: { 'direct-upload-url' => uploads_url }) %>
<%= f.submit %>
<% end %>
@mudge
mudge / webcam-share-ended
Last active October 30, 2023 11:49
A Tuple Trigger to turn an Elgato Key light on and off when your webcam is shared
#!/bin/bash
if [[ "$TUPLE_TRIGGER_IS_SELF" == "true" ]]
then
curl --silent --request PUT --json '{"lights":[{"on":0}]}' http://elgato-key-light-air-2282.local:9123/elgato/lights
fi
@mudge
mudge / production.rb
Last active December 29, 2025 20:13
How to configure Rails and Rack::Attack to use the real client IP when running behind Cloudflare
Rails.application.configure do
# Add Cloudflare's IPs to the trusted proxy list so they are ignored when
# determining the true client IP.
#
# See https://www.cloudflare.com/ips-v4/ and https://www.cloudflare.com/ips-v6/
config.action_dispatch.trusted_proxies = ActionDispatch::RemoteIp::TRUSTED_PROXIES + %w[
173.245.48.0/20
103.21.244.0/22
103.22.200.0/22
103.31.4.0/22
@mudge
mudge / api_controller.rb
Last active March 24, 2023 05:27
Instruct Rails to respect the Accept header even if it contains a wildcard
class ApiController < ApplicationController
before_action :only_respect_accept_header
private
# By default, Rails will ignore the Accept header if it contains a wildcard
# and assume the client wants HTML (or JS if using XMLHttpRequest). See
# https://github.com/rails/rails/blob/a807a4f4f95798616a2a85856f77fdfc48da4832/actionpack/lib/action_dispatch/http/mime_negotiation.rb#L171-L173
#
# If you don't expect your clients to be browsers, we want to override this