Beaglebone Black-Controls the LED lights on the BBB board

Source: Internet
Author: User

BBB has five LEDs on the board, one power supply, four other LEDs, usr0 to USR3. This study is to control usr0 to 3 so that they are on, off, flashing. It's kind of a Hello world experiment. Very simple.

Required Materials:

    • BBB One
    • Buy a data line from BBB
    • Laptop or desktop computer with USB plug

First use the data cable to connect the BBB and the computer, automatically boot up, you can start.

Try all three of these practices:

    1. Bonescript
    2. Python
    3. C++
Bonescript

Operation Bonescript Practice is to use Cloud9, assuming you have not changed the BBB usb0 network card address, from the Computer browser to open the http://192.168.7.2:3000/can.

Right-click the folder directory to add a folder, and then right-click Add File (New file), name the extension, Bonescript is JS, the extension is. js.

var B = require (' Bonescript ');

var leds = ["USR0", "USR1", "USR2", "USR3"];

for (var i in LEDs) {
B.pinmode (Leds[i], b.output);
}

var state = B.high;
for (var i in LEDs) {
B.digitalwrite (Leds[i], state);
}

(saying that the new openwriter is installed without the code Snippet plugin ...) )

First Pinmode set output,led to it high with digitalwrite write in can. Cycle four lights to make them all light. Give it low,led and extinguish. Give it a try. The code is written so that it runs directly on the Run button above.

Then is flashing, JS is a setinterval only:

var B = require (' Bonescript ');

var leds = ["USR0", "USR1", "USR2", "USR3"];

for (var i in LEDs) {
B.pinmode (Leds[i], b.output);
}

var state = B.high;

function Toggle () {
if (State===b.low) {
state = B.high;
} else {
state = B.low;
}
for (Var j in LEDs) {
B.digitalwrite (leds[j],state);
}
}

SetInterval (toggle,1000);

You can add a bit of code to let four lights cycle light off, try it yourself. Bonescript's API please check here.

Python

Python a bit tangled, because adafruit_bbio this module a bit of a problem, originally wanted to use directly, but USR0 did not respond, the actual measurement has not. Detailed I do not study, meet these things more annoyed not to see it.

Just look at how these things work and write directly. For the sake of fast, first look at StackOverflow others posted a piece of code, PYTHON3, please ignore, just look at the code:

import os   class SimpleGPIO:      def __init__(self,gpio_pin):          self.gpio_pin = gpio_pin          os.system("echo %d > /sys/class/gpio/export" % self.gpio_pin)          self.gpio_path = "/sys/class/gpio/gpio%d/"%gpio_pin          with open(self.gpio_path+"direction") as f:              self.direction = f.read()      def write(self,value):          if self.direction != "out":              os.system("echo out > %sdirection"%self.gpio_path)              self.direction = "out"          os.system("echo %s > %svalue"%(value,self.gpio_path))      def read(self):          if self.direction != "in":              os.system("echo in > %sdirection"%self.gpio_path)              self.direction = "in"          with open(self.gpio_path+value) as f:              return f.read()

In the Linux operating hardware, with the SYSFS, the above code is GPIO generic class, to the pin number created, and then can call write and read, relatively simple. If it is Python control, please modify the above on demand, save for future use.

Then see where the usr0 to USR3 lights are, and how they can play:

Within the/sys/class/leds, you'll see four, beaglebone:green:usr0 to 3, and go in one of them.

Simple English can understand words, inside things are written very clear, we want it bright, and out just, only need to move trigger, and brightness, script test effect Bai, take usr0 surgery, show the original value, and I wrote what go in:

echo None > Trigger

echo 1 > Brightness

At this time, the usr0 lamp is often bright, because trigger set to none, brightness of 1. Then destroy it, that's all:

echo 0 > brightness

That is, if you want it to blink, first clear the trigger (to the value of None), and then write the brightness loop to 0 and 1. This is just what Python writes:

Import OS
Import time

Class simpleled:
def __init__ (self,led_no):
Self.led_no = Led_no
Self.led_path = "/sys/class/leds/beaglebone:green:usr%d/"% self.led_no
Os.system ("echo None >%strigger"% self.led_path)
Self.state = "Off"
def on (self):
If self.state! = "on":
Os.system ("Echo 1 >%sbrightness"% self.led_path)
Self.state = "On"
def Off (self):
If self.state! = "Off":
Os.system ("Echo 0 >%sbrightness"% self.led_path)
Self.state = "Off"
LEDs = [Simpleled (i) for I in range (4)]
While True:
For j in Range (4):
LEDS[J]. On ()
Time.sleep (1)
For j in Range (4):
LEDS[J]. OFF ()
Time.sleep (1)

You like to use CLOUD9 to open a file to paste the above code up, and then run, or command line execution, the same effect. Flash ...

Adafruit What's wrong with that I don't study. Hands-on, clothed. It's very simple. The code is the creation of simpleled when the trigger write to none, to turn on the light on () when the brightness write 1, turn off the light off () write 0.

Write the original value back trigger and brightness, you can restore, or silly, restart can also.

C++

SYSFS description See Python section, C + + directly on the code:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace Std;

#define Led_path "/SYS/CLASS/LEDS/BEAGLEBONE:GREEN:USR"

Class led{
Private
string path;
int number;
virtual void writeled (string filename, string value);
virtual void Removetrigger ();
Public
LED (int number);
virtual void turnOn ();
virtual void turnoff ();
virtual void Flash (string delayms);
virtual void outputstate ();
Virtual ~led ();
};

led::led (int number) {
This->number = number;
Ostringstream s;
s << led_path << number;
Path = string (S.str ());
}

void led::writeled (string filename, string value) {
Ofstream FS;
Fs.open ((path+filename). C_STR ());
FS << value;
Fs.close ();
}

void Led::removetrigger () {
Writeled ("/trigger", "none");
}

void Led::turnon () {
cout << "Turning LED" << number << "on" << Endl;
Removetrigger ();
Writeled ("/brightness", "1");
}

void Led::turnoff () {
cout << "Turning LED" << number << "Off" << Endl;
Writeled ("/brightness", "0");
}

void Led::flash (String delayms = "50") {
cout << "Making LED" << number << "Flash." << Endl;
Writeled ("/trigger", "Timer");
Writeled ("/delay_on", delayms);
Writeled ("/delay_off", delayms);
}

void Led::outputstate () {
Ifstream FS;
Fs.open (path + "/trigger"). C_STR ());
String line;
while (Getline (FS, line)) cout << line << Endl;
Fs.close ();
}

Led::~led () {
cout << "Destroying the LED object with path:" << path << Endl;
}

int main (int argc, char* argv[]) {
if (argc! = 2) {
cout << "Args error, On/off/flash/status supported only." << Endl;
return 1;
}
String cmd (argv[1]);

LED LED = LED (3);

if (cmd== "on") Led.turnon ();
else if (cmd== "off") Led.turnoff ();
else if (cmd== "flash") Led.flash ("100");
else if (cmd== "status") Led.outputstate ();
else {
cout << "Command invalid" << Endl;
return 1;
}
return 0;
}

The code comes from exploring Beaglebone this book. The principle is exactly the same as Python, not explained.

Feel the above a bit insulting your wisdom, the next article to a bread board, light up the LED above, will use the GPIO.

Beaglebone Black-Controls the LED lights on the BBB board

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.