Nov-10-2022, 09:50 PM
(This post was last modified: Nov-11-2022, 05:05 AM by deanhystad.)
You call this each time do_POST() runs.
Since the message is structured text, why not pull the information out of the message? The message format is C(index)P(group)MON where index is a digit from 1-8 and group is G or V. It is easy to write a regex that not only verifies the message but extracts the index and group information.
setupGPIO()And that sets all the outputs high.
Since the message is structured text, why not pull the information out of the message? The message format is C(index)P(group)MON where index is a digit from 1-8 and group is G or V. It is easy to write a regex that not only verifies the message but extracts the index and group information.
import re def do_POST(message): try: pin, group = re.match(r'C([1-8])P(G|V)MON', message).groups() print(group, pin) except: print(message, "is invalid") while True: do_POST(input("Enter Message: "))
Output:>>> python test.py
Enter Message: arfarf
arfarf is invalid
Enter Message: C7PGMON
G 7
Enter Message: C3PVMON
V 3
Knowing the group and index, it is easy to map this to a particular output.import RPi_GPIO as GPIO import re # Define the LED groups. led_groups = { "G":(4, 5, 6, 7, 8, 9, 10, 11), "V":(12, 13, 14, 15, 16, 17, 18, 19) } # Process a message def do_POST(message): try: pin, group = re.match(r'C([1-8])P(G|V)MON', message).groups() group = led_groups[group] pin = group[int(pin)-1] for p in group: print(p, "=", GPIO.LOW if p == pin else GPIO.HIGH) except: print(message, "is invalid") while True: do_POST(input("Enter Message: "))
Output:>>> python test.py
Enter Message: C7PVMON
12 = 1
13 = 1
14 = 1
15 = 1
16 = 1
17 = 1
18 = 0
19 = 1
Enter Message:
And since there are lists of outputs, may as well use those to initialize the GPIO outputs.import RPi_GPIO as GPIO import re # Define the LED groups. led_groups = { "G":(4, 5, 6, 7, 8, 9, 10, 11), "V":(12, 13, 14, 15, 16, 17, 18, 19) } # Initalize outputs used to turn LED's on/off def setupGPIO(): GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) for group in led_groups.values(): for pin in group: GPIO.setup(pin, GPIO.OUT, initial=GPIO.HIGH) # Process a message def do_POST(message): try: pin, group = re.match(r'C([1-8])P(G|V)MON', message).groups() group = led_groups[group] pin = group[int(pin)-1] for p in group: GPIO.output(p, GPIO.LOW if p == pin else GPIO.HIGH) except: print(message, "is invalid") if __name__ == '__main__': setupGPIO() # This setup code belongs down here, not in do_post while True: do_POST(input("Enter Message: "))