Skip to content

Instantly share code, notes, and snippets.

View bergkvist's full-sized avatar

Tobias Bergkvist bergkvist

  • Software Engineer @ Ulvetanna
  • Oslo, Norway
View GitHub Profile
@bergkvist
bergkvist / package-tracking.sol
Last active May 1, 2019 08:02
Package Tracking on Ethereum (Solidity)
pragma solidity ^0.5.0;
contract PackageTracking {
struct Place {
bool exists;
string country;
string location;
}
struct Package {

Keybase proof

I hereby claim:

  • I am bergkvist on github.
  • I am bergkvist (https://keybase.io/bergkvist) on keybase.
  • I have a public key ASBysYhr038dSRqnPQ75tCcXQcZsSK-JnXxg9XLlCJQ5Mgo

To claim this, I am signing this object:

@bergkvist
bergkvist / force-full-composition-pipeline.sh
Created August 3, 2021 03:48
Avoid vertical screen tears when you have an Nvidia Graphics card
#!/bin/bash
s="$(nvidia-settings -q CurrentMetaMode -t)"
MODE="${1:-On}"
if [[ "${s}" != "" ]]; then
s="${s#*" :: "}"
nvidia-settings -a CurrentMetaMode="${s//\}/, ForceCompositionPipeline=$MODE, ForceFullCompositionPipeline=$MODE\}}"
fi
@bergkvist
bergkvist / inline_cfunc.py
Last active July 19, 2022 14:10
Allow for writing C extensions inline in IPython
from IPython.core.magic import register_cell_magic
import multiprocessing as mp
from functools import wraps
import importlib.util
import traceback
import sysconfig
import tempfile
import secrets
import sys
import os
@bergkvist
bergkvist / private-pypi-test.sh
Created August 26, 2022 02:20
Check if personal access token is working for private Python package registry using curl
#!/bin/sh
pip_index_url="example.com/pypi"
private_package="myprivatepackage"
personal_access_token="$1"
curl -k -s -L -o/dev/null -w"%{http_code}" --url "https://$pip_index_url/$private_package" --header "Authorization: Basic $(printf '%s' "$personal_access_token:" | base64)"
# To configure pip to use registry:
# export PIP_EXTRA_INDEX_URL="https://$personal_access_token@$pip_index_url"
@bergkvist
bergkvist / watch.sh
Created September 4, 2022 21:25
Watch a file for changes using inotifywait and kill previous process if still running.
#!/bin/sh
build_and_run() {
cc main.c -o main -Wall -Wextra
./main
}
trap 'kill "$p" >/dev/null 2>/dev/null' INT
build_and_run &
p=$!
@bergkvist
bergkvist / asserts.c
Last active September 5, 2022 23:18
Useful assertion macros in C which include debug information like file, line number and error message.
// These require compiler support for statement expressions (https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html)
#define assert_ok(e) ({int x = (e); if (x < 0) { printf("%s:%d: ", __FILE__, __LINE__); fflush(stdout); perror(#e); abort(); } x;})
#define assert_ptr(e) ({void* p = (e); if (p == NULL) { printf("%s:%d: %s: NULL pointer returned\n", __FILE__, __LINE__, #e); abort(); } p;})
@bergkvist
bergkvist / keyd.conf
Last active July 8, 2023 20:35
keyd remapping for M1 macbook with "Norwegian (Macintosh)" keyboard layout
[ids]
*
[main]
` = macro(S-= space)
[shift]
4 = S-A-4
[meta]
@bergkvist
bergkvist / julia_set.py
Last active July 12, 2023 08:43
Mandelbrot/Julia Set computation accelerated using NumPy and OpenCL C, and rendered to video using OpenCV
import numpy as np
import pyopencl as cl
import matplotlib.pyplot as plt
import cv2
W, H = (1500, 1500)
(cxmin, cxmax), (cymin, cymax) = ((-1.5, 1.5), (-1.5, 1.5))
duration, fps = (10, 25)
frame_count = int(fps * duration)
cx, cy = np.meshgrid(np.linspace(cxmin, cxmax, W, dtype=np.float32),
@bergkvist
bergkvist / saleae.py
Last active September 21, 2023 01:57
Saleae Logic 2 Automation in Python - working with UART.
# pip install pyserial==3.5 logic2-automation==1.0.6
from saleae import automation
import serial
import time
# Make sure that this is accessible to your Linux user. It probably isn't.
# Run `sudo chmod a+rw /dev/ttyUSB1` to allow everyone on the machine access.
tty = serial.Serial('/dev/ttyUSB1') # UART over USB