import wx

import sys

import serial

import threading

 

 

SERIALRX = wx.NewEventType()

EVT_SERIALRX = wx.PyEventBinder(SERIALRX, 0)

 

 

class SerialRxEvent(wx.PyCommandEvent):

    eventType = SERIALRX

    def __init__(self, windowID, data):

        wx.PyCommandEvent.__init__(self, self.eventType, windowID)

        self.data = data

       

class ToggleButtons(wx.Dialog):

    def __init__(self, parent, id, title): #set up GUI

        wx.Dialog.__init__(self, parent, id, title, size=(300, 200))

 

        #check command line arguments

        if (len(sys.argv) !=3):

            print "command line: button.44.py serial_port speed"

            sys.exit()

        port = sys.argv[1]

        speed = int(sys.argv[2])

   

        #open serial port

        self.ser = serial.Serial(port,speed)

        self.ser.timeout = 0.5

        self.thread = None

        self.alive = threading.Event()

       

        self.ser.setDTR()

   

        #flush buffers

        self.ser.flushInput()

        self.ser.flushOutput()

       

        self.colour = wx.Colour(0, 0, 0)

        self.picker=wx.StaticText(self,5,'Color Picker',(20,10))

        wx.ToggleButton(self, 1, 'Red', (20, 40))

        wx.ToggleButton(self, 2, 'Green', (20, 80))

        #wx.ToggleButton(self, 3, 'blue', (20, 100))

        self.label=wx.StaticText(self,3,'',(40,120))

 

        self.panel  = wx.Panel(self, -1, (150, 20), (110, 110), style=wx.SUNKEN_BORDER)

        self.panel.SetBackgroundColour(self.colour)

 

        self.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleRed, id=1)

        self.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleGreen, id=2)

        self.Bind(EVT_SERIALRX, self.OnSerialRead)

        #self.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleBlue, id=3)

 

        self.StartThread()

        self.Centre()

        self.ShowModal()

        self.StopThread()

        self.ser.close()

        self.Destroy()

       

    def OnSerialRead(self, event):

        #toggle with any data

        red = self.colour.Red()

        green = self.colour.Green()

        if  self.colour.Blue():

            self.colour.Set(red, green, 0)

        else:

            self.colour.Set(red, green, 255)

        text = event.data

        #change label

        if text == 'u':

            self.label.SetLabel('')

            #self.colour.Set(255, 0, 0)

        elif text == 'd':

            self.label.SetLabel('Blue')

            #self.colour.Set(0, 0, 255)

        self.panel.SetBackgroundColour(self.colour)

 

    def ComPortThread(self):

        while self.alive.isSet():

            char = self.ser.read(1)

            if char:

                event = SerialRxEvent(self.GetId(), char)

                self.GetEventHandler().AddPendingEvent(event)

 

    def ToggleRed(self, event): #if red is already on turn it off, otherwise turn it on

        green = self.colour.Green()

        blue = self.colour.Blue()

        if  self.colour.Red():

            self.colour.Set(0, green, blue)

        else:

            self.colour.Set(255, green, blue)

        self.panel.SetBackgroundColour(self.colour)

 

    def ToggleGreen(self, event): #if green is already on turn it off, otherwise turn it on

        red = self.colour.Red()

        blue = self.colour.Blue()

        if  self.colour.Green():

            self.colour.Set(red, 0, blue)

        else:

            self.colour.Set(red, 255, blue)

        self.panel.SetBackgroundColour(self.colour)

           

    def StartThread(self):

        self.thread = threading.Thread(target=self.ComPortThread)

        self.thread.setDaemon(1)

        self.alive.set()

        self.thread.start()

 

    def StopThread(self):

        if self.thread is not None:

            self.alive.clear()

            self.thread.join()

            self.thread = None

 

app = wx.App()

ToggleButtons(None, -1, 'button.44.py')

app.MainLoop()