Skip to content

Instantly share code, notes, and snippets.

View natevw's full-sized avatar

Nathan Vander Wilt natevw

View GitHub Profile
@natevw
natevw / arduino.cc
Created January 20, 2015 22:01
Maybe the Arduino sketch used with node-nrf/test.js?
/*
Copyright (C) 2011 James Coliz, Jr. <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/
/**
* Example RF Radio Ping Pair
@natevw
natevw / socket_hack.md
Last active March 23, 2022 19:11
Simple TCP multiplexing protocol

TCP multiplexing protocol

A high-level approach to multiplexing multiple TCP connections over a single socket. Probably ignores a lot of queueing theory.

Background

The CC3000 WiFi chip may be especially unreliable if more than one socket is in use. So the idea is to maintain a single connection with a proxy server, which will handle the actual simultaneous outbound connections on a client's behalf. (Inbound connections are lower implementation priority.)

Terminology

@natevw
natevw / table-sorting.html
Last active August 29, 2015 14:10
Boostrap-ish table sorting helper. Inspired by https://github.com/drvic10k/bootstrap-sortable but ended up being a radically simpler design.
<table class="sorting">
<thead>
<tr>
<th>Item</th>
<th data-sorting="natural-dsc">Value</th>
<th data-sorting="default natural-dsc">Last action</th>
</tr>
</thead>
<tbody>
{% for item in items %}
@natevw
natevw / gist:f14812604be62c073461
Created November 17, 2014 23:44
register.simple_assignment_tag — custom Django template tag helper where result can be output directly *or* assigned as caller chooses. See https://gist.github.com/natevw/c30cd4f0c27323e3c275 for a (commented out) example of usage.
from django import template
class Library(template.Library):
def simple_assignment_tag(self, func=None, takes_context=None, name=None):
'''
Like assignment_tag but when "as" not provided, falls back to simple_tag behavior!
NOTE: this is based on Django's assignment_tag implementation, modified as needed.
'''
# (nvw) imports necessary to match original context
@natevw
natevw / gist:c30cd4f0c27323e3c275
Last active August 29, 2015 14:09
Simple fma (usually "fused multiply-add", but in this case it's not really fused so we'll say "fixed", or "functional", or "fun", or the recursive backronym "fma is not unix") template helper for Django — multiply the first two numbers provided and then add the last to that. Optional p= kwarg for rounding results to a fixed precision. When used …
from django import template
#import helper_from_linked_gist as template
register = template.Library()
@register.simple_tag # {% fma m x b %}
#@register.assignment_tag # {% fma m x b as y %}
#@register.simple_assignment_tag # optional "as y", requires using helper_from_linked_gist above
def fma(x, y, z, p=None):
v = float(x) * float(y) + float(z)
return v if p is None else ("%%.%uf" % p) % v
// paste this into browser's JavaScript console
// it should dump out your entire database as JSON text
// (you can also play with `db` and/or `exportedDocs` variables)
var db = Pouch("metakaolin2", function (e, db) {
if (e) throw e;
db.allDocs({include_docs:true}, function (e,d) {
if (e) throw e;
var docs = d.rows.map(function (r) { return r.doc; });
window.exportedDocs = docs;
<!doctype html>
<html><head>
<meta charset="utf-8">
<title>Inset border styling</title>
<style>
.list {
box-shadow: inset 3px 3px 10px grey;
height: 95px;
width: 150px;
@natevw
natevw / fermata.bigcouch.js
Last active December 31, 2015 02:09
Cloudant-specific Fermata plugin to treat write quorum failures as an error. (BigCouch returns a 202 status code which would normally be considered "successful", but the update could still end up lost or in conflict. So you may prefer to handle more similarly to a 500. Note that in this case, the best strategy AFAICT is to *write* until a 201 is…
// WORKAROUND: custom statusCheck to treat 202 responses as an error
// if Cloudant sends this, we can't be sure the write won't conflict
// see https://github.com/cloudant/bigcouch/issues/55
// and https://cloudant.com/for-developers/faq/data/
// and http://bigcouch.cloudant.com/api
// it's futile to attempt a unanimous quorum read to be sure (r=N is only advisory)
// so…assume the worst (e.g. conflicting or lost update) and treat these like a 500
// NOTE: this is copy-pasta from Fermata's builtin plugins.
fermata.registerPlugin('statusCheck_bigcouch', function (transport) {
return function (request, callback) {
@natevw
natevw / gist:7885121
Created December 10, 2013 03:02
extract timestamp/waterTemp aka random notes on (horrible) shell-scripting that (halfway) worked
cat /media/data_usb/logging/greenhouse.log | strings | sed -r 's/^([^ ]+).*waterTemp=([0-9]+).*$/\1 \2/'
// see https://gist.github.com/natevw/5789019 for pins
var NRF24 = require("./index"),
spiDev = "/dev/spidev0.0",
cePin = 24, irqPin = 25, //var ce = require("./gpio").connect(cePin)
pipes = [0xF0F0F0F0E1, 0xF0F0F0F0D2];
var nrf = NRF24.connect(spiDev, cePin, irqPin);
nrf.channel(0x4c).transmitPower('PA_MAX').dataRate('1Mbps').crcBytes(2).autoRetransmit({count:15, delay:500}).begin(function () {
var rx = nrf.openPipe('rx', pipes[0]),