1- # Example of how the all_states function can be used to make things happen in parallel
2- # with the state set of a task. The states cycle the red, green and yellow LEDs on
3- # when the usr pushbutton on the pyboard is pressed. In parallel the blue LED is
4- # flashed on and off using the all_states function and timer events.
5-
6- # Does not require any hardware except a micropython board.
7-
8- from pyControl .utility import *
9- from devices import *
10-
11- # Define hardware (normally done in seperate hardware definition file).
12-
13- pyboard_button = Digital_input ('X17' , falling_event = 'button_press' , pull = 'up' ) # USR button on pyboard.
14-
15- blue_LED = Digital_output ('B4' )
16- red_LED = Digital_output ('A13' )
17- green_LED = Digital_output ('A14' )
18- yellow_LED = Digital_output ('A15' )
19-
20- # States and events.
21-
22- states = ['red_on' ,
23- 'green_on' ,
24- 'yellow_on' ]
25-
26- events = ['button_press' ,
27- 'blue_on' ,
28- 'blue_off' ]
29-
30- initial_state = 'red_on'
31-
32- # Run start behaviour.
33-
34- def run_start ():
35- # Turn on blue LED and set timer to turn it off in 1 second.
36- blue_LED .on ()
37- set_timer ('blue_off' , 1 * second , output_event = True )
38-
39- # State behaviour functions.
40-
41- def red_on (event ):
42- # Red LED on, button press transitions to green_on state.
43- if event == 'entry' :
44- red_LED .on ()
45- elif event == 'exit' :
46- red_LED .off ()
47- elif event == 'button_press' :
48- goto_state ('green_on' )
49-
50- def green_on (event ):
51- # Green LED on, button press transitions to yellow_on state.
52- if event == 'entry' :
53- green_LED .on ()
54- elif event == 'exit' :
55- green_LED .off ()
56- elif event == 'button_press' :
57- goto_state ('yellow_on' )
58-
59- def yellow_on (event ):
60- # Yellow LED on, button press transitions to red_on state.
61- if event == 'entry' :
62- yellow_LED .on ()
63- elif event == 'exit' :
64- yellow_LED .off ()
65- elif event == 'button_press' :
66- goto_state ('red_on' )
67-
68- # State independent behaviour.
69-
70- def all_states (event ):
71- # Turn blue LED on and off when the corrsponding timer trigger, set timer for next blue on/off.
72- if event == 'blue_on' :
73- blue_LED .on ()
74- set_timer ('blue_off' , 1 * second , output_event = True )
75- elif event == 'blue_off' :
76- blue_LED .off ()
77- set_timer ('blue_on' , 1 * second , output_event = True )
78-
79- # Run end behaviour.
80-
81- def run_end ():
82- # Turn off LEDs at end of run.
83- for LED in [blue_LED , red_LED , green_LED , yellow_LED ]:
1+ # Example of how the all_states function can be used to make things happen in parallel
2+ # with the state set of a task. The states cycle the red, green and yellow LEDs on
3+ # when the usr pushbutton on the pyboard is pressed. In parallel the blue LED is
4+ # flashed on and off using the all_states function and timer events.
5+
6+ # Does not require any hardware except a micropython board.
7+
8+ from pyControl .utility import *
9+ from devices import *
10+
11+ # Define hardware (normally done in seperate hardware definition file).
12+
13+ pyboard_button = Digital_input ('X17' , falling_event = 'button_press' , pull = 'up' ) # USR button on pyboard.
14+
15+ blue_LED = Digital_output ('B4' )
16+ red_LED = Digital_output ('A13' )
17+ green_LED = Digital_output ('A14' )
18+ yellow_LED = Digital_output ('A15' )
19+
20+ # States and events.
21+
22+ states = ['red_on' ,
23+ 'green_on' ,
24+ 'yellow_on' ]
25+
26+ events = ['button_press' ,
27+ 'blue_on' ,
28+ 'blue_off' ]
29+
30+ initial_state = 'red_on'
31+
32+ # Run start behaviour.
33+
34+ def run_start ():
35+ # Turn on blue LED and set timer to turn it off in 1 second.
36+ blue_LED .on ()
37+ set_timer ('blue_off' , 1 * second , output_event = True )
38+
39+ # State behaviour functions.
40+
41+ def red_on (event ):
42+ # Red LED on, button press transitions to green_on state.
43+ if event == 'entry' :
44+ red_LED .on ()
45+ elif event == 'exit' :
46+ red_LED .off ()
47+ elif event == 'button_press' :
48+ goto_state ('green_on' )
49+
50+ def green_on (event ):
51+ # Green LED on, button press transitions to yellow_on state.
52+ if event == 'entry' :
53+ green_LED .on ()
54+ elif event == 'exit' :
55+ green_LED .off ()
56+ elif event == 'button_press' :
57+ goto_state ('yellow_on' )
58+
59+ def yellow_on (event ):
60+ # Yellow LED on, button press transitions to red_on state.
61+ if event == 'entry' :
62+ yellow_LED .on ()
63+ elif event == 'exit' :
64+ yellow_LED .off ()
65+ elif event == 'button_press' :
66+ goto_state ('red_on' )
67+
68+ # State independent behaviour.
69+
70+ def all_states (event ):
71+ # Turn blue LED on and off when the corrsponding timer trigger, set timer for next blue on/off.
72+ if event == 'blue_on' :
73+ blue_LED .on ()
74+ set_timer ('blue_off' , 1 * second , output_event = True )
75+ elif event == 'blue_off' :
76+ blue_LED .off ()
77+ set_timer ('blue_on' , 1 * second , output_event = True )
78+
79+ # Run end behaviour.
80+
81+ def run_end ():
82+ # Turn off LEDs at end of run.
83+ for LED in [blue_LED , red_LED , green_LED , yellow_LED ]:
8484 LED .off ()
0 commit comments