Oct-21-2021, 03:47 AM
(This post was last modified: Oct-21-2021, 03:47 AM by deanhystad.)
Based on these assumptions:
input 5 starts the test
input 6 determines if the test fails or passes
You could do something like this:
Unknown: Current state is not known
NotReady: Waiting for badge number to be entered
Ready: Badge number entered and waiting for test button
Testing: Performing a test
Completed: Test is finished
We transition from one state to another when events occur. The transitions are (Transiton: Event):
Unknown->NotReady: Badge number is not entered
Unknown->Ready: Badge number is entered
NotReady->Ready: Badge number is entered
Ready->NotReady: Badge number is not entered
Ready->Testing: Test button is pressed
Testing->Completed: Test finished and 4 seconds passed
Completed->Unknown: Automatic
When we transition to a new state we perform some actions. The actions are:
Ready: Update status label
NotReady: Update status label
Testing: Run test. Record results. Update status label. Schedule transition to Unknown after 4 seconds
Since actions are only performed when the state changes you don't have to worry about recording the same results multiple times for one test or writing the test results over the Test Ready message. Most of the time your software will be do nothing but wait for an event.
input 5 starts the test
input 6 determines if the test fails or passes
You could do something like this:
import enum class TestState(enum.Enum): '''States for testing state machine''' Unknown = 0, # Figure out what the new state should be NotReady = 1, # Need to enter badge number before testing Ready = 2, # Badge entered. Waiting for test button press Testing = 3, # Running test. Completed = 4 # Test completed. Wait for test button release class App(Frame): def __init__(self,master=None): ... self.tester = '' self.time = '' self.set_state(TestState.Unknown) self.update_clock() def set_state(self, new_state): '''Testing state machine. See states above''' if new_state == TestState.NotReady: # Report we are not ready for testing self.label.configure('Test Not Ready') if new_state == TestState.Ready: # Report we are ready for testing self.label.configure('Test Ready') if new_state == TestState.Testing: # Perform the test and report results result = ('FAILED', 'PASSED)[GPIO.input(6)] self.label.configure(f'blah blah blah Test {result} blah blah') with open(self.test_file, "a") as f: # open file f.write(f"Badge#{self.teste}, Test {result} ON: {self.time}n') # Erase test results after 4 seconds self.after(4000, lambda: self.set_state(TestState.Completed)) self.state = new_state def update_clock(self): '''Runs periodically''' ... self.tester = self.entry_widget.get() self.time = some date-time string ... if self.state == TestState.Unknown: if len(self.tester) == 0: self.set_state(TestState.NotReady) else: self.set_state(TestState.Ready) if self.state == TestState.NotReady: # Waiting for tester to enter badge number if len(self.tester) > 0: self.set_state(TestState.Ready) if self.state == TestState.Ready: # Waiting for test button. Can also go to not-ready if if len(self.tester) == 0: self.set_state(TestState.NotReady) elif GPIO.input(5) == 1: self.set_state(TestState.Testing) if self.state == TestState.Completed: # Begin new test sequence when test button is released if GPIO.input(5) == 0: self.set_state(TestState.Unknown) # Update frequently to be responsive to button press self.after(100, lambda: self.update_clock)This implements a "state machine" that has the states:
Unknown: Current state is not known
NotReady: Waiting for badge number to be entered
Ready: Badge number entered and waiting for test button
Testing: Performing a test
Completed: Test is finished
We transition from one state to another when events occur. The transitions are (Transiton: Event):
Unknown->NotReady: Badge number is not entered
Unknown->Ready: Badge number is entered
NotReady->Ready: Badge number is entered
Ready->NotReady: Badge number is not entered
Ready->Testing: Test button is pressed
Testing->Completed: Test finished and 4 seconds passed
Completed->Unknown: Automatic
When we transition to a new state we perform some actions. The actions are:
Ready: Update status label
NotReady: Update status label
Testing: Run test. Record results. Update status label. Schedule transition to Unknown after 4 seconds
Since actions are only performed when the state changes you don't have to worry about recording the same results multiple times for one test or writing the test results over the Test Ready message. Most of the time your software will be do nothing but wait for an event.