# $Id: pyUltraISR.py,
# 2003/05/20
# 2003/05/26
# Author: Ph Dalet (philippe.dalet@voila.fr)        
#

from PythonCard import dialog, graphic, log, model
import os
import wx
from threading import * 
from struct import *
import time, math, sys

from PythonCard.components import statictext
from PythonCard.components import staticline
from PythonCard.components import button
from PythonCard.components import image

from PythonCard import EXIF
from parallel import *

ID_Timer  = wx.NewId()

BORDER = 5
RESIZE_LEFT = 1
RESIZE_RIGHT = 2
DRAG_X = 3
RESIZE_TOP = 4
RESIZE_BOTTOM = 5
DRAG_Y = 6

VER="1.5"

class WidgetsTest(model.Background):

    def on_initialize(self,event):
        self.PORT=Parallel(LPT1)
        self.data=0
        self.PORT.setData(self.data)
        self.Timer = wx.Timer(self,ID_Timer)
        self.Bind(wx.EVT_TIMER, self.OnTimer)
        self.Timer.Start(100) #1 sec
        
                
    def on_Button1_mouseClick(self, event): #data0
        if ( self.components.Button1.GetLabel()=='1' ):
           self.components.Button1.SetLabel('0')
           self.data=self.data & 0xFE
        else:
           self.components.Button1.SetLabel('1')
           self.data=self.data | ~0xFE
        self.PORT.setData(self.data)

    def on_Button2_mouseClick(self, event): #data2
        if ( self.components.Button2.GetLabel()=='1' ):
           self.components.Button2.SetLabel('0')
           self.data=self.data & 0xFB
        else:
           self.components.Button2.SetLabel('1')
           self.data=self.data | ~0xFB 
        self.PORT.setData(self.data)   
        
    def on_Button3_mouseClick(self, event): #data1
        if ( self.components.Button3.GetLabel()=='1' ):
           self.components.Button3.SetLabel('0')
           self.data=self.data & 0xFD
        else:
           self.components.Button3.SetLabel('1')
           self.data=self.data | ~0xFD  
        self.PORT.setData(self.data)  
               
    def on_Button4_mouseClick(self, event): #data3
        if ( self.components.Button4.GetLabel()=='1' ):
           self.components.Button4.SetLabel('0')
           self.data=self.data & 0xF7
        else:
           self.components.Button4.SetLabel('1')
           self.data=self.data | ~0xF7   
        self.PORT.setData(self.data)     

    def OnTimer(self, event):     
        if (self.PORT.getInAcknowledge() ): #ack
            self.components.Image2._setFile('green.bmp')
        else:
            self.components.Image2._setFile('red.bmp')
            
        #c=self.PORT.getInPaperOut()
        #print c
        
        if (self.PORT.getInPaperOut() ): #pe no paper
            self.components.Image1._setFile('green.bmp')
        else:
            self.components.Image1._setFile('red.bmp')
                                        
    def on_menuFileExit_select(self, event):
        self.Timer.Stop()
        self.Close()
        
    def on_menuAbout_select(self, event):   
        msg="""
        pyUltraISR - V%s - UltraISR Cable TEST

        Dalet Philippe
        Laboratoire STS Electronique
        Lyp Champollion 
        avenue pezet
        46100 FIGEAC
        FRANCE
        
        philippe.dalet@voila.fr       
        """   %(VER)
        dialog.scrolledMessageDialog(self, msg, "About")
    
if __name__ == '__main__':
    app = model.Application(WidgetsTest)
    app.MainLoop()