Technology Programming

How to Make an Oscilloscope in Python

    • 1). Open a code editing application or use a text editing application such as TextEdit in Mac OS X or Notepad in Microsoft Windows. Create a new document to house the script of the Python oscilloscope.

    • 2). Define the parameters that will be used in the script, as well as the two Python libraries needed for the script by using the following code:

      import os, sys
      import Image
      import serial
      import pygame
      import time

      from pyBusPirateLite.UART import *
      from pyBusPirateLite.BitBang import *

    • 3). Define the data rate and use the print function to enable the program to graph the inputs made by the oscilloscope:

      DATA_RATE = 5720.0 #measures/second (estimated experimenticaly)
      DEFAULT_TIME_SCALE = RES_X / DATA_RATE #default time in seconds to make one window fill
      pygame.init()
      bp = UART(BUS_PIRATE_DEV,115200)
      print "Entering binmode: ",
      if bp.BBmode():
      print "OK."
      else:
      print "failed."
      sys.exit()

    • 4). Define the parameters for the application window by using the pygame.display function:

      window = pygame.display.set_mode((RES_X, RES_Y))
      background = (0,0,0)
      line = (0,255,0)
      trig_color = (100,100,0)

    • 5). Create the script for the oscilloscope itself by defining the parameters that will be written on the graph and providing arguments for the x and y-axis:

      bp.port.write("\x15")
      while 1:
      plot = {}
      voltage = {}
      maxv = 0
      minv = 100
      time_scale = DEFAULT_TIME_SCALE * time_div
      prev_voltage = 0
      measure = 0;
      if(trig_mode != NO_SYNC):
      for k in range(1,2000):
      prev_voltage = voltage
      measure = bp.response(2, True)
      voltage = ord(measure[0]) << 8
      voltage = voltage + ord(measure[1])
      voltage = (voltage/1024.0) * 6.6
      #rising slope
      if((voltage >= trigger_level) and (prev_voltage < (voltage * TRIG_CAL)) and (trig_mode == RISING_SLOPE)):
      break
      if((voltage < trigger_level) and (voltage > 0.01) and (prev_voltage > voltage/TRIG_CAL) and (trig_mode == FALLING_SLOPE)):
      break
      for i in range(RES_X):
      for k in range(time_div - 1):
      #ignoring (time_div-1) samples to achieve proper time resolution
      bp.response(2, True)
      measure = bp.response(2, True)
      voltage = ord(measure[0]) << 8
      voltage = voltage + ord(measure[1])
      voltage = (voltage/1024.0) * 6.6
      plot[i] = voltage
      for i in range(1,RES_X):
      if plot[i] > maxv:
      maxv = plot[i]
      if plot[i] < minv:
      minv = plot[i]
      y = (RES_Y) - plot[i]*(RES_Y/MAX_VOLTAGE) - OFFSET
      x = i
      px = i-1;
      py = (RES_Y) - plot[i-1]*(RES_Y/MAX_VOLTAGE) - OFFSET
      pygame.draw.line(window, line, (px, py), (x, y))
      trig_y = RES_Y - trigger_level * (RES_Y/MAX_VOLTAGE)
      pygame.draw.line(window, trig_color, (0, trig_y), (RES_X, trig_y))

    • 6). Finalize the oscilloscope by defining the parameters for the oscilloscope's graphical user interface:

      ##GUI)
      font = pygame.font.Font(None, 19)
      text_max_voltage = font.render("Max: %f V" % maxv, 1, (255, 255, 255))
      text_min_voltage = font.render("Min: %f V" % minv, 1, (255, 255, 255))
      text_time_scale = font.render("Timescale: %f s" % time_scale, 1, (255, 255, 255))
      text_maxv_Rect = text_max_voltage.get_rect()
      text_minv_Rect = text_min_voltage.get_rect()
      text_time_scale_Rect = text_time_scale.get_rect()
      text_maxv_Rect.x = 10
      text_maxv_Rect.y = 10
      text_minv_Rect.x = 10
      text_minv_Rect.y = 30
      text_time_scale_Rect.x = 10
      text_time_scale_Rect.y = 50
      window.blit(text_max_voltage, text_maxv_Rect)
      window.blit(text_min_voltage, text_minv_Rect)
      window.blit(text_time_scale, text_time_scale_Rect)

SHARE
RELATED POSTS on "Technology"
WordPress - How to Set up a New Theme to WordPress 3.
WordPress - How to Set up a New Theme to WordPress 3.
Solution of Creative Web Design
Solution of Creative Web Design
The three disciplines of User Experience
The three disciplines of User Experience
Web Design Sheffield Options For Professional Enterprises
Web Design Sheffield Options For Professional Enterprises
Do you have what it takes?
Do you have what it takes?
Segway Cost
Segway Cost
Microsoft Access 2010: What's Coming with Office 2010?
Microsoft Access 2010: What's Coming with Office 2010?
Companies of Web Development in Ireland Provide Designs that Work
Companies of Web Development in Ireland Provide Designs that Work
Penguin Update to Put Red-Flags on Negative SEO
Penguin Update to Put Red-Flags on Negative SEO
Innovative web 2 design templates can make your business famous quickly
Innovative web 2 design templates can make your business famous quickly
Building A Search Engine Friendly Website
Building A Search Engine Friendly Website
Exceptional Advice To Build Up Your Internet Marketing
Exceptional Advice To Build Up Your Internet Marketing
The Benefits of Selecting The Right Hosting Company
The Benefits of Selecting The Right Hosting Company
Is There a Methodology for Making Successful Logos
Is There a Methodology for Making Successful Logos
Benefits of Ruby On Rails Development
Benefits of Ruby On Rails Development
The Power of Colour and Shapes in Your Infant's Life.
The Power of Colour and Shapes in Your Infant's Life.
Advantages of Hiring PSD To HTML Service Providers
Advantages of Hiring PSD To HTML Service Providers
How to Make Responsive Web Design Attractive?
How to Make Responsive Web Design Attractive?
Converting PSD to Responsive HTML
Converting PSD to Responsive HTML
Just a few realy really hints and tips when it comes to website design but look for.
Just a few realy really hints and tips when it comes to website design but look for.

Leave Your Reply

*