Progress bars in Bash

posted 2009-08-10 07:20:31, link to this article

Today a colleague reminded me to dig up a script that I wrote some years ago to generate progress bars (or actually generic horizontal bar graphs) in Bash.

The script is, in this state, more like a proof of concept. It displays an bar that increments until full and then decrements again, this cycle is repeated forever. It should not be too hard to rewrite it to be used in something more useful.
I think it's also trivial to rewrite this to display vertical bar graphs as well. I might write a small Bash library to include in other scripts later, if and when I feel the need to. ;-)

The code of the script follows below, enjoy...

#!/bin/bash

# progress bar
# Hessel Schut, VPRO Automatisering, 2004-04-02

cols=$(tput cols)
bold=$(tput smso)
norm=$(tput rmso)

trap cleanup EXIT TERM INT

cleanup() {
        norm
        tput clear
        tput cnorm
        exit
}

hor_line() {
        for ((i=1; i < cols - 2; i++)); do
                echo -n '-'
        done
}

draw_frame() {
        tput cup 2 2; hor_line
        tput cup 3 1; echo -n '|'
        tput cup 3 ${cols}; echo -n '|'
        tput cup 4 2; hor_line
}

tput clear
tput civis
draw_frame

d=1; pos=2; plen=0
while [ == ]; do
        [ $d -eq 1 ] && face=${bold}
        tput cup 3 $pos
        echo -n ${face}' '
        face=${norm}
        sts='pos: '${pos}' dir: '${d}
        tput cup 1 1; echo -n ${norm}${sts}
        [ ${plen} -gt ${#sts} ] && for ((i=0; i < plen - ${#sts}; i++)); do
                echo -n ' '
        done
        plen=${#sts}
        ((pos += d))
        [ $pos -gt 2 ] && [ $pos -lt $((cols - 2 )) ] || ((d=-d))
done

UDGBUF: a poor man's framebuffer on Psion Organiser II, Part 1

posted 2009-06-13 21:57:15, link to this article

Today I had some more fun with my rediscovered Psion Organiser II. It's about time to get my hands on a model LZ or LZ64 to enjoy twice the amount of screen real estate. ;)

This time I toyed with UDG (user defined characters) again. As the Psion reference manual states you can use these to make small animations as updating these changes them on screen immediately. That gave me the idea to implement some minimalistic framebuffer using all eight UDGs. I print four static UDGs in screen row 1, the other four in screen row 2, this way you get a whopping 40x16 pixel space for graphics!

It would be nice to read/write to the display controller directly and provide some convenience functions to do basic drawing, but I don't feel like learning to write HD6303 machine code just yet, so for now I plan to pass a pointer to some shadow memory (which I plan to allocate by creating a large global array or string) around where you do your graphics stuff, this will be then copied to the UDGs.

As a first test, I wrote a small program that walks through the memory of the Psion and displays it in the UDGs, with this result:

Tomorrow, I'll try to write some functions to provide a few basic graphics operations to set and get a pixel value. And maybe even some Bressenham line/circle drawing after that.

Psion Organiser II bargraphs

posted 2009-06-07 16:48:12, link to this article

Today, when searching for some other stuff, I came across some OPL (Organiser Programming Language, some sort of Pascal-ish bastard child of BASIC) code that I must have written around 2000, or so.

One of the programs was a set of functions to display bargraphs using user-defined characters on the two line text LCD of the Psion Organiser II. So I dug out my trusty old Psion Organiser II XP and cleaned up the code a bit.

Psion LCD bargraph

This might be of use to someone, therefore I publish it here.

udg:(x%, b0%, b1%, b2%, b3%, b4%, b5%, b6%, b7%)
	pokeb $180, 64 + x%*8
	pokeb $181, b0%; pokeb $181, b1%
	pokeb $181, b2%; pokeb $181, b3%
	pokeb $181, b4%; pokeb $181, b5%
	pokeb $181, b6%; pokeb $181, b7%

bargudg:
	udg:(0, 0, 0, 0, 0, 0, 0, 0,31)
	udg:(1, 0, 0, 0, 0, 0, 0,31,31)
	udg:(2, 0, 0, 0, 0, 0,31,31,31)
	udg:(3, 0, 0, 0, 0,31,31,31,31)
	udg:(4, 0, 0, 0,31,31,31,31,31)
	udg:(5, 0, 0,31,31,31,31,31,31)
	udg:(6, 0,31,31,31,31,31,31,31)
	udg:(7,31,31,31,31,31,31,31,31)

barchr$:(n%)
	if n% > 8
		n% = 8
	endif
	if n% = 0
		return " "
	endif
	return chr$(n% - 1) 

When you add these functions to your Organiser, after calling bargudg: you can call barchr$:() just like chr$() to print bargraph characters to the screen or to concatenate them to a string. barchr$:() accepts integers in the range 0..8, 0 being actually a space, not an UDG.

JavaScript Cellular Automata Simulation of Fluid-flow

posted 2009-05-23 11:13:28, link to this article
Read full article

Yesterday, I tried to make a simple Cellular Automaton-like discrete simulation of fluid-flow. Or I am not even sure flow is the right word, it simulates waves coming from the left, with an adjustable frequency, hitting obstacles in their path.

My simulation is by no means attempting to be correct in any way but it is nice to play with and I even spotted something resembling the canceling of waves when passing to a double slit.

Most of the work went into convincing JavaScript to do what I wanted, without being able to use Firebug due to some obscure bug. Maybe I'll write some more about the code behind this in a later article.
I also plan to make it possible to save the configuration of the obstacles in a later version.

Follow the read link to give it a try.

802.11 Wifi Radio spectrograms

posted 2008-11-01 21:45:30, link to this article
Read full article

I've written a small program to plot the 802.11 (b/g) wifi radio spectrum from data collected from a Linksys WAP-54G access point running OpenWRT. Follow the read link to read more...