Wednesday, July 04, 2012

Parse Command Line Options in Bash Script

This is an example of how to use getopts to parse command line options in bash
#!/usr/bin/env bash
LIST_FILE=0
RECURSIVE=0
# parse options from command line 
while getopts l:r: OPT; do
    case "$OPT" in
        r)  RECURSIVE="$OPTARG";;
        l)  LIST_FILE="$OPTARG";;
      [?])  echo "Usage: $0 [OPTION] [ARGS]">&2; exit 1;;
    esac
done
# shift parsed options
shift `expr $OPTIND - 1`
# process the arguments
for file in "$*"; do
    echo $file
done

Tuesday, July 03, 2012

How to Decompress jpeg Images Using libjpeg

This is a function I wrote to decompress jpeg images using libjpeg:
#include <jpeglib.h>

int decode_frame(const char *buffer, int length, void *samples)
{
    int n_samples;
    struct jpeg_error_mgr err;
    struct jpeg_decompress_struct cinfo = {0};

    /* create decompressor */
    jpeg_create_decompress(&cinfo);
    cinfo.err = jpeg_std_error(&err);
    cinfo.do_fancy_upsampling = FALSE;

    /* set source buffer */
    jpeg_mem_src(&cinfo, buffer, length);

    /* read jpeg header */
    jpeg_read_header(&cinfo, 1);

    /* decompress */
    jpeg_start_decompress(&cinfo);

    /* read scanlines */
    while (cinfo.output_scanline < cinfo.output_height) {
        n_samples = jpeg_read_scanlines(&cinfo, (JSAMPARRAY) &samples, 1);
        samples += n_samples * cinfo.image_width * cinfo.num_components;
    }   

    /* clean up */
    jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);
    return 0;
}

Monday, July 02, 2012

Python Class to Read/Write a Serial Port

This is a Python Class to read/write from a serial port:
import serial
class Serializer: 
    def __init__(self, port, baudrate=19200, timeout=5): 
        self.port = serial.Serial(port=port, baudrate=baudrate, 
        timeout=timeout, writeTimeout=timeout) 

    def open(self): 
        ''' Open the serial port.'''
        self.port.open()

    def close(self): 
        ''' Close the serial port.'''
        self.port.close() 
    
    def send(self, msg):
        self.prot.write(msg)

    def recv(self):
        return self.port.readline()

Monday, July 02, 2012

How to Copy/Paste from the Clipboard in vim

On Ubuntu 11.04, accessing the clipboard from vim is disabled by default, this is how to enable it:
Download the package source:
apt-get source vim
Edit vim/debian/rules
TINYFLAGS:=--with-features=normal
TINYFLAGS+=--enable-gui
Build and install the package:
dpkg-buildpackage
dpkg -i vim*.deb
Now you can copy and paste using:
"+y to copy to clipboard
"+p to paste from clipboard

Monday, July 02, 2012

How to Connect to a WPA2-PSK Wireless Network on Ubuntu

Network Manager (nm) may not always be there, this how you connect to a WPA2-PSK wireless network from the command line using wpa_supplicant on Ubnutu:

/etc/wireless-wpa.conf
ctrl_interface=/var/run/wpa_supplicant
network={
ssid="ssid"
scan_ssid=1
key_mgmt=WPA-PSK
psk="key"
}
config_wireless.sh
#!/bin/sh
iface=wlan0
ifdown $iface
ifup $iface
wpa_supplicant -B -Dwext -i $iface -c /etc/wireless-wpa.conf -dd
dhclient $iface

Sunday, July 01, 2012

Parsing C++ Headers With Python

cppheaderparser  is a very useful module that you can use to parse C/C++ headers with Python. The module allows you to walk through the classes, functions, parameters etc... I've been using this for a while now to generate bindings for a C shared library. This is an example of how you can use it:

#!/usr/bin/python
import CppHeaderParser

header = CppHeaderParser.CppHeader('MyClass.h')
for c in cpp_header.classes: #iterate over classes
    for f in cls['methods']['public']: #iterate over public functions
        print 'static:', f['static'], ' return type:', f['rtnType'],
        ' parameters:', f['parameters']

Sunday, July 01, 2012

How to Generate Random Nodes in NS2

I've been trying to get NS2 to generate random nodes, finally, this tcl code seems to work nicely:

set rng [new RNG]
$rng seed next-substream

for {set i 0} {$i < $val(nn)} {incr i} {
    set n($i) [$ns node]
    $n($i) set X_ [$rng uniform 0.0 500.0]
    $n($i) set Y_ [$rng uniform 0.0 500.0]
    $n($i) set Z_ 0.0 #flat ground
    $ns initial_node_pos $n($i) 20
}