On Wed, Mar 26, 2014 at 5:42 PM, Mark A. Yoder <[email protected]> wrote:
> I see the bonescript has some i2c methods.  Are there any examples out there
> that work under Debian on the Black Bone?

Here's one that works on BoneScript 0.2.4 on Debian, but happens to be
calling the I2C methods from a web page:

http://jsfiddle.net/jkridner/PGL92/ [1]

I had something similar for drawing a Flot graph using the
accelerometer, but not sure where I posted it.

Some more running-from-webpage examples:
http://jsfiddle.net/user/jkridner/fiddles/

[1] source
var canvas = document.getElementById("mysketch");
var p = new Processing(canvas, sketchProc);

function sketchProc(pjs) {
    // Sketch global variables
    var radius = 50.0;
    var X, Y;
    var nX, nY;
    var delay = 16;
    var brightness = 0;
    var buttonStatus = 0;
    var sliderStatus = 0;
    var lastSliderValue = 0;
    var BUTTON = 'P8_19';
    var SLIDER = 'P9_36';
    var port = '/dev/i2c-2'
    var address = 0x1c;

    // Get the BoneScript library and begin updating the canvas
    setTargetAddress('192.168.7.2', {
        initialized: run
    });

    function run() {
        var b = require('bonescript');
        b.pinMode(BUTTON, b.INPUT);
        b.i2cOpen(port, address, {}, onI2C);

        // Setup the Processing Canvas
        pjs.setup = function () {
            pjs.size(256, 256);
            pjs.strokeWeight(10);
            pjs.frameRate(15);
            X = pjs.width / 2;
            Y = pjs.height / 2;
            nX = X;
            nY = Y;
        }

        // Main draw loop
        pjs.draw = function () {
            // Calculate some fading values based on the frame count
            radius = 50.0 + (15 - sliderStatus) * pjs.sin(pjs.frameCount / 4);
            brightness = (radius - 40.0) / 20.0;

            // Track circle to new destination
            X += (nX - X) / delay;
            Y += (nY - Y) / delay;

            // Fill canvas grey
            pjs.background(100);

            // Set fill-color to blue or red, based on button status
            if (buttonStatus) pjs.fill(200, 30, 20)
            else pjs.fill(0, 121, 184);

            // Set stroke-color white
            pjs.stroke(255);

            // Draw circle
            pjs.ellipse(X, Y, radius, radius);

            // Update physical values
            readSlider();
        }

        function readSlider() {
            b.analogRead(SLIDER, onAnalogRead);
        }

        // Handle data back from potentiometer
        function onAnalogRead(x) {
            if (!x.err && (x.value >= 0) && (x.value <= 1)) {
                if (Math.abs(x.value - lastSliderValue) > 0.05) {
                    lastSliderValue = x.value;
                    sliderStatus = parseInt(x.value * 10, 10);
                }
            }

            // Fetch button status
            b.digitalRead(BUTTON, onDigitalRead);

        }

        // Handle data back from button
        function onDigitalRead(x) {
            buttonStatus = (x.value == b.LOW) ? 1 : 0;

            // Fetch accelerometer status
            readAccel();
        }

        function onI2C(x) {
            if (x.event == 'return') {
                b.i2cWriteBytes(port, 0x2a, [0x00], onI2C_A);
            }
        }

        function onI2C_A() {
            b.i2cWriteBytes(port, 0x0e, [0x00], onI2C_B);
        }

        function onI2C_B() {
            b.i2cWriteBytes(port, 0x2a, [0x01], pjs.setup);
        }

        // Fetch accelerometer status
        function readAccel() {
            b.i2cReadBytes(port, 1, 6, onReadBytes);
        }

        function onReadBytes(x) {
            console.log(JSON.stringify(x));
            if (x.event == 'callback') {
                var gX = convertToG(x.res[0]);
                var gY = convertToG(x.res[2]);
                var gZ = convertToG(x.res[4]);
                $('#X').html(gX);
                $('#Y').html(gY);
                $('#Z').html(gZ);

                // Update heading of ball
                nX = 128 - (gX / 2) * 256;
                nY = (gY / 2) * 256 + 128;

                //pjs.draw();
            }
        }

        function convertToG(x) {
            if (x >= 128) x = -((x ^ 0xFF) + 1); // Get two's complement
            x = x / 64; // Scale to G
            x = x.toFixed(2); // Limit decimal places
            return (x);
        }
    }
}

>
> --Mark
>
> --
> For more options, visit http://beagleboard.org/discuss
> ---
> You received this message because you are subscribed to the Google Groups
> "BeagleBoard" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.

-- 
For more options, visit http://beagleboard.org/discuss
--- 
You received this message because you are subscribed to the Google Groups 
"BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to