Pi IoT In Python Using GPIO Zero - Getting Input |
Written by Harry Fairhead & Mike James | ||||||||
Monday, 03 June 2024 | ||||||||
Page 4 of 4
You may be attracted by the idea of event handling. It is easy and this makes it great for beginners. Just write what you want to happen when a button is pressed or released and then get on with the rest of your program. No polling loop and no waiting. It is useful for the beginner, but it isn’t a good way to organize things in the long run. The problem is that it is the pin factories that are responsible for implementing the event handling and they do this in different ways. As already discussed in connection with general interrupts and events, this approach slows the overall system down and creates problems when events occur at too fast a rate. For example, the pressed event handler can be interrupted by a release event and the release function will run before the press function ends. If a new button press happens after the release, then the press function is run a second time, even if the first call to it is still running. However, the subsequent release function isn’t called until the first press function finishes. This is a mess! Exactly what happens when multiple events occur depends on the pin factory that is providing the service and this too is unsatisfactory in that you can’t know what will happen. In conclusion, using event handling in GPIO Zero is an easy way to produce a demo or get started, but it isn’t a good way to implement anything beyond the very simple. This is the big problem with asynchronous code that doesn’t use a queue to ensure that multiple events are handled correctly and in the correct order. If events and asynchronous code aren’t the way to do the job what is? The simple answer is the polling loop, but putting order into the polling loop so that it is clean and easy to extend is difficult. If you are happy with events and basic polling loops skip the next section, which describes a way to organize polling loops so that they are easier to work with. In chapter but not in this extract
Summary
Raspberry Pi IoT In Python Using GPIO Zero
|
AI Propels Python To Top Language on GitHub 30/10/2024 This year's Octoverse Report reveals how AI is expanding on GitHub and that Python has now overtaken JavaScript as the most popular language on GitHub. The use of Jupyter Notebooks has also surged. |
Edera Releases Open Source Container Benchmark And Scanner 07/11/2024 Edera has released Am I Isolated, an open source container security benchmark that probes users runtime environments and tests for container isolation. |
More News
|
Comments
or email your comment to: comments@i-programmer.info