Download Untitled
Transcript
use the Enqueue Element At Opposite End function to add a state to the front of the buffer for immediate execution. This allows the application to respond immediately to high-priority actions or events, such as executing the Shutdown state when the user quits the application. A template for the Queued State Machine design pattern is provided in Figure 8-12. Figure 8-12. A template for the Queued State Machine design pattern, which maintains multiple states in a buffer. [View full size image] The queue is implemented as follows: The Obtain Queue and Enqueue Element functions initiate the queue on the left of the While Loop. The enumerated type definition control terminal is wired to the Obtain Queue function's element data type terminal. This specifies the data type of the queue. Enumerated constants are created from the enumerated type definition and wired to the element terminals of the Enqueue Element functions. Initialize and Idle are the first two items added to the queue. These are the first two states executed by the state machine. The Dequeue Element function is placed within the No Error case of an Error Case Structure outside of the main Case structure. If no error is present in the error cluster, the next state is removed from the Queue and passed to the main Case structure's selector terminal. If an error is present, the error is reported using the General Error Handler VI and the Shutdown state executes. Within each case of the Case structure, additional states are added to the queue using the Enqueue Element function. A variation of the Queued State Machine uses a queue element data type consisting of a cluster that contains the enumerated type definition bundled together with a variant. The enumeration contains the desired state for the case selector as normal. The variant is used to pass data from one state to another, using the queue functions instead of shift registers. For example, in Figure 8-13, the Acquire Waveform state acquires a waveform, converts it to variant, combines it with the state enumerated constant with Update GUI selected, and adds the cluster to the queue. In a subsequent loop iteration, the Dequeue Element function removes the cluster and unbundles the State and Data items. Update GUI state converts the variant back to a waveform and updates the waveform indicator. The variant data type maximizes the flexibility of the data that is passed between states. This approach involves fewer wires than the shift register method of data sharing. However, the data is stored only temporarily in the queue and is not accessible in subsequent states after dequeuing. By comparison, shift registers maintain and share the data among all states and use wires and dataflow. Therefore, shift registers are generally preferred over queues for data sharing between the states of a state machine. 260