CYAN YELLOW SPOT MATTE MAGENTA BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS® Companion eBook Available iPhone Accessory Development: The Next Frontier. Building iPhone OS Accessories provides a hands-on look at Apple’s External B Accessory Framework and the iPhone Accessories API, which has now An in-depth examination of Apple’s External Accessory u been extended to the iPad, as well. You’ll learn how to create new, integrated i solutions that combine iPhone apps with dedicated hardware. ld Framework and the iPhone Accessory API. i The iPhone OS Accessories API expands the opportunities for innovative n iPhone developers, allowing you to control and monitor external devices, g whether you’ve built them yourself or obtained them from a third party. i • Develop accessories and apps for the iPhone and iPod touch P • Use Apple’s External Accessory Framework to create hardware h and software interaction • Control and monitor external devices using the iPhone o Accessories API n • Use the specific controller classes within the iPhone OS SDK • e Build and control your own accessory • Learn about all the costs associated with becoming an accessory O developer before committing This book is for iPhone, iPod touch, and iPad developers who want to write S apps to control external accessories and hardware developers who want to create accessories for iPhone OS devices. This includes embedded systems A programmers who have been left out of the iPhone gold rush until now. The iPhone OS Accessories EAAccessory expands the market of developers c Building yet again, and this book is for all those developers wanting to use these c new APIs to control and monitor external devices—whether self-built or ac- quired from other sources. e iPhone OS s s o r Accessories i e s Use the iPhone Accessories API to Control and Monitor Devices M a s k r COMPANION eBOOK SEE LAST PAGE FOR DETAILS ON $10 eBOOK VERSION ISBN 978-1-4302-2931-5 ey Ken Maskrey 55999 US $59.99 Shelve in Mobile Development SOURCE CODE ONLINE User level: www.apress.com Intermediate-Advanced 9 781430 229315 this print for content only—size & color not accurate Trim: 7.5 x 9.25 spine = 0.75" 328 page count Building iPhone OS Accessories Use the iPhone Accessories API to Control and Monitor Devices ■ ■ ■ Ken Maskrey i Building iPhone OS Accessories. Use the iPhone Accessories API to Control and Monitor Devices Copyright © 2010 by Ken Maskrey All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2931-5 ISBN-13 (electronic): 978-1-4302-2932-2 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. President and Publisher: Paul Manning Lead Editors: Clay Andres, Tom Welsh Technical Reviewer: Dean Kaplan Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Mary Tobin Copy Editor: Katie Stence Compositor: MacPS, LLC Indexer: Julie Grady Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders- [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. ii To JB, for helping me make it through this endeavor. iii Contents at a Glance ■Contents at a Glance.......................................................................................iv ■Contents..........................................................................................................v ■About the Author............................................................................................ix ■About the Technical Reviewer.........................................................................x ■Preface...........................................................................................................xi Part I: What is an Accessory?.............................................................................1(cid:1) ■Chapter 1: Accessory Overview.......................................................................3(cid:1) ■Chapter 2: EAAccessory Framework.............................................................21(cid:1) ■Chapter 3: EA Framework Design Patterns...................................................47(cid:1) Part II: Project Walkthrough.............................................................................77(cid:1) ■Chapter 4: The Game Controller....................................................................79(cid:1) ■Chapter 5: Specifying a Pong Game............................................................103(cid:1) ■Chapter 6: Coding a Pong Game..................................................................119(cid:1) ■Chapter 7: Adding the Game Controller.......................................................141(cid:1) ■Chapter 8: Improving the Design.................................................................165(cid:1) ■Chapter 9: Apple Developer Programs........................................................179(cid:1) Part III: Designing an Accessory ....................................................................191(cid:1) ■Chapter 10: Hardware Design......................................................................193(cid:1) ■Chapter 11: Firmware..................................................................................221(cid:1) ■Chapter 12: Everything Else........................................................................241(cid:1) ■Appendix......................................................................................................259 ■Index............................................................................................................305 iv Contents ■Contents at a Glance.......................................................................................iv(cid:1) ■Contents..........................................................................................................v(cid:1) ■About the Author............................................................................................ix(cid:1) ■About the Technical Reviewer.........................................................................x(cid:1) ■Preface...........................................................................................................xi(cid:1) Part I: What is an Accessory?.............................................................................1(cid:1) ■Chapter 1: Accessory Overview.......................................................................3(cid:1) Being Different........................................................................................................................................................3(cid:1) Accessories.............................................................................................................................................................4(cid:1) Uses of Accessories................................................................................................................................................4(cid:1) Accessories: A View From Above............................................................................................................................6(cid:1) Credit Card Reader................................................................................................................................................11(cid:1) But I’m a Programmer...........................................................................................................................................14(cid:1) The Software Approach........................................................................................................................................15(cid:1) The Pong Game.....................................................................................................................................................16(cid:1) Embedded Systems..............................................................................................................................................17(cid:1) Building an Accessory...........................................................................................................................................19(cid:1) Summary..............................................................................................................................................................19(cid:1) ■Chapter 2: EAAccessory Framework.............................................................21(cid:1) Frameworks..........................................................................................................................................................21(cid:1) Streams................................................................................................................................................................22(cid:1) Protocols...............................................................................................................................................................23(cid:1) More on Protocols.................................................................................................................................................26(cid:1) The EA Framework................................................................................................................................................32(cid:1) EAAccessory Class Properties.........................................................................................................................32(cid:1) EAAccessory Class Constants..........................................................................................................................38(cid:1) EAAccessoryManager Class.............................................................................................................................38(cid:1) EASession........................................................................................................................................................41(cid:1) Summary..............................................................................................................................................................45 v ■ CONTENTS ■Chapter 3: EA Framework Design Patterns...................................................47(cid:1) Object-Oriented Programming..............................................................................................................................47(cid:1) The Basics of OOP............................................................................................................................................49(cid:1) The UIApplication Class...................................................................................................................................54(cid:1) Delegation.............................................................................................................................................................57(cid:1) OOP and Delegation..............................................................................................................................................61(cid:1) Model-View-Controller..........................................................................................................................................64(cid:1) Notification............................................................................................................................................................66(cid:1) EA Pattern.............................................................................................................................................................72(cid:1) Summary..............................................................................................................................................................73(cid:1) Part II: Project Walkthrough.............................................................................77(cid:1) ■Chapter 4: The Game Controller....................................................................79(cid:1) The Griffin iTrip.....................................................................................................................................................79(cid:1) iTrip Dissected.................................................................................................................................................82(cid:1) Game Controller Design........................................................................................................................................84(cid:1) The Game Controller’s Functions.....................................................................................................................85(cid:1) Electrical and Physical Design..............................................................................................................................88(cid:1) Test Software........................................................................................................................................................90(cid:1) Summary............................................................................................................................................................101(cid:1) ■Chapter 5: Specifying a Pong Game............................................................103(cid:1) Keeping It Simple................................................................................................................................................103(cid:1) Specification.......................................................................................................................................................103(cid:1) Page One—Drawings....................................................................................................................................104(cid:1) Developing Your Requirements......................................................................................................................112(cid:1) Summary............................................................................................................................................................116(cid:1) ■Chapter 6: Coding a Pong Game..................................................................119(cid:1) Architecture........................................................................................................................................................119(cid:1) The Application Life Cycle..............................................................................................................................119(cid:1) Pong Game Architecture................................................................................................................................126(cid:1) The Application Delegate...............................................................................................................................128(cid:1) The View Controller........................................................................................................................................128(cid:1) The Nib...........................................................................................................................................................130(cid:1) Summary............................................................................................................................................................138(cid:1) ■Chapter 7: Adding the Game Controller.......................................................141(cid:1) The Game Controller Test Program.....................................................................................................................141(cid:1) The Game Controller Object................................................................................................................................145(cid:1) The Game Controller Interface.......................................................................................................................145(cid:1) The Game Controller Implementation............................................................................................................146(cid:1) View Controller Modifications........................................................................................................................156(cid:1) Summary............................................................................................................................................................163(cid:1) ■Chapter 8: Improving the Design.................................................................165(cid:1) Performance Concerns.......................................................................................................................................165(cid:1) Security Concerns...............................................................................................................................................168(cid:1) The Payment Card Industry............................................................................................................................169(cid:1) Code Changes................................................................................................................................................174(cid:1) Summary............................................................................................................................................................177(cid:1) vi ■ CONTENTS ■Chapter 9: Apple Developer Programs........................................................179(cid:1) Mac Developer....................................................................................................................................................179(cid:1) iPhone Developer................................................................................................................................................180(cid:1) Apple Developer Connection...............................................................................................................................182(cid:1) Made For iPod/Works With iPhone......................................................................................................................183(cid:1) Running a Business............................................................................................................................................183(cid:1) Summary............................................................................................................................................................190(cid:1) Part III: Designing an Accessory ....................................................................191(cid:1) ■Chapter 10: Hardware Design......................................................................193(cid:1) Getting Started....................................................................................................................................................193(cid:1) Design Process...................................................................................................................................................196(cid:1) Parts (Processor) Selection............................................................................................................................197(cid:1) Prototyping.....................................................................................................................................................203(cid:1) Circuit Design.................................................................................................................................................205(cid:1) PCB Design....................................................................................................................................................210(cid:1) Fabrication.....................................................................................................................................................213(cid:1) Integration and Testing..................................................................................................................................215(cid:1) Summary............................................................................................................................................................219(cid:1) ■Chapter 11: Firmware..................................................................................221(cid:1) I’m a PC...............................................................................................................................................................221(cid:1) Compilers............................................................................................................................................................222(cid:1) Programming......................................................................................................................................................223(cid:1) Debugging...........................................................................................................................................................227(cid:1) Your PCB.............................................................................................................................................................231(cid:1) Structure.............................................................................................................................................................234(cid:1) Chip Configuration..............................................................................................................................................238(cid:1) Summary............................................................................................................................................................239(cid:1) ■Chapter 12: Everything Else........................................................................241(cid:1) Tools and Test Equipment..................................................................................................................................241(cid:1) Tools..............................................................................................................................................................242(cid:1) Test Equipment..............................................................................................................................................247(cid:1) Mechanical Design..............................................................................................................................................249(cid:1) The Alternative to Custom Mechanical Design..............................................................................................252(cid:1) Packaging...........................................................................................................................................................253(cid:1) Business Processes............................................................................................................................................255(cid:1) Summary............................................................................................................................................................257(cid:1) Appendix.........................................................................................................259(cid:1) PongViewController.h interface listing................................................................................................................259(cid:1) PongViewController.m implementation listing....................................................................................................261(cid:1) GameController.h................................................................................................................................................268(cid:1) GameController.m...............................................................................................................................................269(cid:1) pongViewController.h..........................................................................................................................................273(cid:1) pongViewController.m.........................................................................................................................................275(cid:1) pongAppDelegate.h.............................................................................................................................................285(cid:1) pongAppDelegate.m............................................................................................................................................286(cid:1) GameController.h................................................................................................................................................286(cid:1) vii ■ CONTENTS GameController.m...............................................................................................................................................287(cid:1) pongViewController.h..........................................................................................................................................291(cid:1) ■Index............................................................................................................305 viii
Description: