Xiaopei's DokuWiki

These are the good times in your life,
so put on a smile and it'll be alright

User Tools

Site Tools


it:bbb

Beagle Bone Black BBB

使用 bonescript 操作 GPIO

/**
 * 该程序会同时闪 `BBB 上外接的灯 (P8_13)` 和 `BBB 板载的灯 (USR3)`
 */
 
/**
 * Load the BoneScript module, which you’ll refer to as b throughout
 * the code.
 *
 * 1772ms ~ slow
 */
var b = require('bonescript');
 
 
/**
 * Create a variable called ledPin containing the string “P8_13” to
 * refer to the **GPIO pin 13 on header(端子) P8**.
 */
var ledPin = "P8_13";
 
/**
 * Create a variable called ledPin2 containing the string “USR3” to
 * refer to the on-board LED labeled USR3 (user 3).
 */
var ledPin2 = "USR3";
 
/**
 * Set ledPin as an output.
 *
 * 8091ms ~ very slow
 */
b.pinMode(ledPin, b.OUTPUT);
 
/**
 * Set ledPin2 as an output.
 *
 * 7622ms ~ very slow
 */
b.pinMode(ledPin2, b.OUTPUT);
 
/**
 * Create a new variable called state and store the value LOW in it.
 */
var state = b.LOW;
 
/**
 * Write the value from state (LOW) to ledPin.
 *
 * 所有的 write 操作都比较快 2ms ~ 4ms
 */
b.digitalWrite(ledPin, state);
 
/**
 * Write the value from state (LOW) to ledPin2.
 */
b.digitalWrite(ledPin2, state);
 
/**
 * Execute the toggle function every 1000 milliseconds (1 second).
 */
setInterval(toggle, 1000);
 
function toggle() {
    /**
     * If the variable state contains the value LOW, set it to
     * HIGH. Otherwise, set the value of state to LOW.
     */
    if (state == b.LOW) {
        state = b.HIGH;
    }
    else {
        state = b.LOW;
    }
 
    /**
     * Write the value of state to ledPin.
     */
    b.digitalWrite(ledPin, state);
 
    /**
     * Write the value of state to ledPin2.
     */
    b.digitalWrite(ledPin2, state);
}

BBB 端子说明 (expansion header P8, P9)

it/bbb.txt · Last modified: 2014/06/18 16:29 by admin