summaryrefslogtreecommitdiffstats
path: root/pyk8055/k8055qt.py
blob: fe0e85326cb2fef94f4fecbe1c0facf55e7f8230 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Python version of libK8055 demo program
#
# Copyright (C) 2007 by Pjetur G. Hjaltason
#

import sys, time, string
from PyQt4.Qt import *
#from frmk8055qt import *
from pyk8055 import k8055


class QTK8055(K8055QT):
    def __init__(self):
        K8055QT.__init__(self)

        # Not much sense in making a fancy menu yet
        #
        #puProg = QPopupMenu(self)
        #puProg.insertItem("Quit", Kapp.quit)

        #puHelp = QPopupMenu(self)
        #puHelp.insertItem("About", self.about)

        #self.menu = QMenuBar(self)
        #self.menu.insertItem("Program", puProg)
        #self.menu.insertItem("Help", puHelp)

        # enumerate devices
        BMask = SearchDevices()
        if not BMask:
            print "No K8055 devices found"
            exit

        # Enable those present
        self.CardButton1.setEnabled((BMask & 0x01) > 0)
        self.CardButton2.setEnabled((BMask & 0x02) > 0)
        self.CardButton3.setEnabled((BMask & 0x04) > 0)
        self.CardButton4.setEnabled((BMask & 0x08) > 0)

        # set the first one found as suggestion
        if (BMask & 0x01) > 0:
            self.card = 0
            self.CardButton1.setChecked(1)
        elif (BMask & 0x02) > 0:
            self.card = 1
            self.CardButton2.setChecked(1)
        elif (BMask & 0x04) > 0:
            self.card = 2
            self.CardButton3.setChecked(1)
        elif (BMask & 0x08) > 0:
            self.card = 3
            self.CardButton4.setChecked(1)
            self.time = QTime.currentTime()     # get current time
            internalTimer = QTimer( self )      # create internal timer
            self.connect( internalTimer, SIGNAL("timeout()"), self.timeout )
            internalTimer.start( 100 )         # emit signal every 1/10 second

        self.timeout()

    def AnalogTest(self):
        try:
            for step in range(0,255,10):
                self.BarAD1.setValue(step)
                self.BarAD2.setValue(step)
                time.sleep(0.1)
            # set analog output channel 1 to 200/255*5v = 3.9V
            for step in range(255,0,10):
                self.BarAD1.setValue(step)
                self.BarAD2.setValue(step)
                time.sleep(0.1)
            # read both analog inputs
            # note: this returns a list
            #res = self.k.ReadAllAnalog()
            #sres = res[1:]
            # There should be more interesting information in the messagebox ;-/
            QMessageBox.information( self,'Information', "AllOK","Ok")
        except:
            pass

    def DigitalTest(self):
        try:
            # Create a rotating display of digital outputs
            # while waiting for keypress on digital input 1
            # (longer than .2sec :) Loop at most 3 times
            Input1High = False
            for i in range(1,3):
                d = 1
                self.k.WriteAllDigital(1)
                while Input1High == False and d < 128:
                    Input1High = (self.k.ReadDigitalChannel(1) > 0)
                    if Input1High: break
                    time.sleep(0.2)    # wait .2 sec
                    d *=2        # and continue rotating digital outputs
                    #if d > 128: d=1;
                    self.k.WriteAllDigital(d)
                if Input1High: break

            res = "Digital input 1=" + str(self.k.ReadDigitalChannel(1)) + "<br>"
            # read the counter on input 1
            res+= "Counter(1)=" + str(self.k.ReadCounter(1)) + "<br>"
            # read the counter on input 2
            res+= "Counter(2)=" + str(self.k.ReadCounter(2))
            # set even bits on digital outputs
            self.k.WriteAllDigital(170)
            time.sleep(1)
            # set odd bits on digital outputs
            self.k.WriteAllDigital(85)
            time.sleep(1)
            # Clear all digital outputs
            self.k.WriteAllDigital(0)
            # There should be more interesting information in the messagebox ;-/
            QMessageBox.information( self,'DigitalTest', res,"Ok")
        except:
            QMessageBox.warning( self,'DigitalTest', "Not OK","Ok")
            pass

    def EnableAll(self):
        self.OutputBox1.setEnabled(1)
        self.OutputBox2.setEnabled(1)
        self.OutputBox3.setEnabled(1)
        self.OutputBox4.setEnabled(1)
        self.OutputBox5.setEnabled(1)
        self.OutputBox6.setEnabled(1)
        self.OutputBox7.setEnabled(1)
        self.OutputBox8.setEnabled(1)
        self.SetAllDigitalButton.setEnabled(1)
        self.ClearAllDigitalButton.setEnabled(1)
        self.SetAllAnalogButton.setEnabled(1)
        self.ClearAllAnalogButton.setEnabled(1)
        self.comboCounter1.setEnabled(1)
        self.comboCounter2.setEnabled(1)
        self.ClearCounter1.setEnabled(1)
        self.ClearCounter2.setEnabled(1)
        self.TestDigitalButton.setEnabled(1)
        self.TestAnalogButton.setEnabled(1)
        self.BarAD1.setEnabled(1)
        self.BarAD2.setEnabled(1)
        self.BarDA1.setEnabled(1)
        self.BarDA2.setEnabled(1)

    def DisableAll(self):
        self.OutputBox1.setEnabled(0)
        self.OutputBox2.setEnabled(0)
        self.OutputBox3.setEnabled(0)
        self.OutputBox4.setEnabled(0)
        self.OutputBox5.setEnabled(0)
        self.OutputBox6.setEnabled(0)
        self.OutputBox7.setEnabled(0)
        self.OutputBox8.setEnabled(0)
        self.SetAllDigitalButton.setEnabled(0)
        self.ClearAllDigitalButton.setEnabled(0)
        self.SetAllAnalogButton.setEnabled(0)
        self.ClearAllAnalogButton.setEnabled(0)
        self.comboCounter1.setEnabled(0)
        self.comboCounter2.setEnabled(0)
        self.ClearCounter1.setEnabled(0)
        self.ClearCounter2.setEnabled(0)
        self.TestDigitalButton.setEnabled(0)
        self.TestAnalogButton.setEnabled(0)
        self.BarAD1.setEnabled(0)
        self.BarAD2.setEnabled(0)
        self.BarDA1.setEnabled(0)
        self.BarDA2.setEnabled(0)
