Skip to content

Instantly share code, notes, and snippets.

View justinmklam's full-sized avatar

Justin Lam justinmklam

View GitHub Profile
@justinmklam
justinmklam / add-axlux-repository.sh
Last active April 18, 2021 07:14
Third party repository containing a handful of archives to open source projects. Source: https://packages.azlux.fr/
# Add the source
echo "deb http://packages.azlux.fr/debian/ buster main" | sudo tee /etc/apt/sources.list.d/azlux.list
wget -qO - https://azlux.fr/repo.gpg.key | sudo apt-key add -
# Refresh sources
sudo apt update
@justinmklam
justinmklam / copy-to-clipboard.js
Created March 23, 2020 17:25
Simple function to copy text to a client's clipboard. Source: https://stackoverflow.com/a/33928558
// Copies a string to the clipboard. Must be called from within an
// event handler such as click. May return false if it failed, but
// this is not always possible. Browser support for Chrome 43+,
// Firefox 42+, Safari 10+, Edge and Internet Explorer 10+.
// Internet Explorer: The clipboard feature may be disabled by
// an administrator. By default a prompt is shown the first
// time the clipboard is used (per session).
function copyToClipboard(text) {
if (window.clipboardData && window.clipboardData.setData) {
// Internet Explorer-specific code path to prevent textarea being shown while dialog is visible.
@justinmklam
justinmklam / multiprocessing-class.py
Last active May 4, 2020 17:28
Multiprocessing examples. For python's version of threading (minus the global intepreter lock)
"""Multiprocessing demo with externally accessible variables
"""
import time
from ctypes import c_bool
from multiprocessing import Process, Value
class MyProcess:
def __init__(self):
self._thread = None
@justinmklam
justinmklam / rsync-watch.sh
Last active May 31, 2022 15:53
Script to watch for changes in a directory and push files to a remote server. Add to /usr/local/bin for easy access (either directly or as a symlink from your home directory).
#!/usr/bin/env bash
"""Monitor a directory and rsync to a remote host on file change.
Example usage:
./rsync-watch.sh [email protected] . /home/pi/myproject
"""
RED=`tput setaf 1`
GREEN=`tput setaf 2`
YELLOW=`tput setaf 3`
@justinmklam
justinmklam / ipython-autoreloader.md
Last active January 13, 2020 21:03
iPython magic to use autoreloader module

Use the following snippet at the top of your Juyter Notebook or iPython shell:

%load_ext autoreload
%autoreload 2

Then you can use it as so:

@justinmklam
justinmklam / install-virtualenvwrapper.md
Last active April 18, 2021 07:16
Steps to install virtualenvwrapper on linux

Official documentation here, but steps had to be slightly modified to install it on python3.

Installation

Install the package:

pip3 install virtualenvwrapper
@justinmklam
justinmklam / cfd-demo.cpp
Created December 19, 2019 18:20
Example using constant fraction discriminator signal detection.
#include "Arduino.h"
#include "FSR.h"
#define CFD_DELAY_MS 500
#define CFD_ZERO_CROSSING_TRIGGER_VALUE 50.0
Fsr::Fsr(int pin, int threshold) :pin(pin), threshold(threshold) {
cfd_timer = millis();
cfd_prev_value = -read();
}
class Flasher
{
// Class Member Variables
// These are initialized at startup
int ledPin; // the number of the LED pin
long OnTime; // milliseconds of on-time
long OffTime; // milliseconds of off-time
// These maintain the current state
int ledState; // ledState used to set the LED
@justinmklam
justinmklam / backup-sd-card-to-file.md
Last active August 13, 2023 15:58
Backup SD card to file using dd

To find the partition, use the command:

$ df

The output will be something like below. In this case, the SD card is on /dev/mmcblk (ignore the two partitions).

Filesystem Size Used Avail Use% Mounted on
@justinmklam
justinmklam / live-plot.py
Created December 6, 2019 21:23
Utility to easily create a live plotting figure of incoming data (ie. from a sensor)
import matplotlib.pyplot as plt
import matplotlib.animation as animation
class LivePlot():
def __init__(self, y_range:list, x_points=200, interval_ms=50, title="Data Stream", ylabel="Data", xlabel="Samples"):
"""Creates a figure that continually updates with live data.
To set data value stream, the user must inherit this class.
Example: