#!/usr/bin/env python
# Emily Mackevicius
# Nov 25, 2012

import wx
import wx.py
import sys,time,serial
from select import *

# change to have GUI entries...
port = "/dev/tty.usbserial-FTFBGOT5"
speed = 9600
#
# open serial port
#
ser = serial.Serial(port,speed)
ser.setDTR()
#
# flush buffers
#
ser.flushInput()
ser.flushOutput()


def key(event):
   key = event.char
   if (ord(key) == 13):
      key = chr(10)
   ser.write(key)

def quit():
   sys.exit()

"""def idle(parent):
   #
   # idle loop
   #
   wait = ser.inWaiting()
   global byte
   if (wait != 0):
      #
      # read character
      #
      byte = ser.read()
   #time.sleep(0.001)
   parent.after_idle(idle,parent)"""


class MyFrame(wx.Frame):
    def __init__(self,parent,title):
        wx.Frame.__init__(self,parent, title=title, size=(800,600))
        class Editor(wx.py.editwindow.EditWindow):
            def __init__(self,parent, margins=True, **kwargs):
                wx.py.editwindow.EditWindow.__init__(self,parent, **kwargs)
            @property
            def text(self):
                return self.GetText()
        global Ed
        Ed = Editor(self, parent, size = (1,1))
        self.CreateStatusBar()
        # create the OK button
        OKbutton = wx.Button(self, wx.ID_OK, pos = (200,500), label = 'evaluate')
        self.Bind(wx.EVT_BUTTON, self.OnClick, OKbutton)
        # list controls
        ControlText = "Vehicle Controls: \n ser.write('_') \n'0': M1 off\n'1': M1 forward\n'2': M1 backwards\n'3': M2 off\n'4': M2 forward\n'5': M2 backwards"
        VehicleControls = wx.StaticText(self, -1, ControlText, pos = (600,100))
        #make a menu
        filemenu=wx.Menu()
        # wx.ID_ABOUT and wx.ID_EXIT are standard IDs provided by wxWidgets.
        menuAbout = filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
        filemenu.AppendSeparator()
        menuExit = filemenu.Append(wx.ID_EXIT,"&Exit"," Terminate the program")
        self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
        self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.
        self.Show(True)
    def OnAbout(self,event):
            #dialog box
        dlg = wx.MessageDialog(self, "Press OK to execute code \n by Emily Mackevicius", "Vehicle brain", wx.OK)
        dlg.ShowModal()
        dlg.Destroy()
    def OnExit(self,event):
        self.Close(True)
    def OnClick(self,event):
        global Ed
        code_globals = {'ser': ser}
        code_locals = {}
        code_object = compile("import sys,time,serial\nfrom select import *\n" + Ed.text, '<string>', 'exec')
        exec code_object in code_globals, code_locals
        #ser.write(code_globals['command'])

app = wx.App(False)  # Create a new app, don't redirect stdout/stderr to a window.
frame = MyFrame(None, "Emily's Editor")
#app.after(100, idle, app)
app.MainLoop()
