Tag Archives: code

LXPanel Plugins: Simplified

I started using LXDE last night, and I’m really liking it. It seems to take a whole ton less memory than Gnome did, and as a result my Intel Atom box runs a lot smoother because it doesn’t have to continuously swap. Anyway, I’ve been customizing some things, and I eventually found myself in plugin development land.

LXPanel is the gnome-panel equivalent for LXDE. There doesn’t seem to be a Trash can plugin for it, and I think that’s just sad. So, I decided to learn how these plugins are coded and make one of my own. I’m not sure if I’ll end up succeeding, but at least it will be a learning experience either way.

I found this page, which outlines a simple plugin that doesn’t do anything. It seems to be a good starting point. I followed the directions and ended up looking at an autoconf project that required a lot of work to get running in Ubuntu. Even though I finally got it to compile, I decided it took way to long to do so. I converted it to a scons project, and now it’s just one directory with a simple build script:

The shared object that’s output is exactly the same size as the one created by autoconf, so that’s good enough for me at the moment. Time to keep hammering away. The code for the example is available here for reuse.


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!

Diving into OOC, a fun new programming language

OOC is cool.  Yesterday I started writing some code in it after reading about it on the github blog.  Here is the first result:

I’m extremely happy with how well this performs.  Using the latest ooc Java compiler from the github trunk to handle the each() functions, this compiles down to a bunch of C code, and then is automagically compiled behind the scenes into an ELF Binary!  This is totally awesome, and I have to commend nddrylliog and the other contributors for their work on this awesome project.  Now I should use it for something useful :D

A quick note about getting it running on Ubuntu:


sudo apt-get install sun-java6-jdk
git clone git://github.com/nddrylliog/ooc.git
cd ooc
JAVA_HOME=/usr/lib/jvm/java-6-sun/ make

At least, that’s how I did it. Then I compile all my ooc with a Makefile like this:


INPUTS=$(wildcard *.ooc)
TARGETS=$(patsubst %.ooc, %, $(INPUTS))

all: $(TARGETS)

%: %.ooc
  java -jar ~/repos/ooc/bin/ooc.jar $@

That will compile all ooc files in the directory.

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 (!!):

#include 
#include 
#include 
#include 
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

Ruby to generate RSS feeds for sites that don’t offer them

There’s this site that has an equipment exchange I wanted to keep track of. Yet, it’s done with what seems to be a custom php file rather than vbulletin, so none of the usual RSS feeds from the site apply to it. So, I decided to make a scraper/feed-generator to get me the latest version every 5 minutes and generate a nice RSS feed, so I can view it in Google Reader. The volume of posting is low enough that this won’t be annoying to see in my daily feeds.

I usually use Ruby for this because it offers Hpricot, a very nice and fast scraper and XPath interface. This time, I resolved to find something that does RSS generation better, and I stumbled upon RubyRSS, which happens to be in the core ruby distribution!
Continue reading

Greasemonkey Script to get Jabber Room for Google Group Chat

So, today I was invited to Google Group chat for the first time. This would be really cool on a terminal that’s not equipped with an IM client, since it’s all done through the web with flash and magic. But, since I don’t like having 2 IM clients running at the same time (Pidgin and the Flash one from Google), I decided I needed to rip out the Jabber Group Chat Room name from the invite page. I got some tips from this site, and wrote a Greasemonkey script to do so.

whoa

All you have to do is copy the room name into the Join Chat window, and put in groupchat.google.com as the server.  I hope you find it useful.  If anyone knows how to make this a link that GNOME will throw at Pidgin, let me know.  That would be awesome.

Get the Script