Commit 570253d1 authored by Mikaël BRIDAY's avatar Mikaël BRIDAY

update scripts, now thread safe

parent 15cd665f
...@@ -13,6 +13,8 @@ import struct ...@@ -13,6 +13,8 @@ import struct
pipc_gpio_magic = 0xdeadbeef pipc_gpio_magic = 0xdeadbeef
pipc_adc_magic = 0xcafecafe pipc_adc_magic = 0xcafecafe
pipc_serial_magic = 0xabcdef01 pipc_serial_magic = 0xabcdef01
pipc_mcp_magic = 0xfeedfeed
pipc_reset_magic = 0xbadf00d
import posix_ipc as pipc import posix_ipc as pipc
mq_to_qemu = pipc.MessageQueue("/to_qemu",flags=pipc.O_CREAT, read=False, write=True) mq_to_qemu = pipc.MessageQueue("/to_qemu",flags=pipc.O_CREAT, read=False, write=True)
...@@ -59,10 +61,19 @@ class MainWindow(QMainWindow): ...@@ -59,10 +61,19 @@ class MainWindow(QMainWindow):
self.setCentralWidget(widget) self.setCentralWidget(widget)
@pyqtSlot()
def reset(self):
"""called by QEmu at startup. It should send back information to QEmu """
print('reset')
for gpio in self.gpios:
for chkbox in self.gpios[gpio]:
chkbox.setPalette(self.palDis)
chkbox.setCheckState(Qt.Unchecked)
def buttonChecked(self,state,gpioId,pin): def buttonChecked(self,state,gpioId,pin):
""" called when a checkbox is checked """ """ called when a checkbox is checked """
print("send msg: GPIO "+str(gpioId)+", pin "+str(pin)+', state '+str(state==Qt.Checked) ) print("send msg: GPIO "+str(gpioId)+", pin "+str(pin)+', state '+str(state==Qt.Checked) )
s=struct.pack("=IIII",pipc_magic,pin,state==Qt.Checked,gpioId) s=struct.pack("=IIII",pipc_gpio_magic,pin,state==Qt.Checked,gpioId)
mq_to_qemu.send(s) mq_to_qemu.send(s)
...@@ -85,27 +96,39 @@ class MainWindow(QMainWindow): ...@@ -85,27 +96,39 @@ class MainWindow(QMainWindow):
app = QApplication(sys.argv) app = QApplication(sys.argv)
window = MainWindow() window = MainWindow()
def receiver(): class Receiver(QThread):
while True: resetSig = pyqtSignal()
msg = mq_from_qemu.receive() gpioSig = pyqtSignal(str,int,int)
magic = struct.unpack("=I",msg[0][0:4]) #get magic value. return a tuple.
#print("mg=",mg," pin=",pin," gpio=",gpio," state=",state) def __init__self():
if magic[0] == pipc_gpio_magic: QThread.__init__(self)
magic, changed_out, dir_mask,output,gpio = struct.unpack("=IHHHH",msg[0])
name = ['A','B','C','D','F'] def run(self):
if gpio < 5: while True:
window.setGPIO(name[gpio],dir_mask,output) #TODO: thread safe? msg = mq_from_qemu.receive()
elif magic[0] == pipc_serial_magic: magic = struct.unpack("=I",msg[0][0:4]) #get magic value. return a tuple.
pass #print("mg=",mg," pin=",pin," gpio=",gpio," state=",state)
else: if magic[0] == pipc_gpio_magic:
raise Exception("Wrong magic number in GPIO IPC message: 0x{val:08x}".format(val=magic[0]) ) magic, changed_out, dir_mask,output,gpio = struct.unpack("=IHHHH",msg[0])
#required if we get too many messages to let time for the UI. name = ['A','B','C','D','F']
time.sleep(.01) if gpio < 5:
self.gpioSig.emit(name[gpio],dir_mask,output)
elif magic[0] == pipc_serial_magic:
pass
elif magic[0] == pipc_mcp_magic:
pass
elif magic[0] == pipc_reset_magic:
self.resetSig.emit()
else:
raise Exception("Wrong magic number in GPIO IPC message: 0x{val:08x}".format(val=magic[0]) )
#required if we get too many messages to let time for the UI.
time.sleep(.01)
window.show() window.show()
thread = threading.Thread(target=receiver) thread = Receiver()
thread.daemon = True thread.resetSig.connect(window.reset)
thread.gpioSig.connect(window.setGPIO)
thread.start() thread.start()
app.exec_() #event loop app.exec_() #event loop
...@@ -10,9 +10,11 @@ import sys,time ...@@ -10,9 +10,11 @@ import sys,time
import threading import threading
# Communication part # Communication part
import struct import struct
pipc_gpio_magic = 0xdeadbeef pipc_gpio_magic = 0xdeadbeef
pipc_adc_magic = 0xcafecafe pipc_adc_magic = 0xcafecafe
pipc_serial_magic = 0xabcdef01 pipc_serial_magic = 0xabcdef01
pipc_mcp_magic = 0xfeedfeed
pipc_reset_magic = 0xbadf00d
import posix_ipc as pipc import posix_ipc as pipc
mq_to_qemu = pipc.MessageQueue("/to_qemu",flags=pipc.O_CREAT, read=False, write=True) mq_to_qemu = pipc.MessageQueue("/to_qemu",flags=pipc.O_CREAT, read=False, write=True)
...@@ -113,6 +115,19 @@ class MainWindow(QMainWindow): ...@@ -113,6 +115,19 @@ class MainWindow(QMainWindow):
self.setCentralWidget(widget) self.setCentralWidget(widget)
@pyqtSlot()
def reset(self):
print('reset')
for (pin,chkbox) in self.leds:
chkbox.setPalette(self.palOff)
for (pin,chkbox) in self.buttons:
chkbox.setCheckState(Qt.Unchecked)
self.state = 0
self.step = 0
self.labelStepperState.setText('state : '+str(self.state))
self.labelStepperStep.setText('step : '+str(self.step))
self.slider.setValue(0)
def buttonChecked(self,state,pin): def buttonChecked(self,state,pin):
""" called when a checkbox is checked """ """ called when a checkbox is checked """
gpioId = 1 #2 buttons on GPIOB gpioId = 1 #2 buttons on GPIOB
...@@ -180,24 +195,34 @@ class MainWindow(QMainWindow): ...@@ -180,24 +195,34 @@ class MainWindow(QMainWindow):
app = QApplication(sys.argv) app = QApplication(sys.argv)
window = MainWindow() window = MainWindow()
def receiver(): class Receiver(QThread):
while True: resetSig = pyqtSignal()
msg = mq_from_qemu.receive() gpioSig = pyqtSignal(str,int,int)
magic = struct.unpack("=I",msg[0][0:4]) #get magic value. return a tuple. mcpSig = pyqtSignal(str,int,int)
#print("mg=",mg," pin=",pin," gpio=",gpio," state=",state)
if magic[0] == pipc_gpio_magic: def __init__self():
magic, changed_out, dir_mask,output,gpio = struct.unpack("=IHHHH",msg[0]) QThread.__init__(self)
name = ['A','B','C','D','F']
if gpio < 5: def run(self):
window.setGPIO(name[gpio],dir_mask,output) #TODO: thread safe? while True:
elif magic[0] == pipc_serial_magic: msg = mq_from_qemu.receive()
magic, char = struct.unpack("=II",msg[0]) magic = struct.unpack("=I",msg[0][0:4]) #get magic value. return a tuple.
#print('char {0:c}'.format(char)) #print("mg=",mg," pin=",pin," gpio=",gpio," state=",state)
window.serial.insertPlainText(chr(char & 0xff)) if magic[0] == pipc_gpio_magic:
else: magic, changed_out, dir_mask,output,gpio = struct.unpack("=IHHHH",msg[0])
raise Exception("Wrong magic number in GPIO IPC message: 0x{val:08x}".format(val=magic[0]) ) name = ['A','B','C','D','F']
#required if we get too many messages to let time for the UI. if gpio < 5:
time.sleep(.01) self.gpioSig.emit(name[gpio],dir_mask,output)
elif magic[0] == pipc_serial_magic:
pass
elif magic[0] == pipc_mcp_magic:
pass
elif magic[0] == pipc_reset_magic:
self.resetSig.emit()
else:
raise Exception("Wrong magic number in GPIO IPC message: 0x{val:08x}".format(val=magic[0]) )
#required if we get too many messages to let time for the UI.
time.sleep(.01)
def warningPersistence(): def warningPersistence():
while True: while True:
...@@ -209,8 +234,9 @@ def warningPersistence(): ...@@ -209,8 +234,9 @@ def warningPersistence():
window.show() window.show()
thread = threading.Thread(target=receiver) thread = Receiver()
thread.daemon = True thread.resetSig.connect(window.reset)
thread.gpioSig.connect(window.setGPIO)
thread.start() thread.start()
threadWarning = threading.Thread(target=warningPersistence) threadWarning = threading.Thread(target=warningPersistence)
......
...@@ -13,6 +13,8 @@ import struct ...@@ -13,6 +13,8 @@ import struct
pipc_gpio_magic = 0xdeadbeef pipc_gpio_magic = 0xdeadbeef
pipc_adc_magic = 0xcafecafe pipc_adc_magic = 0xcafecafe
pipc_serial_magic = 0xabcdef01 pipc_serial_magic = 0xabcdef01
pipc_mcp_magic = 0xfeedfeed
pipc_reset_magic = 0xbadf00d
import posix_ipc as pipc import posix_ipc as pipc
mq_to_qemu = pipc.MessageQueue("/to_qemu",flags=pipc.O_CREAT, read=False, write=True) mq_to_qemu = pipc.MessageQueue("/to_qemu",flags=pipc.O_CREAT, read=False, write=True)
...@@ -94,12 +96,12 @@ class MainWindow(QMainWindow): ...@@ -94,12 +96,12 @@ class MainWindow(QMainWindow):
#ADC pin state #ADC pin state
gbox=QGroupBox("ADC - potentiometer") gbox=QGroupBox("ADC - potentiometer")
layout = QHBoxLayout() layout = QHBoxLayout()
slider = QSlider(Qt.Horizontal) self.slider = QSlider(Qt.Horizontal)
slider.setMinimum(0) self.slider.setMinimum(0)
slider.setMaximum(4095) self.slider.setMaximum(4095)
layout.addWidget(slider) layout.addWidget(self.slider)
self.labelADC = QLabel('0') self.labelADC = QLabel('0')
slider.valueChanged.connect(self.updateSlider) self.slider.valueChanged.connect(self.updateSlider)
layout.addWidget(self.labelADC) layout.addWidget(self.labelADC)
gbox.setLayout(layout) gbox.setLayout(layout)
layoutV.addWidget(gbox) layoutV.addWidget(gbox)
...@@ -109,6 +111,17 @@ class MainWindow(QMainWindow): ...@@ -109,6 +111,17 @@ class MainWindow(QMainWindow):
self.setCentralWidget(widget) self.setCentralWidget(widget)
@pyqtSlot()
def reset(self):
"""called by QEmu at startup. It should send back information to QEmu """
print('reset')
for (pin,chkbox) in self.leds:
chkbox.setPalette(self.palOff)
for (pin,chkbox) in self.buttons:
chkbox.setCheckState(Qt.Unchecked)
self.serial.clear()
self.slider.setValue(0)
def buttonChecked(self,state,pin): def buttonChecked(self,state,pin):
""" called when a checkbox is checked """ """ called when a checkbox is checked """
gpioId = 1 #2 buttons on GPIOB gpioId = 1 #2 buttons on GPIOB
...@@ -137,24 +150,35 @@ class MainWindow(QMainWindow): ...@@ -137,24 +150,35 @@ class MainWindow(QMainWindow):
app = QApplication(sys.argv) app = QApplication(sys.argv)
window = MainWindow() window = MainWindow()
def receiver(): class Receiver(QThread):
while True: resetSig = pyqtSignal()
msg = mq_from_qemu.receive() gpioSig = pyqtSignal(str,int,int)
magic = struct.unpack("=I",msg[0][0:4]) #get magic value. return a tuple. serialSig = pyqtSignal(str)
#print("mg=",mg," pin=",pin," gpio=",gpio," state=",state)
if magic[0] == pipc_gpio_magic: def __init__self():
magic, changed_out, dir_mask,output,gpio = struct.unpack("=IHHHH",msg[0]) QThread.__init__(self)
name = ['A','B','C','D','F']
if gpio < 5: def run(self):
window.setGPIO(name[gpio],dir_mask,output) #TODO: thread safe? while True:
elif magic[0] == pipc_serial_magic: msg = mq_from_qemu.receive()
magic, char = struct.unpack("=II",msg[0]) magic = struct.unpack("=I",msg[0][0:4]) #get magic value. return a tuple.
#print('char {0:c}'.format(char)) #print("mg=",mg," pin=",pin," gpio=",gpio," state=",state)
window.serial.insertPlainText(chr(char & 0xff)) if magic[0] == pipc_gpio_magic:
else: magic, changed_out, dir_mask,output,gpio = struct.unpack("=IHHHH",msg[0])
raise Exception("Wrong magic number in GPIO IPC message: 0x{val:08x}".format(val=magic[0]) ) name = ['A','B','C','D','F']
#required if we get too many messages to let time for the UI. if gpio < 5:
time.sleep(.01) self.gpioSig.emit(name[gpio],dir_mask,output)
elif magic[0] == pipc_serial_magic:
magic, char = struct.unpack("=II",msg[0])
self.serialSig.emit(chr(char & 0xff))
elif magic[0] == pipc_mcp_magic:
pass
elif magic[0] == pipc_reset_magic:
self.resetSig.emit()
else:
raise Exception("Wrong magic number in GPIO IPC message: 0x{val:08x}".format(val=magic[0]) )
#required if we get too many messages to let time for the UI.
time.sleep(.01)
def warningPersistence(): def warningPersistence():
while True: while True:
...@@ -165,8 +189,10 @@ def warningPersistence(): ...@@ -165,8 +189,10 @@ def warningPersistence():
window.show() window.show()
thread = threading.Thread(target=receiver) thread = Receiver()
thread.daemon = True thread.resetSig.connect(window.reset)
thread.gpioSig.connect(window.setGPIO)
thread.serialSig.connect(window.serial.insertPlainText)
thread.start() thread.start()
threadWarning = threading.Thread(target=warningPersistence) threadWarning = threading.Thread(target=warningPersistence)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment