An open source LED grid clock

So your analog wall clock breaks… What do you do about it? Make a digital clock, of course. Lumitock is an “LED grid” clock, where the time is represented by the number of lit LEDs within a segment of the clock.

Lumitock is based on the Arduino platform, specifically using an Arduino Pro Mini 328 (socketed, I have commitment issues). It also uses a DS1337C RTC for time-keeping, and TLC5940 LED drivers to keep track of all that light. I used BatchPCB to fab some boards for it, and they turned out great. Very solid, and such an easy service when time isn’t much of a factor.

It is open hardware and software, available on github. The Arduino project as well as Eagle CAD project files are included.

Beware! The current version (tag version-1) has a fairly nasty issue with the PCB layout, the TLC5940 sockets were drawn as .4″ wide, not .3″. Not fun to solder that up, you can see the kludgey fixes in the images. I will be updating the PCB as well as making a SMT-only version to slim things up.

Images

Migrating multi-project Subversion repositories to Git

So I finally gave in and submitted to the version control Gods, Git is the way to go.  Of course it would happen that Git repositories are typically a single-project-per-repo situation, and I have a couple globbed together Subversion repositories with many projects in them.  How do I separate out my histories from the individual projects, maintain tags and branches, and not lose that precious history that nobody really ever looks at anyway?

Well, luckily there is a nifty script called svn2git that gets us the bulk work of migrating history of a single Subversion repo to a new Git repo.  Coupled with a few admin tools that come with Subversion, we got it covered.

The current situation

I have a couple repositories, looking like the following:

/var/lib/svn/repo1
  /project1
    /trunk
    /tags
    /branches
  /project2
    /trunk
    /tags
    /branches
/var/lib/svn/repo2
  /project3
    /trunk
    /tags
    /branches
  /project4
    /trunk
    /tags
    /branches

The desired situation

I’d like them all separated, with all history!!!  Resulting in the following structure:

/var/lib/git/project1.git
/var/lib/git/project2.git
/var/lib/git/project3.git
/var/lib/git/project4.git

Step 1 – Dump your projects from the Subversion repository

Subversion ships with a tool, svnadmin, that will help dump a repository.  It must operate on the local filesystem of the Subversion repository, not a working copy.  As a result, it only dumps full repositories, hence the problem in general.

Be not afraid though!  Subversion also ships with svndumpfilter, the key to our salvation.

For my example above, to dump project1, project2, project3 and project4 into separated Subversion repositories, I would:

mkdir svn-convert && cd svn-convert
svnadmin create project1.svn
svnadmin create project2.svn
svnadmin create project3.svn
svnadmin create project4.svn
svnadmin dump /var/lib/svn/repo1 | svndumpfilter include project1 | svnadmin load project1.svn
svnadmin dump /var/lib/svn/repo1 | svndumpfilter include project2 | svnadmin load project2.svn
svnadmin dump /var/lib/svn/repo2 | svndumpfilter include project3 | svnadmin load project3.svn
svnadmin dump /var/lib/svn/repo2 | svndumpfilter include project4 | svnadmin load project4.svn

And viola, separated Subversion repositories.

Step 2 – Convert via svn2git

First, download and install svn2git, it’s a Ruby script, the instructions are good, it’ll work for you.

Next, create a text file, ‘authors.txt’ with contents like the following:

name1 = Full Name <email@example.com>
name2 = Another Name <email2@example.com>

Substituting the names of users who have committed to the Subversion repositories of course. This will map the Subversion users to Git authors.

Next, we make a place for the new repository and run the conversion utility.  It will init the Git repository and create the necessary history.

mkdir project1.git && cd project1.git
svn2git file:///home/nick/svn-convert/project1.svn \
    --trunk project1/trunk --branches project1/branches \
    --tags project1/tags --authors ../authors.txt

Repeated for each repository with appropriate substitutions, and you’ve got yourself some Git repositories.

Quick verification should show you you’re in business:

$ git tag
v1.0
v1.0.1
v1.0.2
v1.0.3
$ git branch
* master
  ticket102
  ticket87

All that is left is to put them in a safe place and start cloning!

Restoring from a Samba-based Time Machine backup (kinda)

Panicking about not being about to find or mount your Samba share you’ve been blissfully backing up to over your network?  Trying to restore to a new hard drive using the Leopard boot DVD?

I recently had the pleasure of a hard disk crash on my MacBook, and only a month earlier had started backing up to a Samba-share via Time Machine.  I had the “how to restore” question nagging in the back of my head when I set it all up but I figured someone had it figured out, otherwise why would so many articles exist to show you how to set it up?

