# AMIGA_Magic_Eye.py
#
# A simple animation DEMO to simulate the action of a thermionic tuning indicator valve/tube.
# The valve/tube type is EM87, 6HU6, CV10407, 6E2 and other equivalent numbers too.
# http://www.akh.se/tubes/htm/em87.htm
#
# Original copyright, (C)2007-2013, B.Walker, G0LCU.
# It is issued now as Public Domain and you may do with it as you please.
#
# Save the file as AMIGA_Magic_Eye.py inside the PYTHON: volume.
# To test from a normal Python Command Prompt call as......
#
# >>> exec(open("PYTHON:AMIGA_Magic_Eye.py").read())<CR>
#
# ......and away you go and enjoy... ;o)
#
# Designed and tested on, and for, a classic AMIGA 1200(HD), WinUAE and E-UAE running AMIGA OS 3.0.x
# and above running Python 1.4.0 to 2.0.1. The E-UAE variant might be a little slow...
#
# Written in such a way that anyone can understand how it works!
# This code CAN be simplified quite a lot but it has been left as is...
#
# It is useful for quick glance readings from say an 8 bit ADC used as a simple level
# indicator, for example a tuning and/or level indicator. It is at 5 bit depth.
#
# $VER: AMIGA_Magic_Eye.py_Version_0.00.10_(C)2007-2013_B.Walker_G0LCU.
#
# Enjoy finding simple solutions to often very difficult problems... ;o)

# Do any imports for this DEMO as required.
import random
import os

# Just for this DEMO set these variables as global. My choice... ;o)
global AMIGA_Magic_Eye
global count
global grab
global lightcount

# AMIGA_Magic_Eye == the anim section to be printed.
# count == A re-usable value for this DEMO.
# grab == The psuedo-value to display. A typical value say read from the parallel port.
# lightcount == Specific for the non _illumnated_ area only.
AMIGA_Magic_Eye="(C)2007-2013, B.Walker, G0LCU."
count=0
grab=0
lightcount=0

# Run continuously until the Ctrl-C keys are pressed.
def main():
	# Set up a user screen.
	print("\f\nA simple pseudo-EM87/6HU6/6E2 tuning indicator style DEMO for AMIGA Python.")
	print("$VER: AMIGA_Magic_Eye.py_Version_0.00.10_(C)2007-2013_B.Walker_G0LCU.\n")
	print("Designed to work on Classic AMIGA OS 3.x.x using Python 1.4.0 minimum.\n")
	print("It uses the four, (default?), Workbench 3.0.x colours in this DEMO only.\n\n\n")
	print("                               EM87/6HU6 simulator.")

	# Generate the AMIGA_Magic_Eye display as though the value 0 has been read.
	print("\x1B[0m       +--------------------------------------------------------------+")
	print("       |\x1B[0;30;42m                                                              \x1B[0m|")
	print("\x1B[0m       +--------------------------------------------------------------+\n")

	# Finish the screen display.
	print("\n\nPress Ctrl-C to STOP...")

	while 1:
		# Randomly generate an 8 bit value as though grabbed from a serial, parallel or USB port.
		grab=int(random.random()*256)
		# Set to a 5 bit value for the DEMO.
		grab=int(grab/8)
		# Although no error should ever occur, never allow one.
		if grab<=0: grab=0
		if grab>=31: grab=31

		# Now generate the AMIGA_Magic_Eye start only if the grabbed value is GREATER than 0.
		if grab>=1:

			# Do the left hand side bright part first.
			AMIGA_Magic_Eye=""
			count=1
			while count<=grab:
				AMIGA_Magic_Eye=AMIGA_Magic_Eye+"\x1B[0;30;43m "
				count=count+1

			# Now generate the non-illuminated centre section.
			count=grab
			lightcount=61-(grab*2)
			while lightcount>=0:
				AMIGA_Magic_Eye=AMIGA_Magic_Eye+"\x1B[0;30;42m "
				lightcount=lightcount-1

			# Finally finish off with the other bright part.
			lightcount=61-(grab*2)
			count=lightcount+grab+1
			while count<=61:
				AMIGA_Magic_Eye=AMIGA_Magic_Eye+"\x1B[0;30;43m "
				count=count+1

		# When the grab value equals 0 override the above and generate a full band only.
		if grab==0: AMIGA_Magic_Eye=("\x1B[0;30;42m                                                              \x1B[0m|")

		# Print the animation to the screen.
		print("\x1B[13;9f"+AMIGA_Magic_Eye+"\x1B[0m\n\n\n\n")

		# Sound an error beep at the highest grab value only...
		if grab==31: print("\a")

		# Add a short delay for this DEMO...
		# NOTE:- time.sleep() does NOT work on the Classic AMIGA variants...
		os.system("C:Wait 1")

main()

# End of AMIGA_Magic_Eye.py DEMO.
# Enjoy finding simple solutions to often very difficult problems. ;o)