8
CONTENTS IN DETAIL ACKNOWLEDGMENTS xvii INTRODUCTION xix Who Is This Book For? xix What’s in This Book? xx Part I: Warming Up xx Part II: Simulating Life xx Part III: Fun with Images xx Part IV: Enter 3D xx Part V: Hardware Hacking xxi Why Python? xxi Python Versions xxii The Code in This Book xxii PART I: WARMING UP 1 1 PARSING ITUNES PLAYLISTS 3 Anatomy of the iTunes Playlist File 4 Requirements 5 The Code 5 Finding Duplicates 6 Extracting Duplicates 7 Finding Tracks Common Across Multiple Playlists 7 Collecting Statistics 8 Plotting Your Data 9 Command Line Options 10 The Complete Code 11 Running the Program 14 Summary 15 Experiments! 15 2 SPIROGRAPHS 17 Parametric Equations 18 Spirograph Equations 19 Turtle Graphics 22 Requirements 23 The Code 23 The Spiro Constructor 23 The Setup Functions 24 The restart() Method 24 Python Playground: Geeky Projects for the Curious Programmer © 2015 Mahesh Venkitachalam

acknowledgments xvii IntroductIon xix - No Starch … · Contents in detail acknowledgments xvii IntroductIon xix Who Is This Book For

  • Upload
    lebao

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

Page 1: acknowledgments xvii IntroductIon xix - No Starch … · Contents in detail acknowledgments xvii IntroductIon xix Who Is This Book For

C o n t e n t s i n d e t a i l

acknowledgments xvii

IntroductIon xixWho Is This Book For? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xixWhat’s in This Book? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx

Part I: Warming Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxPart II: Simulating Life . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxPart III: Fun with Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxPart IV: Enter 3D . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxPart V: Hardware Hacking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

Why Python? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiPython Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiiThe Code in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxii

Part I: warmIng uP 1

1 ParsIng Itunes PlaylIsts 3Anatomy of the iTunes Playlist File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Finding Duplicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Extracting Duplicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7Finding Tracks Common Across Multiple Playlists . . . . . . . . . . . . . . . . . . . . . . 7Collecting Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8Plotting Your Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9Command Line Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

The Complete Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Running the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15Experiments! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

2 sPIrograPhs 17Parametric Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

Spirograph Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Turtle Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

The Spiro Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23The Setup Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24The restart() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

Python Playground: Geeky Projects for the Curious Programmer © 2015 Mahesh Venkitachalam

Page 2: acknowledgments xvii IntroductIon xix - No Starch … · Contents in detail acknowledgments xvii IntroductIon xix Who Is This Book For

x Contents in Detail

The draw() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25Creating the Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25The SpiroAnimator Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26The genRandomParams() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27Restarting the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28The update() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28Showing or Hiding the Cursor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29Saving the Curves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29Parsing Command Line Arguments and Initialization . . . . . . . . . . . . . . . . . . . 30

The Complete Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Running the Spirograph Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37Experiments! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

Part II: sImulatIng lIfe 39

3 conway’s game of lIfe 41How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

Representing the Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44Initial Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45Boundary Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46Implementing the Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47Sending Command Line Arguments to the Program . . . . . . . . . . . . . . . . . . . . 47Initializing the Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

The Complete Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49Running the Game of Life Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52Experiments! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

4 generatIng musIcal overtones wIth the karPlus-strong algorIthm 55How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

The Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57Creating WAV Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59The Minor Pentatonic Scale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

Implementing the Ring Buffer with deque . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Implementing the Karplus-Strong Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . 62Writing a WAV File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62Playing WAV Files with pygame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63The main() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64

Python Playground: Geeky Projects for the Curious Programmer © 2015 Mahesh Venkitachalam

Page 3: acknowledgments xvii IntroductIon xix - No Starch … · Contents in detail acknowledgments xvii IntroductIon xix Who Is This Book For

Contents in Detail xi

The Complete Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65Running the Plucked String Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69Experiments! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

5 BoIds: sImulatIng a flock 71How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

