Skip to content

Instantly share code, notes, and snippets.

View TooTallNate's full-sized avatar

Nathan Rajlich TooTallNate

View GitHub Profile
@TooTallNate
TooTallNate / ref-buffer-type.js
Last active June 3, 2022 13:26
Fixed length "Buffer" type, for use in `ref-struct` type definitions.
var ref = require('ref');
module.exports = BufferType;
/**
* Fixed length "Buffer" type, for use in Struct type definitions.
*
* Optionally setting the `encoding` param will force to call
* `toString(encoding)` on the buffer returning a String instead.
*/
@TooTallNate
TooTallNate / ref-fixed-string.js
Last active April 27, 2016 23:59
Fixed length "String" type, for use in `ref-struct` type definitions.
var ref = require('ref');
module.exports = FixedString;
/**
* Fixed length "String" type, for use in Struct type definitions.
* Null-terminates by default.
* Throws an Error if there's not enough space available when setting a string.
*/
@TooTallNate
TooTallNate / binding.cc
Last active September 18, 2015 19:39
OS X Yosemite / Node.js v4.0.0 / Segfault - See https://github.com/node-ffi/node-ffi/issues/239
#include <node.h>
#include <node_buffer.h>
#include <nan.h>
#include <dlfcn.h>
#include <stdio.h>
using namespace v8;
using namespace node;
@TooTallNate
TooTallNate / temp.js
Last active August 29, 2015 14:26
Module for finding and reading OneWire temperature sensors, specifically beginning with ID "28" (like the DS18B20)
var fs = require('fs');
var path = require('path');
var devicesRoot = '/sys/bus/w1/devices/';
var slavesFilename = path.resolve(devicesRoot, 'w1_bus_master1/w1_master_slaves');
exports.find = function find (fn) {
fs.readFile(slavesFilename, 'ascii', function (err, data) {
if (err) return fn(err);
var slaves = data.trim().split('\n');
@TooTallNate
TooTallNate / touch.js
Last active August 29, 2015 14:26
Touchscreen class for the PiTFT screen for Raspberry Pi
var fs = require('fs');
var Struct = require('ref-struct');
var inherits = require('util').inherits;
var EventEmitter = require('events').EventEmitter;
// https://www.kernel.org/doc/Documentation/input/event-codes.txt
// https://www.kernel.org/doc/Documentation/input/input.txt
// /usr/include/linux/input.h
/*
@TooTallNate
TooTallNate / mpi.js
Created June 11, 2015 20:52
MPI and node-ffi
var ref = require('ref');
var Struct = require('ref-struct');
var ffi = require('ffi');
// typedefs
var MPI_Datatype = 'int';
var MPI_Comm = 'int';
var MPI_Status = Struct({
count: 'int',
MPI_SOURCE: 'int',
#!/usr/bin/perl -w
my $file = "logo.png";
my $length = -s $file;
print "Content-type: image/png\n";
print "Content-length: $length \n\n";
binmode STDOUT;
open (FH,'<', $file) || die "Could not open $file: $!";
my $buffer = "";
while (read(FH, $buffer, 10240)) {
print $buffer;
@TooTallNate
TooTallNate / git-http-backend.js
Created January 19, 2015 23:00
Runs the `git http-backend` CGI server via `node-cgi`
var cgi = require('cgi');
var http = require('http');
// See: http://git-scm.com/docs/git-http-backend
http.createServer( cgi('git', {
args: [ 'http-backend' ],
env: {
'GIT_PROJECT_ROOT': process.env.HOME,
'GIT_HTTP_EXPORT_ALL': '1'
}
/*
wcwidth.js: JavaScript Portng of Markus Kuhn's wcwidth() Implementation
=======================================================================
Copyright (C) 2012 by Jun Woong.
This package is a JavaScript porting of `wcwidth()` implementation
[by Markus Kuhn](http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c).
Permission is hereby granted, free of charge, to any person obtaining a copy of
@TooTallNate
TooTallNate / index.js
Last active August 29, 2015 14:08
requirebin sketch
var domPaste = require('dom-paste');
console.log(domPaste);
var input = document.createElement('div');
input.contentEditable = 'true';
input.style.width='300px';
input.style.height='100px';
input.style.border='solid 1px black';
document.body.appendChild(input);