Hardware Reference
In-Depth Information
Imagine if you had multiple chickens, one connected to each of the relays on PiFace. You
could create two chickens by typing the following:
tweetyPi1 = Chicken(pfio,0)
tweetyPi2 = Chicken(pfio,1)
Now, if you wanted to start them both wobbling you could type this:
tweetyPi1.start_wobble()
tweetyPi2.start_wobble()
By using objects, you can use the same function for each chicken. You don't have to worry
about which pin they are connected to after you've created the object because the object
stores it for you. his is an example of why using OOP can be advantageous.
OOP can be tricky to understand at irst, but it should become clearer as you see more exam-
ples. For now, you can ignore the details, accept that it works and hide it in the chicken mod-
ule, in the same way you use other modules without knowing what is inside them. Believe it
or not, professional programmers do this too - as long as the interface to a module is clear
(that is, the functions it provides are clearly documented), it doesn't matter if they don't fully
understand how it works!
Breaking Up Your Code
Chapter 2, “Introductory Software Project: The Insult Generator”, talks about splitting pro-
grams up into functions and how important it is to structure your code in computing.
In this example, separate iles are used for modules to help structure the program. Classes
also help by grouping related data and functions together.
As you will see, what may have sounded like a daunting task of making a Twitter-enabled
soft toy move and talk becomes manageable when tackled in smaller chunks. You also
tested each chunk so it's easier to see where a problem is. Similarly, as you become more
experienced, you'll be able to take almost any big and seemingly hard problem, and split it
up into little steps. Why not try it? Think of a project you want to do, and then think how you
can split it up into smaller parts. Hopefully, as you complete the projects in this topic, you'll
learn the skills necessary to implement each of these little parts, and you'll be able to build
almost anything!
 
Search WWH ::




Custom Search