Skip to content

Instantly share code, notes, and snippets.

View hrydgard's full-sized avatar

Henrik Rydgård hrydgard

View GitHub Profile
// .h file
// Similar to strncpy but with safe zero-termination behavior
// and doesn't fill extra space with zeroes.
// Returns true if the string fit without truncation.
// Always performs the copy though.
bool truncate_cpy(char *dest, size_t destSize, const char *src);
template<size_t Count>
inline bool truncate_cpy(char(&out)[Count], const char *src) {
@hrydgard
hrydgard / gist:f73b8d9f2bab46e716d09902f08bcb43
Last active June 9, 2019 21:05
Attempt at exponent-aligned fp addition
#include <iostream>
uint32_t bit_cast(float f) {
uint32_t x;
memcpy(&x, &f, 4);
return x;
}
float bit_cast(uint32_t x) {
float f;
@hrydgard
hrydgard / timinggame.ino
Created October 5, 2017 14:44
Simple Arduino timing game
// Simple Arduino timing game.
// Made for Nano but probably works on Uno too.
// by Henrik Rydgård
// hrydgard@gmail.com
//
// Connect:
// * a 0.96" no-name I2C OLED screen to pins SDA=A4,SCL=A5
// * a button to pin 4 and ground (we use internal pullup to avoid adding a resistor)
// * a 8-LED RGB strip to pin 2
// and that's pretty much it.
@hrydgard
hrydgard / OpenSLWrap.cpp
Created July 8, 2012 19:48
A minimal implementation of audio streaming using OpenSL in the Android NDK
// Minimal audio streaming using OpenSL.
//
// Loosely based on the Android NDK sample code.
// Hardcoded to 44.1kHz stereo 16-bit audio, because as far as I'm concerned,
// that's the only format that makes any sense.
#include <assert.h>
#include <string.h>
// for native audio