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

This is a two channel version of the "Four Bit Vertical Coloured Analogue Bar Graph Generator DEMO...".

These are DUAL LED style "VU" meters in the vertical plane...

This is for standard Python 3.x.x.

It has different overload beeps for each channel, and has all the unwanted code has been removed.

It demonstrates how to obtain multichannel ability, (it is possible to add more channels).

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

Bazza...

Python, 221 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# Dual4BitVerticalBargraph3x.py
#
# A DEMO DUAL 4 bit slow analogue bargraph generator in colour for STANDARD Python 3.x.x
# and Linux... This is a DUAL vertical version of the SINGLE one also given away by myself.
# It is written so that anyone can understand how it works.
#
# (Original copyright, (C)2011, B.Walker, G0LCU.)
#
# Saved as Dual4BitVerticalBargraph3x.py wherever you like.
#
# This DEMO goes from safe green, to warning amber, to danger red, with a crirical
# error beep above 14 on both the vertical displays...
# 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 two 8 bit ADCs used as a simple
# voltmeters, ammeters, etc...
#
# To run use the following from inside a Python prompt...
# >>> exec(open("/full/path/to/code/Dual4BitVerticalBargraph3x.py").read())
#
# This looks like two "LED" style "VU" displays side by side...

# 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 byteone
global bytetwo
global blank
global greenlines
global yellowlines
global redlines
global waveform
global unichar
global spacer

# Startup variable values here.
count=0
byteone=0
bytetwo=0
blank="(C)2011, B.Walker, G0LCU."
greenlines=blank
yellowlines=blank
redlines=blank
unichar=chr(0x2588)+chr(0x2588)
spacer=" ____ "
# This is a squarewave binary for the critical error beep(s).
waveform=b"\x00\x00\x00\x00\xff\xff\xff\xff"

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!
		blank="\033[0m                              "
		# Generate two byte values as though grabbed from a serial, parallel or USB port.
		# E.G... The Arduino Diecimila Dev Board as a multiple analogue source.
		byteone=int(random.random()*256)
		bytetwo=int(random.random()*256)
		# Now divide by 16 to simulate a 4 bit values.
		byteone=int(byteone/16)
		bytetwo=int(bytetwo/16)
		# Although this should never occur, don't allow any errors.
		if byteone>=15: byteone=15
		if byteone<=0: byteone=0
		if bytetwo>=15: bytetwo=15
		if bytetwo<=0: bytetwo=0

		# Do a full, clean, clear screen and start looping.
		os.system("clear"),chr(13),"  ",chr(13),
		print("\033[0mDual Four Bit Level Vertical Analogue Bar Graph Display...")
		print()
		print("Original copyright, (C)2011, B.Walker, G0LCU.")
		print()
		print(blank+"\033[1;31m15 __ __ ____ __ __ 15")

		redlines=blank+"\033[1;31m14 __ "
		if byteone>=15: redlines=redlines+unichar+spacer
		else: redlines=redlines+"  "+spacer
		if bytetwo>=15: redlines=redlines+unichar+" __ 14"
		else: redlines=redlines+"   __ 14"
		print(redlines)

		redlines=blank+"\033[1;31m13 __ "
		if byteone>=14: redlines=redlines+unichar+spacer
		else: redlines=redlines+"  "+spacer
		if bytetwo>=14: redlines=redlines+unichar+" __ 13"
		else: redlines=redlines+"   __ 13"
		print(redlines)

		yellowlines=blank+"\033[1;33m12 __ "
		if byteone>=13: yellowlines=yellowlines+unichar+spacer
		else: yellowlines=yellowlines+"  "+spacer
		if bytetwo>=13: yellowlines=yellowlines+unichar+" __ 12"
		else: yellowlines=yellowlines+"   __ 12"
		print(yellowlines)

		yellowlines=blank+"\033[1;33m11 __ "
		if byteone>=12: yellowlines=yellowlines+unichar+spacer
		else: yellowlines=yellowlines+"  "+spacer
		if bytetwo>=12: yellowlines=yellowlines+unichar+" __ 11"
		else: yellowlines=yellowlines+"   __ 11"
		print(yellowlines)

		yellowlines=blank+"\033[1;33m10 __ "
		if byteone>=11: yellowlines=yellowlines+unichar+spacer
		else: yellowlines=yellowlines+"  "+spacer
		if bytetwo>=11: yellowlines=yellowlines+unichar+" __ 10"
		else: yellowlines=yellowlines+"   __ 10"
		print(yellowlines)

		greenlines=blank+"\033[1;32m 9 __ "
		if byteone>=10: greenlines=greenlines+unichar+spacer
		else: greenlines=greenlines+"  "+spacer
		if bytetwo>=10: greenlines=greenlines+unichar+" __ 9"
		else: greenlines=greenlines+"   __ 9"
		print(greenlines)

		greenlines=blank+"\033[1;32m 8 __ "
		if byteone>=9: greenlines=greenlines+unichar+spacer
		else: greenlines=greenlines+"  "+spacer
		if bytetwo>=9: greenlines=greenlines+unichar+" __ 8"
		else: greenlines=greenlines+"   __ 8"
		print(greenlines)

		greenlines=blank+"\033[1;32m 7 __ "
		if byteone>=8: greenlines=greenlines+unichar+spacer
		else: greenlines=greenlines+"  "+spacer
		if bytetwo>=8: greenlines=greenlines+unichar+" __ 7"
		else: greenlines=greenlines+"   __ 7"
		print(greenlines)

		greenlines=blank+"\033[1;32m 6 __ "
		if byteone>=7: greenlines=greenlines+unichar+spacer
		else: greenlines=greenlines+"  "+spacer
		if bytetwo>=7: greenlines=greenlines+unichar+" __ 6"
		else: greenlines=greenlines+"   __ 6"
		print(greenlines)

		greenlines=blank+"\033[1;32m 5 __ "
		if byteone>=6: greenlines=greenlines+unichar+spacer
		else: greenlines=greenlines+"  "+spacer
		if bytetwo>=6: greenlines=greenlines+unichar+" __ 5"
		else: greenlines=greenlines+"   __ 5"
		print(greenlines)

		greenlines=blank+"\033[1;32m 4 __ "
		if byteone>=5: greenlines=greenlines+unichar+spacer
		else: greenlines=greenlines+"  "+spacer
		if bytetwo>=5: greenlines=greenlines+unichar+" __ 4"
		else: greenlines=greenlines+"   __ 4"
		print(greenlines)

		greenlines=blank+"\033[1;32m 3 __ "
		if byteone>=4: greenlines=greenlines+unichar+spacer
		else: greenlines=greenlines+"  "+spacer
		if bytetwo>=4: greenlines=greenlines+unichar+" __ 3"
		else: greenlines=greenlines+"   __ 3"
		print(greenlines)

		greenlines=blank+"\033[1;32m 2 __ "
		if byteone>=3: greenlines=greenlines+unichar+spacer
		else: greenlines=greenlines+"  "+spacer
		if bytetwo>=3: greenlines=greenlines+unichar+" __ 2"
		else: greenlines=greenlines+"   __ 2"
		print(greenlines)

		greenlines=blank+"\033[1;32m 1 __ "
		if byteone>=2: greenlines=greenlines+unichar+spacer
		else: greenlines=greenlines+"  "+spacer
		if bytetwo>=2: greenlines=greenlines+unichar+" __ 1"
		else: greenlines=greenlines+"   __ 1"
		print(greenlines)

		greenlines=blank+"\033[1;32m 0 __ "
		if byteone>=1: greenlines=greenlines+unichar+spacer
		else: greenlines=greenlines+"__"+spacer
		if bytetwo>=1: greenlines=greenlines+unichar+" __ 0"
		else: greenlines=greenlines+"__ __ 0"
		print(greenlines)

		# Print the two byte values onto the screen...
		print("\033[1;34mByteone =",byteone,"\b, bytetwo =",bytetwo,"\b...   ")
		# Now reset to the default colours, etc...
		print("\033[0mPress Ctrl-C to stop...")

		time.sleep(1)

		# Use two different beeps for the two displays.
		# Both are different frequency squarewaves.
		if byteone==15 or bytetwo==15:
			# Select an error beep for each display...
			if byteone==15: waveform=b"\x00\x00\x00\x00\xff\xff\xff\xff"
			if bytetwo==15: waveform=b"\x00\x00\xff\xff\x00\x00\xff\xff"
			# 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.
				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...

This displays two vertical LED style "VU" meters, more are possible...

To speed it up change the time.sleep(1) to something faster; the limit is about time.sleep(0.1)...

2 comments

Oleg 12 years, 11 months ago  # | flag

i just replaced list with "for loop" from line 85 to line 188 with that:

    for n in range(15,1,-1):
        line = ("\033[1;32m %d __ " if n < 10 else
                "\033[1;33m%d __ " if n < 13 else
                "\033[1;31m%d __ ")

        line = blank+line % n
        if byteone>=n: line+= unichar+spacer
        else: line += "  "+spacer
        if bytetwo>=n: line = line+unichar+" __ %d" %n
        else: line += "   __ %d" %n
        print(line)
Barry Walker (author) 12 years, 11 months ago  # | flag

Hi Oleg...

Thanks for the mega-shortening of the code but I will leave mine as is.

The reason being that as one of the comments inside the code says:-

"5 # It is written so that anyone can understand how it works."

It shows that by printing characters line by line in a specific manner, simple anims can be made.

And in the case of Linux/*nix in colour too...

I will be trying your code out very soon however... :)