# Distributed under the terms of the GNU General Public License v2

function to_lcd() {
	#PATH="$PATH:/usr/local/bin"
	#local lcd=$( type -p usblcd )
	local lcd=/usr/local/bin/usblcd
	[[ -x "${lcd}" ]] || return 0

	local tmp="/dev/shm"
	local last
	if [[ -d $tmp ]] && [[ -w $tmp ]]; then
		[[ -e "$tmp/.lcdsplash_last" ]] && last="$( tail -n 1 "$tmp/.lcdsplash_last" )"
	else
		unset tmp
	fi

	local text="$@"
	if [[ "" = "${text/ */}" ]]; then
		return
	fi

	if [[ "__CLEAR__" = "${text}" ]]; then
		[[ -n "$tmp" ]] && [[ -e "$tmp/.lcdsplash_last" ]] && rm "$tmp/.lcdsplash_last" >/dev/null 2>&1
		${lcd} backlight 1 clear >/dev/null 2>&1
		return
	fi

	# We don't want to do:
	#${lcd} clear
	# ... as this causes things to flash, so let's pad the output
	local length
	(( length = ${#text} ))
	if (( 20 == length )); then
		[[ -n "$tmp" ]] && echo "$text" > "$tmp/.lcdsplash_last" 2>/dev/null
		if [[ -n "$last" ]]; then
			${lcd} backlight 1 text 1 0 "$text" >/dev/null 2>&1
			if [[ "${text:3}" != "${last:3}" ]]; then
				${lcd} backlight 1 text 0 0 "$last" >/dev/null 2>&1
			fi
		else
			${lcd} backlight 1 text 0 0 "$text" >/dev/null 2>&1
		fi
	elif (( 20 > length )); then
		local pos count

		(( count = 20 - length ))
		for pos in $( seq $count -1 1 ); do
			text="$text "
		done
		[[ -n "$tmp" ]] && echo "$text" > "$tmp/.lcdsplash_last" 2>/dev/null
		if [[ -n "$last" ]]; then
			${lcd} backlight 1 text 1 0 "$text" >/dev/null 2>&1
			if [[ "${text:3}" != "${last:3}" ]]; then
				${lcd} backlight 1 text 0 0 "$last" >/dev/null 2>&1
			fi
		else
			${lcd} backlight 1 text 0 0 "$text" >/dev/null 2>&1
		fi
	elif (( 20 < length )); then
		local pos count
		local line0 line1

		line0="${text:0:20}"
		line1="${text:20:20}"
		(( count = 20 - ${#line1} ))
		for pos in $( seq $count -1 1 ); do
			line1="$line1 "
		done
		[[ -n "$tmp" ]] && echo "$line1" > "$tmp/.lcdsplash_last" 2>/dev/null
		${lcd} backlight 1 text 1 0 "$line1" >/dev/null 2>&1
		${lcd} backlight 1 text 0 0 "$line0" >/dev/null 2>&1
	fi
}

# The picoLCD is a 20x2 display with 5 number keys, "+" and "-" keys, and a
# 4-way cursor pad with an "Enter" key in the middle.
#
# If we save state, then we scroll lines from the bottom to the top line of the
# display, intelligently replace lines (e.g. "Started $1 OK" would replace
# "Starting $1" instead of causing it to scroll), rotate lights around the
# cursor keys to show an operation is in progress, and count on the numerical
# keys to show overall progress.
#
# Longer messages up to 40 characters (exit, critical) can be split into two
# parts and fill the screen.

function lcdsplash_clear()    { to_lcd "__CLEAR__";}

function lcdsplash_start()    { to_lcd "-> $1";}
function lcdsplash_stop()     { to_lcd "<- $1";}
function lcdsplash_started()  { to_lcd ">> $1";}
function lcdsplash_stopped()  { to_lcd "<< $1";}
function lcdsplash_init()     { to_lcd "I: $1";}
function lcdsplash_exit()     { to_lcd "   $*";}
function lcdsplash_critical() { to_lcd "!! $*";}

# vi: set syntax=sh:
