Skip to content

Instantly share code, notes, and snippets.

View kbob's full-sized avatar

Bob Miller kbob

  • Eugene, Oregon, USA
View GitHub Profile
@kbob
kbob / AlpenGlow_LED_rotor.ino
Last active January 9, 2024 07:21
LED rotor Arduino sketch
// Example Arduino sketch shows how to make a "rotor" of LEDs that light up,
// then fade away as the rotor passes them by.
// See https://mstdn.social/@alpenglow/111722310220283286
#define ROTOR_LED_COUNT 10
#define ROTOR_UPDATE_MSEC 1
#define ROTOR_ADVANCE_MSEC 100
#define ROTOR_MAX_BRIGHTNESS 255
#define ROTOR_MIN_BRIGHTNESS 0
// Polyphase decimation filter.
//
// Convert an oversampled audio stream to non-oversampled. Uses a
// windowed sinc FIR filter w/ Blackman window to control aliasing.
// Christian Floisand's 'blog explains it very well.
//
// This version has a very simple main processing loop (the decimate
// method) which vectorizes easily.
//
// Refs:
@kbob
kbob / Si.py
Created February 22, 2020 17:29
Numerically evaluate sine integral $ Si(x) $
#!/usr/bin/env python3
from itertools import count
# This is stable up to about x=34.
# Above that, reimplement with rational arithmetic.
def Si(x):
"""Sine integral. Numerically stable up to about x=34."""
sign = +1
@kbob
kbob / fb-lambda2.py
Created January 2, 2020 02:27
Yet Another FizzBuzz
#!/usr/bin/env python3
from functools import reduce
from itertools import count, cycle, islice, repeat
def fizzbuzz(*args):
class Fizz(int):
__add__ = lambda fi, z: z
__call__ = lambda b, u, z: z(u)
@kbob
kbob / foo.py
Created November 17, 2019 14:14
nMigen sign extension issue
from nmigen import *
from nmigen_boards.icebreaker import ICEBreakerPlatform
from nmigen.cli import main
class Foo(Elaboratable):
def __init__(self):
self.bar = Signal(signed(24))
def elaborate(self, platform):
@kbob
kbob / opt_decorator.py
Created November 11, 2019 14:35
Python Decorator with Optional Keyword Arguments
"""
Function decorator with optional keyword arguments.
"""
from functools import wraps
def decorate(func=None, kind=''):
if func is None:
# was called pre-decoration. Return anonymous decorator.
return lambda f: decorate(f, kind)
@kbob
kbob / voronoi.glsl
Created July 7, 2019 14:43
This fragment shader is just slightly too slow for an LED cube with Raspberry Pi 3 A+.
#!/usr/bin/env shaderbox
#define TAU 6.283185307179586
#define A vec3(0.5, 0.5, 0.5)
#define B vec3(0.5, 0.5, 0.5)
#define C vec3(1.0, 0.7, 0.4)
#define D vec3(0.0, 0.15, 0.20)
vec3 pal(in float t, in vec3 a, in vec3 b, in vec3 c, in vec3 d)
// Ask Hackaday: How do you DIY a Top-Octave Generator?
// https://hackaday.com/2018/05/24/ask-hackaday-diy-top-octave-generator/?utm_source=feedburner
// Target Arduino Mega.
// N.B., Timer 1 has highest interrupt priority, so
// use it for the highest frequency oscillators.
// Note Comp Port/Pin Arduino Pin
// C OC1A PB5 pin 11
// B OC1B PB6 pin 12
`default_nettype none
module blinky (CLK, LED1);
input wire CLK;
output wire LED1;
parameter WIDTH = 24;
parameter CLK_HZ = 12_000_000;
reg [WIDTH-1:0] counter;
reg [7:0] led1;
@kbob
kbob / main.rs
Created December 16, 2018 13:21
I fail to understand Rust types.
#[macro_use]
extern crate vulkano;
extern crate vulkano_shaders;
extern crate winit;
extern crate vulkano_win;
use vulkano::buffer::{CpuAccessibleBuffer, CpuBufferPool, BufferUsage};
use vulkano::command_buffer::{AutoCommandBufferBuilder, DynamicState};
use vulkano::descriptor::descriptor_set::PersistentDescriptorSet;
use vulkano::device::{Device, DeviceExtensions};