ebook img

Python 3: Programming and GUIs for Electronic Engineers PDF

226 Pages·2017·13.718 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Python 3: Programming and GUIs for Electronic Engineers

175 × 235 SPINE: 11.4 FLAPS: 0 P FOR ELECTRONIC ENGINEERS FOR ELECTRONICS ENGINEERS Y T PYTHON 3 H PYTHON 3 O N PROGRAMMING AND GUIS 3 Andrew Pratt - PROGRAMMING AND GUIS P R O This is the second edition of a book aimed at engineers, G scientists and hobbyists who want to interface PCs with R hardware projects using graphical user interfaces. Desktop A and web based applications are covered. M M The programming language used is Python 3 which is I one of the most popular languages around: speed of N programming being a key feature. The book has been G revised and updated with emphasis on getting the user to produce practical designs with ease - a text editor is all A Andrew Pratt served for N that is required to produce Python programs. 25 years in the Royal D Air Force as an Aircraft Hardware interfacing is achieved using an Arduino Uno as G Technician. a remote slave. A full description and source code of the U communication interface is given in the book. The slave I He holds a Higher S provides digital and analogue input and outputs. Multiple National Certificate in Unos can be included in one project with all control code • electrical and electronic written in Python and running on a PC A engineering and a n Degree from the Open One project involves a PIC microcontroller with code d University. provided that can be loaded into the PIC using the Uno. r e The web applications and server are all implemented in He continued his career w Python allowing you to access your electronic hardware working in industrial over the Internet. The Raspberry Pi computer can be used P controls. Currently as your web server. r he is an instructor a t teaching industrial An introductory chapter is provided to get you started with t control systems. using Linux. The book is written for use with Debian or variations including Mint or Ubuntu. All of the programs in the book are freely available, ready to use and experiment with by way of a download from Elektor. ISBN 978-1-907920-61-5 L E A R N Elektor International Media BV www.elektor.com DE Andrew Pratt S IG N LEARN DESIGN SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SSHHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESARIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SEHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • SHARE • LEARN • DESIGN • Python 3: Programming and GUIs for Electronic Engineers ● Andrew Pratt an Elektor Publication LEARN DESIGN SHARE ● This is an Elektor Publication. Elektor is the media brand of Elektor International Media B.V. 78 York Street London W1H 1DP, UK Phone: (+44) (0)20 7692 8344 © Elektor International Media BV 2017 First published in the United Kingdom 2017 169043-1/EN ● All rights reserved. No part of this book may be reproduced in any material form, including photocopying, or storing in any medium by electronic means and whether or not transiently or incidentally to some other use of this publication, without the written permission of the copyright holder except in accordance with the provisions of the Copyright, Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licensing Agency Ltd, 90 Tottenham Court Road, London, England W1P 9HE. Applications for the copyright holder’s written permission to reproduce any part of this publication should be addressed to the publishers. The publishers have used their best efforts in ensuring the correctness of the information contained in this book. They do not assume, and hereby disclaim, any liability to any party for any loss or damage caused by errors or omissions in this book, whether such errors or omissions result from negligence, accident or any other cause. ● British Library Cataloguing in Publication Data Catalogue record for this book is available from the British Library ● ISBN 978-1-907920-61-5 Prepress production: DMC ¦ daverid.com Printed in the Netherlands by Wilco Elektor is part of EIM, the world’s leading source of essential technical information and electronics products for pro engineers, electronics designers, and the companies seeking to engage them. Each day, our international team develops and delivers high-quality content - via a variety of media channels (e.g., magazines, video, digital media, and social media) in several languages - relating to electronics design and DIY electronics. www.elektor.com LEARN DESIGN SHARE Table of Contents Table of Contents Preface to the Second Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Chapter 1 Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.1 Introduction..................................................13 1.2 Getting the Operating System .....................................13 1.3 Finding Your Way Round .........................................13 1.4 File and Directory Handling .......................................14 1.5 Wild Cards...................................................17 1.6 Redirection ..................................................18 1.7 Working with the Python Programs..................................18 1.8 File Permissions ...............................................18 1.9 Bash Scripts .................................................19 1.10 The X-Server ................................................20 Chapter 2 Getting Started with Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.1 Introduction..................................................21 2.2 Installing Python 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22 2.3 Start Programming.............................................22 2.4 IDLE.......................................................24 2.5 Interactive Sessions ............................................24 2.6 Data Types ..................................................24 2.6.1 Integers ................................................24 2.6.2 Floats ..................................................25 2.6.3 Strings and Writing To and Reading from Files ......................26 2.6.4 Lists ...................................................36 2.6.5 Dictionaries ..............................................40 2.6.6 Tuples..................................................42 2.7 Functions....................................................42 2.8 Imports.....................................................44 2.9 Logic.......................................................46 2.10 Bit and Bytes................................................46 2.11 Comparison Operators..........................................48 ● 5 Python 3: Programming and GUIs for Electronic Engineers 2.12 Hexadecimal Notation..........................................49 2.13 Persistence of Objects..........................................50 2.14 Compiled Python Files..........................................51 2.15 Unicode Strings ..............................................51 2.16 Bytes and Bytearrays ..........................................54 2.17 Encoding and Decoding Bytes and Unicode ...........................56 2.18 Variable names...............................................57 Chapter 3 Graphic User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 3.1 Introduction..................................................59 3.2 Tkinter .....................................................59 3.2.1 Click to Start Programs......................................60 3.3 Adding Widgets ...............................................61 3.4 Callbacks....................................................61 3.5 Grid Manager Options...........................................62 3.5.1 Simple Layout ............................................62 3.5.2 Nested Layout ............................................64 3.6 Inputting and Outputting Data with Widgets ...........................65 3.6.1 Scale Widget and Label......................................66 3.6.2 Entry...................................................67 3.6.3 Control Variables ..........................................68 3.6.4 Check Buttons ............................................68 3.6.5 Radio Buttons.............................................69 3.6.6 The Canvas Widget.........................................70 Chapter 4 Object Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 4.1 Introduction..................................................73 4.2 Class.......................................................73 4.3 Inheritance ..................................................75 4.4 Summary of Object Orientation (OO) ................................79 Chapter 5 The Arduino Uno as a Slave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 5.1 Introduction..................................................81 5.2 The Master Slave Design.........................................81 5.2.1 USB Serial...............................................81 ● 6 Table of Contents 5.2.2 Uploading the Arduino Program ................................81 5.2.3 Configuring the Arduino with its Address and Project ID ...............82 5.3 The Uno Class ................................................84 5.3.1 Creating Instances of Slaves ..................................85 5.3.2 Writing to Digital Output Pins..................................85 5.3.3 Reading from Digital Input Pins ................................86 5.3.4 Writing to Analogue Output Pins................................87 5.3.5 Reading from Analogue Input Pins ..............................87 5.3.6 Summary of the IO Functions..................................87 5.4 Practical Examples Using Hardware .................................88 5.4.1 Write to Digital Outputs......................................88 5.4.2 Write to Analogue Outputs....................................91 5.4.3 Read from Digital Inputs .....................................92 5.4.4 Read from Analogue Inputs ...................................93 5.5 The Slave State Diagram and the Communications Protocol.................95 5.5.1 The Slave State Diagram.....................................95 5.5.2 Control of Traffic...........................................96 5.5.3 The Telegram Formats.......................................97 5.6 The Python End of the Communications Protocol ........................99 Chapter 6 Further Examples of GUIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 6.1 Introduction.................................................101 6.2 A Graph Plotting Class..........................................101 6.2.1 Passing Arguments to a Function without Specifying How Many (*args) ...102 6.2.2 Passing Arguments to a Function with Keyword Arguments ............102 6.2.3 Binding Events...........................................103 6.2.4 The Module __name__ .....................................103 6.2.5 Following the Code ........................................104 6.3 CR Charge Discharge Experiments .................................109 6.4 Plotting Transistor Characteristics..................................111 6.4.1 Threading ..............................................116 Chapter 7 Bit Map Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 7.1 Introduction.................................................119 ● 7 Python 3: Programming and GUIs for Electronic Engineers 7.2 The Bit Map File..............................................119 7.3 Reading and Writing to Binary Files.................................121 7.4 The Image..................................................122 7.5 The Modified Image ...........................................124 7.6 The Header .................................................125 7.6.1 The Struct Module.........................................126 7.7 Junk Bytes..................................................128 7.8 Class BmpDraw ..............................................128 Chapter 8 A Project to Monitor Your Electricity Consumption . . . . . . . . . . . . . . . . . 131 8.1 Introduction.................................................131 8.2 The Probe and Remote Sensing Head ...............................131 8.3 The PIC Microcontroller.........................................133 8.3.1 The Arduino Slave as a PIC Programmer .........................134 8.4 The Radio Link and the Arduino Slave...............................135 8.5 Commissioning the System and the Desktop Program. ...................135 8.5.1 The Desktop Calibration Program ..............................136 Chapter 9 Web Browser Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 9.1 Introduction.................................................145 9.2 HTML 5....................................................145 9.3 CherryPy a Python Web Framework ................................146 9.3.1 User Input using Web Forms .................................148 9.4 The Ammeter and Current Trend Web Applications......................150 9.4.1 The Uno Slave Program.....................................150 9.4.2 The CherryPy Program .....................................151 9.5 The Raspberry Pi as a Web Server .................................157 9.5.1 Configuring Wi-Fi from the Command Line on the Pi.................158 9.5.2 Running the Python Programs Automatically After Boot. ..............159 9.6 Access from the Internet........................................160 9.7 Adapting this Project...........................................161 Chapter 10 Curses Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 10.1 Introduction................................................163 10.2 Getting Started with Curses.....................................163 ● 8 Table of Contents 10.2.1 The Wrapper Function .....................................164 10.2.2 Windows on the stdscr.....................................164 10.2.3 Colour................................................165 10.3 Custom Widgets.............................................169 10.3.1 An Indicator Lamp .......................................169 10.3.2 An Analogue Gauge Widget .................................172 10.3.3 Combining Attributes with the Bitwise OR Operator.................173 10.3.4 Mouse Events for User Input ................................173 10.3.5 A Thumbwheel Widget.....................................174 10.3.6 A Button Widget.........................................178 10.3.7 A Label Widget..........................................179 10.4 The Module with all the Classes ..................................180 Appendix A Arduino Uno Slave Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Appendix B The Python Uno Slave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 B.1 Pydoc .....................................................199 Appendix C The Arduino Slave Programming Tool . . . . . . . . . . . . . . . . . . . . . . . . . 201 Appendix D The Arduino Slave ID Setting Tool Source Code . . . . . . . . . . . . . . . . . 203 Appendix E The PIC Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Appendix F The PIC Programming Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 F.1 12F1622_PROGRAMMER_V1.0.py ..................................209 Appendix G The Curses Widgets Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 G.1 Curses_widgets.py ...........................................215 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 ● 9

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.