I would like some help please...

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

I would like some help please...

Postby Bazza » Thu Mar 17, 2011 6:17 pm

Hi all...

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
# SevenBitBargraph3x.py
#
# 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...
# exec(open("/full/path/to/code/SevenBitBargraph3x.py").read())

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

def main():

   # For this DEMO set up variables as global...
   global column
   global count
   global bargraph
   
   column=0
   count=2
   bargraph="(C)2010, B.Walker, G0CLU."
   
   # Disable the cursor as it looks much nicer... ;o)
   os.system("setterm -cursor off")

   while 1:
      # Do a full, clean, clear screen and start looping.
      print(os.system("clear"),chr(13),"  ",chr(13),)

      # Set to terminal default colour(s).
      print("\033[0mSeven Bit Level Horizontal Analogue Bar Graph Display...")
      print()
      print("Original copyright, (C)2010, B.Walker, G0LCU.")
      print()
      print("Issued to LXF on 17-03-2011 as Public Domain.")
      print()
      print()
      # Set the bargraph to light green for this DEMO.
      # This is equivalent to 0 for the column value.
      bargraph="        \033[1;32m|"

      # Generate a byte value as though grabbed from a serial, parallel or USB port.
      column=int(random.random()*256)
      # Now divide by 2 to simulate a 7 bit value.
      column=int(column/2)
      # 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...
      count=0
      while count<=column:
         # It is equivalent to BIOS character 222 for column value of 1 ONLY.
         if count==1: bargraph="        \033[1;32m"+chr(0x2590)
         count=count+1
         if count>=2:
            while count<=column:
               # 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"
               if count%2==0:
                  # For every odd column value print this BIOS character 221.
                  bargraph=bargraph+chr(0x258c)
               if count%2==1:
                  # For every even column value OVERWRITE the above with BIOS character 219.
                  bargraph=bargraph+"\b"+chr(0x2588)
               count=count+1
      # 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;32m        +++++++++++++++++++++++++++++++++++++++++++++\033[1;33m+++++\033[1;31m+++++++++++++++")
      print(bargraph)
      print("\033[1;32m        +++++++++++++++++++++++++++++++++++++++++++++\033[1;33m+++++\033[1;31m+++++++++++++++")
      print()
      print("           \033[1;34m  Analogue resolution is half of one division, that is 1.")
      print()
      # Return back to the default colours and for this DEMO the column value...
      print("\033[0mColumn number",column,"\b...     ")
      print()
      print("Press Ctrl-C to stop...")

      # Do a critical error beep, [sine wave(ish)] for about 1second.
      if column>=120:
         # Set up the binary code as a crude sinewave.
         waveform=b"\x0f\x2d\x3f\x2d\x0f\x03\x00\x03"
         # 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()

      # 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")
main()

# DEMO end.
# Enjoy finding simple solutions to often very difficult problems...
73...

Bazza, G0LCU...

Team AMIGA...
User avatar
Bazza
LXF regular
 
Posts: 1482
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough

Postby Bazza » Sat Mar 26, 2011 9:36 am

Oh well...

That went down like a lead balloon...

However the Python 2.6+ version has reached 2nd position with an
up-vote to 3 and I only uploaded it 7 days ago...

http://code.activestate.com/recipes/langs/python/

So some people are interested...

This site is cool for "giving" away your code snippets and defaults
to the MIT licen(c)(s)e, unless otherwise stated. Mine are nearly always
Public Domain...
73...

Bazza, G0LCU...

Team AMIGA...
User avatar
Bazza
LXF regular
 
Posts: 1482
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough

Postby nelz » Sat Mar 26, 2011 11:52 am

Bazza wrote:That went down like a lead balloon...


Probably because you posted the code in tags, but no link to the script. Cutting and pasting while dealing with wrapping and indentation issues is just too much effort for some people.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8550
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby bobthebob1234 » Sat Mar 26, 2011 8:55 pm

I copy and pasted it, but cos I only have 2.6 python installed and it bombed out quite spectacularly so I didn't bother replying :(
For certain you have to be lost to find the places that can't be found. Elseways, everyone would know where it was
User avatar
bobthebob1234
LXF regular
 
Posts: 1373
Joined: Thu Jan 03, 2008 9:38 pm
Location: A hole in a field

Postby Bazza » Sun Mar 27, 2011 11:56 am

Hi bobthebob1234...

Thanks but......

The code is specific about needing 3.x.x as it does much stuff totally
differently to the 2.x.x series...

If you want to try it out on Python 2.6.x go to the pointer in my previous
upload, you never know, YOU might find it useful or even develop a
multiple colour bargraph generator module... Yes it is easily possible
to make an import(able) module that can be called on the fly!
(My version is a stand alone program - not a module!)

(A standard Linux terminal, (80 x 24/25 text mode only), white on back,
will only support 4 of these at the same time in one window.)

There is/are lots of notes inside the code so be aware of these items...
73...

Bazza, G0LCU...

Team AMIGA...
User avatar
Bazza
LXF regular
 
Posts: 1482
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough

Postby bobthebob1234 » Sun Mar 27, 2011 4:24 pm

Well I didn't know what version I had (didn't even know I had python installed, can't remember ever using it!) so I tried it anyway then found my version

I did try the 2.6 version on activestate and that worked and looked quite nice.

Might have to start learning some python. To the subs area! :D
For certain you have to be lost to find the places that can't be found. Elseways, everyone would know where it was
User avatar
bobthebob1234
LXF regular
 
Posts: 1373
Joined: Thu Jan 03, 2008 9:38 pm
Location: A hole in a field

Postby Bazza » Sun Mar 27, 2011 9:19 pm

Hi bobthebob1234...

Thanks for that...

An "at a glance" analogue colour display can be much more useful
than trying to read and focus on numbers.

You might like Python - why not give it a try?
73...

Bazza, G0LCU...

Team AMIGA...
User avatar
Bazza
LXF regular
 
Posts: 1482
Joined: Sat Mar 21, 2009 11:16 am
Location: Loughborough


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests