MagPi27.pdf

(9797 KB) Pobierz
ISSUE 27 - OCT 2014
Get printed copies
at themagpi.com
A Magazine for Raspberry Pi Users
Game Console Controllers
FUZE BASIC
Matboard
Using Git
Ultrasonic Range Finder
C++ Operator Overloading
Build QR codes
in Minecraft
Raspberry Pi is a trademark of The Raspberry Pi Foundation.
This magazine was created using a Raspberry Pi computer.
http://www.themagpi.com
27
Welcome to Issue 27 of The MagPi magazine. This month's issue is packed cover to cover with
something for just about everyone!
Are you tired of controlling your Raspberry Pi with the same old mouse and keyboard? Have you ever
wished you could have the ergonomic feel of a console controller in your hands when playing some of
those retro games we have written about in past issues? If you answered yes to either of these
questions, why not take a look at Mark Routledge's fantastic article describing how to do just that.
Alec Clews talks us through the use of Git, a free version control software package that we also use
here at The MagPi to ensure that all of the team work on the most up to date copy of each issue. This is
a great read, especially if you work with any type of document or file as part of a team.
As you can see from our front cover, we return to the popular world of Minecraft in Dougie Lawson's
clever article on building QR code structures inside the game. We also have more physical computing
from ModMyPi, and a great father and son story on building and funding a Raspberry Pi project through
Kickstarter.
Of course we have not forgotten about programming. William Bell continues his popular C++ series and
we also have part three of our game programming series using FUZE BASIC. Start thinking of some
game ideas now because in the next issue we will have a game programming competition.
If you want even more from The MagPi this month then why not join us on the 1 1
th
October at the
SWAMP Fest event (see this month's Events page) where we will have our own stand. We look forward
to seeing you there.
We hope you enjoy this month's issue and don't forget to
like our Facebook page and leave a comment at
http://www.facebook.com/MagPiMagazine.
Chief Editor of The MagPi
The MagPi Team
Ash Stone
- Chief Editor / Administration
Ian McAlpine
- Issue Editor / Layout / Proof Reading
W.H. Bell
- Administration / Layout
Bryan Butler
- Page Design / Graphics
Matt Judge
- Website / Layout
Aaron Shaw
- Layout
Nick Hitch
- Administration
Colin Deady
- Layout / Proof Reading
Dougie Lawson
- Testing
Nick Liversidge
- Proof Reading
Age-Jan (John) Stap
- Layout
2
Contents
4
CHOOSE YOUR WEAPON
or Wiimote controller to a Raspberry Pi
Connecting an XBOX360, PS3
1 2
THE MATBOARD PROJECT
A story of Kickstarter, GPIO and water buckets
1 8
PHYSICAL COMPUTING
Part 3: Using an HC-SR04 ultrasonic range finder
THIS
23
Bristol
MONTH'S EVENTS GUIDE
UK, London UK
UK, Swansea UK, Huddersfield
PI EDITION
24
MINECRAFT
structures inside Minecraft
Build QR Code
28
VERSION CONTROL
using Git
Part 1 : Version control basics
BASIC
34
FUZE
Keyboard input, animation and arrays
Part 3:
CACHE
42
C++
7: Operator overloading
Part
48
HAVE YOUR SAY
and article ideas
Send us your feedback
http://www.themagpi.com
3
CHOOSE YOUR WEAPON
Adding console game controllers
Connecting an XBOX360, PS3 or
Wiimote controller to a Raspberry Pi
SKILL LEVEL : INTERMEDIATE
Choose your Weapon
It is possible, and lots of fun, to use a variety of
today's modern console controllers for your
Raspberry Pi projects. They are quite easy to
install, readily available and you get quite a bit of
kit for your buck! This article describes how to
setup a Raspberry Pi to use a Microsoft® XBOX
360 gamepad (wired and wireless), a Sony®
PS3 gamepad and Nintendo® Wiimote.
For wireless devices you will need either the
XBOX gamepad wireless adapter, or for the
Wiimote and PS3 gamepad you can use an
expensive bluetooth dongle. See
http://www.
elinux.org/RPi_USB_Bluetooth_adapters
for a
list of known working bluetooth dongles.
I have tested all the code on a Raspberry Pi
Model B, as well as on the new Model B+ board.
Before installing any of the software ensure you
are using a recent version of Raspbian and have
updated your system. At the command line enter:
sudo apt-get update
sudo apt-get upgrade
Mark Routledge
Guest Writer
Enter the following command to install the
software:
sudo apt-get i nstal l j stest-gtk j oysti ck
This program should now show up in your
desktop ready for you to test. You will find the
link under
Menu
Other
j oysti ck testi ng
and confi gurati on tool
.
Once installed it should display your hardware
for example: Connecting your hardware.
A brief note about using the bluetooth adapter. I
have found these can be a little tempremental at
times, so I strong recommend connecting your
dongle directly to the Raspberry Pi and connect
any other hardware via a powered USB hub, e.g.
the excellent Raspberry Pi Hub! The bluetooth
dongle will work from a powered hub on a low
level, but the bluetooth stack may not be able to
see it.
Setting up the XBOX gamepad
First open up LXTerminal and install the required
driver for the XBOX 360 gamepad. The same
driver is used for both the wired gamepad and
wireless gamepad, using the adapter. Enter:
sudo apt-get i nstal l xboxdrv
To test all of the hardware, an easy piece of
software to use on the Raspberry Pi is
j stest-
gtk
and the standard
j oysti ck
library.
4
It is possible to now use the XBOX gamepad with
a variety of Linux games by using:
sudo xboxdrv --si l ent
However, this does not really give you much
control over what each axis / button does. You
can use the
j oysti ck
testi ng
and
confi gurati on tool
mentioned earlier but it
can be difficult to get setup. Luckily it is possible
to configure the XBOX gamead in a variety of
ways. Each configuration requires a setup (text)
file which you provide when launching the
gamepad and driver. Through this you can turn
one of the analogue sticks into a mouse and the
buttons into keypresses etc. This will make it
usable with programs like Minecraft, Doom,
Quake3 plus all known emulators. The
possibilities are endless, and exciting.
Let us make a directory to store these setup files,
which contain the button mappings. Enter:
cd ~
mkdi r XBOX
cd XBOX
configuration text file called
basi c_confi g
. You can use any editor, but
nano
is quick and simple. Enter:
nano basi c_confi g
Create
a
Now enter the following mappings:
#----------------------------------------
# Thi s fi l e i s the Basi c Defi ni ti on of
# the control l er, al l owi ng a deadzone.
# It wi l l run si l entl y.
#----------------------------------------
# Setup the DPad as buttons and tri ggers.
#
[xboxdrv]
si l ent=true
deadzone=6000
dpad-as-button=true
tri gger-as-button=true
#
#----------------------------------------
# Map the ri ght anal og sti ck as absol ute
# val ues (x2 and y2) and the l eft anal og
# sti ck as mouse rel ati ve (x1 and y1).
#
[ui -axi smap]
x2=ABS_X
y2=ABS_Y
x1=REL_X: 10
y1=REL_Y: -10
#
# ---------------------------------------
# Map the four col oured buttons a, b, x
# and y and set each one as a di fferent
# key, or equi val ent j oysti ck button.
# In thi s case Left Shi ft, j oysti ck
# buttons C and A and the key C.
# Map the tri ggers and bumpers i n the
# same way; l t, rt, l b and rb.
# Map the DPad du, dl , dd, dr as WASD.
# Map the Back, Start and Gui de (XBOX)
# buttons to Home, Escape and Enter.
#
[ui -buttonmap]
a=KEY_LEFTSHIFT
b=BTN_C
x=BTN_A
y=KEY_C
#
l b=KEY_LEFT
rb=KEY_RIGHT
l t=KEY_Z
rt=KEY_SPACE
#
dl =KEY_A
dr=KEY_D
du=KEY_W
dd=KEY_S
#
gui de=KEY_HOME
back=KEY_ESC
start=KEY_ENTER
#--------------- EOF --------------------
Save and close the configuration file and exit
from
nano
by pressing the <Ctrl>+<X> keys
together, followed by the <Y> key then <Enter>.
Description of the XBOX config file
This file is the basic definition of the controller,
allowing a deadzone. It will run silently, meaning
it will not output additional information to the
terminal.
The file sets up the DPad as buttons and
triggers. The next few sections are used to map
the right analogue stick as absolute values on the
x2 and y2 axes and the left analogue stick as
mouse relative values on the x1 and y1 axes.
5
Zgłoś jeśli naruszono regulamin