06.08.2015 Views

RASPBERRY PI

MagPi36

MagPi36

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

TutorialWALKTHROUGHNow that the hardware side of the project iscomplete, we just need to get the software running.The program is written in Python and uses a Pythonlibrary that, in turn, uses a C library that manages thehigh-speed communication. The software used hereis based on this Adafruit tutorial: learn.adafruit.com/neopixels-on-raspberry-pi.However, the C library used in the Adafruit tutorialis (at the time of writing) not compatible with theRaspberry Pi 2. Fortunately for any Pi 2 owners, RichardHurst has made a version of the software that does workwith Raspberry Pi 2. We have also tested it on a B+ andan original B.‘Colors’ is where you can alterthe colours of light that thelamp will cycle aroundStart by making sure that your package manager is upto date, using the command:sudo apt-get updateNext, install the packages and library software that areneeded, using:sudo apt-get install build-essential pythondevgit scons swigThe next step is where the directions diverge from theAdafruit tutorial, because you need to fetch the versionof the software that has been fixed for Raspberry Pi 2. Ifyou don’t have a Raspberry Pi 2, you can just stick withthe Adafruit tutorial if you prefer.Fetch the modified NeoPixel code from GitHub usingthe command:git clone https://github.com/richardghirst/rpi_ws281x.gitChange directory into the software that you justfetched from GitHub, using:cd rpi_ws281x…and then build the C code, using the command:sconsWhen the C is compiled, you need to install thePython library that interfaces with the fast C code:cd pythonsudo python setup.py installFinally, you can download the Python program for thisproject, using the commands:cd /home/pigit clone https://github.com/simonmonk/pi_magazine.gitThis command will actually bring down the code for allthe projects in this MagPi series, so if you have alreadyissued this command for one of the earlier articles,change directory to pi_magazine and run the followingcommand to update your directory with this project(06_mood_light).git pullHow the code worksThe Python code for this project is prettystraightforward. All the hard work takes placein the libraries.If you are interested in how the code works, load itup into an editor while we go through it.The first section of code includes all of the librariesyou need. This includes the neopixel library and alsothe RPi.G<strong>PI</strong>O library that is needed to detect whetherthe switch has been pressed.The next section contains a load of variables that areused to configure the neopixel library so that it agreeswith the actual hardware. If you wanted to use moreor less LEDs, then change LED_COUNT to the number ofLEDs in your strip. Another value that you might needto change is LED_FREQ_HZ. Some LED strips operate at400,000Hz rather than the more usual 800,000Hz.The section labelled ‘Colors’ is where you can alterthe colours of light that the lamp will cycle around.The first one (NO_COLOR) needs to be unchanged or youwon’t be able to turn the lamp off using the button,but you can change the values of the other colours asyou like. Remember to keep the variable colors up todate, adding any extra colours to the end of the list.The second, third and fourth colours (MORNING_COLOR,DAY_COLOR, and NIGHT_COLOR) are also special, becauseit is between these three colours that a choice will bemade, based on the time of day when you hold downthe button for more than half a second.The color_index variable is used to indicate theindex position 0 to 6 of the currently selected colour.After this, there is a section to set up G<strong>PI</strong>O pin 23as the switch input, and then we come to the startof the functions.The function set_all will set every LED in the stripto the colour specified; set_color_index_from_timewill pick the colour index 1, 2, or 3, based on whetherit’s morning, daytime, or evening.Then the display is initialised and all the LEDcolours set to colors[0], which means off, so thatinitially the lamp is off.The main program loop is contained in a try:finally: block so that the G<strong>PI</strong>O pins will be tidied upwhen the program exits. This main loop only has todo anything when the switch is pressed. When thishappens, the current time is stored in the variabledown_time so that the length of the button press canlater be calculated. There is then a sleep of 0.2 of asecond to give the switch contacts time to settle. Thisis called ‘debouncing’ and without it, a single buttonpress can register as multiple presses. The whileloop that follows this ensures that nothing furtherhappens until either the button is released or half asecond (LONG_PRESS_THRESHOLD) has elapsed.The duration of the button press is then calculatedand if it’s a long one, set_color_index_from_time iscalled to set the colour automatically; otherwise, 1 isadded to the color_index variable that is returned to46 August 2015raspberrypi.org/magpi

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!