######################################## oscopegraphlib.py ################################
# Peter Halverson  May 10, 2015

import turtle
import time

oscope_turtle = None
oscope_display_needs_refresh = True

def oscope(user_y,smallest_y=0.0,biggest_y=2.5,sample_period=0.1,samples_per_sweep=20, \
  record_for_xgraph=False,use_seconds_since_midnight=False):
  global oscope_turtle
  global oscope_display_needs_refresh
  global oscope_sample_number_for_xgraph
  global oscope_sample_number_this_sweep
  global data_file
  global midnight_seconds_for_oscope
  if oscope_turtle is None:
      print "Starting turtle graphics"
      oscope_turtle = turtle.Pen()                # "Pen" must start with a capital P!
      oscope_turtle.speed(0)                      # 0 is fastest
      oscope_turtle.hideturtle()        
      if record_for_xgraph:
        file_name=raw_input('''Enter a file name make a xgraph (I will add ".dat" for you):  ''')
        file_name=file_name+".dat"
        title = raw_input("Enter the title of the graph. (Or just press Enter): ")
        data_file=open(file_name,"w")
        if title != "":
          data_file.write("Title =")
          data_file.write(title)
          data_file.write("\n")
        data_file.write("thickness = 2\n")
        data_file.write("title_x = time\n")
        data_file.write("title_y = V\n")
        oscope_sample_number_for_xgraph=0
        ############ Special code to record x-axis in seconds since midnight
        now_struct = time.localtime()
        now_list = list(now_struct)     #convert struct_time to a list
        midnight_list=now_list
        midnight_list[3]=0      #Set the hours to zero
        midnight_list[4]=0      #Set the minutes to zero
        midnight_list[5]=0      #Set the seconds to zero
        midnight_struct=time.struct_time(midnight_list) # convert to a struct_time 
        #print "midnight_struct=",midnight_struct
        midnight_seconds_for_oscope=time.mktime(midnight_struct)
        #print "midnight_seconds_for_oscope=",midnight_seconds_for_oscope
  smallest_x=0.0
  biggest_x=sample_period*samples_per_sweep
  window_max_x = oscope_turtle.window_width()/2.1
  window_max_y = oscope_turtle.window_height()/2.1
  #print "window_max_x",window_max_x
  #print "window_max_y",window_max_y
  if oscope_display_needs_refresh:
    oscope_turtle.clear()
    # Draw a box
    oscope_turtle.penup()
    oscope_turtle.goto(-window_max_x,-window_max_y)
    oscope_turtle.pendown()
    oscope_turtle.goto(-window_max_x,window_max_y)
    oscope_turtle.goto(window_max_x,window_max_y)
    oscope_turtle.goto(window_max_x,-window_max_y)
    oscope_turtle.goto(-window_max_x,-window_max_y)  
    # Draw x and y axes
    # Draw the X axis
    oscope_turtle.penup()
    y=1.0*(-smallest_y+0.0)/(biggest_y-smallest_y)  #Make y between 0 and 1
    y=-window_max_y+(y*2.0*window_max_y)  #Make y have the full window width range
    oscope_turtle.goto(-window_max_x,y)
    oscope_turtle.pendown()
    oscope_turtle.goto(window_max_x,y)      #X axis
    oscope_turtle.penup()    
    oscope_turtle.goto(window_max_x*0.98,0.0)
    oscope_turtle.write(str(biggest_x)+" s",align="right",font=("Arial", 18, "normal"))
    # Draw the Y axis
    oscope_turtle.penup()
    x=1.0*(-smallest_x+0.0)/(biggest_x-smallest_x)  #Make x be tween 0 and 1
    x=-window_max_x+(x*2.0*window_max_x)  #Make x have the full window width range
    oscope_turtle.goto(x,-window_max_y)
    oscope_turtle.pendown()
    oscope_turtle.goto(x,window_max_y)      #Y axis
    oscope_turtle.penup()    
    oscope_turtle.goto(x+window_max_x*0.025,0.92*window_max_y)
    oscope_turtle.write(str(biggest_y),align="left",font=("Arial", 18, "normal"))
    oscope_turtle.goto(x+window_max_x*0.025,-0.96*window_max_y)
    oscope_turtle.write(str(smallest_y),align="left",font=("Arial", 18, "normal"))
    
    oscope_sample_number_this_sweep=0
    oscope_display_needs_refresh = False
  oscope_x=1.0 * sample_period * oscope_sample_number_this_sweep
  #print "oscope_x=",oscope_x,"  user_y=",user_y
  x=1.0*(-smallest_x+oscope_x)/(biggest_x-smallest_x)  #Make x be tween 0 and 1
  y=1.0*(-smallest_y+user_y)/(biggest_y-smallest_y)  #Make y be tween 0 and 1
  x=-window_max_x+(x*2.0*window_max_x)  #Make x have the full window width range
  y=-window_max_y+(y*2.0*window_max_y)  #Make y have the full window height range
  oscope_turtle.goto(x,y)
  #  if i==0:          #You want to draw after the turtle is at the first point location
  oscope_turtle.pendown()
  oscope_sample_number_this_sweep = oscope_sample_number_this_sweep + 1
  oscope_display_needs_refresh = (oscope_sample_number_this_sweep == samples_per_sweep)
  if record_for_xgraph:
    if use_seconds_since_midnight:
      time_to_record = time.time() - midnight_seconds_for_oscope
    else:
      time_to_record = oscope_sample_number_for_xgraph*sample_period
    data_file.write(str(time_to_record)+" "+str(user_y)+"\n")
    oscope_sample_number_for_xgraph=oscope_sample_number_for_xgraph + 1
  return