I’ve been trying to find out how people using the TMShowUnsupportedNetworkVolumes hack to use Time Machine on smbfs shares have been restoring and it would seem that the answer is “they aren’t”.  That, or they’re simply using it as an “oops” fixer, restoring a file here and there.

When booting from the Leopard DVD, firing up the terminal and attempting to mount the share, the following delightful message shows up:

mount_smbfs --> mount_smbfs: failed to load the smb library: Unknown error: 1102

Searching for that was even more disappointing.  Other people running into the issue, no solutions.  It seems smb support is just not available on the boot DVD.

I ran into a possible solution, copy the Time Machine sparse bundle onto a removable hard disk, and hook it up to the laptop. Unfortunately all my external storage is formatted ReisferFS or ext3, neither are supported filesystems, and I didn’t feel like changing one just to fix this.

So in comes the hack.  Luckily the Samba share is on an Ubuntu 9.04 (Jaunty) server, so adding support for something Apple does support on the boot DVD is tragically easy. This is a fairly specific solution, but variations on it will work for many different servers.

Enter AFP

Looking through the other available mount applications, we also have mount_afp available.  This mounts Apple Filing Protocol-based shares, and it works too, bonus!

So it boils down to enabling AFP on the server and sharing the same volume via AFP.  AFP on Linux (BSD, etc) is supplied by netatalk, and here’s a step-by-step of how I wrapped it all up.

On the server:

  • sudo aptitude install netatalk
  • Edit /etc/netatalk/AppleVolumes.default
  • Add entry for the volume, such as:
    • /mnt/time_machine "tmachine"
  • Save the file
  • sudo /etc/init.d/netatalk restart

On the Mac:

  • Boot from the Leopard install DVD
  • Enable Airport (if on WiFi), join your network
  • From the menu bar, select Utilities -> Terminal
  • Navigate to /Volumes
  • Create a new mount point for the Time Machine volume
    • mkdir /Volumes/tmachine
  • Mount the AFP share on the new point (details)
    • mount -t afp afp://username:password@server.hostname/tmachine /Volumes/tmachine
  • Quit Terminal
  • Back at the main menu bar, select Utilities -> Restore System From Backup…
  • You should see your Time Machine backup volume listed
  • Select it, and select the date from which you wish to restore
  • Wait a considerable amount of time for it to determine the space needed
  • Enjoy the hours and hours of restore time!

Aftermath

The basic AFP installation added to the server is likely pretty insecure, I purged it as soon as the restore completed.  Read this for a more formal treatment on setting up an AFP server on Linux.  It is likely that the real solution is to stop suggesting people use Samba as a file server for Time Machine backups, instead switching to AFP altogether.

Update

Just recently came back to this for a Snow Leopard restore, the directions are unchanged.  The nice bonus is there have apparently been some optimizations during the restore, and an initial space calculation was practically instant.  Nice.

Time to broadcast, iPhone style

Ran into a need to dynamically determine the current UDP broadcast address for the WiFi interface on the ole’ iPhone. Since NSHost appears to be a private API even w/the 3.0 software, it seems one must go lower. I wrapped it up in a neat little bundle that seems fairly usable if not verbose and full of magic (but understandable) numbers.

A few things of note. en0 is the WiFi interface. There are others. Instrument the following code w/some debug to get them all out. The ip/netmask methods return nil when the WiFi interface is not active. I would also be shocked if there were no corner cases I am ignoring…

#include <arpa/inet.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <errno.h>
#include <ifaddrs.h>
#include <stdio.h>
 
static NSString *kWifiInterface = @"en0";
 
@implementation NetUtil
 
+ (NSString *)broadcastAddressForAddress:(NSString *)ipAddress withMask:(NSString *)netmask {
    NSAssert(nil != ipAddress, @"IP address cannot be nil");
    NSAssert(nil != netmask, @"Netmask cannot be nil");
    NSArray *ipChunks = [ipAddress componentsSeparatedByString:@"."];
    NSAssert([ipChunks count] == 4, @"IP does not have 4 octets!");
    NSArray *nmChunks = [netmask componentsSeparatedByString:@"."];
    NSAssert([nmChunks count] == 4, @"Netmask does not have 4 octets!");
 
    NSUInteger ipRaw = 0;
    NSUInteger nmRaw = 0;
    NSUInteger shift = 24;
    for (NSUInteger i = 0; i < 4; ++i, shift -= 8) {
        ipRaw |= [[ipChunks objectAtIndex:i] intValue] << shift;
        nmRaw |= [[nmChunks objectAtIndex:i] intValue] << shift;
    }
 
    NSUInteger bcRaw = ~nmRaw | ipRaw;
    return [NSString stringWithFormat:@"%d.%d.%d.%d", (bcRaw & 0xFF000000) >> 24,
            (bcRaw & 0x00FF0000) >> 16, (bcRaw & 0x0000FF00) >> 8, bcRaw & 0x000000FF];
}
 
