Thursday, September 9, 2010

Migrated to ruhoh

Friday, July 23, 2010

Gumstix Overo Serial-USB with OS X 10.6 Snow Leopard

In a previous post I was using the PL2303 adapter. Now I'm using an FTDI that's built onto the Tobi board.

  • Download and install the driver from FTDI
  • reboot
  • ls /dev/tty*
  • /dev/tty.usbserial*
  • /dev/tty.usbserial-A100e0TP (this may change when unplugging and replugging)
  • sudo minicom -s (setup the port as the tty.usbserial-xxxxx)
Works for me!


This is reported to also work for OSX 10.3 Panther, 10.4 Tiger, 10.5 Leopard


Thanks to PixHawks for the info.

Thursday, July 22, 2010

Schneier Facts

Vs lbh nfxrq Oehpr Fpuarvre gb qrpelcg guvf, ur'q pehfu lbhe fxhyy jvgu uvf ynhtu. - Schneier Facts

Lucky for me, I didn't ask him. I asked a retard.

Hopefully, if you're wondering what the above text means, you find this page. Hopefully, if you're offended by retards, you didn't.

Friday, July 2, 2010

FuturesJS - Promises, Subscriptions, Joins, etc for JavaScript Part 1

What is FuturesJS?


FuturesJS is a JavaScript library which (when used as directed) simplifies handling Callbacks, Errbacks, Promises, Subscriptions, Joins, Synchronization of asynchronous data, and Eventually Consistent data. It is akin to this well documented this MSDN library, but with a liberal MIT license.


What is a Promise?


A promise (as per Douglas Crockford) is (essentially) a chainable callback object with the following methods:
  • when() - pass in a function which accepts a data parameter
  • fulfill() - pass in a single data parameter which will be sent to all wheners once
  • fail() - pass in a function which accepts an error parameter


  • smash() - pass in a single data parameter which will be sent to all failers once
Chainable means that the object returns itself so these methods can be called in sequence. A normal callback function might look like this:
result = getAsyncData(arg1, arg2, callback, {onError : errback, setTimeout : 5000});
To use a promise with such a function you would do something like this:
var p = Futures.promise();
    result = getAsyncData(arg1, arg2, p.fulfill, {onError : p.fail, setTimeout : 5000});
    p.when(callback1)
     .when(callback2)
     .when(callback3)
     .fail(callback4);
But you can also "promisify" a function in a number of ways: A) return an object with the when() and fail() methods
function getAsyncData(param1, param2) {
        var p = Futures.promise(),
        result = oldGetAsyncData(arg1, arg2, p.fulfill, {onError : p.smash, setTimeout : 5000});
        // Implements a synchronous callback to hand back the original data
        p.withResult = function(func) {
            func(result); // XMLHTTPRequest object is the result in this case
        }
        return p;
    }
    var xhr;
    getAsyncData(param1, param2)
        .withResult(function (r) {
            xhr = r;
        })
        .when(doStuff)
        .when(doMoreStuff)
        .fail(undoStuff);
    if (i_change_my_mind) {
        xhr.abort();
    }
B) allow a promise to be passed instead of a callback or returned with a synchronous callback.
function getAsyncData(param1, param2, p) {
        return oldGetAsyncData(arg1, arg2, p.fulfill, {onError : p.smash, setTimeout : 5000});
    }
    var promise = Futures.promise(),
    xhr;
    xhr = getAsyncData(param1, param2, promise);
    promise
        .when(doStuff)
        .when(doMoreStuff)
        .fail(undoStuff);
    if (i_change_my_mind) {
        xhr.abort();
    }
C) pass back a promise as a synchronous callback
function getAsyncData(param1, param2, promiseback) {
        var p = Futures.promise();
        return oldGetAsyncData(arg1, arg2, p.fulfill, {onError : p.smash, setTimeout : 5000});
    }
    var promise,
    xhr;
    xhr = getAsyncData(param1, param2, function (p) {
        promise = p;
    });
    promise
        .when(doStuff)
        .when(doMoreStuff)
        .fail(undoStuff);
    if (i_change_my_mind) {
        xhr.abort();
    }
Where many functions allow only one callback, a promise allows you to enlist multiple callbacks before and after the target function has been called. FuturesJS provides convenience functions for you to wrap existing functions via promisify() (method A) and noConflict() (method C).

What are Joins?

