# $Id: pyPotentiometer.py,
# 2003/08/15
# 2003/10/23
# Author: Ph Dalet (philippe.dalet@voila.fr)        
#

"""
__version__ = "$Revision: 1.0 $"
__date__ = "$Date: 2004/08/18 16:53:26 $"
"""

from PythonCard import dialog, graphic, log, model
import os
import wx
from PythonCard.components import button
from PythonCard.components import staticline
from PythonCard.components import statictext
from PythonCard.components import image
from PythonCard.components import combobox
from PythonCard.components import textfield
from PythonCard.components import bitmapcanvas
from PythonCard.components import textarea
from PythonCard.components import gauge
from PythonCard.components import imagebutton
from PythonCard.components import slider
from parallel import *

VER="1.5"

class MyBackground(model.Background):

    def on_initialize(self,event):
        self.PORT=Parallel(LPT1)
        self.io=0
        self.PORT.setData(self.io)
        
        self.R1=1000
        self.components.R1.SetValue("1000") 

        self.Data=128
        self.components.Data.SetValue( str(self.Data) )
        
        self.Amplification=1+(100000*self.Data)/(self.R1*255)
        self.components.Amplification.SetValue( str(self.Amplification) )

    def on_Slider1_select(self, event):
        self.components.Data.text = str(event.target.value)
        self.Data=event.target.value
        self.Amplification= 1+ (100000*self.Data*1.0)/(self.R1*255)
        self.components.Amplification.SetValue( str(self.Amplification) )
        
    def on_Quit_mouseClick(self, event):
        self.Close() 

    def Send(self,k):
        #data0 sda
        #data1 sck
        #data2 rst
        
        self.io=4  # rst=1
        self.PORT.setData(self.io)
        
        self.Send1(0) #stack select bit
        self.Send8(0) #pot1
        self.Send8(k) #pot0
        self.io=0  # rst=0
        self.PORT.setData(self.io)
        
    def Clock(self):
        self.io=self.io|0x02
        self.PORT.setData(self.io)
        self.io=self.io&0xFD
        self.PORT.setData(self.io)

    def Send8(self,k):
        #msb -> lsb
        mask=[128,64,32,16,8,4,2,1]
        for i in range(0, 8, 1):
            if (k & mask[i]):
                #print '1',
                self.io=self.io|0x01
            else:
                #print '0',
                self.io=self.io&0xFE
            self.PORT.setData(self.io)
            self.Clock()
        #print ' '

    def Send1(self, k):
        if (k & 0x01):
           self.io=self.io|0x01
        else:
           self.io=self.io&0xFE
        self.PORT.setData(self.io)
        self.Clock()

    def on_Send_mouseClick(self, event):
        self.Send(self.Data)
        max=2
        dlg = wx.ProgressDialog("Downloading","LPT1 -> DS1267", max, self, wx.PD_AUTO_HIDE )
        count = 0
        while count < max:
              count = count + 1
              wx.Sleep(1)
              dlg.Update(count)
        dlg.Destroy()

    def on_menuFileExit_select(self, event):
        self.Close()

    def on_menuAbout_select(self, event):   
        msg="""       pyDS1267 - V%s - DS1267 numerical potentiometer (MAXIM-DALLAS)
        
        Pot=100K
        A=1+ k*(100K/R1) Non-Inverting Amplifier  
        with k in [0...1] and R1=4.7K

        data0-sda   data1-sck  data2 rst        gnd 
           pin 2          pin 3         pin 4      pin 18-25
          
        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(MyBackground)
    app.MainLoop()