Skip to content

Instantly share code, notes, and snippets.

View jhurliman's full-sized avatar
🐨

John Hurliman jhurliman

🐨
View GitHub Profile
@jhurliman
jhurliman / proposal001.ts
Created June 15, 2021 22:11
proposal001.ts
import { PanelExtensionContext, Topic, MessageEvent } from "@foxglove/studio";
import { useLayoutEffect, useState } from "react";
import ReactDOM from "react-dom";
function ExamplePanel({ context }: { context: PanelExtensionContext }): JSX.Element {
const [topics, setTopics] = useState<readonly Topic[] | undefined>();
const [messages, setMessages] = useState<readonly MessageEvent<unknown>[] | undefined>();
// We use a layout effect to setup render handling for our panel. We also setup some topic subscriptions.
useLayoutEffect(() => {
@jhurliman
jhurliman / utm.ts
Created May 10, 2021 19:25
Latitude/Longitude conversion to UTM in TypeScript
export type UTM = {
easting: number;
northing: number;
zoneNumber: number;
};
export function LatLngToUtm(
lat: number,
lng: number,
zoneNumber?: number,
@jhurliman
jhurliman / RosTcpMessageStream.ts
Created March 12, 2021 08:22
RosTcpMessageStream.ts
import { TransformStream, TransformerTransformCallback } from "web-streams-polyfill/ponyfill";
class TcpMessageStreamImpl {
private _inMessage = false;
private _bytesNeeded = 4;
private _chunks: Uint8Array[] = [];
transform: TransformerTransformCallback<Uint8Array, Uint8Array> = (chunk, controller) => {
let idx = 0;
while (idx < chunk.length) {
@jhurliman
jhurliman / nvtxrange.h
Created February 8, 2021 21:54
RAII wrapper for NVIDIA nvtxRangePush/nvtxRangePop
#pragma once
#include <nvToolsExt.h>
#include <string>
struct NvtxRange {
NvtxRange() = delete;
explicit NvtxRange(const std::string_view name) { nvtxRangePush(name.data()); }
@jhurliman
jhurliman / Colors.ts
Created October 24, 2020 09:38
TypeScript methods for color conversion
export type HSL = [number, number, number]
export function HexToHSL(hex: string): HSL | undefined {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
if (!result) return undefined
const r = parseInt(result[1], 16) / 255
const g = parseInt(result[2], 16) / 255
const b = parseInt(result[3], 16) / 255
const max = Math.max(r, g, b)
@jhurliman
jhurliman / akai-apc40mkii-colors.json
Created October 24, 2020 08:02
Akai APC40 MkII LED colors as hex RGB list
[
"#FF4C4C",
"#FF0000",
"#590000",
"#190000",
"#FFBD6C",
"#FF5400",
"#591D00",
"#271B00",
"#FFFF4C",
@jhurliman
jhurliman / split2.sh
Created May 21, 2020 08:41
Use spleeter to split an input MP3 into separate vocal and accompaniment WAV files
#!/usr/bin/env bash
DIR=$(dirname `readlink -e $1`)
FILE=$(basename `readlink -e $1`)
OUT_DIR=`basename ${FILE} .mp3`
MODEL_DIRECTORY="${HOME}/.cache/spleeter"
mkdir -p ${MODEL_DIRECTORY}
docker run \
-v ${DIR}:/work \
@jhurliman
jhurliman / circular_array.h
Created April 5, 2020 08:42
CircularArray - Wraps std::array and provides a circular iterator in C++
#pragma once
#include <algorithm>
#include <array>
template <class T, size_t N>
class CircularArray {
public:
CircularArray() {}
CircularArray(const T initValue) { std::fill_n(data_.begin(), data_.size(), initValue); }
@jhurliman
jhurliman / HexArrayToStr.cpp
Created February 29, 2020 09:01
C++ Print a char* out as a hex dump
static void HexArrayToStr(const char* data, size_t length, std::string& output) {
const char* NIBBLE_TO_HEX = {"0123456789ABCDEF"};
output.assign(length * 2 + 1, 0);
char* buffer = output.data();
for (size_t i = 0; i < length; i++) {
int nibble = uint8_t(data[i]) >> 4;
buffer[2 * i] = NIBBLE_TO_HEX[nibble];
nibble = uint8_t(data[i]) & 0x0F;
buffer[2 * i + 1] = NIBBLE_TO_HEX[nibble];
}
@jhurliman
jhurliman / color.h
Last active February 8, 2020 10:21
COLOR macro for C/C++ ANSI color code printing
// Assumes the surrounding context has a bool named `color`
#define COLOR( code, s ) ( color ? "\033[0;" #code "m" : "" ) << s << ( color ? "\033[0;m" : "" )