|
1 | | -Using Finite State Machine |
2 | | --------------------------- |
3 | | - |
4 | 1 | [API Reference](../../api-reference/thing/state-machine.md) |
5 | 2 |
|
6 | | -Often, certain operations are not allowed during certain conditions, for example, |
7 | | -one cannot turn ON a motor twice in a row, or one does not wish to change the |
| 3 | +Often, certain operations are not allowed during certain conditions, for example, |
| 4 | +one cannot turn ON a motor twice in a row, or one does not wish to change the |
8 | 5 | exposure of a camera during video capture (say). |
9 | 6 |
|
10 | | -To implement these contraints, a state machine may be used to prevent property writes or |
11 | | -action invokations in certain states (events are not supported). A `StateMachine` is a class-level |
12 | | -attribute which accepts a finite list of states and the allowed properties and actions |
| 7 | +To implement these contraints, a state machine may be used to prevent property writes or |
| 8 | +action invokations in certain states (events are not supported). A `StateMachine` is a class-level |
| 9 | +attribute which accepts a finite list of states and the allowed properties and actions |
13 | 10 | in these states: |
14 | 11 |
|
15 | 12 | ```py title="Definition" linenums="1" |
16 | | ---8<-- "docs/howto/code/fsm/def.py:1:1" |
17 | | ---8<-- "docs/howto/code/fsm/def.py:12:15" |
18 | | ---8<-- "docs/howto/code/fsm/def.py:34:36" |
19 | | ---8<-- "docs/howto/code/fsm/def.py:40:41" |
20 | | -``` |
21 | | - |
22 | | -Specify the machine conditions as keyword arguments to the `state_machine` with properties and actions |
| 13 | +--8<-- "docs/beginners-guide/code/fsm/def.py:1:1" |
| 14 | +--8<-- "docs/beginners-guide/code/fsm/def.py:12:15" |
| 15 | +--8<-- "docs/beginners-guide/code/fsm/def.py:34:36" |
| 16 | +--8<-- "docs/beginners-guide/code/fsm/def.py:40:41" |
| 17 | +``` |
| 18 | + |
| 19 | +Specify the machine conditions as keyword arguments to the `state_machine` with properties and actions |
23 | 20 | in a list: |
24 | 21 |
|
25 | 22 | ```py title="Specify Properties and Actions" linenums="1" |
26 | | ---8<-- "docs/howto/code/fsm/def.py:1:2" |
27 | | ---8<-- "docs/howto/code/fsm/def.py:12:41" |
| 23 | +--8<-- "docs/beginners-guide/code/fsm/def.py:1:2" |
| 24 | +--8<-- "docs/beginners-guide/code/fsm/def.py:12:41" |
28 | 25 | ``` |
29 | 26 |
|
30 | 27 | As expected, one needs to set the `StateMachine` state to indicate state changes: |
31 | 28 |
|
32 | 29 | ```py title="set_state()" linenums="1" |
33 | | ---8<-- "docs/howto/code/fsm/def.py:13:15" |
34 | | ---8<-- "docs/howto/code/fsm/def.py:54:63" |
| 30 | +--8<-- "docs/beginners-guide/code/fsm/def.py:13:15" |
| 31 | +--8<-- "docs/beginners-guide/code/fsm/def.py:54:63" |
35 | 32 | ``` |
36 | 33 |
|
37 | 34 | One can also sepcify the allowed state of a property or action directly |
38 | 35 | on the corresponding objects: |
39 | 36 |
|
40 | 37 | ```py title="Specify State Alternate" linenums="1" |
41 | | ---8<-- "docs/howto/code/fsm/def.py:13:15" |
42 | | ---8<-- "docs/howto/code/fsm/def.py:64:" |
| 38 | +--8<-- "docs/beginners-guide/code/fsm/def.py:13:15" |
| 39 | +--8<-- "docs/beginners-guide/code/fsm/def.py:64:" |
43 | 40 | ``` |
44 | 41 |
|
45 | 42 | State machines also push state change event when the state changes: |
46 | 43 |
|
47 | 44 | ```py title="Definition" linenums="1" |
| 45 | + |
48 | 46 | ``` |
49 | 47 |
|
50 | | -One can suppress state change events by setting ``push_state_change_event=False``. |
| 48 | +One can suppress state change events by setting `push_state_change_event=False`. |
51 | 49 |
|
52 | | -Lastly, one can also supply callbacks which are executed when entering and exiting certain states, |
| 50 | +Lastly, one can also supply callbacks which are executed when entering and exiting certain states, |
53 | 51 | irrespective of where or when the state change occured: |
54 | 52 |
|
55 | 53 | ```py title="Definition" linenums="1" |
56 | | -``` |
| 54 | + |
| 55 | +``` |
0 commit comments