beagleboard.org

Bonescript

Bacon Cape

The Bacon Cape is an add-on board meant to give you access to some hardware to help build your understanding of doing basic embedded I/O on BeagleBone.

Board activity

Components

  • S1: a simple push button provides a digital input
    • P8_19: digital I/O connection to BeagleBone
    • Configured as active-low (low when pressed) and uses and external pull-up
  • D1: an RGB LED provides an indicator for digital output
    • P9_16: digital I/O connection for BLUE
    • P9_14: digital I/O connection for GREEN
    • P9_42: digital I/O connection for RED
    • Configured as active-high (lit when high)
  • R10: a slide potentiometer provides an analog input
    • P9_36: analog input connection to BeagleBone
  • U1: shift register feeding a 7 segment display
    • P9_22: CLOCK
    • P9_17: LATCH
    • P9_18: DATA
    • LED1: connected to shift register output in common anode (active-low) configuration
    • a: last bit shifted out
    • h: first bit shifted out
  • U2: I2C EEPROM connected to I2C2

Demo

console.log('Starting Bacon Cape demo');
var b = require('bonescript');
var s = b.LOW;
var LED_RED = 'P9_42';
var LED_GREEN = 'P9_14';
var LED_BLUE = 'P9_16';
var BUTTON = 'P8_19';
var POT = 'P9_36';

try {
    $('#slider1').slider();
    $('#slider2').slider();
    $('#slider3').slider();
    $('#led1').css('width', '50px');
    $('#led1').css('height', '50px');
    $('#led1').css('background-color', 'rgb(0,0,0)');
} catch(ex) {}
b.pinMode('USR0', b.OUTPUT);
b.pinMode('USR1', b.OUTPUT);
b.pinMode('USR2', b.OUTPUT);
b.pinMode('USR3', b.OUTPUT);
b.pinMode(LED_RED, b.OUTPUT);
b.pinMode(LED_GREEN, b.OUTPUT);
b.pinMode(LED_BLUE, b.OUTPUT);
b.pinMode(BUTTON, b.INPUT);
b.digitalWrite('USR0', b.LOW);
b.digitalWrite('USR1', b.LOW);
b.digitalWrite('USR2', b.LOW);
b.digitalWrite('USR3', b.LOW);
b.analogWrite(LED_RED, 0);
b.analogWrite(LED_GREEN, 0);
b.analogWrite(LED_BLUE, 0);
//b.attachInterrupt(BUTTON, true, b.CHANGE, handleButton);
blink();
$("#slider3").bind("slidechange", function(event, ui) {
    updateRed({value:(ui.value/100.0)});
});

function blink(x) {
    s = (s == b.LOW) ? b.HIGH : b.LOW;
    b.digitalWrite('USR0', s, doAnalogRead);
}

function doAnalogRead() {
    b.digitalRead(BUTTON, updateBlue);
    b.analogRead(POT, updateGreen);
    setTimeout(blink, 100);
}

function updateBlue(x) {
    if(typeof x.value == 'undefined') return;
    updateLED({blue:1-x.value});
}

function updateGreen(x) {
    if(typeof x.value == 'undefined') return;
    updateLED({green:1-x.value});
}

function updateRed(x) {
    if(typeof x.value == 'undefined') return;
    updateLED({red:x.value});
}

function updateLED(led) {
    if(typeof led.red == 'number') {
        b.analogWrite(LED_RED, led.red);
        this.red = led.red;
    }
    if(typeof led.green == 'number') {
        b.analogWrite(LED_GREEN, led.green);
        this.green = led.green;
    }
    if(typeof led.blue == 'number') {
        b.analogWrite(LED_BLUE, led.blue);
        this.blue = led.blue;
    }
    try {
        if(typeof this.red == 'undefined') this.red = 0;
        if(typeof this.green == 'undefined') this.green = 0;
        if(typeof this.blue == 'undefined') this.blue = 0;
        var rgb = 'rgb(' + Math.round(this.red*255) + ',' + 
            Math.round(this.green*255) + ',' +
            Math.round(this.blue*255) + ')';
        $('#led1').css('background-color', rgb);
        $("#slider1").slider("option", "value", this.blue*100);
        $("#slider2").slider("option", "value", this.green*100);
    } catch(ex) {}
}

See also

Topics

Related functions