Tag Archives: programming

A zsh adventure

Oh yeah, zsh guys.  It’s awesome, and you should know that, but what makes one switch from good ol’ bash?  Generally, it’s prompt magic, but really there’s some other nice features that make it worth considering.  But first, prompt magic.  I had some trouble getting the prompt to respect the width of my terminal, but it was resolved by escaping some things I had in there.  I ended up with this:

To do that, you just need the following in your .zshrc in your home directory:

autoload -U colors && colors

# Prompt Customization
PS1="%{$fg[blue]%}%n%{$fg_bold[yellow]%}✯%{$fg[blue]%}%m%{$fg_bold[black]%}:%{$fg_no_bold[green]%}%~ %{$fg_bold[black]%}%#%{$reset_color%} "
RPS1="%{$fg_bold[black]%}%D{%Y%m%d %H:%M}%{$reset_color%}"

Yes, that’s a unicode character.  That’s perfectly legal, which is awesome.  Next, we have to get bash reverse history search functionality working in zsh, which is really easy with a simple keybinding for control-r:

## History search like bash
bindkey '\e[3~' delete-char
bindkey '^R' history-incremental-search-backward

And we’re off to the races.  So far, I really like it.  I recommend that you *nix nerds try it.

Learn from my AVR mistakes

I’m learning a lot programming in AVR C.  There’s are a few subtleties to watch out for, and some of them have had me banging my head against the wall for days.  This post is an attempt to prevent anyone else playing with AVR outside of AVR Studio (I’m using Linux and the command line) from having the same issues.

Sections for avr-objcopy

When you compile an AVR C program, you get object code, which you have to convert into HEX format to flash onto the chip.  You do this using avr-objcopy, and there are apparently several valid ways of doing it.  I recommend learning about what the -j and -R flags are doing when you come across tutorials with this command in them.  I was using one from a tutorial that looked like this:

avr-objcopy -j .text -O ihex some.o some.hex

That’s all fine and dandy except it only works for programs that don’t end up using the .data section of the object code! Now, this wouldn’t be a big deal if there were any warnings about this, but there aren’t – you just have to know to add a -j .data. A better solution, posted here by clawson, is to use the -R flag to remove parts you know you do not want. Here’s my current strategy:

avr-objcopy -R .fuse -R .lock -R .eeprom some.o some.hex

This fixed 2 separate problems when I did it, one with struct initialization and one with my LCD displaying block characters for strings but working for single characters.

LCDiesel: Yet another AVR HD44780 LCD library

I created an HD44780 LCD library for the AVR architecture today. There were a ton already, and mine is based off of one by Peter Fleury. I added a couple cool little features, and decided it needed to be on github to allow people to improve it and access it easier.

LCDiesel on Github

Here’s a quick code example using the library:

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include "lcd.h"

// Include the chars we want
#include "chars.h"

int main(void)
    /* initialize display, cursor off */
    lcd_command(LCD_FUNCTION_4BIT_2LINES );

    // Testing if x,y are set wrong
    lcd_gotoxy(3, 1);

    // Load character
    lcd_custom_char_p(0x00, _char_open_rectangle);
    lcd_custom_char_p(0x01, _char_heart);

    // We better still be at 3, 1


See how easy it is to define custom characters? Cool, huh? I think so. The heart and rectangle bit vectors are in chars.h, and are only compiled in when they are defined, keeping code size down. Here’s how I have it hooked up on the breadboard (and the output of the program above):


Parsing WFM Oscilloscope files in C

So, I got this Rigol Oscilloscope – I wrote a little instructable about it. One cool feature is the ability to save waveforms to a USB stick. There are many options for the file format, but the default is a WFM file. Now, a true minimalist could just use the CSV option, but the WFM is a binary format that contains a lot more information directly from the scope. I wrote a little C program hosted on github that allows you to convert those pesky proprietary files into something useful – namely a gnuplot image!

This is super alpha, and I’ll happily take patches. Cheers!

The Chronic: A Ruby Time Library

So, I was trying to use Ruby to get the date 6 months ago.  This is usually accomplished using the core extensions in ActiveSupport, which is OK, but it was too much work.  Then, I found an awesome article talking about Chronic, a minimal date/time parsing library for Ruby:

$ irb -rrubygems -rchronic
irb(main):001:0> Chronic.parse('6 months ago')
=> 2010-03-25 21:10:10 -0400

