Tutorial WALKTHROUGH
(^136) The Official Raspberry Pi Projects Book raspberrypi.org/magpi
The buttons are simple: one bit in one byte of the
returned data block, and other controls are spread
over several bits. The two axes of the joystick are the
first six bits in the first two bytes, whereas the values
from the slider are in bits 4 to 1 of byte number 2. The
rotary controlġs five bits are split up over two bytes,
whereas the turntable is scattered all over the place in
four locations. You’ll need a bit of software to pull out
the data you want from this block.
The turntable returns a value, not of its position,
but of the change in the position since the last time it
BIT
7 6 5 4 3 2 1 0
0 1 2 3 4 5
BYTE
TT 4:3
TT 2:1
TT 0
Save Joystick X 0:5
Save Joystick Y 0:5
ROT 4:3 TT 5
ROT 2:0
TTR
TTG
TTB
ROT
B-
B+
Sign Extend
RD
TTR
TTG TTB
Turntable Increment
B- B+
RD
TT5 TT5 TT5 TT4 TT3 TT2 TT1 TT0
Turntable Red Button
Turntable Green Button
Turntable Blue Button
Rotary - Not Used
Invert Screen Button
Select Parameter to Edit Button
Redraw Button
Redraw Depth 3:0
was read. This is a five-bit signed value, with the most
significant bit being the sign bit. To be useful, this
must be converted into the sort of number that Python
can understand, so bit 5 of the turntable number is
propagated to all the higher bits in the word; this is
known as sign extension. Also, for negative values,
the number must be in the ones’ complement format;
that is, with bits 0 to 4 inverted and 1 subtracted. This
format is universally used in computer languages and
prevents you from having two different bit patterns for
plus zero and minus zero, meaningless concepts.
The graphics
We’re going to use this controller to control a simulation
of a harmonograph. These devices were originally a very
popular Victorian contraption, consisting of a pen on the
end of a compound pendulum that was sent swinging
and produced Lissajous-like patterns. Here, however,
we’re going to simulate a system with four pendulums
that will exceed the ľexibility of any mechanical device.
Two pendulums control the X position of the pen, and
two control the Y position, the final position being the
simple sum of each of the two pendulums on each axis.
There are four parameters that control the exact path
of each pendulum.
- The Amplitude or extent of the swing
- The Frequency of the swing, determined
by the pendulum’s length - The Phase of the swing, a fixed value added
to the frequency - The Damping or decay, which is the slow
reduction of amplitude as the friction takes
energy out of the pendulum
It’s the subtle interaction of these four parameters
that produces the near-infinite number of patterns
that can be obtained. There’s also the factor of how
long you let it swing for, producing a less or more
dense pattern. Eventually the damping will make all
the pendulums stop, although in this simulation you
can set the damping value to zero if you wish.
The software
The code for this is DJArt.py and is written under the
Pygame framework. The drawing area is set by the
screenSize variable, and you could make the window
size larger simply by altering this value if you have a
higher-resolution monitor. In addition, the window is
wider by 100 pixels given by the controlBar variable,
in order to have room for the editable parameters.
The swing function is the one that actually draws the
path of the pendulums on the screen, with the points
calculated by the calcNewPoint function. Rather than
Figure 3 The data block returned from the DJ Hero