Skip to content

Instantly share code, notes, and snippets.

View justinmklam's full-sized avatar

Justin Lam justinmklam

View GitHub Profile
@justinmklam
justinmklam / kill-subprocess-group-demo.py
Last active March 8, 2019 17:50
Example code to launch a subprocess and terminate all spawned/child processes. For use when .kill() and .terminate() don't work.
# Source: https://stackoverflow.com/a/4791612
import os
import signal
import subprocess
# The os.setsid() is passed in the argument preexec_fn so
# it's run after the fork() and before exec() to run the shell.
pro = subprocess.Popen(cmd, stdout=subprocess.PIPE,
shell=True, preexec_fn=os.setsid)
@justinmklam
justinmklam / enable-arduino-upload-linux.sh
Created March 9, 2019 19:37
Change serial port permissions for Arduino to enable sketch upload.
# Error:
# Loading configuration...
# Initializing packages...
# Preparing boards...
# Verifying...
# Sketch uses 4044 bytes (12%) of program storage space. Maximum is 32256 bytes.
# Global variables use 407 bytes (19%) of dynamic memory, leaving 1641 bytes for local variables. Maximum is 2048 bytes.
# Uploading...
# An error occurred while uploading the sketch
# avrdude: ser_open(): can't open device "/dev/ttyACM0": Permission denied
@justinmklam
justinmklam / common-shebangs.md
Last active September 20, 2019 18:03
Common shebangs for shell scripts.

Bash

For POSIX (ie. not just Linux):

#!/bin/sh.

For GNU bash:

@justinmklam
justinmklam / linux-colours-in-bash-script.sh
Created March 21, 2019 23:17
Simple demo for adding terminal colours to a bash script.
#!/usr/bin/env bash
# Colours for terminal colours
RED=`tput setaf 1`
GREEN=`tput setaf 2`
YELLOW=`tput setaf 3`
PINK=`tput setaf 5`
CYAN=`tput setaf 6`
RESET=`tput sgr0`
@justinmklam
justinmklam / i3-setup.sh
Last active March 29, 2019 16:15
Handy commands to modify i3
# Add latest repo to ubuntu
/usr/lib/apt/apt-helper download-file http://debian.sur5r.net/i3/pool/main/s/sur5r-keyring/sur5r-keyring_2019.02.01_all.deb keyring.deb SHA256:176af52de1a976f103f9809920d80d02411ac5e763f695327de9fa6aff23f416
dpkg -i ./keyring.deb
echo "deb http://debian.sur5r.net/i3/ $(grep '^DISTRIB_CODENAME=' /etc/lsb-release | cut -f2 -d=) universe" >> /etc/apt/sources.list.d/sur5r-i3.list
apt update
apt install i3
# Copy config to home directory. Modify ctrl+grave
mkdir ~/.config/dunst
cp /etc/xdg/dunst/dunstrc ~/.config/dunst/dunstrc
@justinmklam
justinmklam / write-32bit-to-8bit-eeprom.c
Last active April 9, 2019 18:31
Reference for bit shifting 32 bit to 8 bit and vice versa.
#include <EEPROM.h>
void EEPROMWrite32bit(uint8_t address, uint32_t value)
{
uint8_t bytes[4];
bytes[0] = (value >> 24) & 0xFF;
bytes[1] = (value >> 16) & 0xFF;
bytes[2] = (value >> 8) & 0xFF;
bytes[3] = value & 0xFF;
@justinmklam
justinmklam / setup-ssh-keys.md
Last active November 20, 2019 00:59
Setup SSH keys

Backup existing keys (optional)

cd ~/.ssh

mkdir key_backup
cp id_rsa* key_backup

Create keys

@justinmklam
justinmklam / android-studio.gitignore
Created April 16, 2019 00:34
Minimal gitignore for Android Studio project.
# Gradle files
.gradle/
build/
# Local configuration file (sdk path, etc)
local.properties
# IntelliJ
*.iml
.idea/workspace.xml
@justinmklam
justinmklam / linux-ubuntu-install-nvidia.sh
Created April 17, 2019 21:34
Install nvidia drivers in Ubuntu 16.04
# Check current driver
sudo lshw -C display
# Check available drivers. Example output:
# == /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 ==
# modalias : pci:v000010DEd000013B0sv00001462sd0000116Bbc03sc02i00
# vendor : NVIDIA Corporation
# model : GM107GLM [Quadro M2000M]
# driver : nvidia-384 - distro non-free recommended
# driver : xserver-xorg-video-nouveau - distro free builtin
@justinmklam
justinmklam / bitwise-operations.c
Last active April 26, 2019 23:08
Cheat sheet for bitwise operations
// Set certain bits without affecting the rest.
// Clear all bits to a known value, then set them.
// newvalue must contain all bits
value = (value & ~mask) | (newvalue & mask);