Computing the Position and Velocities of the Boids . . . . . . . . . . . . . . . . . . . . 73Setting Boundary Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74Drawing a Boid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75Applying the Rules of the Boids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77Adding a Boid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79Scattering the Boids . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80Command Line Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80The Boids Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

The Complete Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82Running the Boids Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85Experiments! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

Part III: fun wIth Images 87

6 ascII art 89How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

Defining the Grayscale Levels and Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92Computing the Average Brightness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93Generating the ASCII Content from the Image . . . . . . . . . . . . . . . . . . . . . . . 93Command Line Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95Writing the ASCII Art Strings to a Text File . . . . . . . . . . . . . . . . . . . . . . . . . . 95

The Complete Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95Running the ASCII Art Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99Experiments! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

7 PhotomosaIcs 101How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

Splitting the Target Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103Averaging Color Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103Matching Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

Python Playground: Geeky Projects for the Curious Programmer © 2015 Mahesh Venkitachalam

Page 4: acknowledgments xvii IntroductIon xix - No Starch … · Contents in detail acknowledgments xvii IntroductIon xix Who Is This Book For

xii Contents in Detail

Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104

Reading in the Tile Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104Calculating the Average Color Value of the Input Images . . . . . . . . . . . . . . . 105Splitting the Target Image into a Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106Finding the Best Match for a Tile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106Creating an Image Grid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107Creating the Photomosaic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108Adding the Command Line Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110Controlling the Size of the Photomosaic . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

The Complete Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110Running the Photomosaic Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116Experiments! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

8 autostereograms 117How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

Perceiving Depth in an Autostereogram . . . . . . . . . . . . . . . . . . . . . . . . . . . 118Depth Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

Repeating a Given Tile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121Creating a Tile from Random Circles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122Creating Autostereograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123Command Line Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

The Complete Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125Running the Autostereogram Generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129Experiments! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129

Part Iv: enter 3d 131

9 understandIng oPengl 133Old-School OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134Modern OpenGL: The 3D Graphics Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136

Geometric Primitives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1373D Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137Shaders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139Vertex Buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141Texture Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141Displaying OpenGL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142

Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142

Python Playground: Geeky Projects for the Curious Programmer © 2015 Mahesh Venkitachalam

Page 5: acknowledgments xvii IntroductIon xix - No Starch … · Contents in detail acknowledgments xvii IntroductIon xix Who Is This Book For

Contents in Detail xiii

The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142Creating an OpenGL Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142Setting Callbacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143The Scene Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

The Complete Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151Running the OpenGL Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157Experiments! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

10 PartIcle systems 159How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161

Modeling the Motion of a Particle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162Setting a Maximum Spread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162Rendering the Particles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163Using OpenGL Blending to Create More Realistic Sparks . . . . . . . . . . . . . . . 164Using Billboarding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165Animating the Sparks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166

Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166The Code for the Particle System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166

Defining the Particle Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167Defining the Time-Lag Array for the Particles . . . . . . . . . . . . . . . . . . . . . . . . 168Setting the Initial Particle Velocities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168Creating the Vertex Shader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169Creating the Fragment Shader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171Rendering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172The Camera Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

The Complete Particle System Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174The Box Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180The Code for the Main Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

Updating the Particles at Each Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184The Keyboard Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185Managing the Main Program Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

The Complete Main Program Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186Running the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189Experiments! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189

11 volume renderIng 191How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192

Data Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193Generating Rays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193Displaying the OpenGL Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196

Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196An Overview of the Project Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196Generating a 3D Texture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197

Python Playground: Geeky Projects for the Curious Programmer © 2015 Mahesh Venkitachalam

Page 6: acknowledgments xvii IntroductIon xix - No Starch … · Contents in detail acknowledgments xvii IntroductIon xix Who Is This Book For

xiv Contents in Detail

The Complete 3D Texture Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199Generating Rays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200

Defining the Color Cube Geometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201Creating the Frame Buffer Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203Rendering the Back-Faces of the Cube . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204Rendering the Front-Faces of the Cube . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205Rendering the Whole Cube . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205The Resize Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206

The Complete Ray Generation Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206Volume Ray Casting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212

The Vertex Shader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214The Fragment Shader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214

The Complete Volume Ray Casting Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2162D Slicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220

The Vertex Shader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222The Fragment Shader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223A User Interface for 2D Slicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223

The Complete 2D Slicing Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224Putting the Code Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227The Complete Main File Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228Running the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231Experiments! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232

Part v: hardware hackIng 233

12 IntroductIon to the arduIno 235The Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236The Arduino Ecosystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237

Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238Community . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238Peripherals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239

Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239Building the Light-Sensing Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239

How the Circuit Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240The Arduino Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240Creating the Real-Time Graph . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

The Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242The Complete Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244Running the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247Experiments! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247

Python Playground: Geeky Projects for the Curious Programmer © 2015 Mahesh Venkitachalam

Page 7: acknowledgments xvii IntroductIon xix - No Starch … · Contents in detail acknowledgments xvii IntroductIon xix Who Is This Book For

Contents in Detail xv

13 laser audIo dIsPlay 249Generating Patterns with a Laser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250

Motor Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251The Fast Fourier Transform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252

Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254Constructing the Laser Display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254Wiring the Motor Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256

The Arduino Sketch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258Configuring the Arduino’s Digital Output Pins . . . . . . . . . . . . . . . . . . . . . . . 258The Main Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259Stopping the Motors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261

The Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261Selecting the Audio Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261Reading Data from the Input Device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262Computing the FFT of the Data Stream . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263Extracting Frequency Information from the FFT Values . . . . . . . . . . . . . . . . . 263Converting Frequency to Motor Speed and Direction . . . . . . . . . . . . . . . . . . 263Testing the Motor Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264Command Line Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265Manual Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266

The Complete Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267Running the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271Experiments! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271

14 a rasPBerry PI–Based weather monItor 273The Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274

The DHT11 Temperature and Humidity Sensor . . . . . . . . . . . . . . . . . . . . . . 274The Raspberry Pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275Setting Up Your Pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276

Installing and Configuring Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277The Operating System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277Initial Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277Wi-Fi Setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277Setting Up the Programming Environment . . . . . . . . . . . . . . . . . . . . . . . . . . 278Connecting via SSH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279The Bottle Web Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280Plotting with flot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281Shutting Down the Pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282

Building the Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283The Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284

Handling Sensor Data Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285Plotting the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285The update() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288

Python Playground: Geeky Projects for the Curious Programmer © 2015 Mahesh Venkitachalam

Page 8: acknowledgments xvii IntroductIon xix - No Starch … · Contents in detail acknowledgments xvii IntroductIon xix Who Is This Book For

xvi Contents in Detail

The JavaScript Handler for the LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289Adding Interactivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290

The Complete Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290Running the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295Experiments! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295

a software InstallatIon 297Installing Source Code for the Book’s Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297Installing on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298

Installing GLFW . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298Installing Prebuilt Binaries for Each Module . . . . . . . . . . . . . . . . . . . . . . . . 298Other Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299

Installing on OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299Installing Xcode and MacPorts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300Installing Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300

Installing on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301

B BasIc PractIcal electronIcs 303Common Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304Essential Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306Building Circuits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307Going Further . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310

c rasPBerry PI tIPs and trIcks 311Setting Up Wi-Fi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311Checking Whether Your Pi Is Connected . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312Preventing the Wi-Fi Adapter from Going to Sleep . . . . . . . . . . . . . . . . . . . . . . . . . . 312Backing Up Your Code and Data from the Pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313Backing Up Your Entire Pi OS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314Logging In to Your Pi with SSH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314Using the Raspberry Pi Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315Enabling Sound on Your Pi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315Making Your Pi Talk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316Making HDMI Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316Making Your Pi Mobile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317Checking Your Raspberry Pi Hardware Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317

Index 319

Python Playground: Geeky Projects for the Curious Programmer © 2015 Mahesh Venkitachalam