Skip to content

Instantly share code, notes, and snippets.

@rlcamp
rlcamp / coroutine.c
Last active March 4, 2022 22:53
Coroutines for generator functions, sequential pipelines, state machines, and other uses in C
see https://github.com/rlcamp/coroutine
/* allows c code to printf() within arduino sketches, given arm newlib or avr-libc */
#ifdef __arm__
/* TODO: proper check for newlib */
#ifdef USE_TINYUSB
#include <Adafruit_TinyUSB.h>
#endif
#include <Arduino.h>
extern "C"
@rlcamp
rlcamp / one_interval_has_elapsed.c
Last active November 19, 2023 15:04
Blink without delay, but with the one_interval_has_elapsed() function in its own .c file, and with processor-specific sleep modes
#include "one_interval_has_elapsed.h"
#if defined(__AVR__)
#include <avr/power.h>
#if 1
/* avr using timer2 and SLEEP_MODE_PWR_SAVE, uses 400-800 uA at 3.3V depending on clock speed */
#include <avr/io.h>
@rlcamp
rlcamp / catty.sh
Last active July 31, 2020 17:10
Get around the "stty not persistent" issue on macOS
#!/bin/bash
# Get around the "stty not persistent" issue on macOS (works fine on linux too)
# Usage: ./catty.sh /dev/cu.SLAB_USBtoUART 115200
exec 3<>$1
stty speed $2 <&3 >/dev/null
exec cat <&3
@rlcamp
rlcamp / lora_minimalist.c
Last active June 1, 2022 16:59
Minimalist nonblocking-ish RFM9x LORA transceiver driver
#include "lora_minimalist.h"
#include "lora_minimalist_hal.h"
unsigned char lora_minimalist_recv_buffer[242];
volatile size_t lora_minimalist_recv_buffer_filled;
static struct spi_settings * spi_settings;
static volatile enum { STANDBY, TRANSMITTING, RECEIVING } mode, mode_after_tx;
static void spi_write_register(uint8_t reg, uint8_t val) {
@rlcamp
rlcamp / configure
Last active February 1, 2024 20:38
#!/usr/bin/env bash
# richard campbell
# isc license
#
# assumptions/caveats:
# - this script is run by bash 3.2+, produces a Makefile suitable for gnu make 3.81+
# - targets to build have a main function in a .c file of the same name
# - each module depended upon is in the same directory
# - each module depended upon consists of one .c and one .h file
# - if you need to link in external libraries, you still need to manually provide those to make via LDLIBS
@rlcamp
rlcamp / tunnel_wireguard_over_serial.c
Last active May 19, 2022 16:18
allow Wireguard to replace ppp
/* isc license probably */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
@rlcamp
rlcamp / inline_c.sh
Last active October 11, 2022 22:54
for when you really just need to run a bit of C logic from a bash script
#!/bin/bash
TMPFILE=$(mktemp)
cc -Wall -Wextra -Wshadow -Os -lm -o ${TMPFILE} -x c <(tail -n+$(awk '/^exit$/ { print NR + 1 }' $0) $0) && chmod +x ${TMPFILE} && ${TMPFILE} "$@"
rm ${TMPFILE}
exit
#include <stdio.h>
#include <unistd.h>
int main(const int argc, const char ** const argv) {
/* standalone test case for https://github.com/kaniini/libucontext/issues/2
# compile libucontext:
git clone --depth 1 https://github.com/kaniini/libucontext.git
cd libucontext
make FREESTANDING=yes libucontext.a
# and then compile this test case:
cc -Wall -Wextra -Wshadow -Os -o libucontext_fp_test libucontext_fp_test.c -I${HOME}/Downloads/libucontext/include/ ${HOME}/Downloads/libucontext/libucontext.a
*/
@rlcamp
rlcamp / decrypt_with_privkey.sh
Last active March 11, 2023 20:35
encryption and decryption using an openssh-formatted rsa public-private key pair
#!/bin/bash
# TODO: this leaks the session key to arguments visible to ps while decrypting
set -euo pipefail
# if an argument was provided, use it as the path to the rsa private key, otherwise assume openssh
keypath=${1:-"$HOME/.ssh/id_rsa"}
# deal with converting openssh special file format to something openssl understands
TMPFILE=$(mktemp)
cp -p "$keypath" $TMPFILE