Apr 09 2004 Serial port control using Linux
dev
Quick little shell script hack to control the Ambient Orb. Currently reflects load of machine. Feel free to modify, change, but post your modifications!

code
#!/bin/sh
# Bash shell script to change Ambient Orb device based on system load.
# By Anthony Sheetz. http://bovineamerica.com. No warranties whatsoever, etc..

get_value () # used to get load average. Replace with desired value and adjust scale_value to suit.
{
value=`more /proc/loadavg|cut -d \ -f 1`
}

scale_value () # used to adjust value to a scale between 0 and 25 (corresponding to an orb color from red to blue).
{
value=`echo $value*100 | bc`
value=`echo $value | head -c -4`
value=`expr 500 - $value`
value=`expr $value / 20`
}

get_ascii() #gets ascii value from a decimal number for sending to orb
{
case $1 in
32) temp=\ ;;
33) temp=\! ;;
34) temp=\" ;;
35) temp=\# ;;
36) temp=$ ;;
37) temp=% ;;
38) temp=& ;;
39) temp=\` ;;
40) temp=\( ;;
41) temp=\) ;;
42) temp=\* ;;
43) temp=\+ ;;
44) temp=\' ;;
45) temp=\- ;;
46) temp=. ;;
47) temp=/ ;;
48) temp=0 ;;
49) temp=1 ;;
50) temp=2 ;;
51) temp=3 ;;
52) temp=4 ;;
53) temp=5 ;;
54) temp=6 ;;
55) temp=7 ;;
56) temp=8 ;;
57) temp=9 ;;
esac
}
stty -F /dev/ttyS0 19200 # set com0 to 19200 baud. Adjust to appropriate com port.

oldvalue=99 # initialize oldvalue variable

while :; # main loop
do
get_value
# echo load $value
scale_value
if [ "$value" != "$oldvalue" ]; then
oldvalue=$value

if [ "$value" = "0" ]; then
echo ~A 9
echo ~A 9 > /dev/ttyS0
else

firstbyte=`expr $value / 94 + 32`
get_ascii $firstbyte
firstletter=$temp

secondbyte=`expr $value % 94 + 32`
get_ascii $secondbyte
secondletter=$temp

# echo ~A$firstletter$secondletter
`echo ~A$firstletter$secondletter > /dev/ttyS0`
fi
fi

sleep 2
done

register / login
you must be a member to reply or post. signup or login