I am looking for some general guidance as to the correct / best / pythonic approach to writing basic control system programmes in Python. In short, I am looking to use a RasPi to perform basic PLC-style control of hardware.
I have worked with PLCs a lot professionally (Siemens SIMATIC S7 in particular - a bit of programming but mainly debugging during commissioning and digging through the code to aid with hardware fault finding), the RasPi stuff is just a hobby, but I am struggling to stop thinking of programming in a PLC-type way; as in
Read inputs
process / logic
Write outputs
repeat
For a basic example. say I want to use the RasPi to read pushbuttons, and when pressed set outputs high for X seconds.
My immediate instinct is just to run everything in one main loop (with a sleep delay to prevent 100% CPU usage) such as in the following pseudopython (ignoring debouncing, edge detection etc)
while True:
read_GPIO_inputs()
if PB1_pressed:
output1_timer = time_now() + output1_on_time
if output1_timer < time_now():
output1 = True
else:
output1 = False
write_GPIO_outputs()
sleep DELAY_TIME
This approach does of course work, but I feel it is not the "correct" way, especially as the system and programme grows. I'm sure there are much better approaches, using threads, flags, callbacks, interrupts etc, but I'm even struggling to find the correct Google search terms to help me start!
Any advice would be much appreciated, and I hope you can understand what I am trying to ask!