+ (NSString *)ipAddressForInterface:(NSString *)ifName {
    NSAssert(nil != ifName, @"Interface name cannot be nil");
 
    struct ifaddrs *addrs = NULL;
    if (getifaddrs(&addrs)) {
        NSLog(@"Failed to enumerate interfaces: %@", [NSString stringWithCString:strerror(errno)]);
        return nil;
    }
 
    /* walk the linked-list of interfaces until we find the desired one */
    NSString *addr = nil;
    struct ifaddrs *curAddr = addrs;
    while (curAddr != NULL) {
        if (AF_INET == curAddr->ifa_addr->sa_family) {
            NSString *curName = [NSString stringWithCString:curAddr->ifa_name];
            if ([ifName isEqualToString:curName]) {
                char* cstring = inet_ntoa(((struct sockaddr_in *)curAddr->ifa_addr)->sin_addr);
                addr = [NSString stringWithCString:cstring];
                break;
            }
        }
        curAddr = curAddr->ifa_next;
    }
 
    /* clean up, return what we found */
    freeifaddrs(addrs);
    return addr;
}
 
+ (NSString *)ipAddressForWifi {
    return [NetUtil ipAddressForInterface:kWifiInterface];
}
 
+ (NSString *)netmaskForInterface:(NSString *)ifName {
    NSAssert(nil != ifName, @"Interface name cannot be nil");
 
    struct ifreq ifr;
    strncpy(ifr.ifr_name, [ifName UTF8String], IFNAMSIZ-1);
    int fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (-1 == fd) {
        NSLog(@"Failed to open socket to get netmask");
        return nil;
    }
 
    if (-1 == ioctl(fd, SIOCGIFNETMASK, &ifr)) {
        NSLog(@"Failed to read netmask: %@", [NSString stringWithCString:strerror(errno)]);
        close(fd);
        return nil;
    }
 
    close(fd);
    char *cstring = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
    return [NSString stringWithCString:cstring];
}
 
+ (NSString *)netmaskForWifi {
    return [NetUtil netmaskForInterface:kWifiInterface];
}
 
@end

NSURLConnection + startImmediately:NO == boom?

Having issues creating NSURLConnections using initWithRequest:delegate:startImmediately?

NSURLConnection *c = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:url]
                                                     delegate:self
                                             startImmediately:NO];

Apparently when not using the simpler initWithRequest:delegate:, or even startImmediately:YES, the connection does not get scheduled in the current run loop. And again apparently, this causes unhappiness to occur when you eventually get around to calling start.

Simple fix, just stuff it in the current run loop before calling start and everyone gets along just fine.

[c scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[c start];

If there is something I am doing wrong or something I can do to prevent this, I’d like to know. Alas, the API is fairly brief on NSURLConnection, I don’t think I’m missing anything. This seems consistent with Cocoa, Cocoa Touch.

Some Airsoft Turret play

Been toying with the idea of making an Airsoft Gun controller wirelessly via Wii Remote.  Inspired by a previous DefconBots challenge.  Just managed to get control of 2 servos via a ATMega8, serial link to a PC and a Wii Remote talking to said PC via BlueTooth.  Pretty hacky but it works, and it’s way easier than grokking BlueTooth on the MCU for now.

First video is of 1 servo working with really jittery input.

Second video is 2 servos on X and Y axis with smoothed input.  Much nicer.

Using libwiimote on the host side for Wii Remote interfacing.

Python SubWCRev

Fired out a little Python script for exercise…

pysubwcrev is a Python version of TortoiseSVN’s SubWCRev app. SubWCRev is a windows-only console app, pysubwcrev is a command-line argument compatible replacement that is Python-based, and therefore runs on any platform with an available Python interpreter and pysvn. Currently only Linux is tested.

The code is hosted @ github. Currently no packaged release exists but it is (should be?) feature complete.

Note: This is just a hack at playing w/Python, jabs and criticism w/the style can come in the form of patches.