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
pipc_gpio_magic = 0xdeadbeef
pipc_adc_magic = 0xcafecafe
pipc_serial_magic = 0xabcdef01
pipc_mcp_magic = 0xfeedfeed
pipc_reset_magic = 0xbadf00d
import posix_ipc as pipc
mq_to_qemu = pipc.MessageQueue("/to_qemu",flags=pipc.O_CREAT, read=False, write=True)
......@@ -59,10 +61,19 @@ class MainWindow(QMainWindow):
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):
""" called when a checkbox is 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)
......@@ -85,7 +96,14 @@ class MainWindow(QMainWindow):
app = QApplication(sys.argv)
window = MainWindow()
def receiver():
class Receiver(QThread):
resetSig = pyqtSignal()
gpioSig = pyqtSignal(str,int,int)
def __init__self():
QThread.__init__(self)
def run(self):
while True:
msg = mq_from_qemu.receive()
magic = struct.unpack("=I",msg[0][0:4]) #get magic value. return a tuple.
......@@ -94,9 +112,13 @@ def receiver():
magic, changed_out, dir_mask,output,gpio = struct.unpack("=IHHHH",msg[0])
name = ['A','B','C','D','F']
if gpio < 5:
window.setGPIO(name[gpio],dir_mask,output) #TODO: thread safe?
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.
......@@ -104,8 +126,9 @@ def receiver():
window.show()
thread = threading.Thread(target=receiver)
thread.daemon = True
thread = Receiver()
thread.resetSig.connect(window.reset)
thread.gpioSig.connect(window.setGPIO)
thread.start()
app.exec_() #event loop
......@@ -13,6 +13,8 @@ import struct
pipc_gpio_magic = 0xdeadbeef
pipc_adc_magic = 0xcafecafe
pipc_serial_magic = 0xabcdef01
pipc_mcp_magic = 0xfeedfeed
pipc_reset_magic = 0xbadf00d
import posix_ipc as pipc
mq_to_qemu = pipc.MessageQueue("/to_qemu",flags=pipc.O_CREAT, read=False, write=True)
......@@ -113,6 +115,19 @@ class MainWindow(QMainWindow):
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):
""" called when a checkbox is checked """
gpioId = 1 #2 buttons on GPIOB
......@@ -180,7 +195,15 @@ class MainWindow(QMainWindow):
app = QApplication(sys.argv)
window = MainWindow()
def receiver():
class Receiver(QThread):
resetSig = pyqtSignal()
gpioSig = pyqtSignal(str,int,int)
mcpSig = pyqtSignal(str,int,int)
def __init__self():
QThread.__init__(self)
def run(self):
while True:
msg = mq_from_qemu.receive()
magic = struct.unpack("=I",msg[0][0:4]) #get magic value. return a tuple.
......@@ -189,11 +212,13 @@ def receiver():
magic, changed_out, dir_mask,output,gpio = struct.unpack("=IHHHH",msg[0])
name = ['A','B','C','D','F']
if gpio < 5:
window.setGPIO(name[gpio],dir_mask,output) #TODO: thread safe?
self.gpioSig.emit(name[gpio],dir_mask,output)
elif magic[0] == pipc_serial_magic:
magic, char = struct.unpack("=II",msg[0])
#print('char {0:c}'.format(char))
window.serial.insertPlainText(chr(char & 0xff))
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.
......@@ -209,8 +234,9 @@ def warningPersistence():
window.show()
thread = threading.Thread(target=receiver)
thread.daemon = True
thread = Receiver()
thread.resetSig.connect(window.reset)
thread.gpioSig.connect(window.setGPIO)
thread.start()
threadWarning = threading.Thread(target=warningPersistence)
......
......@@ -13,6 +13,8 @@ import struct
pipc_gpio_magic = 0xdeadbeef
pipc_adc_magic = 0xcafecafe
pipc_serial_magic = 0xabcdef01
pipc_mcp_magic = 0xfeedfeed
pipc_reset_magic = 0xbadf00d
import posix_ipc as pipc
mq_to_qemu = pipc.MessageQueue("/to_qemu",flags=pipc.O_CREAT, read=False, write=True)
......@@ -94,12 +96,12 @@ class MainWindow(QMainWindow):
#ADC pin state
gbox=QGroupBox("ADC - potentiometer")
layout = QHBoxLayout()
slider = QSlider(Qt.Horizontal)
slider.setMinimum(0)
slider.setMaximum(4095)
layout.addWidget(slider)
self.slider = QSlider(Qt.Horizontal)
self.slider.setMinimum(0)
self.slider.setMaximum(4095)
layout.addWidget(self.slider)
self.labelADC = QLabel('0')
slider.valueChanged.connect(self.updateSlider)
self.slider.valueChanged.connect(self.updateSlider)
layout.addWidget(self.labelADC)
gbox.setLayout(layout)
layoutV.addWidget(gbox)
......@@ -109,6 +111,17 @@ class MainWindow(QMainWindow):
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):
""" called when a checkbox is checked """
gpioId = 1 #2 buttons on GPIOB
......@@ -137,7 +150,15 @@ class MainWindow(QMainWindow):
app = QApplication(sys.argv)
window = MainWindow()
def receiver():
class Receiver(QThread):
resetSig = pyqtSignal()
gpioSig = pyqtSignal(str,int,int)
serialSig = pyqtSignal(str)
def __init__self():
QThread.__init__(self)
def run(self):
while True:
msg = mq_from_qemu.receive()
magic = struct.unpack("=I",msg[0][0:4]) #get magic value. return a tuple.
......@@ -146,11 +167,14 @@ def receiver():
magic, changed_out, dir_mask,output,gpio = struct.unpack("=IHHHH",msg[0])
name = ['A','B','C','D','F']
if gpio < 5:
window.setGPIO(name[gpio],dir_mask,output) #TODO: thread safe?
self.gpioSig.emit(name[gpio],dir_mask,output)
elif magic[0] == pipc_serial_magic:
magic, char = struct.unpack("=II",msg[0])
#print('char {0:c}'.format(char))
window.serial.insertPlainText(chr(char & 0xff))
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.
......@@ -165,8 +189,10 @@ def warningPersistence():
window.show()
thread = threading.Thread(target=receiver)
thread.daemon = True
thread = Receiver()
thread.resetSig.connect(window.reset)
thread.gpioSig.connect(window.setGPIO)
thread.serialSig.connect(window.serial.insertPlainText)
thread.start()
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