You are not logged in.

#1 2009-05-10 10:34:49

Melson
Member
Registered: 2008-08-29
Posts: 39

Binary Clock

There is a neat program from Magnus Pĺlsson which resamples the LED Binary Clock from ThinkGeek. Its output looks like this:

------
----OO
-O-O-O
O-----

I thought an inline display would be more suitable for some, so I came to this C code:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

inline char* b(int n) {
    int i;
    char* s = calloc(7, sizeof(char));

    for (i = 5; i >= 0; i--) {
        s[i] = n & 1 ? '#' : '-';
        n >>= 1;
    }

    return s;
}

int main() {
    time_t now;
    struct tm* info;
    
    time(&now);
    info = localtime(&now);

    printf("%s:%s:%s\n", b(info->tm_hour), b(info->tm_min), b(info->tm_sec));

    return 0;
}

You can use the Makefile from the linked program, and try both apps using this command:

watch -tn 1 /usr/local/bin/binary-clock
--##--:--#---:##--##

Enjoy!

Offline

Board footer

Powered by FluxBB