Wow that’s like so awesome.  I wonder what else it can do…

irb(main):002:0> Chronic.parse('three days after memorial day')
=> 2010-09-28 23:00:00 -0400

Well, that didn’t work.  Maybe I’ll submit a patch…  How about this?:

irb(main):014:0> Chronic.parse('three days after today')
=> 2010-09-28 23:00:00 -0400
irb(main):015:0> Chronic.parse('three days after tomorrow')
=> 2010-09-29 12:00:00 -0400

Well, that worked.  Patch time!


Well, I added US non-religious holidays to chronic.  It took me a while to figure it out, but it works!

irb(main):001:0> Chronic.parse("MLK Day")
=> 2011-01-17 12:00:00 -0500
irb(main):002:0> Chronic.parse("christmas eve")
=> 2010-12-24 12:00:00 -0500
irb(main):005:0> Chronic.parse("christmas")
=> 2010-12-25 12:00:00 -0500

Of course, my original example still fails, because Memorial Day is very special:

# (last Monday of May, traditionally 30 May)
text.gsub!(/\bMemorial Day\b/i, 'last monday in may')

The last selector still doesn’t work since it’s not an identifiable ordinal.  If someone wants to fix this, please do it.  Once things like “the last monday in may” work in all of Chronic, Memorial day will start working automatically.

You can get the ruby gem using the instructions here:


The git repository is here if you want to fork:


Notes about Google Go

While trying out Google Go, I found out whitespace seriously matters, certain constructs you’re used to are not present, there is more than one way to declare variables, etc. This is a short collection of those observations.


Blocks cannot have a newline between the declaration and the first opening brace.

This is a bit strange, as many people think that having all the curly braces in line is more readable, and it is a common practice in many languages to do so. I’ve recently converted to this line of thinking, but I’ll have to revert back to hanging curlies for Go. I’m OK with this, it’s just weird that it actually enforces one side of this hotly-debated topic.

Environment Variables

There might be a way to get rid of this, and I think it’s nice that you can point Go to a specific location for libraries, it would be nice if the default installation didn’t force me to update my .bashrc.


So, to start, I realize I can use gccgo for a more traditional compilation process, but I’d like to use the most feature-rich version of the language. Why does go have 2 different binaries for compiling and linking? Also, the naming scheme, while I think I understand where they’re coming from, is ridiculous. Should I use 8g or 6g? I don’t know – why is it not go and go64? That would actually be easy to remember. For reference, 8g is for i686, and 6g is for amd64. I don’t get it. So, I’m ready to compile, and I just run 8g myfile.go right? Yes, then I have to run 8l myfile.8. Wait, if I use 6g I get a different file extension! This makes for horrendous Makefiles that I’ll have to manage somehow. Perhaps I’ll just do everything with Scons instead, since I think it wouild be a lot cleaner.

I found a great post on Stack Overflow about how to hook this thing up to Scons. I ended up with this:

Maybe someone at the Scons project will eventually integrate something like this into Scons…


So far, I’ve noticed there’s no pre-increment operator for integers like there is in C++. This is a bit annoying, but I’ll get over it.


There’s no while or do-while loops. Just a for loop. This makes me sad – I really like while and do-while loops for certain cases. I’m sure the for construct in Go is flexible enough to handle this omission, but it makes me uncomfortable at the moment. We’ll see if I like what they’ve done.


I like Google Go, but it’s going to take some getting used to.

Burst C++ Library

I made a little C++ library called burst. The idea is to extend the awesomeness of Boost with convenience functions for common tasks (mainly my own common tasks, but maybe others can benefit as well). The current trunk is here. Here’s a quick example:

And just that quick (3 lines) you’re sending an HTTP request to boost.org’s root page and streaming the result to STDOUT. Pretty cool, eh? What if you want a different website?

It’s nearly as easy as Ruby’s open-uri! Inheriting from it makes it even better, since you can quickly customize a web api class around the gory HTTP details Burst.Asio.HTTP.Client does for you.

1,000,000th Fibonacci Number One-Liner in C

This is possibly the best one-liner I’ve ever written:

