I'm using Python 2.7 to read data from approximately sensor, VL6180X, connect to the Raspberry Pi through GPIO.
I'm really a beginner in both RPi and Python, so please bare with me. My goal, for now, is to read the sensor measurements, display it on the screen and on 7Seg screen attached to the sensor, and save it to a file.
Attached is the code, where I omitted part seems irrelevant to me:
#!/usr/bin/python
import sys
import threading
import RPi.GPIO as GPIO # Import GPIO functions
from ST_VL6180X import VL6180X
from PyQt4 import QtGui, QtCore
sensor_result = 0
Debug=False
def init_sensor():
...
def init_GPIO():
GPIO.setmode(GPIO.BCM)
...
def main_loop():
global sensor_result
try:
while True:
sensor_result = tof_sensor.get_distance()
except Exception as e:
print repr(e) + "in main_loop"
sys.exit()
finally:
GPIO.cleanup()
def writeDataToFile():
try:
while True:
with open(filename, 'w') as f:
f.write()...
except Exception as e:
print repr(e) + "in file Thread"
finally: f.close()
def update_display():
try:
...#code to update the 7SegDisplay
except Exception as e:
print repr(e)
sys.exit()
class QtLCDScreen(QtGui.QWidget):
def __init__(self):
super(QtLCDScreen, self).__init__()
self.initUI()
timer = QtCore.QTimer(self)
timer.timeout.connect(self.showlcd)
timer.start(0)
def initUI(self):
self.lcd = QtGui.QLCDNumber(self)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Sensor module rev(mm)')
vbox = QtGui.QVBoxLayout()
vbox.addWidget(self.lcd)
self.setLayout(vbox)
QtThread = threading.Thread(target=self.showlcd)
#QtThread.daemon = True
QtThread.start()
def showlcd(self):
try:
global sensor_result, app
self.lcd.display(sensor_result)
self.show()
sleep(0.1)
except Exception as e:
print str(e) + "in Qt"
self.terminate()
finally:
GPIO.cleanup()
if __name__ == "__main__":
try:
init_sensor()
init_GPIO()
sleep(1)
mainLoopThread = threading.Thread(target=main_loop)
mainLoopThread.start()
app = QtGui.QApplication(sys.argv)
qtWindow = QtLCDScreen()
sevenSegThread = threading.Thread(target=update_display)
sevenSegThread.start()
fileThread = threading.Thread(target=writeDataToFile)
fileThread.start()
sys.exit(app.exec_())
except Exception as e:
print str(e) + "in main"
sys.exit()
finally:
GPIO.cleanup()
The things that bugs me out:
- I think I got the idea, that the QLCDNumber Widget will appear only
after the line
sys.exit(app.exec_())
will execute. Is this correct? - Before realize that - I had the main loop (
while True: sensor_result = tof_sensor.get_distance()
) in main, and I guess that's why I couldn't see the QtWidget, because it was blocked by this loop. Do I have other choices, beside starting this loop in a different thread? I tried to run self.exec_(), to no avail. Even set the QTimer to 0, as stated here - didn't get me nowhere. - Last thing about the QtWidget: I know that maybe I should use QtSignal and Slots, just don't know how to implement this over here. Seen a lot of examples how to build Digital Clock, but over there - signals came from QTimer, not some arbitrary variable that gets update all the time.
- About the GPIO - since I've got so much threads - I can't exit the program 'normally' - and this yields the GPIO got broken at the end of each run. Sometime I close the QtWindow, and then it got stuck, and sometimes I brutally terminate the process with 'KILL' from terminal. Anyhow - this results a mess in the 7SegScreen, and warning all over, when I start the program again, about using GPIO already in use. As you can see - I tried to enter
GPIO.cleanup()
at different points in the code, but this just got it worse. What is 'the proper' way to terminate each thread, and the main process?
Hope this is not asking too much, but I really tried over here, and got nothing...
Aucun commentaire:
Enregistrer un commentaire