Skip to content

Instantly share code, notes, and snippets.

@TheEyesightDim
TheEyesightDim / code.py
Created January 31, 2021 06:40 — forked from sandyjmacdonald/code.py
MIDI CC knob controller example for Raspberry Pi Pico
import time
import board
import usb_midi
import adafruit_midi
from analogio import AnalogIn
from adafruit_midi.control_change import ControlChange
# MIDI CC knob controller example for Raspberry Pi Pico
require "app/strings.rb"
def undo_last_stroke(args)
args.state.counts.pop
stroke = args.state.counts.pop
args.state.commands.slice!(0 - stroke, stroke) if stroke
end
def tick(args)
args.state.help_layer ||= :shown
#include <cmath>
#include <cstdio>
double smoothstep(double low, double high, double factor){
auto k = std::fmin(1, std::fmax(0, (factor - low)/(high - low)));
return k*k*(3-2*k);
}
int main(){
@TheEyesightDim
TheEyesightDim / miller_rabin.py
Created November 8, 2019 01:28
Python implementation of Miller-Rabin primality test
def is_prime(n):
"""A very basic implementation of the Miller-Rabin primality test."""
#from Wikipedia: "if n < 341,550,071,728,321, it is enough to test a = 2, 3, 5, 7, 11, 13, and 17."
small_primes = {2,3,5,7,11,13,17}
if n in small_primes: return True
if n < 2 or n & 1 == 0: return False
r, d = (0, n-1)
while (d & 1) == 0:
@TheEyesightDim
TheEyesightDim / into_bytes.cpp
Last active September 5, 2019 12:02
Converts some type into its representation as a byte array.
#include <algorithm>
#include <array>
template <typename T>
constexpr std::array<char, sizeof(T)> into_bytes(const T& t){
std::array<char, sizeof(T)> bytes;
std::copy( reinterpret_cast<const char*>(&t), reinterpret_cast<const char*>(&t) + sizeof(t), bytes.begin() );
return bytes;
}
@TheEyesightDim
TheEyesightDim / rot_n_string.js
Last active December 17, 2018 19:01
Rotate alphabet characters in a string by an arbitrary amount
//needed to make friendly for map function, which expects function taking dereferenced string index as argument
function charCodeOfChar(character){
return character.charCodeAt(0);
}
//String's prototype function will give weird UTF-16 literal output otherwise
function charFromCharCode(charCode){
return String.fromCharCode(charCode);
}
<?php
require 'vendor/autoload.php';
use Symfony\Component\Stopwatch\Stopwatch;
/**
* @param callable $callable is a function or functor callable with parentheses.
* @param array ...$args is the argument list which will be supplied to the $callable function.
* @return float|int which represents the time elapsed in the function called, in milliseconds.
*/
function benchmarker(Callable $callable, ...$args){
@TheEyesightDim
TheEyesightDim / ostream_itr_example.cpp
Last active July 1, 2018 00:34
Example of using ostream iterators with standard algorithms to produce interesting results.
#include <vector>
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
std::vector<int> v {7,4,1,8,5,2,9,6,3};
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout , " "));
}
@TheEyesightDim
TheEyesightDim / point_inversion.cc
Last active July 1, 2018 03:34
Inversion of a point around another point
//point inversion
#include <vector>
#include <iostream>
struct Point { int x, y; };
struct Line { Point petal, center; };
//reflects Point 'mover' across another Point 'focus'.
//'focus' is the midpoint of 'mover' and the return value.
Point invert_point(const Point mover, const Point focus){
//this function converts any member of enum of any underlying type to that type.
//Effective Modern C++ by Scott Meyers. See item 10
template <typename E>
constexpr auto to_underlying(E e) noexcept
{
return static_cast<std::underlying_type_t<E>>(e);
}