Skip to content

Instantly share code, notes, and snippets.

View larsch's full-sized avatar

Lars Christensen larsch

View GitHub Profile
#!/usr/bin/env ruby
def lesseq(a, b, c, d)
return a * d <= c * b
end
lower_numer = 2
lower_denom = 1
upper_numer = 3
upper_denom = 1
@larsch
larsch / rotary.cpp
Created October 12, 2017 19:45
Rotary Encoder Interrupt Handler
//
// Rotary Encoder Driver
//
// Copyright (C) 2016 by Lars Christensen <[email protected]>
// MIT License
//
#include "rotary.hpp"
#include <avr/interrupt.h>
#include <avr/io.h>
@larsch
larsch / install-arch-linux-rpi-zero-w.sh
Created July 6, 2017 06:05
Install Arch Linux ARM for Raspberry Pi Zero W on SD Card (with commands to configure WiFi before first boot).
#!/bin/sh -exu
dev=$1
cd $(mktemp -d)
function umountboot {
umount boot || true
umount root || true
}
# RPi1/Zero (armv6h):
@larsch
larsch / testpcap.c
Last active January 11, 2022 08:14
libpcap latency/break test
#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
pcap_t* pcap;
void* thread(void* arg) {
@larsch
larsch / throttled-event-handler.js
Created February 4, 2017 15:09
Throttled Event Handling in JavaScript
/**
* Attach an event listener to an object. Invokes the handle()
* callback on every event, and the apply() handler eventually, but
* with at least 'delay' milliseconds between each invokation.
*/
function addThrottledEventHandler(elem, event, handle, apply, delay) {
let nextApply = performance.now();
let timerId = null;
function invokeApply() { apply(); timerId = null; }
elem.addEventListener(event, (ev) => {
#!/bin/sh -exu
ip netns delete ns1
ip netns delete ns2
ip netns add ns1
ip netns add ns2
ip link add veth-ns1 type veth peer name veth-ns2
ip link set veth-ns1 netns ns1
ip link set veth-ns2 netns ns2
ip -n ns1 link set veth-ns1 up
ip -n ns2 link set veth-ns2 up
#!/bin/sh -exu
ip netns delete ns1
ip netns delete ns2
ip netns add ns1
ip link add veth-ns1 type veth peer name veth-ns2
ip link set veth-ns1 netns ns1
ip -n ns1 link set veth-ns1 up
ip link set veth-ns2 up
ip -n ns1 address add 10.0.0.1/30 dev veth-ns1
ip address add 10.0.0.2/30 dev veth-ns2
@larsch
larsch / README.md
Last active October 2, 2016 13:41
Cura 2 Machine Definition for Ultimaker 2 Clone/RAMPS

Ulti-Alu-Maker Cura 2 Configuration file

Machine definition for custom Aluminium Extrusion Ultimaker 2 clone (Ulti-Alu-Maker). Copy to ~/.local/share/cura/machines.

#include <avr/sleep.h>
// const int com[4] = { 10, 13, 14, 4 };
// const int com[4] = { 4, 14, 13, 10 };
// const int dig[8] = { 11, 15, 6, 8, 9, 12, 5, 7 };
// const int dig[8] = { 13, 17, 4, 6, 7, 14, 3, 5 };
// const int num[10] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f };
// int nmd[10] = {};
// int nmb[10] = {};
#include <avr/interrupt.h>
#include <avr/io.h>
volatile uint8_t rotaryPosition = 0;
void __attribute__((constructor)) init_rotary()
{
EICRA |= _BV(ISC00); // enable interrupt on change INT0 (for rotary encoder)
EIMSK |= (1 << INT0); // enable INT0 (for rotary encoder)
DDRD &= ~(_BV(PORTD3) | _BV(PORTD4) | _BV(PORTD5)); // input mode (rotary encoder clk, dt, sw)