Skip to content

Instantly share code, notes, and snippets.

View keshavsaharia's full-sized avatar

Keshav Saharia keshavsaharia

View GitHub Profile
@keshavsaharia
keshavsaharia / Arduino - nextInt
Created June 30, 2013 05:39
nextInt() for Arduino serial communication
// Main method to call to get an integer.
// It will block until an integer followed by a non-digit delimiter is read from the serial input stream.
int getInt() {
return getInt(0);
}
// Recursive helper method.
// @param sum - an accumulator that "builds" the next number, character by character.
int getInt(int sum) {
// Read a single character from the serial input stream.
@keshavsaharia
keshavsaharia / Mathematica - ASCII
Created June 30, 2013 05:44
A Mathematica function for generating beautiful ASCII art out of any image.
(*ASCIIrange = { " ","` ",". "," .","..",".:",":.","::",":#","##","#@","x@","o@","@@","@X","XX"};*)
(*ASCIIrange = { " "," ",".","..",".:","::",":i","io","ex","pq","Xq","AG","ZY","VW"};*)
(* Produces the best results by randomizing letters using a held random choice call *)
ASCIIrange = {" ", Hold[RandomChoice[{" ", " `"}]], ". ", "..",
Hold[RandomChoice[{".:", ":."}]],
Hold[RandomChoice[{"::", ";:", ":;", ";;"}]],
Hold[RandomChoice[{":x", "x:"}]],
Hold[RandomChoice[{"io", "ae", "oc"}]],
Hold[RandomChoice[{"yj", "tp", "qw"}]],
@keshavsaharia
keshavsaharia / Mathematica - StringTakeUntil
Created June 30, 2013 05:46
A missing function in Mathematica that is essential for many string parsing applications. Inputs a string and a regular expression delimiter, and takes from the string until it matches the delimiter (or takes the entire string if the delimiter is not found).
StringTakeUntil[str_, delim_] :=
If[StringMatchQ[str, delim ~~ ___] || StringLength[str] == 0, "",
StringTake[str, 1] <> StringTakeUntil[StringDrop[str, 1], delim]];
@keshavsaharia
keshavsaharia / Mathematica - Templatize
Created June 30, 2013 05:50
Use Mathematica to generate static webpages by filling HTML comments with strings generated from Mathematica expressions.
Templatize[source_, replacement_] := If[StringQ[replacement],
StringReplace[source, {"<!--$[]-->" -> ToString[replacement]}, 1],
If[ListQ[replacement] && Length[replacement] > 0,
Templatize[Templatize[source, First[replacement]],
Rest[replacement]],
If[Head[replacement] === Rule,
If[ListQ[Last[replacement]],
Templatize[ source, (First[replacement] -> #) & /@ Last[replacement]],
StringReplace[ source, {"<!--$[" <> ToString[First[replacement]] <> "]-->" ->ToString[Last[replacement]]}, 1]], source]]]
@keshavsaharia
keshavsaharia / PERL - web power switch
Last active November 27, 2021 17:55
PERL script for communicating with the Digital Loggers Web Power Switch that uses a simple decomposition-based natural language parser.
my $cmd = "";
my $base = "/path/to/dlogger.pl"; # Change this to the path where the provided PERL utility is kept
my $auth = " admin:1234"; # Change this to the authentication you use to log in to your switch
my $status = "";
my $buffer = "";
my $temp = "";
my @outlets;
my @outletstatus;
@keshavsaharia
keshavsaharia / bash - ec2 upload
Created July 4, 2013 00:19
A shell script and sample target file to upload to an EC2 instance, which abstracts the tedious scp commands. Be sure to rename "example.target" to the name of your project, i.e. "foo.target". To run it, use "./upload example" to completely rewrite the remote directory, or use "./upload example file.html" to only rewrite a specific file, or use …
--- in example.target ---
WEB_LOCATION=1.2.3.4 # remote location
PEM_LOCATION=example.pem # pem file location
DIR_LOCATION=/path/to/your/website # directory location
REM_LOCATION=~/htdocs # the directory on the server to write to
REM_USER=admin # change to the server's user account name
--- in upload ---
source $1.target
@keshavsaharia
keshavsaharia / ultra.ino
Last active December 19, 2015 17:38
Ultrasonic Distance Ethernet.ino
/*
Web Server
A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)
@keshavsaharia
keshavsaharia / gist:7610053
Created November 23, 2013 02:34
product 1
$(document).ready(function() {
$('#productList').empty();
for (var i in products) {
var product = products[i];
$('#productList').append(
$('<div></div>') // make a div
.addClass('col-md-4 portfolio-item')
.append(
$('<a></a>')
.attr('href', 'linktome.html')
@keshavsaharia
keshavsaharia / WebStarter
Last active December 30, 2015 13:38
Basic HTML starter page with Bootstrap, Font Awesome, and Firebase, for the students of TechLab learning about web development.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
<script type="text/javascript" src="http://cdn.firebase.com/v0/firebase.js"></script>
<script type="text/javascript" src="https://cdn.firebase.com/js/simple-login/1.4.1/firebase-simple-login.js"></script>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
<link href="http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
<style>
@keshavsaharia
keshavsaharia / keypadmatrix
Created December 15, 2013 19:56
4x4 keypad matrix for Arduino
int getKey() {
int A = 2;
while (A <= 5) {
pinMode(A, OUTPUT);
digitalWrite(A, HIGH);
int B = 6;
while (B <= 9) {
pinMode(B, OUTPUT);
pinMode(B, INPUT);
if (digitalRead(B) == HIGH) {