SPI test panel

SPI_gui.PNG

  1##  Example_SPI/ main.py
  2##  This is example for SPI with WPC DAQ Device with synchronous mode.
  3##  Copyright (c) 2023 WPC Systems Ltd.
  4##  All rights reserved.
  5
  6## Python
  7import sys
  8import os
  9
 10## Third party
 11from PyQt5 import QtWidgets, QtGui
 12from UI_design.Ui_example_GUI_SPI import Ui_MainWindow
 13
 14## WPC
 15from wpcsys import pywpc
 16
 17WREN = 0x06
 18WRITE = 0x02
 19
 20class MainWindow(QtWidgets.QMainWindow):
 21    def __init__(self):
 22        super(MainWindow, self).__init__()
 23
 24        ## UI initialize
 25        self.ui = Ui_MainWindow()
 26        self.ui.setupUi(self)
 27
 28        ## Get Python driver version
 29        print(f'{pywpc.PKG_FULL_NAME} - Version {pywpc.__version__}')
 30
 31        ## Connection flag
 32        self.connect_flag = 0
 33
 34        ## Handle declaration
 35        self.dev = None
 36
 37        ## Material path
 38        file_path = os.path.dirname(__file__)
 39        self.trademark_path = file_path + "\Material\\trademark.jpg"
 40        self.blue_led_path = file_path + "\Material\LED_blue.png"
 41        self.red_led_path = file_path + "\Material\LED_red.png"
 42        self.green_led_path = file_path + "\Material\LED_green.png"
 43        self.gray_led_path = file_path + "\Material\LED_gray.png"
 44
 45        ## Set trademark & LED path
 46        self.ui.lb_trademark.setPixmap(QtGui.QPixmap(self.trademark_path))
 47        self.ui.lb_led.setPixmap(QtGui.QPixmap(self.gray_led_path))
 48
 49        ## Define callback events
 50        self.ui.btn_connect.clicked.connect(self.connectEvent)
 51        self.ui.btn_disconnect.clicked.connect(self.disconnectEvent)
 52        self.ui.btn_write.clicked.connect(self.writeEvent)
 53        self.ui.btn_read.clicked.connect(self.readEvent)
 54        self.ui.btn_set.clicked.connect(self.setEvent)
 55
 56    def selectHandle(self):
 57        handle_idx = int(self.ui.comboBox_handle.currentIndex())
 58        if handle_idx == 0:
 59            self.dev = pywpc.USBDAQF1D()
 60        elif handle_idx == 1:
 61            self.dev = pywpc.USBDAQF1AD()
 62        elif handle_idx == 2:
 63            self.dev = pywpc.USBDAQF1TD()
 64        elif handle_idx == 3:
 65            self.dev = pywpc.USBDAQF1RD()
 66        elif handle_idx == 4:
 67            self.dev = pywpc.USBDAQF1CD()
 68        elif handle_idx == 5:
 69            self.dev = pywpc.USBDAQF1AOD()
 70
 71    def updateParam(self):
 72        ## DO port and pin
 73        self.DO_port = 0
 74        self.DO_index = [0] ## CS pin
 75
 76        ## Get IP or serial_number from GUI
 77        self.ip = self.ui.lineEdit_IP.text()
 78
 79        ## Get port from GUI
 80        self.port = int(self.ui.comboBox_port.currentIndex())+1
 81
 82        ## Get mode from GUI
 83        self.mode = self.ui.comboBox_mode.currentIndex()
 84
 85        ## Get prescaler from GUI
 86        self.prescaler = self.ui.comboBox_prescaler.currentIndex()
 87
 88        ## Get write (Hex) from GUI
 89        data = self.ui.lineEdit_write.text()
 90
 91        ## Convert string to int list
 92        self.write_data = self.converStrtoIntList(data)
 93
 94    def setEvent(self):
 95        ## Update Param
 96        self.updateParam()
 97
 98        ## Set SPI port and prescaler
 99        status = self.dev.SPI_setPrescaler(self.port, self.prescaler)
