Help needed with application control, loops, and JavaScript
-
I’m looking for some help with the project that I am working on. A high level demo project can be found at
https://github.com/jstockton/DemoQMLProgramI will be running this program on a Raspberry Pi and will be controlling a couple of different devices. I’m new to QML/Python/C++ so I may not use the correct terminology, but hopefully I will make some sense in explains things. I’ve done various research to get to the point that I am at now, but am struggling with putting everything together so that it works the way I envision. My example is built to control 3 motors and 1 light, read various sensors and display some results from the sensors on the screen.
Main.qml: This is the main application window that builds the framework of the UI. It includes a menu and a drawer to load various pages.
Page1.qml: This is what I am considering the “home” page. This is the page that will the default page. In my example it shows the temperature that I will be reading from a sensor (as of now a DHT11).
Page2.qml: This page is used to setup some of the controlling settings for my motors. I have 3 motors in my example, but in the end this may change. There is a time that will be used to turn the motors on and then other values that will be used to dictate how long the motors will run.Page3.qml: This page is used to setup the light. Like Page2, it has a start time and a duration of how long the light will stay on.
For various reasons I’ve chosen to use Python as the framework to interact with the Raspberry PI GPIO and this is where I need the help. From what I’ve read the Python application needs to control the system. By this I mean the Python will load the QML app and be used to interact with the GPIO pins.I want my program to:
- Run and load the UI
- Get the temperature ever X min/sec and update the screen
- Turn on the motors and run for the specified time
- Turn on the light for the specified time.
These activities may or may not happen at the same time, meaning the light could be on at the same time the motors are running and the UI could also be updating the temperature. They could happen in any order after the UI is loaded and it’s possible that while any of these events are running the user is navigating to the different pages in the UI.
My first question is how do I control the loops that update the screen and turn the devices on? Do I put a timer in the QML or do I put it in Python or a mixture? I’m assuming I have to make these calls synchronously to avoid blocking so was thinking about using some code I found that queues callbacks. The article can be found at: https://wiki.python.org/moin/PyQt/QML callback function
Is this the correct way/possible way of running multiple threads so to speak? If using the callback function is a viable option how is this done? I understand the concept of a callback and I partially understand the sample that is provided, but what I am missing is how is this put into practice is? The sample adds 3 callbacks to a queue and then processes them one right after another. Is the only option for this code to run the functions sequentially or can they be processed as they complete? Assuming in a real world situation it’s possible that you would want to process one function while another one is doing its thing. If possible with the sample code, how is this code used to process the callbacks as they complete? A small snippet of code showing how to do this would be appreciated or even a more detailed explanation of what this code is doing and when would be great too.Second question is what is the best way to run the events based on the schedule that is created? Can this be done in a loop fashion from either QML or Python or should I have my program create and maintain CRON jobs? If the Python script is the one in charge of running the loops, how do you initiate sending data from Python to QML without doing a callback from QML?
Also, I have a couple comments in my QML files that I haven't figured out yet. Specifically in Page1.qml regarding importing a JS file. If anyone has suggestions on that, please share.
I know I’m asking a lot of questions, but it’s what I came up with when trying to wrap everything together. Thank you for any help that is provided.