Reader small image

You're reading from  Learning BeagleBone

Product typeBook
Published inDec 2014
Publisher
ISBN-139781783982905
Edition1st Edition
Right arrow
Author (1)
Hunyue Yau
Hunyue Yau
author image
Hunyue Yau

Hunyue Yau is an electrical engineer who graduated from the California Institute of Technology. He has worked with Linux since the early '90s, starting with the Soft Landing System. He created one of the first embedded Linux devices in 1996 with a custom-made distribution. He has worked on various products such as embedded Linux appliances, embedded BSD-based devices, and embedded Linux mobile devices. As an active member of the BeagleBoard community, he has volunteered at numerous community events, from Maker Faire to Google Summer of Code mentoring. Today, Hunyue synergizes his electrical engineering skills with embedded Linux software skills to provide turnkey embedded Linux consulting services through HY Research LLC (http://www.hy-research.com/), a company founded by him.
Read more about Hunyue Yau

Right arrow

Chapter 3. Building an LED Flasher

Now that we have a basic foundation of what's on the BeagleBone and how to install the basic system software on it, we will proceed to the basic exercises on the BeagleBone with minimal fear of corrupting the BeagleBone. We'll get our feet wet with classic introductory exercises but, first, we'll write a Hello, World exercise to familiarize ourselves with the basics and then build our own embedded product—an LED flasher. This will be done in small steps, each introducing a set of skills needed for the LED flasher.

In this chapter, we will cover the following topics:

  • Use a simple editor on the BeagleBone itself

  • File permissions (what they are and how to determine them numerically)

  • Set and examine file permissions

  • Use sysfs to control the LEDs

  • Read and write files using the shell language

  • Loop and create delays in the shell language

  • Debug shell scripts

  • Flash LEDs

Setting up a foundation for the exercises


Let's go through a few basics for all the exercises in this book. The exercises in this book should work with the BeagleBone powered by the USB interface for the majority of users. However, a minority of users might have a desktop/laptop not permitting enough power to be drawn from the USB port. In this case, you will need the barrel power adapter as described in Chapter 1, Introducing the Beagle Boards.

All the exercises in this book assume that you will interact with the BeagleBone over the USB interface. To do this, you will need an SSH client suitable for your laptop/desktop. Refer to Chapter 1, Introducing the Beagle Boards, for examples of SSH clients on Linux and Windows. To recap the basics, the process is as follows:

  1. Plug in the USB port and allow the BeagleBone to boot up.

  2. Start the SSH client.

  3. Connect to 192.168.7.2 using the SSH client.

  4. Log in as root.

After a successful login, you will be at a shell prompt similar to this:

foo $
$
#

The exact...

Hello, World for the BeagleBone


Hello, World is the classic first program. While it is very simple, it will go through running programs on the BeagleBone to familiarize ourselves with the process before diving fully into LED blinking. The goal of a Hello, World program is to print this message on the screen.

Exercise 1 – creating a Hello, World program

For this exercise, we will first log in to the BeagleBone and create a file containing the Hello, World program.

We will use shell as our programming language. All the following exercises in this and the next chapter will be in shell. This language was intentionally selected to make it easy to build your own software later on without it being overly complex. The exact version of the shell will vary depending on the distribution used. However, exercises should work on all known versions. A common version of the shell with many extensions, which we will not use in the exercises, is bash. Some distributions will use a simpler and smaller shell called...

Flashing the LEDs


Hello, World might be a good first exercise on the BeagleBone to build a foundation, but it doesn't leverage any of the unique embedded features of the BeagleBone. The BeagleBone can interact with the real world. Our next exercise will be a classic first exercise for embedded devices such as the BeagleBone. We will attempt to blink some of the LEDs on the BeagleBone over the course of the next few exercises.

LEDs on the BeagleBone

The BeagleBone boards come with four programmable LEDs. These are the same LEDs that are used to indicate the software flashing status located next to the RJ45 Ethernet connector. On the BBW, the LEDs are green, whereas on the BBB, the LEDs are blue. The LEDs can be programmed in the same way on both the BBB and BBW. They are the LEDs that we will be blinking in our exercises.

You can see all the four LEDs on the BeagleBone White and BeagleBone Black turned on in the following image:

Exercise 2 – taking control of the LEDs

The goal of this exercise...

Using the controls in a program


Now that we know how to disconnect the LEDs from the system control and turn them on, let's put it together in a program.

Type the following program and name it ledon.sh:

ledon.sh
1: #!/bin/sh
2: # Replace the names with what is used on your system
3: # Disconnect all 4 LEDs on the BeagleBone so we can use it.
4: echo none > /sys/class/leds/beaglebone:green:usr0/trigger
5: echo none > /sys/class/leds/beaglebone:green:usr1/trigger
6: echo none > /sys/class/leds/beaglebone:green:usr2/trigger
7: echo none > /sys/class/leds/beaglebone:green:usr3/trigger
8: # Turn on all 4 LEDs
9: echo 255 > /sys/class/leds/beaglebone:green:usr0/brightness
10: echo 255 > /sys/class/leds/beaglebone:green:usr1/brightness
11: echo 255 > /sys/class/leds/beaglebone:green:usr2/brightness
12: echo 255 > /sys/class/leds/beaglebone:green:usr3/brightness

Let's analyze the preceding program:

  • The first line tells the Linux kernel that this is a shell script.

  • From lines...

Summary


In this chapter, we went through the basic foundation to get a basic program on the BeagleBone. We looked at using a simple editor on the BeagleBone. Then, you learned about Linux file permissions. Linux file permissions are divided into three groups, namely, user, group, and other. Each group can have read, write, and execute permissions. We also looked at calculating the numeric representation of the permissions. The permissions can be examined using the ls command and set using the chmod command.

Using this basic foundation, we dived into our first Hello, World program on the BeagleBone. While being simple, it illustrated the importance of permissions. Building upon this with exercises 1 and 2, you learned about the sysfs files and how to use them to control the LEDs on the BeagleBone. We controlled the LEDs by first disconnecting them from the system control, and then turning them on and off by writing to the sysfs files using the echo command and shell redirection.

In the next...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learning BeagleBone
Published in: Dec 2014Publisher: ISBN-13: 9781783982905
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Hunyue Yau

Hunyue Yau is an electrical engineer who graduated from the California Institute of Technology. He has worked with Linux since the early '90s, starting with the Soft Landing System. He created one of the first embedded Linux devices in 1996 with a custom-made distribution. He has worked on various products such as embedded Linux appliances, embedded BSD-based devices, and embedded Linux mobile devices. As an active member of the BeagleBoard community, he has volunteered at numerous community events, from Maker Faire to Google Summer of Code mentoring. Today, Hunyue synergizes his electrical engineering skills with embedded Linux software skills to provide turnkey embedded Linux consulting services through HY Research LLC (http://www.hy-research.com/), a company founded by him.
Read more about Hunyue Yau