Book Image

BeagleBone Black Cookbook

Book Image

BeagleBone Black Cookbook

Overview of this book

There are many single-board controllers and computers such as Arduino, Udoo, or Raspberry Pi, which can be used to create electronic prototypes on circuit boards. However, when it comes to creating more advanced projects, BeagleBone Black provides a sophisticated alternative. Mastering the BeagleBone Black enables you to combine it with sensors and LEDs, add buttons, and marry it to a variety of add-on boards. You can transform this tiny device into the brain for an embedded application or an endless variety of electronic inventions and prototypes. With dozens of how-tos, this book kicks off with the basic steps for setting up and running the BeagleBone Black for the first time, from connecting the necessary hardware and using the command line with Linux commands to installing new software and controlling your system remotely. Following these recipes, more advanced examples take you through scripting, debugging, and working with software source files, eventually working with the Linux kernel. Subsequently, you will learn how to exploit the board's real-time functions. We will then discover exciting methods for using sound and video with the system before marching forward into an exploration of recipes for building Internet of Things projects. Finally, the book finishes with a dramatic arc upward into outer space, when you explore ways to build projects for tracking and monitoring satellites.
Table of Contents (16 chapters)
BeagleBone Black Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Edit a text file from the command shell


There is nothing pretty about the text editing tools in Linux. After all, it is a command shell environment; how pretty can it be? But if you are a minimalist at heart, you will feel right at home.

Two editors mostly dominate Developerland: nano and vim. The latter is the most powerful and fully featured. However, the vim command is not a good starting point for neophytes as it is more complex. Instead, our text editing tool of choice for the rest of this book will be nano. In this section, you will learn your way around the editor and then create and edit a text file.

How it works...

When you're using nano, take note of all the options at the bottom of the screen: Exit, Get Help, Read File, and so on. You access these functions with the keyboard shortcuts shown next to each option. Beware that these shortcuts actually may change their function or mode when you navigate to another screen or choose an option. So, remember what the bottom menu says before assuming that the shortcut you just used remains in the same mode.

In the main (default) window, here's a quick rundown on the most commonly used functions and their shortcuts:

  • Ctrl + X: Closes the file.

  • Ctrl + V: Takes you to the next page.

  • Ctrl + Y: Takes you back to the previous page.

  • Ctrl + K: Cuts text (deletes the line of text where your cursor is).

  • Ctrl + U: Uncuts text (undoes the line deletion from the last line that you cut. Repeating this command will keep adding the same line of text that you cut).

  • Ctrl + G: Help menu.

  • Ctrl + C: Shows the current position (gives you line, column, and character coordinates).

  • Ctrl + R: Inserts a file into the file you have open. Now that's a time saver, huh?

I recommend using Ctrl + G periodically when you're just beginning as a reminder for the additional functions available, many of which are powerful and useful. Overall, you will find that even a simple Linux-based text editor such as nano comes loaded with features that most Wintel/Mac desktop versions don't typically include.

How to do it...

This function can be performed through the following steps:

  1. Let's begin by opening nano and simultaneously creating a file named major_tom:

    ~$ nano major_tom
    

    Pow! A window opens. That's the nano interface in all its glory. All right, not so sexy. But very functional, as you can see in the following image:

  2. On the first line, which is where your cursor should be by default, type (or paste) the following:

    I love my Beaglebone Black so much I want to send it into orbit with Major Tom.
    

    Quickly looking around the window, you will notice the following:

    At the top of the window, you will see the version of nano that you're using and the file name, major_tom.

    At the bottom of the window is an assortment of options.

    If you've played around with any of the options in the menu, ensure that you're back in the main screen where we began with our typed line about Major Tom.

  3. Next, save the file by pressing Ctrl + x; when prompted, type y for yes:

    Error! What a bummer that we couldn't save our work. This is because we didn't first execute nano with the right user privileges. So, try again by closing the file by pressing Ctrl + x; when prompted, type n for no.

We lost our work, but it was only one line of text. We would now have popped back into the command line, and we will have another go; this time, we need a few more steps:

  1. Let's make a custom directory; run this command:

    mkdir bbb_recipe_book
    
  2. Go into this new directory and make a subdirectory; then, make a subdirectory of this one. This may sound overly complicated, but we're just trying to set up some files and directories by organizing principles early. Use the following commands for this:

    cd bbb_recipe_book
    mkdir projects
    cd projects
    
  3. Check your work to ensure that you've set up the directory structure properly. Use the following command:

    pwd
    
  4. Now, let's type sudo nano major_tom.txt. This time, we appended the filename with a text file type. If you don't do this, nano—and Linux—won't know with which application type to associate the file.

  5. In the nano window again, type I love my Beaglebone Black so much I want to send it into orbit with Major Tom., as shown in this image:

  6. Then, press Ctrl + x; when prompted type y for yes.

  7. Now you will see that the bottom menu has a new prompt, which reads as follows:

    File name to write: major_tom.txt
    
  8. Hit return (Enter) on your keyboard. The file is saved to the new directory we're working in (/home/debian/bbb_recipe_book) and closed simultaneously.

You've now made a very exciting .txt file that you can go back and read whenever you want. Okay, that wasn't terribly challenging. But don't worry; we will be adding more complexity as we proceed in upcoming chapters.

See also

For those who find nano a bit Spartan in functionality and prefer a more powerful editor, vim is a popular choice. Although challenging to learn at first, you may find it to be a more rewarding and flexible choice as a tool. Learn how to use it at http://www.openvim.com/tutorial.html.