Next Baby Step

So, I seen what the device could do, I demonstrated that I could the device.  I selected a development toolset.

Now, I needed to see if I could with code, control an LED.  It was time to write a Hello World GPIO program.

If you recall, I found that an open source solution existed that made interfacing with the GPIO almost trivial.  http://wiringpi.com  From the documentation I learned that I had learned the hard part, properly wiring and properly testing the Red LED.

It seems that this is installed as a standard part of the OS install.  It is safe to assume that if you can issue the command gpio from the command line that you have what is needed to start development.  If you find that this is not the case, please refer to the link above as it will have the latest instructions.

As a matter of fact, I did not even need to examine the docs to hard, as one of the examples was exactly what I wanted to perform.  Let me be clear, I love to program.  However,  I tend to not want to reinvent the wheel, if open source code exists that meets your need, use it.  This allows you to focus on your creation.

Here is the link to the program.  http://wiringpi.com/examples/blink/

#include <wiringPi.h>
int main (void)
{
  wiringPiSetup () ;
  pinMode (0, OUTPUT) ;
  for (;;)
  {
    digitalWrite (0, HIGH) ; delay (500) ;
    digitalWrite (0,  LOW) ; delay (500) ;
  }
  return 0 ;
}

Wow, so clean, so simple.

We need to include wiringPi.h to gain access to the library calls.  You also need to tell the linker to use the  -lwiringPi library, failure to do so will leave you with linker errors to the functions that reside in the library.  If you need to know, you should find the header file in /usr/local/include and the lib in /usr/local/lib .  Again, the best source of up to-date info is the link I provided.                      

The first call is required, wiringPisetup.  This call initializes the GPIO for use.  http://wiringpi.com/reference/setup/

It is important to note that there are several ways of initializing the GPIO with this library.  Some of them do not require root access for your program.  However, this particular call does require root access.

The next thing to note, pin mode(0, OUTPUT).  This is the same call I made from the command line. gpio mode 0 out .  This tells GPIO pin that it will be an OUTPUT pinned to expect us to set it HIGH and LOW.

Inside the never ending loop (I don't advise this on the PI, sometimes hard to terminate these types of programs.) you see we call digitalWrite(0, HIGH) to turn the light on, and digitalWrite(0, LOW) to turn the light off.

In the end I was able to show that controlling the Pi was doable and that my idea would be able to see the light of day.

Next, Planning.




No comments:

Post a Comment