Theremin

Untitled

May 25th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 KB | None | 0 0
  1. import wx
  2. import serial
  3.  
  4. #map function to convert sensor input range to any other range
  5. def map(x,in_min, in_max,out_min, out_max):
  6.   return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
  7.  
  8. class MyFrame(wx.Frame):
  9.     def __init__(self, parent, title=""):
  10.        
  11.         super(MyFrame, self).__init__(parent, title=title, size=(800,800))
  12.         # Attach the paint event to the frame
  13.         self.Bind(wx.EVT_PAINT, self.OnPaint)
  14.         # Set the background color
  15.         self.SetBackgroundColour( wx.Colour( 255, 10, 20) )
  16.  
  17.         # Setup StatusBar
  18.         self.CreateStatusBar()
  19.         self.PushStatusText("inputInterface application")
  20.  
  21.         # Numbers showing the values
  22.         self.magValueTxt = wx.StaticText(self, wx.ID_ANY, label="mag",pos=(10,10), style=wx.ALIGN_CENTER)
  23.         self.lightValueTxt = wx.StaticText(self, wx.ID_ANY, label="light", pos=(10,25), style=wx.ALIGN_RIGHT)
  24.      
  25.         # Centre and show the frame
  26.         self.Centre()
  27.         self.Show()
  28.        
  29.        
  30.     def OnPaint(self, event=None):
  31.         mag=0
  32.         light=0
  33.         # Create the paint surface
  34.         dc = wx.PaintDC(self)
  35.         # Refresh the display
  36.         self.Refresh()        
  37.         # Get data from serial port until both sensors data acquired
  38.         while (mag==0 or light==0):
  39.             serialValue=inputDev.readline()
  40.             if serialValue.split(',')[0]=='m':
  41.                 mag = int(serialValue.split(',')[1])
  42.             if serialValue.split(',')[0]=='l':
  43.                 light = int(serialValue.split(',')[1])
  44.        
  45.         mag = map(mag, 0, 1024, 0, 800)
  46.         light = map(light, 0, 1024, 0, 800)
  47.        
  48.         print light
  49.         print "----"
  50.         print mag
  51.         print "------"
  52.         self.lightValueTxt.SetLabel(str(light))
  53.         self.magValueTxt.SetLabel(str(mag))
  54.         thickness = 4
  55.         border_color = "#FFFFFF"
  56.         fill_color = "#FF944D"
  57.         dc.SetPen(wx.Pen(border_color, thickness))
  58.         dc.SetBrush(wx.Brush(fill_color))
  59.        
  60.         #Draw things!
  61.        
  62.         for i in range (1,100):
  63.         #dc.DrawArc( mag+i+light/i, mag/i-light+i, light/i-mag/i+i, -light+i/i-mag, light/i, mag+i)
  64.         #dc.DrawSpline([(mag,light),(mag-light, mag-light),(light,light)])
  65.             dc.DrawPoint(mag+2*i,light+3*i);
  66.            
  67.  
  68. class MyApp(wx.App):
  69.     def OnInit(self):
  70.         #machineSetup()
  71.  
  72.         self.frame = MyFrame(None, title="inputInterface application")
  73.         self.frame.Show();
  74.        
  75.         return True
  76.  
  77.  
  78. if __name__ == "__main__":
  79.     try:
  80.         #inputDev = serial.Serial('COM3', 9600)
  81.         inputDev = serial.Serial('/dev/ttyUSB0', 9600)
  82.     except:
  83.         print "Failed to connect"
  84.         exit()
  85.     app = MyApp(False)
  86.     app.MainLoop()
  87.     inputDev.close()
Advertisement
Add Comment
Please, Sign In to add comment