Can those of you who have Python 3.x installed do me a favour...
The code below generates an analogue display in STANDARD
Python for Linux, in glorious technicolour.
I will be using four of them simultaneously for a project I am doing...
I would like any errors you get please, if any occur, and, your
opinions are also welcome, good or bad...
It is for Linux, (*nix?). only and two commands must be available for
it to work, "clear" and "setterm"...
Read the code for more info...
Watch for any wordwrapping etc...
Thanks, to all who help, in advance...
- Code: Select all
# A DEMO 7 bit analogue bargraph generator in colour for STANDARD Python 3.x and Linux...
# (Original copyright, (C)2010, B.Walker, G0LCU.)
# Issued initially to LXF as Public Domain, and to other sites later.
# Saved as SevenBitBargraph3x.py wherever you like.
# This DEMO goes from safe green, to warning amber, to danger red, with a crirical
# error beep above 120 on the horizontal scale...
# Two system commands are required, "clear" and "setterm", for this to work.
# I assume that these are available on all recent and current Linux distros.
# The device /dev/audio is used so this must be free also.
# It is useful for quick glance readings from say an 8 bit ADC used as a simple
# voltmeter. Getting a digital readout is SO simple I left it out this time...
# To run use the following from inside a Python prompt...
# Add the required imports for this DEMO.
# For this DEMO set up variables as global...
bargraph="(C)2010, B.Walker, G0CLU."
# Disable the cursor as it looks much nicer... ;o)
os.system("setterm -cursor off")
# Do a full, clean, clear screen and start looping.
# Set to terminal default colour(s).
print("\033[0mSeven Bit Level Horizontal Analogue Bar Graph Display...")
print("Original copyright, (C)2010, B.Walker, G0LCU.")
print("Issued to LXF on 17-03-2011 as Public Domain.")
# Set the bargraph to light green for this DEMO.
# This is equivalent to 0 for the column value.
# Generate a byte value as though grabbed from a serial, parallel or USB port.
# Now divide by 2 to simulate a 7 bit value.
# Although this should never occur, don't allow any error.
if column>=127: column=127
if column<=0: column=0
# Now to generate the bargraph...
# It is equivalent to BIOS character 222 for column value of 1 ONLY.
if count==1: bargraph=" \033[1;32m"+chr(0x2590)
# Change bargraph colour on the fly when entering the YELLOW zone... :)
if count>=90: bargraph=bargraph+"\033[1;33m"
# Change bargraph colour on the fly when entering the RED zone... :)
if count>=100: bargraph=bargraph+"\033[1;31m"
# For every odd column value print this BIOS character 221.
# For every even column value OVERWRITE the above with BIOS character 219.
# Print the "scale" in the default colour(s)...
print("\033[0m 0 10 20 30 40 50 60 70 80 90 100 110 120")
# Now print the meter and bargraph in colours of your choice... :)
print("\033[1;32m | | | | | | | | | \033[1;33m| \033[1;31m| | |")
print(" \033[1;34m Analogue resolution is half of one division, that is 1.")
# Return back to the default colours and for this DEMO the column value...
print("\033[0mColumn number",column,"\b... ")
print("Press Ctrl-C to stop...")
# Do a critical error beep, [sine wave(ish)] for about 1second.
# Set up the binary code as a crude sinewave.
# Set audio timing to zero, "0".
# Open up the audio device to write to.
# This could be /dev/dsp also...
# A "count" value of 1 = 1mS, so 1000 = 1S.
# Send 8 bytes of data to the audio device 1000 times.
# This is VERY close to 1KHz and almost sinewave.
# Close the audio device access.
# Add a DEMO delay to simulate a simple digital voltmeter speed...
if column<=119: time.sleep(1)
# Enable the cursor again if it ever gets here... ;oO
os.system("setterm -cursor on")
# DEMO end.
# Enjoy finding simple solutions to often very difficult problems...