application design patterns in labview ™ kevin hogan staff software engineer - labview
TRANSCRIPT
![Page 1: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/1.jpg)
Application Design Patternsin LabVIEW™
Kevin HoganStaff Software Engineer - LabVIEW
![Page 2: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/2.jpg)
What Are Design Patterns?
• “…simple and elegant solutions to specific problems in … software design. [They] capture solutions that have developed and evolved over time…”– Design Patterns, Gamma, Helm, Johnson, Vlissides
![Page 3: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/3.jpg)
Why Use Design Patterns?
• They represent “tried and true” solutions• They free you from “reinventing the wheel”• They make it easier for others to read and modify your code
![Page 4: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/4.jpg)
UI Event Loop
• Used to handle user interaction, e.g.– User-initiated control value changes– Mouse moves and clicks– Keyboard events
• Many advantages over polling:– Less processor load– No lost events
![Page 5: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/5.jpg)
Standard Event Loop
![Page 6: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/6.jpg)
Standard Event Loop Caveats
• Keep event handling code short and quick– You can lock your UI if you take too long!– Combine this pattern with Producer/Consumer if you need to
• Terminating the Event Loop:– Compute the loop end condition in your event handling code
(best for stand-alone loop)– Use a sequence to ensure that the loop-end condition
computes after the event handler fires, or– Include a timeout event
![Page 7: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/7.jpg)
State Machine
• Used to implement decision-making algorithms, e.g. :– Diagnostic routines– Process monitoring and control
• Appropriate for algorithms described by flow chart or state diagram
![Page 8: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/8.jpg)
Example State Diagram
Controller for Buck Rogers’ Laser Cannon
![Page 9: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/9.jpg)
Standard State Machine
![Page 10: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/10.jpg)
Transition Code Options
![Page 11: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/11.jpg)
State Machine Demo
• StateMachineExample.vi
![Page 12: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/12.jpg)
Master / Slave
• Used to decouple processes that should run at different rates (asynchronously)– e. g. responding to user interface controls and
collecting data simultaneously
![Page 13: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/13.jpg)
Master / Slave – The Big Picture
![Page 14: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/14.jpg)
Master / Slave Synchronization
![Page 15: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/15.jpg)
Master / Slave Example
• SynchLoops.vi
![Page 16: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/16.jpg)
Master / Slave – Key Elements
• Consists of parallel loops– Writer loop can communicate with reader via
locals, globals, occurrences, notifiers, queues• Writer loops
– It is best for only one loop to write to any given local or global variable
– If both loops must write to a variable, use a semaphore to prevent race conditions
![Page 17: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/17.jpg)
Producer / Consumer
• Used to decouple processes that produce and consume data at different rates
• Consists of parallel loops– One loop writes data to a queue while the other reads
![Page 18: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/18.jpg)
Producer / Consumer Big Picture
![Page 19: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/19.jpg)
Producer / Consumer Example
• ProduceConsume.vi
![Page 20: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/20.jpg)
Queued Message Handler
• Often handles events generated by the user interface, but not limited to this.
• Useful when the system handles input events with a well-defined sequence of actions
• Similar to the Event Loop – but allows finer control over message order.
![Page 21: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/21.jpg)
Standard Queued Message Handler
![Page 22: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/22.jpg)
Queued Message Handler Demo
• QueuedMessageHandler.vi
![Page 23: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/23.jpg)
Queued Message Handler – Key Points
• Terminate the loop by checking the latest message, not by polling a control
• You may generate new messages when handling a message – but be careful!
![Page 24: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/24.jpg)
“Daemon”
• Used to create and launch applications that run invisibly in the background, e.g.– Auto-save utility– Periodic back-up service– Garbage collection of temporary files
• Typically perform low-priority monitoring and/or maintenance services
BSD Daemon Copyright 1988 by Marshall Kirk McKusick. All Rights Reserved.
![Page 25: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/25.jpg)
Standard Self-launching Daemon
• Key point – a Daemon must keep an open reference to itself to keep from being purged
![Page 26: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/26.jpg)
Standard Launched Daemon
• Key points – launcher must transfer responsibility for reference to daemon VI.
• Launcher must not close reference to daemon VI
![Page 27: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/27.jpg)
Launched Daemon Example
• LaunchSneezy.vi
![Page 28: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/28.jpg)
Proxy
• Used to defer load-time cost of infrequently called sub-VIs
• Used to hide details of remote communication
![Page 29: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/29.jpg)
Standard Proxy
• Stores reference to real VI• Loads the VI only on first call
![Page 30: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/30.jpg)
Splash Screen (Launcher)
• Used to replace the LabVIEW load dialog with a custom dialog
• Displays application name, version, and load progress as large application is loaded
• Closes itself after application starts running
![Page 31: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/31.jpg)
Standard Launcher
![Page 32: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/32.jpg)
Patterns Wrap-up
• UI Event Loop• State machine• Master/slave• Producer/consumer• Queued message handler• Daemon• Proxy• Splash screen launcher
![Page 33: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/33.jpg)
When Should You Use Them?
• Whenever you can find one that fits your problem• Do not assume that the patterns presented here are the only solutions available in LabVIEW!
![Page 34: Application Design Patterns in LabVIEW ™ Kevin Hogan Staff Software Engineer - LabVIEW](https://reader035.vdocuments.us/reader035/viewer/2022062308/56649d145503460f949e815e/html5/thumbnails/34.jpg)
Credits
• The LabVIEW team, especially Kennon Cotton, Rob Dye, Greg McKaskle, Doug Norman, Greg Richardson, and Joel Sumner
• LabVIEW Technical Resource