A join is a special type of promise which allows you to get the results of multiple promises once all of them have completed or failed.
// Futures.join(p1, p2, p3, ..., pN)
    Futures.join(p1, p2, p3) // There is no limit to how many promises you pass in
        .when(function (p_arr) {
            // p_arr[0] is the result of p1
            // p_arr[N-1] is the result of pN
        })
        .fail(function (p_arr) {
            // at least one of the promises was smashed
            // you'll have to discover which on your own
        });

Recap: Why is this useful?

I have two main use cases:
  • Client-side Caching in Memory - you may be using some sort of eventual consistency already have the data the user wants (fresh), you may have an older version of the data (stale), or the data you have is too old or non-existant (useless). Using a promise you can always promise that the data will be available, without worrying about when it will be available. If it's available now it is essentially a synchronous callback. If it's available later, it may take time.
  • Mashups - You need to get datasets from multiple sources (facebook, twitter, flickr, amazon, google maps, etc) and run sort of transformation on the combination of that data before presenting it to the user.

If you're lucky I'll write some psuedo-code for these examples in Part 2.

Hang around and I'll explain more about subscriptions and other fun stuff.

Monday, June 28, 2010

JSON-C Example

So you want to parse JSON with C? Welcome aboard!

First get json-c, configure, compile, and update your ld cache
wget http://oss.metaparadigm.com/json-c/json-c-0.9.tar.gz
tar xf json-c-0.9.tar.gz
cd json-c-0.9
./configure && make && sudo make install
sudo ldconfig

Now use one of the test files provided or this example:
http://www.jroller.com/RickHigh/entry/json_parsing_with_c_simple
cd ..
vim json-example.c
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>

#include "json.h"

int main(int argc, char **argv)
{
  struct json_object *new_obj;

  MC_SET_DEBUG(1);
  // I added some new lines... not in real program
  new_obj = json_tokener_parse("/* more difficult test case */ { \"glossary\": { \"title\": \"example glossary\", \"pageCount\": 100, \"GlossDiv\": { \"title\": \"S\", \"GlossList\": [ { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\", \"markup\"] } ] } } }");
  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

  new_obj = json_object_object_get(new_obj, "glossary");
  printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));

  new_obj = json_object_object_get(new_obj, "pageCount");


  int pageCount = json_object_get_int(new_obj);

  printf("Page count = %d", pageCount);

  json_object_put(new_obj);

  return 0;
}

Put it in a Makefile with the right settings.
vim Makefile
all: static

static:
  gcc -static json-example.c -L/usr/local/lib -ljson -I/usr/local/include/json -o json-example-static
  ./json-example-static

shared:
  gcc json-example.c -L/usr/local/lib -ljson -I/usr/local/include/json -o json-example-shared
  ./json-example-shared

.PHONY: all static shared

Finally test that it works:
make static
make shared


Hopefully that's enough to get you started. The big thing is that when you compile your own projects, make sure that you include the json library and paths.

Monday, June 21, 2010

Refactor from the Commandline

Here's an example script I used to refactor the names of files in a C project.
It can easily be adapted to refactor variable names, etc in JavaScript, Ruby, Python, PHP (*shudder*), etc.

#!/bin/bash

if [ ! -n "${1}" ]
then
    echo "
Usage: refactor.sh new_basename old_basename

Note: do not include '.c' or '.h', this is added for you.
    "
    exit
fi

NEW=${1}
OLD=${2}

svn mv include/${OLD}.h include/${NEW}.h
svn mv ${OLD}.c ${NEW}.c


find ./ -name '*.c' | xargs sed -i "s/${OLD}\.h/${NEW}\.h/gi"
find ./ -name '*.c' | xargs sed -i "s/${OLD}\.c/${NEW}\.c/gi"

find ./ -name '*.h' | xargs sed -i "s/${OLD}\.h/${NEW}\.h/gi"
find ./ -name '*.h' | xargs sed -i "s/${OLD}\.c/${NEW}\.c/gi"

Saturday, June 19, 2010

OSX-style Screen Capture in Ubuntu Linux

Do you ever think to your self "Man, I love doing screen captures, but on Ubuntu Linux it's just cumbersome in comparison to Mac OS X" (notice that Windows 7 followed the trend with its new Snipping Tool too).


Guess What! It doesn't have to be cumbersome anymore:

System -> Preferences -> Keyboard Shortcuts
Name: OSX-style Screen Capture
Command: gnome-screenshot --area
Shortcut: alt-ctrl-4

Unfortunately there's a bug and you can't map alt-shift-4 as you would like. Sad day. Oh well.