Introduction

One of the first things you would like to do when you connect to the Beaglebone Black is see that you are having an impact on the hardware. In this short post I am going to look at how you can change the behaviour of the Beaglebone on-board LEDs – the four (blue on the BBB) LEDs in the corner near the reset button.

Now, the LEDs are there for a reason, and that reason is to give information about the Beaglebone state (from beaglebone.org):

USR0 is configured at boot to blink in a heartbeat patternUSR1 is configured at boot to light during microSD card accessesUSR2 is configured at boot to light during CPU activityUSR3 is configured at boot to light during eMMC accesses

We can change the behaviour of these LEDs, but obviously we will temporarily lose this valuable information. The heartbeat tells you that the BBB is alive, which is always useful to know. The others are fairly self explanatory (the eMMC is the solid state memory that you are booting from).

First – At the command line

Sysfs is a virtual file system that is available under Linux that gives information and access to devices and drivers that would otherwise only be accessible in Kernel Space. It gives us a virtual file system in Linux User Space, which is convenient to use; however, it is not highly efficient.

You have to set up the timer before writing to delay_on and delay_off. So the first line sets up a timer and on the second and third lines we set up the time for the first LED to be on and off (in milliseconds). So this will cause the first LED to flash 10 times per second (i.e. time on + time off = 100ms, so 10 flashes per second).

Setting it Back to a Heartbeat

C++ Implementation

Now that we have this working at the command line it would be useful if we could write programs that interface to the LEDs in a similar fashion. My C++ implementation code is below. It uses the C++ fstream and iostream libraries to read and write data to the input/outputs using sysfs.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

/** Simple On-board LED flashing program - written by Derek Molloy

for the ee402 module

This program uses USR LED 0 and can be executed in three ways:

makeLED on

makeLED off

makeLED flash (flash at 100ms intervals - on 50ms/off 50ms)

makeLED status (get the trigger status)

*/

#include<iostream>

#include<fstream>

#include<string>

usingnamespacestd;

#define LED0_PATH "/sys/class/leds/beaglebone:green:usr0"

voidremoveTrigger(){

// remove the trigger from the LED

std::fstream fs;

fs.open(LED0_PATH"/trigger",std::fstream::out);

fs<<"none";

fs.close();

}

intmain(intargc,char*argv[]){

if(argc!=2){

cout<<"Usage is makeLED and one of: on, off, flash or status"

<<endl;

cout<<"e.g. makeLED flash"<<endl;

}

stringcmd(argv[1]);

std::fstream fs;

cout<<"Starting the LED flash program"<<endl;

cout<<"The LED Path is: "<<LED0_PATH<<endl;

// select whether it is on, off or flash

if(cmd=="on"){

removeTrigger();

fs.open(LED0_PATH"/brightness",std::fstream::out);

fs<<"1";

fs.close();

}

elseif(cmd=="off"){

removeTrigger();

fs.open(LED0_PATH"/brightness",std::fstream::out);

fs<<"0";

fs.close();

}

elseif(cmd=="flash"){

fs.open(LED0_PATH"/trigger",std::fstream::out);

fs<<"timer";

fs.close();

fs.open(LED0_PATH"/delay_on",std::fstream::out);

fs<<"50";

fs.close();

fs.open(LED0_PATH"/delay_off",std::fstream::out);

fs<<"50";

fs.close();

}

elseif(cmd=="status"){

// display the current trigger details

fs.open(LED0_PATH"/trigger",std::fstream::in);

stringline;

while(getline(fs,line))cout<<line;

fs.close();

}

else{

cout<<"Invalid command"<<endl;

}

cout<<"Finished the LED flash program"<<endl;

return0;

}

Effectively, this code carries out the exact same steps as what I did in the first part of this post; however, now it is wrapped up as a command that we can add to our $PATH in order that it can be called from any location. The way you build the code on the Beaglebone Black is as follows:

1

2

3

4

#!/bin/bash

echo"EE402 - Building the Test LED program on the Beaglebone Black"

g++makeLED.cpp-omakeLED

echo"Finished"

I have made all of the code available via github, so you can download this by the following steps: