Deocument - 961full Download Book Programming The Raspberry Pi Third Edition Getting Started With Python PDF
Deocument - 961full Download Book Programming The Raspberry Pi Third Edition Getting Started With Python PDF
Deocument - 961full Download Book Programming The Raspberry Pi Third Edition Getting Started With Python PDF
ISBN: 978-1-26-425736-2
MHID: 1-26-425736-8
The material in this eBook also appears in the print version of this title:
ISBN: 978-1-26-425735-5, MHID: 1-26-425735-X.
All trademarks are trademarks of their respective owners. Rather than put a
trademark symbol after every occurrence of a trademarked name, we use
names in an editorial fashion only, and to the benefit of the trademark owner,
with no intention of infringement of the trademark. Where such designations
appear in this book, they have been printed with initial caps.
TERMS OF USE
This is a copyrighted work and McGraw-Hill Education and its licensors
reserve all rights in and to the work. Use of this work is subject to these
terms. Except as permitted under the Copyright Act of 1976 and the right to
store and retrieve one copy of the work, you may not decompile, disassemble,
reverse engineer, reproduce, modify, create derivative works based upon,
transmit, distribute, disseminate, sell, publish or sublicense the work or any
part of it without McGraw-Hill Education’s prior consent. You may use the
work for your own noncommercial and personal use; any other use of the
work is strictly prohibited. Your right to use the work may be terminated if
you fail to comply with these terms.
2 Getting Started
3 Python Basics
8 Games Programming
9 Interfacing Hardware
12 Raspberry Pi Robot
13 What Next
Index
CONTENTS
Preface
Acknowledgments
Introduction
1 Introduction
What Is the Raspberry Pi?
What Can You Do with a Raspberry Pi?
A Tour of the Raspberry Pi
Setting Up Your Raspberry Pi
Buying What You Need
Connecting Everything Together
Booting Up
Summary
2 Getting Started
Linux
The Desktop
The Command Line
Navigating with the Terminal
sudo
Applications
Internet Resources
Summary
3 Python Basics
Mu
Python Versions
Python Shell
Editor
Numbers
Variables
For Loops
Simulating Dice
If
Comparisons
Being Logical
Else
While
The Python Shell from the Terminal
Summary
8 Games Programming
What Is Pygame?
Coordinates
Hello Pygame
A Raspberry Game
Following the Mouse
One Raspberry
Catch Detection and Scoring
Timing
Lots of Raspberries
Summary
9 Interfacing Hardware
GPIO Pin Connections
Pin Functions
Serial Interface Pins
Power Pins
Hat Pins
Breadboarding with Jumper Wires
Digital Outputs
Step 1. Put the Resistor on the Breadboard
Step 2. Put the LED on the Breadboard
Step 3. Connect the Breadboard to the GPIO Pins
Analog Outputs
Digital Inputs
Analog Inputs
Hardware
The Software
HATs
Summary
12 Raspberry Pi Robot
Set Up the Raspberry Pi Zero W
Web-Controlled Rover
What You Need
Hardware
Software
Autonomous Rover
What You Need
Hardware
Software
Summary
13 What Next
Linux Resources
Python Resources
Raspberry Pi Resources
Programming Languages
Scratch
C
Other Languages
Applications and Projects
Media Center (Kodi)
Home Automation
Summary
Index
PREFACE
The Raspberry Pi™ is rapidly becoming a worldwide phenomenon. People
are waking up to the possibility of a $35 (U.S.) computer that can be put to
use in all sorts of settings—from a desktop workstation to a media center to a
controller for a home automation system.
This book explains in simple terms, to both nonprogrammers and
programmers new to the Raspberry Pi, how to start writing programs for the
Pi in the popular Python programming language. It then goes on to give you
the basics of creating graphical user interfaces and simple games using the
pygame module.
The software in the book uses Python 3, and the Mu editor. The Raspberry
Pi OS distribution recommended by the Raspberry Pi Foundation is used
throughout the book.
The book starts with an introduction to the Raspberry Pi and covers the
topics of buying the necessary accessories and setting everything up. You
then get an introduction to programming while you gradually work your way
through the next few chapters. Concepts are illustrated with sample
applications that will get you started programming your Raspberry Pi.
Four chapters are devoted to programming and using the Raspberry Pi’s
GPIO connector, which allows the device to be attached to external
electronics. These chapters include three sample projects—a LED lighting
controller, a LED clock, and a Raspberry Pi–controlled robot, complete with
ultrasonic rangefinder.
Here are the key topics covered in the book:
All the code listings in the book are available for download from the
book’s repository on Github at https://github.com/simonmonk/prog_pi_ed3,
where you can also find other useful material relating to the book, including
errata.
Simon Monk
ACKNOWLEDGMENTS
As always, I thank Linda for her patience and support.
At TAB/McGraw Hill, my thanks go out to my editor Lara Zoble, and I
also thank Jyoti Shaw of MPS Limited. As always, it was a pleasure working
with such a great team.
INTRODUCTION
Since the first Raspberry Pi™ model B revision 1 was released in 2012, there
have been a number of upgrades to the original hardware. The Raspberry Pi 4
has increased the processing power and memory of the Raspberry Pi and the
Pi Zero provides a very low cost option, while the Raspberry Pi 400 is
actually built into a keyboard. These new versions of the Raspberry Pi have
been largely compatible with the original device, but there are a few changes
to both the hardware and the standard Raspberry Pi OS distribution that
warrant a new edition of this book.
In particular, I have changed all the user interface code from Tkinter to the
much easier to use guizero and I have also changed the code examples that
used RPi.GPIO to gpiozero.
The Raspberry Pi™ went on general sale at the end of February 2012 and
immediately crashed the websites of the suppliers chosen to take orders for it.
Since then a number of new models culminating in the Raspberry Pi 4 (at
the time of writing) have been released. So what was so special about this
little device and why has it created so much interest?
November 6th.
The last three days have been days of curious disorder and
anarchy. I will try and note briefly what has been taking place.
The day after Bauman’s funeral (November 3rd) was the
anniversary of the Emperor’s accession, and all the “hooligans” of the
city, who are now called the “Black Gang,” used the opportunity to
make counter demonstrations under the ægis of the National flag.
The students did nothing, they were in no way aggressive; but the
hooligans when they came across a student beat him and in many
cases killed him. The police did nothing; they seemed to have
disappeared. These hooligans paraded the town in small groups,
sometimes uniting, blocking the traffic, demanding money from
well-dressed people, killing students, and making themselves
generally objectionable. When the police were appealed to they
shrugged their shoulders and said: “Liberty.” The hooligans
demanded the release of the man who had killed Bauman. “They
have set free so many of their men,” they said, referring to the
revolutionaries, “we want our man set free.” Practically the town was
in a state of anarchy; anybody could kill any one else with impunity.
In one of the biggest streets a hooligan came up to a man and asked
him for money; he gave him ten kopecks. “Is that all?” said the
hooligan, “Take that,” and he killed him with a Finnish knife. I was
myself stopped by a band on the Twerskaia and asked politely to
contribute to their fund, the fund of the “Black Gang,” which I did
with considerable alacrity. However, students, or those whom they
considered to be students in disguise, were the people they mostly
attacked. The citizens of the town in general soon began to think that
this state of things was intolerable, and vigorous representations
were made to the town Duma that some steps should be taken to put
an end to it. The hooligans broke the windows of the Hôtel
Métropole and those of several shops. There was a general
impression among them that liberty meant doing as much damage as
they pleased. This state of things lasted three days, and now it has
been stopped—utterly and completely stopped. A notice is published
forbidding all demonstrations in the streets with flags. The police
have reappeared, and everything has resumed its normal course.
These bands of hooligans were small and exceedingly easy to deal
with. The disorders were completely unnecessary. But they have
done some good in one way; they have brought home to everybody
the necessity for order and the maintenance of order, and the
conviction that if the police are removed the result will be anarchy.
However, considering the state of lawlessness that existed, it is
remarkable how little damage was done.
The night before last, as I was walking back to the hotel after
dinner, I met two omnibuses full of wounded students being driven
to the hospital. One student was torn to pieces by a hooligan crowd
yesterday afternoon; and a friend of mine from the windows of the
Hôtel National saw another trampled to death.
The general tenour of opinion among the Liberals is that the
Government must prove their good intentions by deeds and not by
words, and that soothing Manifestoes are of no use now. There are
three points on which they insist. First, they demand that the partial
amnesty should be made complete; that there should be an amnesty
for all political offenders without exception. Secondly, they say that
they have no guarantee for the new reforms, because the Ministers
are taken from the Bureaucracy. They demand new Ministers taken
from the Zemstva. Thirdly, they demand the resignation of General
Trepoff. With regard to the first point it would be a wise step on the
part of the Government to satisfy the Liberals. The concessions will
probably be made gradually, and I suppose their maxim is not to give
too much at once or the demands will increase. It is a pity, if the
Government have decided to give in on this point, that they did not
do so at once. Every concession seems, as it is, to have been extorted
by force. That is the general impression of the Liberals: “We have
been given nothing; everything we have obtained we have extorted
by force.” With regard to the resignation of General Trepoff, if I were
a Russian and guarantees were given that the police were no longer
going to interfere with individuals, and that the secret police force
was to be abolished—and I believe this is being done—I should make
a demonstration for the retention of General Trepoff. If there are to
be police at all to keep order, somebody must manage them. The
Russian police are a hopelessly incompetent body, and General
Trepoff is a thoroughly competent man. He may have used too heavy
a hand sometimes, and I can understand the people being indignant
that he should rule Russia like a dictator. On the other hand he
knows how to keep order; he knows how to manage the police, and if
the liberty of individuals is respected—and, so far, since the
publication of the Manifesto it has been—I cannot see why any one
should desire his retirement. However, the discussion of this point is
futile. General Trepoff has become a symbol, like General Galliffet in
France after the Commune. To the ordinary Russian Liberal he
represents the Bureaucracy and all the evils of the old régime, and
nothing can change that impression. His position is probably
untenable, and he will be forced to resign, though, as far as liberty is
concerned, at present the people do exactly what they like in the way
of political meetings, the newspapers write what they please, and the
prisons at Moscow and St. Petersburg have been partially emptied of
their political prisoners. The Russians have, in fact, got the two
boons which Matthew Arnold said were so dearly cherished by the
English middle class—liberty to make fools of themselves, and
publicity to show the world how they are doing it. The extreme
revolutionary party wish to do away with the Emperor altogether and
to have a Republic. If this is done some people say there will be a civil
war in every town and village in the Russian Empire. As it is, half the
people do not understand what a Constitution means. A soldier, for
instance, asked whether it meant that the Emperor had resigned; and
some of the educated class understand it still less.
Altogether the Liberals seem to me to be in too great a hurry;
nevertheless things are apparently calming down. The question is—
Will Count Witte between now and then succeed in thoroughly
gaining the confidence of the Liberals and of the representatives of
the Zemtsva? If he succeeds all will be well. The extreme
Conservative Party, represented by the Moskovski Viedomosti, is
really of no practical account. Its point of view was admirably
represented by the hall porter of one of the old and conservative
families at Moscow, who on the day of the publication of the
Manifesto said: “The Emperor has deserted us.”
CHAPTER V
ST. PETERSBURG BEFORE THE SECOND
STRIKE
I arrived here this morning from Moscow. The chief piece of news
which is being discussed is the dismissal of Trepoff. Of Trepoff one
always hears contradictory accounts: some people saying he is an
inveterate reactionary, a Jew-baiter, &c.; others that he is a man of
great common sense, liberal in policy as far as this is compatible with
common sense, and never afraid of speaking his mind. I heard from
people who ought to know that he was strongly in favour of the
granting of the Manifesto of October 17th. What aroused the general
hatred against him was his “order for the day” telling the soldiers not
to spare cartridges, should there be disorders. I met him once here
before the war broke out. I thought he seemed a sensible, strong-
willed person. When he was Chief of the Police at Moscow he had the
reputation of being sensible, of keeping order well, of being rather
heavy-handed, and of committing foolish gaffes from time to time. I
am certain he is neither the monster he is made out by the Liberal
press here and abroad, nor yet the Cromwellian genius other people
would have us believe him to be. I expect that his strength lies in the
fact that he does not mind saying what he thinks. In St. Petersburg
this quality is so rare that people who possess it appear to be
geniuses.
November 12th.
November 14th.
November 17th.
Last night, while I was at the Opéra Bouffe, where the “Country
Girl” was being given, the electric light went out. The performance
continued all the same; the actors holding bedroom candles in their
hands, while the auditorium remained in the dimmest of twilights.
This is owing to the strike.
November 21st.
Christmas Day.