Practical Statecharts in C/C++ Quantum Programming for Embedded Systems phần 1 pps

28 451 0
Practical Statecharts in C/C++ Quantum Programming for Embedded Systems phần 1 pps

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Practical Statecharts in C/C++: Quantum Programming for Embedded Systems Table of Contents Practical Statecharts in C/C++−Quantum Programming for Embedded Systems 1 Preface 3 Mission 3 Why Quantum Programming? 4 QP versus XP and Other Agile Methodologies 5 Audience 6 Guide to Readers 7 Acknowledgments 8 Part I: Statecharts 10 Chapter List 10 Chapter 1: Whirlwind Tour of Quantum Programming 11 Overview 11 1.1 The Ultimate Hook — Anatomy of a GUI Application 11 1.2 A Better Way of Programming — A Calculator That Works 13 1.2.1 Shortcomings of the Traditional Event–Action Paradigm 13 1.2.2 Calculator Statechart 14 1.2.3 Integration with Windows 17 1.2.4 State Handler Methods 18 1.3 Object−Oriented Analogy 20 1.3.1 State Hierarchy and Class Taxonomy 20 1.3.2 Entering/Exiting States and Instantiating/Finalizing Classes 21 1.3.3 Programming−by−Difference 21 1.3.4 Behavioral Inheritance as a Fundamental Meta−Pattern 21 1.3.5 State Patterns 21 1.3.6 Refactoring State Models 22 1.3.7 Beyond Object−Oriented Programming 22 1.4 Quantum Analogy 23 1.5 Summary 23 Chapter 2: A Crash Course in Statecharts 25 Overview 25 2.1 The Essence of Finite State Machines 25 2.1.1 States 26 2.1.2 Extended States 26 2.1.3 Guards 27 2.1.4 Events 27 2.1.5 Actions and Transitions 28 2.1.6 Mealy and Moore Automata 28 2.1.7 Execution Model — Run−to−Completion Step 28 2.1.8 State Transition Diagrams 29 2.2 The Essence of UML Statecharts 30 2.2.1 Hierarchical States 30 2.2.2 Behavioral Inheritance 31 2.2.3 Orthogonal Regions 33 2.2.4 Entry and Exit Actions 34 Practical Statecharts in C/C++: Quantum Programming for Embedded Systems i Table of Contents Chapter 2: A Crash Course in Statecharts 2.2.5 Transition Execution Sequence 35 2.2.6 Internal Transitions 36 2.2.7 Pseudostates 36 2.2.8 Refined Event Handling 37 2.2.9 Semantics versus Notation 37 2.2.10 Statecharts versus Flowcharts 38 2.2.11 Statecharts and Automatic Code Synthesis 38 2.3 Examples of State Models 39 2.3.1 Quantum Calculator 40 2.3.2 Hydrogen Atom 42 2.4 Summary 45 Chapter 3: Standard State Machine Implementations 47 Overview 47 3.1 State Machine Interface 47 3.2 Nested switch Statement 48 3.3 State Table 51 3.4 State Design Pattern 54 3.5 Optimal FSM Implementation 57 3.6 State Machines and C++ Exception Handling 60 3.7 Role of Pointer−to−Member Functions 60 3.8 Implementing Guards, Junctions, and Choice Points 62 3.9 Implementing Entry and Exit Actions 63 3.10 Dealing with State Hierarchy 63 3.11 Summary 64 Chapter 4: Implementing Behavioral Inheritance 66 Overview 66 4.1 Structure 67 4.1.1 Events 69 4.1.2 States 71 4.1.3 Entry/Exit Actions and Initial Transitions 72 4.1.4 State Transitions 73 4.1.5 The top State and the initial Pseudostate 74 4.2 An Annotated Example 75 4.2.1 Enumerating Signals and Subclassing QHsm 76 4.2.2 Defining State Handler Methods 77 4.2.3 Initialization and Dispatching Events 79 4.2.4 Test Run 80 4.3 Heuristics and Idioms 81 4.3.1 Structuring State Machine Code 81 4.3.2 Choosing the Right Signal Granularity 83 4.3.3 UML−Compliant HSMs 83 4.4 The Event Processor 85 4.4.1 Initializing the State Machine: The init() Method 85 4.4.2 Dispatching Events: The dispatch() Method 86 4.4.3 Static and Dynamic State Transitions: Macros Q_TRAN() and Q_TRAN_DYN() 87 Practical Statecharts in C/C++: Quantum Programming for Embedded Systems ii Table of Contents Chapter 4: Implementing Behavioral Inheritance 4.4.4 Dynamic State Transition: The tran() Method 88 4.4.5 Static State Transition: The tranStat() Method and the Tran Class 92 4.4.6 Initializing the QTran Object: The tranSetup() Method 93 4.5 C Implementation 95 4.5.1 QHsm Class in "C+" 96 4.5.2 QHsm Constructor and Destructor 96 4.5.3 State Handler Methods and Pointer−to−Member Functions 97 4.5.4 QHsm Methods 97 4.5.5 Statechart Example in C 98 4.6 Caveats 99 Chapter 5: State Patterns 102 Overview 102 5.1 Ultimate Hook 103 5.1.1 Intent 103 5.1.2 Problem 103 5.1.3 Solution 103 5.1.4 Sample Code 104 5.1.5 Consequences 106 5.2 Reminder 106 5.2.1 Intent 106 5.2.2 Problem 107 5.2.3 Solution 107 5.2.4 Sample Code 107 5.2.5 Consequences 110 5.3 Deferred Event 111 5.3.1 Intent 111 5.3.2 Problem 111 5.3.3 Solution 111 5.3.4 Sample Code 112 5.3.5 Consequences 114 5.4 Orthogonal Component 115 5.4.1 Intent 115 5.4.2 Problem 115 5.4.3 Solution 115 5.4.4 Sample Code 117 5.4.5 Consequences 121 5.5 Transition to History 122 5.5.1 Intent 122 5.5.2 Problem 123 5.5.3 Solution 123 5.5.4 Sample Code 123 5.5.5 Consequences 125 5.6 Summary 126 Practical Statecharts in C/C++: Quantum Programming for Embedded Systems iii Table of Contents Chapter 6: Inheriting State Models 127 Overview 127 6.1 Statechart Refinement Example in C++ 128 6.1.1 How Does It Work? 131 6.2 Statechart Refinement Example in C 134 6.2.1 Preparing a "C+" State Machine for Polymorphism 134 6.2.2 Inheriting and Refining the "C+" State Machine 135 6.3 Caveats 137 6.3.1 Static versus Dynamic State Transitions 137 6.3.2 Multiple Inheritance 137 6.3.3 Heuristics for Inheriting and Refining Statecharts 138 6.4 Summary 140 Part II: Quantum Framework 142 Chapter List 142 Chapter 7: Introducing the Quantum Framework 143 Overview 143 7.1 Conventional Approach to Multithreading 144 7.1.1 Dining Philosophers − Conventional Approach 144 7.1.2 Therac−25 Story 147 7.2 Computing Model of the QF 148 7.2.1 Active Object−Based Multithreading 149 7.2.2 Quantum Analogy 150 7.2.3 Publish−Subscribe Event Delivery 151 7.2.4 General Structure 152 7.2.5 Dining Philosophers Revisited 152 7.3 Roles of the QF 154 7.3.1 Source of Conceptual Integrity 155 7.3.2 RTOS Abstraction Layer 155 7.3.3 Software Bus 156 7.3.4 Platform for Highly Parallel Computing 157 7.3.5 Basis for Automatic Code Generation 158 7.4 Summary 159 Chapter 8: Design of the Quantum Framework 161 Overview 161 8.1 Embedded Real−Time Systems 161 8.2 Handling Errors and Exceptional Conditions 163 8.2.1 Design by Contract 164 8.2.2 State−Based Handling of Exceptional Conditions 166 8.3 Memory Management 168 8.3.1 A Heap of Problems 168 8.3.2 Memory Management in the QF 170 8.4 Mutual Exclusion and Blocking 171 8.4.1 Perils of Mutual Exclusion 172 8.4.2 Blocking in Active Object−Based Systems 174 8.5 Passing Events 175 Practical Statecharts in C/C++: Quantum Programming for Embedded Systems iv Table of Contents Chapter 8: Design of the Quantum Framework 8.5.1 Dynamic Event Allocation 176 8.5.2 Publish−Subscribe Model 178 8.5.3 Multicasting Events 182 8.5.4 Automatic Event Recycling 183 8.6 Active Objects 184 8.6.1 Internal State Machine 185 8.6.2 Event Queue 185 8.6.3 Thread of Execution 186 8.7 Initialization and Cleanup 188 8.7.1 Initializing the Framework 188 8.7.2 Starting the QF Application 189 8.7.3 Gracefully Terminating the QF Application 189 8.8 Time Management 190 8.8.1 QTimer Class 190 8.8.2 Clock Tick, QF::tick() Method 191 8.9 QF API Quick Reference 192 8.9.1 QF Interface 192 8.9.2 QActive Interface 193 8.9.3 QTimer Interface 194 8.10 Summary 195 Chapter 9: Implementations of the Quantum Framework 197 Overview 197 9.1 The QF as a Parnas Family 197 9.2 Code Organization 198 9.2.1 Directory Structure 199 9.2.2 Public Scope Header File 200 9.2.3 Package Scope Header File 201 9.3 Common Elements 202 9.3.1 Critical Section 202 9.3.2 Event Pool 203 9.3.3 Event Queue 205 9.4 DOS: The QF without a Multitasking Kernel 210 9.4.1 Foreground/Background Processing 210 9.4.2 Foreground/Background with the QF 211 9.4.3 DOS−Specific Code 214 9.4.4 Benefits of the QF in a Foreground/Background System 215 9.5 Win32: The QF on the Desktop 216 9.5.1 Critical Section, Event Queue, and Execution Thread 216 9.5.2 Clock Tick 218 9.6 RTKernel−32: The QF with a Preemptive Priority−Based Kernel 219 9.6.1 Critical Section and Event Pool 220 9.6.2 Event Queue and Execution Thread 221 9.6.3 RTKernel−32 Initialization and Clock Tick 223 9.6.4 Cross−Development with RTKernel−32 224 9.7 Summary 225 Practical Statecharts in C/C++: Quantum Programming for Embedded Systems v Table of Contents Chapter 10: Sample Quantum Framework Application 226 Overview 226 10.1 Generating a QF Application 226 10.1.1 Signals and Events 227 10.1.2 Table Active Object 228 10.1.3 Philosopher Active Object 230 10.1.4 Deploying the DPP 232 10.1.5 Notes 232 10.2 Rules for Developing QF Applications 234 10.3 Heuristics for Developing QF Applications 235 10.4 Sizing Event Queues and Event Pools 236 10.4.1 Event Queues 236 10.4.2 Event Pools 238 10.5 System Integration 239 10.6 Summary 239 Chapter 11: Conclusion 241 Overview 241 11.1 Key Elements of QP 241 11.1.1 A Type of Design, Not a Tool 242 11.1.2 A Modeling Aid 242 11.1.3 A Learning Aid 243 11.1.4 A Useful Metaphor 243 11.2 Propositions of QP 244 11.2.1 Quantum Programming Language 245 11.2.2 RTOS of the Future 245 11.2.3 Hardware/Software Codesign 246 11.3 An Invitation 246 Appendix A: "C+" − Object−Oriented Programming in C 248 Overview 248 A.1 Abstraction 249 A.2 Inheritance 251 A.3 Polymorphism 253 A.4 Costs and Overheads 258 A.5 Summary 259 Appendix B: Guide to Notation 261 Overview 261 B.1 Class Diagrams 261 B.2 Statechart Diagrams 262 B.3 Sequence Diagrams 263 B.4 Timing Diagrams 264 Appendix C: CD−ROM 265 Overview 265 C.1 Source Code Structure 266 C.2 Installation 266 Practical Statecharts in C/C++: Quantum Programming for Embedded Systems vi Table of Contents Appendix C: CD−ROM C.2.1 Source Code 266 C.2.2 On Time RTOS−32 Evaluation Kit 266 C.2.3 Adobe Acrobat Reader 266 C.3 Answers to the Exercises 267 C.4 Resources 267 Bibliography 268 Practical Statecharts in C/C++: Quantum Programming for Embedded Systems vii Practical Statecharts in C/C++−Quantum Programming for Embedded Systems Miro Samek, Ph.D. CMP Books Lawrence, Kansas 66046 CMP Books CMP Media LLC 1601 West 23rd Street, Suite 200 Lawrence, Kansas 66046 USA http://www.cmpbooks.com Designations used by companies to distinguish their products are often claimed as trademarks. In all instances where CMP Books is aware of a trademark claim, the product name appears in initial capital letters, in all capital letters, or in accordance with the vendor's capitalization preference. Readers should contact the appropriate companies for more complete information on trademarks and trademark registrations. All trademarks and registered trademarks in this book are the property of their respective holders. Copyright © 2002 CMP Books except where noted otherwise. Published by CMP Books, CMP Media LLC. All rights reserved. No part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher. The programs in this book are presented for instructional value. The programs have been carefully tested, but are not guaranteed for any particular purpose. The publisher does not offer any warranties and does not guarantee the accuracy, adequacy, or completeness of any information herein and is not responsible for any errors or omissions. The publisher assumes no liability for damages resulting from the use of the information in this book or for any infringement of the intellectual property rights of third parties that would result from the use of this information. Acquisitions Editor: Robert Ward Technical Editor: Jeff Claar Editors: Catherine Janzen, Julie McNamee, and Rita Sooby Layout design & production: Justin Fulmer Managing Editor: Michelle O'Neal Cover art design: Rupert Adley and Damien Castaneda Distributed in the U.S. by: Publishers Group West 1700 Fourth Street Berkeley, California 94710 1−800−788−3123 http://www.pgw.com Distributed in Canada by: Jaguar Book Group 100 Armstrong Avenue Georgetown, Ontario M6K 3E7 Canada Practical Statecharts in C/C++−Quantum Programming for Embedded Systems 1 905−877−4483 1−57820−110−1 CMP Books Practical Statecharts in C/C++−Quantum Programming for Embedded Systems 2 [...]... 1. 1: Declaration of the calculator statechart; the unusual indentation of state handler methods (lines 14 –29) indicates state nesting 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 #include #include "qf_win32.h" struct CalcEvt : public QEvent { int keyId; }; // include the Quantum Framework (QF) // ID of the key depressed class Calc... environment within which a state machine can execute and needs only minor customizations for this particular application The main Windows procedure, WinMain(), performs only basic initializations and then invokes the dialog procedure int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, PSTR cmdLine, int iCmdShow) { InitCommonControls(); // load common controls library locHinst = hInst; // store instance... jargon into something actually readable Reviewing their corrections was for me the best possible lesson in lucid writing I also thank Justin Fulmer, for polishing my drawings and integrating them into the final version of the book I am indebted to Jeff Claar, for the technical review of the manuscript and for scrutinizing the accompanying code Jeff's contributions include the complete C++ state machine... section) 1 QSTATE Calc::calc(QEvent const *e) { 1. 2.4 State Handler Methods 18 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 switch (e−>sig) { case Q_ENTRY_SIG: dispState("calc"); case Q_INIT_SIG: clear(); Q_INIT(&Calc::ready); case IDC_C: clear(); Q_TRAN(&Calc::calc); case TERMINATE: Q_TRAN(&Calc::final);... Course in Statecharts Chapter 3: Standard State Machine Implementations Chapter 4: Implementing Behavioral Inheritance Chapter 5: State Patterns Chapter 6: Inheriting State Models State machines are a superb formalism for specifying and implementing event−driven systems that must react to incoming events in a timely fashion The UML statecharts represent the current state of the art in state machine theory... as state handler methods in lines 14 through 29 The unusual use of indentation indicates state nesting 1. 2.3 Integration with Windows For simplicity, this example uses the raw Win32 API rather than a higher level wrapper like MFC The calculator GUI is a dialog box, so it declares friendship with the corresponding Windows dialog procedure (Listing 1. 1, lines 44–45) Because Windows is an event−driven... statechart started in opEntered and ended in begin Actually, the statechart could have been in any of the other 11 substates of calc (refer to Exercise 1. 3) and would still end up in begin The classical flat state machine would require specifying each of the 11 transitions explicitly The statechart allows reusing one transition 11 times The gain is not only the drastic reduction in the sheer number... user interfaces (graphical and otherwise), real−time systems (e.g., computer games), or embedded systems In Part II (Chapters 7 11 ), I describe the Quantum Framework, which is a software architecture designed specifically for embedded real−time systems Surveys of programmers[2] consistently indicate that C and C++ clearly dominate embedded systems programming The vast majority (some 80 percent) of embedded. .. can start using immediately without investing in sophisticated tools [1] The concept of the windows, icons, menus, and pointing (WIMP) interface was first publicly displayed by Doug Englebart and his team from the Stanford Research Institute at the Western Joint Computer Conference in 19 68 [Englebart+ 68] 1. 1 The Ultimate Hook — Anatomy of a GUI Application The early GUI designers faced a formidable... that statecharts have been taught as a high−level, visual language, mandating the use of sophisticated computer−aided software engineering (CASE) tools, rather than as a type of design This has created many misconceptions in the industry and has resulted in a lack of practical advice on how to code statecharts in mainstream programming languages such as C or C++ Chapter 1: Whirlwind Tour of Quantum Programming . Event 11 1 5.3 .1 Intent 11 1 5.3.2 Problem 11 1 5.3.3 Solution 11 1 5.3.4 Sample Code 11 2 5.3.5 Consequences 11 4 5.4 Orthogonal Component 11 5 5.4 .1 Intent 11 5 5.4.2 Problem 11 5 5.4.3 Solution 11 5 5.4.4. Tool 242 11 .1. 2 A Modeling Aid 242 11 .1. 3 A Learning Aid 243 11 .1. 4 A Useful Metaphor 243 11 .2 Propositions of QP 244 11 .2 .1 Quantum Programming Language 245 11 .2.2 RTOS of the Future 245 11 .2.3. Canada Practical Statecharts in C/C++ Quantum Programming for Embedded Systems 1 905−877−4483 1 57820 11 0 1 CMP Books Practical Statecharts in C/C++ Quantum Programming for Embedded Systems 2 Preface What

Ngày đăng: 12/08/2014, 21:21

Mục lục

  • Table of Contents

  • Practical Statecharts in C/C++-Quantum Programming for Embedded Systems

  • Preface

    • Mission

    • Why Quantum Programming?

    • QP versus XP and Other Agile Methodologies

    • Audience

    • Guide to Readers

    • Acknowledgments

    • Part I: Statecharts

      • Chapter List

      • Chapter 1: Whirlwind Tour of Quantum Programming

        • Overview

        • 1.1 The Ultimate Hook Š Anatomy of a GUI Application

        • 1.2 A Better Way of Programming Š A Calculator That Works

          • 1.2.1 Shortcomings of the Traditional EventŒAction Paradigm

          • 1.2.2 Calculator Statechart

          • 1.2.3 Integration with Windows

          • 1.2.4 State Handler Methods

          • 1.3 Object-Oriented Analogy

            • 1.3.1 State Hierarchy and Class Taxonomy

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan