This chapter describes several different programming options for the Raspberry Pi (RPi), including scripted and compiled languages. An external LED control program is provided in most of the languages so that you can investigate each language’s structure and syntax. The advantages and disadvantages of each language type are discussed along with example uses. The chapter then focuses on the C/C++ and Python programming languages, describing their principles, and why object-oriented programming (OOP) is appropriate for the development of scalable embedded systems applications. The chapter details how you can interface directly to the Linux kernel using the GNU C library and finishes with a discussion on how the computational performance of Python code can be greatly improved. A single chapter can only scratch the surface on this topic, so this one focuses on physical programming with the RPi.

After completing this chapter, you should hopefully be able to do the following:

  • Describe the multitude of issues that would impact on your choice of programming languages to use in building physical-computing applications for the RPi.
  • Write basic scripting language program code on the RPi that interfaces to an LED, which is attached to an RPi GPIO.
  • Compare and contrast scripting, hybrid, and compiled programming languages, and their application to the RPi.
  • Write C code examples that interface to the RPi’s GPIOs.
  • Describe the principles of OOP programming, and write C++ classes that provide program structure for physical-computing applications.
  • Write C/C++ code that can interface directly to the Linux OS.
  • Write C/C++ modules that can be called directly from Python.

Digital Media Resources

Here the digital resources referred to in the chapter web page are provided. There are high-resolution versions of some of the important figures and links to videos, resources and websites that are described in the chapter.

This is the circuit that is used to test interfacing the Raspberry Pi to many different programming languages in this chapter. The circuit can be wired using (a) a FET or (b) a bipolar transistor, as illustrated in these figures. Please click on the figure for a larger version.

Source Code Examples

As described in the book, here are source code examples for flashing an LED that is attached to a GPIO using different languages with markup highlighting. Essentially, each code example is performing the same task.  Note: these code examples, and all code on this website, are pulled live from the exploringrpi GitHub repository.


External Resources

Errata

  • Page 184, the compiler generates platform-dependent assembler code, not platform-independent assembler code.

Recommended Books on the Content in this Chapter