Skip to content

Instantly share code, notes, and snippets.

@bklimt
bklimt / event.c
Created June 14, 2015 01:29
How to make the bluez bluetooth stack work with hex pin codes.
/*
* An excerpt from https://github.com/pcacjr/bluez/blob/c70a4afe4328053f462d8e30a0a787245aa7b948/src/event.c#L132
*/
static size_t decode_hex(const char *pin, char *out)
{
size_t i;
for (i = 0; i < 16 && pin[i * 2] && pin[i * 2 + 1]; i++)
sscanf(&pin[i * 2], "%02hhX", &out[i]);
@bklimt
bklimt / covariance.swift
Created June 4, 2014 02:50
Testing covariance in Swift
class Fruit {
var name = "Fruit"
var description : String {
return name
}
}
class Apple : Fruit {
init() {
super.init()
@bklimt
bklimt / emulator64-arm
Created April 1, 2014 04:57
How to move the Android emulator window back onto the screen.
tell application "System Events"
repeat with theProcess in (every process)
if name of theProcess is "emulator64-arm" then
set the position of (every window of theProcess) to {0, 0}
end if
end repeat
end tell
@bklimt
bklimt / gist:8266066
Last active January 2, 2016 06:49
Core Image filter kernel function crashes
/*
* I'm trying to write Core Image filter kernel functions in Quartz Composer,
* but it's crashing consistently on certain pieces of code that should be fine.
*/
// This works fine:
kernel vec4 coreImageKernel(sampler image, __color color1) {
vec4 inputColor = sample(image, samplerCoord(image));
return inputColor;
}
@bklimt
bklimt / gist:5022739
Created February 24, 2013 05:26
Output Macintosh boot beep as a WAVE file.
/**
* Runs the bootbeepASM methods and then converts the audio data to a WAVE file.
* @returns {Buffer} a Buffer containing the contents of a WAVE file.
*/
var bootbeep = function() {
// 22050 Hz is an approximation of the Mac's 370 * 60.15 Hz = 22255.5 Hz.
var writer = new WaveWriter(1.5, 22050, 8);
var samples = bootbeepASM();
console.log("Copying samples to WAVE.");
var i = 0;
@bklimt
bklimt / gist:5022125
Created February 24, 2013 01:01
Generate C# from JavaScript
var sineWave = function(frequency) {
var writer = new WaveWriter(2.0, 11025, 8);
var time = 0;
var sample = 0;
do {
sample = Math.sin(frequency * time * 2 * 3.14159);
time = time + 1/11025;
} while (writer.writeSample(sample));
writer.close();
return writer.buffer();
@bklimt
bklimt / gist:5022117
Last active December 14, 2015 03:38
WaveWriter for JavaScript
/**
* Creates a helper to write a WAVE file to a Buffer.
* @param {Number} seconds - The length of the WAVE file to write.
* @param {Number} sampleRate - The sample rate in Hz, such as 44100 for a CD.
* @param {Number} bitsPerSample - Either 8 or 16.
*/
var WaveWriter = function(seconds, sampleRate, bitsPerSample) {
this._sampleRate = sampleRate;
this._bitsPerSample = bitsPerSample;
this._writer = new BufferWriter();
@bklimt
bklimt / gist:5022112
Last active December 14, 2015 03:38
BufferWriter for Cloud Code Buffer
/**
* Lets you fill a Buffer without having to know its size beforehand.
*/
var BufferWriter = function() {
this._size = 0;
this._buffer = new Buffer(100);
};
_.extend(BufferWriter.prototype, {
buffer: function() {
@bklimt
bklimt / gist:5022105
Last active December 14, 2015 03:38
Mac Boot Beep translated to JavaScript
/**
* Adapted from http://folklore.org/projects/Macintosh/more/BootBeep.txt
* A direct translation of the m68k asm into JavaScript.
* @returns {Buffer} a Buffer which corresponds to the audio buffer on the
* original Macintosh, repeated for the duration of the boot beep. The Buffer
* contains a series of words, where the lower byte of each word is the
* amplitude of the audio signal at this point.
*/
var bootbeepASM = function() {
d3 = 40; // D3 contains the duration: 40 is boot beep
@bklimt
bklimt / gist:4700955
Created February 3, 2013 08:33
Objective-C Blocks Example E
typedef void (^eBlock)();
eBlock exampleE_getBlock() {
char e = 'E';
void (^block)() = ^{
printf("%c\n", e);
};
return block;
}