#
## The QTimer::timeout() signal is received by this slot.
#

    def timeout( self ):
        #new_time = QTime.currentTime()                # get the current time
        if self.k != None:
            try:
                AllVal = self.k.ReadAllValues()
                #print AllVal,AllVal[2]
                self.BarDA1.setValue(255 - AllVal[2])
                self.BarDA2.setValue(255 - AllVal[3])
                self.DA1Value.setProperty("intValue",QVariant(AllVal[2]))
                self.DA2Value.setProperty("intValue",QVariant(AllVal[3]))
                self.Counter1Value.setProperty("intValue",QVariant(AllVal[4]))
                self.Counter2Value.setProperty("intValue",QVariant(AllVal[5]))

                if (AllVal[1] & 0x01) > 0:
                    self.DigitalLed1.setState(KLed.On)
                else:
                    self.DigitalLed1.setState(KLed.Off)
                if (AllVal[1] & 0x02) > 0:
                    self.DigitalLed2.setState(KLed.On)
                else:
                    self.DigitalLed2.setState(KLed.Off)
                if (AllVal[1] & 0x04) > 0:
                    self.DigitalLed3.setState(KLed.On)
                else:
                    self.DigitalLed3.setState(KLed.Off)
                if (AllVal[1] & 0x08) > 0:
                    self.DigitalLed4.setState(KLed.On)
                else:
                    self.DigitalLed4.setState(KLed.Off)
                if (AllVal[1] & 0x10) > 0:
                    self.DigitalLed5.setState(KLed.On)
                else:
                    self.DigitalLed5.setState(KLed.Off)
            except:
                print "K8055 Read failed"

    def about(self):
        QMessageBox.about( self, "About k8055qt",
                "k8055qt is a small GUI program using libk8055\n\n"
                "Copyright 2007 Pjetur G. Hjaltason.  "
                "Distributed under GPL license\n\n"
                "For technical support, study the source code or surf\n"
                "http://sourceforge.net/projects/libk8055/\n" )

if __name__ == "__main__":
    Kapp = QApplication(sys.argv)
    QObject.connect(Kapp,SIGNAL("lastWindowClosed()"),Kapp,SLOT("quit()"))
    w = QTK8055()
    Kapp.setMainWidget(w)
    w.show()
    Kapp.exec_loop()