gcc -x c -o /tmp/out - -lgmp <<< '#include <stdlib.h> 
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <gmp.h>
void omg_i_love_leonardo_of_pisa(uint32_t num, mpz_t * result) { mpz_t retval, last, tmp; mpz_init(retval);
 mpz_init(last); mpz_init(tmp); uint32_t i = 1; if(num == 0) return; mpz_set_ui(retval, 1U); 
mpz_set_ui(last, 0U); for(; i < num; i++) { mpz_set(tmp, retval); mpz_add(retval, retval, last); 
mpz_set(last, tmp); } mpz_set(*result, retval); } int main() { uint32_t num; mpz_t fibo; mpz_init(fibo);
omg_i_love_leonardo_of_pisa(1000001, &fibo); mpz_out_str(stdout, 10, fibo); printf("\n"); return 1; }
' && time /tmp/out

It compiles a C program given from STDIN, puts it in /tmp/out, and runs it with time to find the time it takes to run. It generates the 1,000,000th Fibonacci number. Try it!

Update May 21, 2011

I changed the algorithm to do a matrix multiplication trick. The only problem is it goes over the number you ask for currently. I'm going to fix this with memoization soon.

gcc -x c -o /tmp/out - -lgmp <<< '#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmp.h>
void print_state(mpz_t* fm2, mpz_t* fm1, mpz_t* f, uint32_t n){gmp_printf("fib(%d) = %Zd\n", n, f);}
#define NEXT_FIB() mpz_set(oldfm1, fm1);mpz_set(oldf, f);mpz_mul(f, f, f);mpz_mul(tmp, fm1, fm1);\
mpz_add(f, f, tmp);mpz_mul(fm1, oldf, fm1);mpz_mul(tmp, oldfm1, fm2);mpz_add(fm1, fm1, tmp); \
mpz_set(tmp, fm2);mpz_mul(fm2, oldfm1, oldfm1);mpz_mul(tmp, tmp, tmp);mpz_add(fm2, fm2, tmp);\
n += i;i *= 2;
int main(){mpz_t fm2, fm1, f;uint32_t n = 2;uint32_t i = 1;mpz_inits(fm2, fm1, f, NULL);mpz_set_si(fm2,
0);mpz_set_si(fm1, 1);mpz_set_si(f, 1);mpz_t oldf, oldfm1, tmp;mpz_inits(oldf, oldfm1, tmp, NULL);
uint32_t g = 1000000;while(n<g){NEXT_FIB();}print_state(&fm2, &fm1, &f, n);return 0;}' && time /tmp/out

This outputs almost immediately on my Intel Atom:

fib(1048577) = 19202837189514814.................

real	0m0.840s
user	0m0.280s
sys	0m0.010s

The code is here. Feel free to fork and improve!

Update August 30, 2013

I ended up looking at this again, and I improved it immensely. Apparently, gmp has built-in fibo functions (!!):

int main()
  int n = 1000000;
  mpz_t fm2;
  mpz_inits(fm2, NULL);
  mpz_fib_ui(fm2, n);
  gmp_printf("fib(%d) = %Zd\n", n, fm2);
  return 1;

This produces a number MUCH faster than the above implementation. It also makes a nicer oneliner:

gcc -x c -o /tmp/out - -lgmp <<< '#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmp.h>
int main() { int n = 1000000; mpz_t fm2; mpz_inits(fm2, NULL); mpz_fib_ui(fm2, n); gmp_printf(\"fib(%d) = %Zd\n\", n, fm2); return 1; }" && time /tmp/out

real    0m0.057s
user    0m0.040s
sys     0m0.008s

GitHub Widget for WordPress

For all you coders out there wanting to show off all those forked repositories, the github-widget is for you.

There are a few ways to install.

  1. Download from here.  The most recent stable release will be available.  As of this writing, it’s REL-1.3.
  2. Clone the git repository: git clone git://github.com/hank/github-widget.git
  3. Get it through WordPress (Work in Progress)

If you use one of the first 2 methods, just drop the resultant directory into wp-content/plugins/.

Using any of the three methods, after you have it installed, go to the Admin panel, Plugins -> Installed.  Then, activate the plugin.  Then, go to Appearance -> Widgets.  Put it into a sidebar, and go look at the results.

Please report any errors or problems as a comment to this post.

UPDATE: I just found out that my minimal searching for ‘Github’ in the plugins area, which found no results when I tried it, was insufficient.  I should have used Google.  Oh, well – at least I know how a widget works now!