100        print("SPI_setPrescaler status: ", status)
101
102        ## Set SPI port and mode
103        status = self.dev.SPI_setMode(self.port, self.mode)
104        print("SPI_setMode status: ", status)
105
106    def writeEvent(self):
107        ## Update Param
108        self.updateParam()
109
110        ## Set CS(pin0) to low
111        status = self.dev.DO_writePins(self.DO_port, self.DO_index, [0])
112        print("DO_writePins status: ", status)
113
114        ## Write WREN byte
115        status = self.dev.SPI_write(self.port, [WREN])
116        print("SPI_write status: ", status)
117
118        ## Set CS(pin0) to high
119        status = self.dev.DO_writePins(self.DO_port, self.DO_index, [1])
120        print("DO_writePins status: ", status)
121
122        ## Set CS(pin0) to low
123        status = self.dev.DO_writePins(self.DO_port, self.DO_index, [0])
124        print("DO_writePins status: ", status)
125
126        ## Set SPI port and write bytes
127        status = self.dev.SPI_write(self.port, self.write_data)
128        print("SPI_write status: ", status)
129
130        ## Set CS(pin0) to high
131        status = self.dev.DO_writePins(self.DO_port, self.DO_index, [1])
132        print("DO_writePins status: ", status)
133
134    def readEvent(self):
135        ## Update Param
136        self.updateParam()
137
138        ## Set CS(pin0) to low
139        status = self.dev.DO_writePins(self.DO_port, self.DO_index, [0])
140        print("DO_writePins status: ", status)
141
142        ## Set SPI port and read bytes
143        data = self.dev.SPI_readAndWrite(self.port, self.write_data)
144        data = ['{:02x}'.format(value) for value in data]
145        print("read data :", data)
146
147        ## Update data in GUI
148        self.ui.lineEdit_read.setText(str(data))
149
150        ## Set CS(pin0) to high
151        status = self.dev.DO_writePins(self.DO_port, self.DO_index, [1])
152        print("DO_writePins status: ", status)
153
154    def connectEvent(self):
155        if self.connect_flag == 1:
156            return
157
158        ## Select handle
159        self.selectHandle()
160
161        ## Update Param
162        self.updateParam()
163
164        ## Connect to device
165        try:
166            self.dev.connect(self.ip)
167        except pywpc.Error as err:
168            print("err: " + str(err))
169            return
170
171        ## Change LED status
172        self.ui.lb_led.setPixmap(QtGui.QPixmap(self.blue_led_path))
173
174        ## Change connection flag
175        self.connect_flag = 1
176
177        ## Open pin0 with digital output
178        status = self.dev.DO_openPins(self.DO_port, self.DO_index)
179        print("DO_openPins status: ", status)
180
181        ## Open SPI
182        status = self.dev.SPI_open(self.port)
183        print("SPI_open status: ", status)
184
185        ## Set CS(pin0) to high
186        status = self.dev.DO_writePins(self.DO_port, self.DO_index, [1])
187        print("DO_writePins status: ", status)
188
189    def disconnectEvent(self):
190        if self.connect_flag == 0:
191            return
192
193        ## Update Param
194        self.updateParam()
195
196        ## Close SPI port
197        status = self.dev.SPI_close(self.port)
198        print("SPI_close status: ", status)
199
200        ## Close pin0 with digital output
201        status = self.dev.DO_closePins(self.DO_port, self.DO_index)
202        print("DO_closePins status: ", status)
203
204        ## Disconnect device
205        self.dev.disconnect()
206
207        ## Change LED status
208        self.ui.lb_led.setPixmap(QtGui.QPixmap(self.green_led_path))
209
210        ## Change connection flag
211        self.connect_flag = 0
212
213    def closeEvent(self, event):
214        if self.dev is not None:
215            ## Disconnect device
216            self.dev.disconnect()
217
218            ## Release device handle
219            self.dev.close()
220
221    def converStrtoIntList(self, str_):
222        ## Split string by commas
223        write_data_strlist = str_.replace(' ','').split(',')
224
225        ## Convert string list to int list
226        write_data_int = []
227        for item in write_data_strlist:
228            write_data_int.append(int(item, 16))
229        return write_data_int
230
231if __name__ == "__main__":
232    app = QtWidgets.QApplication([])
233    WPC_main_ui = MainWindow()
234    WPC_main_ui.show()
235    sys.exit(app.exec_())