Welcome, guest | Sign In | My Account | Store | Cart

This is a(n) LED style "VU" meter in the vertical plane instead of the horizontal one in the recipe below:-

http://code.activestate.com/recipes/577612-seven-bit-colored-analogue-bar-graph-generator-dem/?in=lang-python

It is another "AT A GLANCE" display with an overload error beep too.

This is for Linux only and tested on Python 2.6.x although it should work on earlier 2.x.x versions.

A Python 3.x.x version can be found here:-

http://www.linuxformat.com/forums/viewtopic.php?t=13637

This is issued as Public Domain and you may do with it as you please.

Enjoy finding simple solutions to often very difficult problems...

Python, 181 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# 4BitVerticalBargraph2x.py
#
# A DEMO 4 bit slow analogue bargraph generator in colour for STANDARD Python 2.6.x and Linux...
# This is a vertical version of the horizontal one also given away by myself.
# It is written so that anyone can understand how it works.
#
# (Original copyright, (C)2011, B.Walker, G0LCU.)
# Issued initially to LXF as Public Domain, and to other sites later.
#
# Saved as 4BitVerticalBargraph2x.py wherever you like.
#
# This DEMO goes from safe green, to warning amber, to danger red, with a crirical
# error beep above 14 on the vertical scale...
# It is a slow "AT A GLANCE" display for quick assessments, not for accuracy.
#
# 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 "AT A GLANCE" readings from say an 8 bit ADC used as a simple
# voltmeter, ammeter, etc. Getting a digital readout is SO simple I left it out this time...
#
# To run use the following from inside a Python prompt...
# >>> exec(open("/full/path/to/code/4BitVerticalBargraph2x.py").read())
# OR...
# >>> execfile("/full/path/to/code/4BitVerticalBargraph2x.py")
#
# This looks like an "LED" style "VU" display...

# Add the required imports for this DEMO.
import os
import random
import time

# Just for this DEMO set up variables as global...
global count
global row
global blank
global greenlines
global yellowlines
global redlines
global waveform

# Startup variable values here.
count=0
row=0
blank="(C)2011, B.Walker, G0LCU."
greenlines=blank
yellowlines=blank
redlines=blank
# This is a square wave binary for the critical error beep.
waveform=chr(15)+chr(45)+chr(63)+chr(45)+chr(15)+chr(3)+chr(0)+chr(3)

def main():
	# Disable the cursor as it looks much nicer... ;o)
	os.system("setterm -cursor off")

	while 1:
		# Run continuously and use Ctrl-C to STOP!
		count=15
		blank="\033[0m                                 "
		# Generate a byte value as though grabbed from a serial, parallel or USB port.
		row=int(random.random()*256)
		# Now divide by 16 to simulate a 4 bit value.
		row=int(row/16)
		# Although this should never occur, don't allow any error.
		if row>=15: row=15
		if row<=0: row=0

		while count>=0:
			# Do a full, clean, clear screen and start looping.
			os.system("clear"),chr(13),"  ",chr(13),
			print "\033[0mFour Bit Level Vertical Analogue Bar Graph Display..."
			print "Original copyright, (C)2011, B.Walker, G0LCU."
			print "Issued to LXF on 24-04-2011 as Public Domain."
			print
			print blank+"\033[1;31m15 __ "
			
			redlines=blank+"\033[1;31m14 __ "
			if row>=15: redlines=redlines+unichr(0x2588)+unichr(0x2588)
			count=count-1
			print redlines

			redlines=blank+"\033[1;31m13 __ "
			if row>=14: redlines=redlines+unichr(0x2588)+unichr(0x2588)
			count=count-1
			print redlines

			yellowlines=blank+"\033[1;33m12 __ "
			if row>=13: yellowlines=yellowlines+unichr(0x2588)+unichr(0x2588)
			count=count-1
			print yellowlines

			yellowlines=blank+"\033[1;33m11 __ "
			if row>=12: yellowlines=yellowlines+unichr(0x2588)+unichr(0x2588)
			count=count-1
			print yellowlines

			yellowlines=blank+"\033[1;33m10 __ "
			if row>=11: yellowlines=yellowlines+unichr(0x2588)+unichr(0x2588)
			count=count-1
			print yellowlines

			greenlines=blank+"\033[1;32m 9 __ "
			if row>=10: greenlines=greenlines+unichr(0x2588)+unichr(0x2588)
			count=count-1
			print greenlines

			greenlines=blank+"\033[1;32m 8 __ "
			if row>=9: greenlines=greenlines+unichr(0x2588)+unichr(0x2588)
			count=count-1
			print greenlines

			greenlines=blank+"\033[1;32m 7 __ "
			if row>=8: greenlines=greenlines+unichr(0x2588)+unichr(0x2588)
			count=count-1
			print greenlines

			greenlines=blank+"\033[1;32m 6 __ "
			if row>=7: greenlines=greenlines+unichr(0x2588)+unichr(0x2588)
			count=count-1
			print greenlines

			greenlines=blank+"\033[1;32m 5 __ "
			if row>=6: greenlines=greenlines+unichr(0x2588)+unichr(0x2588)
			count=count-1
			print greenlines

			greenlines=blank+"\033[1;32m 4 __ "
			if row>=5: greenlines=greenlines+unichr(0x2588)+unichr(0x2588)
			count=count-1
			print greenlines

			greenlines=blank+"\033[1;32m 3 __ "
			if row>=4: greenlines=greenlines+unichr(0x2588)+unichr(0x2588)
			count=count-1
			print greenlines

			greenlines=blank+"\033[1;32m 2 __ "
			if row>=3: greenlines=greenlines+unichr(0x2588)+unichr(0x2588)
			count=count-1
			print greenlines

			greenlines=blank+"\033[1;32m 1 __ "
			if row>=2: greenlines=greenlines+unichr(0x2588)+unichr(0x2588)
			count=count-1
			print greenlines
			
			greenlines=blank+"\033[1;32m 0 __ "
			if row>=1: greenlines=greenlines+unichr(0x2588)+unichr(0x2588)
			count=count-1
			if row==0: greenlines=greenlines+"__"
			count=count-1
			print greenlines
			
			# Reset to default colours...
			print
			print "\033[0mPress Ctrl-C to stop..."

		if row<=14: time.sleep(1)
		if row==15:
			# Set audio timing to zero, "0".
			count=0
			# Open up the audio device to write to.
			# This could be /dev/dsp also...
			audio=open("/dev/audio", "wb")
			# A "count" value of 1 = 1mS, so 1000 = 1S.
			while count<=1000:
				# Send 8 bytes of data to the audio device 1000 times.
				# This is VERY close to 1KHz and almost sinewave.
				audio.write(waveform)
				count=count+1
			# Close the audio device access.
			audio.close()
			
	# Enable the cursor again if it ever gets here... ;oO
	os.system("setterm -cursor on")
main()

# End of DEMO...
# Enjoy finding simple solutions to often very difficult problems...

It is not possible to get better than 4 bit depth using a normal shell window due to the way the characters are drawn on screen, but......

......16 lines could be made into pseudo 32 lines giving 5 bit depth using the graphics characters available. However when I tried it the end result looked very odd so I decided to abandon it.

Bazza, G0LCU...

1 comment

Barry Walker (author) 13 years ago  # | flag

NOTE!

This code can be considerably simplified...

The "while count>=0:" loop can be removed along with all the references to "count" inside that loop and the code indented as appropriate.

As I used this loop for _something_else_ I left it intact.

It makes little or no difference to the performance and running of the code and I may upload that "_something_else_" in the future...