User Manual

22
The test program flashes the LEDs in three patterns. The patterns are specified by a collection of
global arrays given values using an initializer. The number in each of the arrays says which LEDs will
be turned on at that point in the pattern so, pattern value is submitted sequentially to produce the
changing pattern, switching all the LEDs off between successive pattern values. Each pattern is run
through twice. The first pattern lights the LEDs one at a time in sequence, left to right. The second
pattern does the same but when it reaches the rightmost LED, it then reverses direction and lights
them in sequence right to left. The third pattern starts at the left end and at each step switches on one
more LED until they are all lit up, then starting at the left it switches them off one by one until they
are all off.
Finally, the test program switches off all the LEDs and then finally calls restore_io to clean up all
the LEDs to a predictable final state.
LEDs Test in Python
This test is available for both RPi.GPIO and WiringPi for Python. The only differences between the
leds-rg.py and leds-wp.py programs are the ways the GPIO ports are driven and cleaned up,
and the Raspberry Pi board revision checker.
In the programs, first the Raspberry Pi board revision is checked and, based on the result, the correct
ports for the LEDs are defined in a list called ports. Since we need to go both forwards and
backwards, we then make a copy of the list, name it ports_rev and reverse it. Then we set up the
ports for output by iterating through the list ports.
After that, we define the main "engine" of this script, the function led_drive(), which requires
three arguments (reps, multiple, direction).
1. reps defines how many times to run the process (1 or 3 in this demo)
2. multiple defines whether or not to switch an led off before switching on the next one (1 =
leave it on, i.e. multiple LEDs lit)
3. direction defines whether to use the forward or reverse ports list (ports for forwards,
ports_rev for reverse)
There are eight calls to the led_drive() function, so we've saved a lot of program lines by reusing
the same code multiple times. All the calls to the led_drive() function are enclosed in a try:
except: block. This ensures that if you exit via CTRL-C, the GPIO ports will be reset or "cleaned
up". This avoids false warnings, the next time you want to use them, that the ports are already in use
by another program.
Suggested tweaks to experiment with. Try changing these one at a time and see what they do...
led_drive(3, 0, ports) change the 3
led_drive(3, 0, ports) change ports to ports_rev
led_drive(3, 0, ports) change the 0 to a 1