Skip to content

Instantly share code, notes, and snippets.

@Tmw
Tmw / 01-process.sh
Last active August 29, 2023 20:47
Bash script to process lines combined from multiple files excluding the lines in another file
#!/usr/bin/env bash
FILES="fruits.txt veggies.txt"
EXCLUSION_FILE="nope-list.txt"
COMBINED=$(cat $FILES)
FOODS=$(grep -vx -f $EXCLUSION_FILE <(echo $COMBINED | tr ' ' '\n'))
TOTAL_FOODS=$(echo "$FOODS" | wc -l | awk '{print $1}')
CURRENT_FOOD=1
echo "about to eat $TOTAL_FOODS foods"
echo "---------------------------------"
@Tmw
Tmw / restful-responses.go
Created May 15, 2023 20:31
Quick experimentation on how to setup generic restful responses using Go
package main
import (
"encoding/json"
"fmt"
)
type Formatter interface {
Format() any
}
@Tmw
Tmw / RoundableRectangle.swift
Created January 4, 2022 11:32
RoundedRectangle with per-corner radii
//
// RoundableRectangle.swift
// Created by Tiemen Waterreus on 20/12/2021.
//
import SwiftUI
/// RoundableRectangle is a shape (rounded rectangle) where each
/// corner of the rectangle can be rounded with a separate radius
struct RoundableRectangle: Shape {
struct Case<'a> {
title: &'a str,
max_amount: usize,
wanted_amounts: Vec<usize>,
}
fn main() {
// Setup the various cases
let simulations = vec![
@Tmw
Tmw / Huffman.ex
Created September 2, 2020 14:42
Huffman encoder / decoder from scratch using Elixir
defmodule Huffman do
defmodule Node do
defstruct [:left, :right]
end
defmodule Leaf do
defstruct [:value]
end
def encode(text \\ "cheesecake") do
@Tmw
Tmw / cube_v1.ino
Created February 1, 2016 21:04
Source for Cube V1
#include <SPI.h>
// define pins and other variables
const int latchPin = 8;
const int clockPin = 13;
const int dataPin = 11;
const int NUMBER_OF_CONNECTED_LEDS = 16;
// global array that keeps track of the LEDs brightnesses
bool brightnessMask_layer1[NUMBER_OF_CONNECTED_LEDS*4];
@Tmw
Tmw / refresh.ino
Last active February 1, 2016 21:57
The expanded refresh function
void refresh(){
// Loop over each LED
for (int cycle = 0; cycle < 16; cycle++) {
for (int currentLed = 0; currentLed < NUMBER_OF_CONNECTED_LEDS; currentLed++) {
int maskPosition = currentLed * 4;
if (cycle == 1 && brightnessMask_layer1[maskPosition]) {
turnOnLed(currentLed, 0);
}
@Tmw
Tmw / write_to_brightnessmask.ino
Last active February 1, 2016 21:48
Writing values to the brightnessMask array
void led(int x, int y, int z, int brightness){
// ensure 4-bit limited brightness
brightness = constrain(brightness, 0, 15);
int ledNr = y*4+x;
// turn 4-bit brightness into brightness mask
for (int i = 3; i >= 0; i--) {
if (brightness - (1 << i) >= 0) {
brightness -= (1 << i);
@Tmw
Tmw / brightnessMasks.ino
Last active February 1, 2016 21:50
expanding our brightnessMask array
bool brightnessMask_layer1[NUMBER_OF_CONNECTED_LEDS*4];
bool brightnessMask_layer2[NUMBER_OF_CONNECTED_LEDS*4];
bool brightnessMask_layer3[NUMBER_OF_CONNECTED_LEDS*4];
bool brightnessMask_layer4[NUMBER_OF_CONNECTED_LEDS*4];
@Tmw
Tmw / turnOnLed.ino
Last active February 1, 2016 21:51
expanding our turnOnLed function
// turn on correct LED using 595's
void turnOnLed(int ledNr, int layer) {
digitalWrite(latchPin, LOW);
SPI.transfer(1<<layer);
if (ledNr >= 8) {
SPI.transfer(1<<ledNr-8);
SPI.transfer(0);
}
else {