1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <?xml-stylesheet type="text/xsl" href="helma.xsl"?>
3 <xmlroot xmlns:hop="http://www.helma.org/docs/guide/features/database">
4 <hopobject id="3008" name="accelerometer" prototype="Page" created="1375888514900" lastModified="1376560050943">
5 <hop:parent idref="2892" prototyperef="Page"/>
6 <is_xhtml type="boolean">true</is_xhtml>
7 <http_remotehost>192.91.75.30</http_remotehost>
8 <http_language>en-US,en;q=0.8</http_language>
9 <uri>accelerometer</uri>
10 <http_browser>Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36</http_browser>
11 <time type="date">15.08.2013 04:47:30 CDT</time>
12 <hopsession>192.91.75.172.24.1.1mdcrwb0yynys</hopsession>
13 <body><h1><a href="http://processingjs.org/" class="external">Processing.JS</a></h1>
\r
14 <canvas id="mysketch"></canvas>
\r
15 <script src="/static/bonescript.js"></script>
\r
16 <script src="/static/processing.js"></script>
\r
19 var canvas = document.getElementById('mysketch');
\r
20 var pjs = new Processing(canvas);
\r
28 var buttonStatus = 0;
\r
29 var sliderStatus = 0;
\r
30 var lastSliderValue = 0;
\r
31 var LED_RED = 'P9_42';
\r
32 var LED_GREEN = 'P9_14';
\r
33 var LED_BLUE = 'P9_16';
\r
34 var BUTTON = 'P8_19';
\r
35 var POT = 'P9_36';
\r
36 var S_DATA = 'P9_18';
\r
37 var S_CLOCK = 'P9_22';
\r
38 var S_LATCH = 'P9_17';
\r
39 var S_CLEAR = 'P9_15';
\r
41 var segDigits = [ 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90 ];
\r
43 // Get the BoneScript library and begin updating the canvas
\r
44 setTargetAddress('192.168.7.2', {initialized: run});
\r
46 var b = require('bonescript');
\r
47 var port = '/dev/i2c-2'
\r
49 b.pinMode(LED_RED, b.OUTPUT);
\r
50 b.pinMode(LED_GREEN, b.OUTPUT);
\r
51 b.pinMode(LED_BLUE, b.OUTPUT);
\r
52 b.pinMode(BUTTON, b.INPUT);
\r
53 b.pinMode(S_DATA, b.OUTPUT);
\r
54 b.pinMode(S_CLOCK, b.OUTPUT);
\r
55 b.pinMode(S_LATCH, b.OUTPUT);
\r
56 b.pinMode(S_CLEAR, b.OUTPUT);
\r
57 b.digitalWrite(S_DATA, b.LOW);
\r
58 b.digitalWrite(S_CLOCK, b.LOW);
\r
59 b.digitalWrite(S_LATCH, b.LOW);
\r
60 b.digitalWrite(S_CLEAR, b.HIGH);
\r
61 b.i2cOpen(port, address, {}, onI2C);
\r
64 b.i2cWriteBytes(port, 0x2a, [0x00], nextA);
\r
68 b.i2cWriteBytes(port, 0x0e, [0x07], nextB);
\r
72 b.i2cWriteBytes(port, 0x2a, [0x01], pjs.setup);
\r
75 // Setup the Processing Canvas
\r
76 pjs.setup = function() {
\r
78 pjs.strokeWeight(10);
\r
87 pjs.draw = function() {
\r
88 // Calculate some fading values based on the frame count
\r
89 radius = 50.0 + (15-sliderStatus)*pjs.sin(pjs.frameCount / 4);
\r
90 brightness = (radius - 40.0) / 20.0;
\r
92 // Set the RGB LED color based on the status of the button
\r
94 b.analogWrite(LED_RED, brightness);
\r
95 b.analogWrite(LED_BLUE, 0);
\r
97 b.analogWrite(LED_RED, 0);
\r
98 b.analogWrite(LED_BLUE, brightness);
\r
101 // Track circle to new destination
\r
105 // Fill canvas grey
\r
106 pjs.background(100);
\r
108 // Set fill-color to blue or red, based on button status
\r
109 if(buttonStatus) pjs.fill(200, 30, 20)
\r
110 else pjs.fill(0, 121, 184);
\r
112 // Set stroke-color white
\r
116 pjs.ellipse(X, Y, radius, radius);
\r
118 // Fetch slider location for next time
\r
119 b.analogRead(POT, onAnalogRead);
\r
121 // Fetch accelerometer status
\r
122 b.i2cReadBytes(port, 1, 6, onReadBytes);
\r
124 // Fetch button status
\r
125 b.digitalRead(BUTTON, onDigitalRead);
\r
128 // Handle data back from potentiometer
\r
129 function onAnalogRead(x) {
\r
130 if(!x.err && (x.value >= 0) && (x.value <= 1)) {
\r
131 if(Math.abs(x.value - lastSliderValue) > 0.05) {
\r
132 lastSliderValue = x.value;
\r
133 sliderStatus = parseInt(x.value*10, 10);
\r
134 $('#sliderStatus').html(sliderStatus);
\r
135 write7Seg(sliderStatus);
\r
140 function write7Seg(x) {
\r
141 var c = segDigits[x%10];
\r
142 b.shiftOut(S_DATA, S_CLOCK, b.MSBFIRST, c, onShiftOut);
\r
145 function onShiftOut(x) {
\r
146 b.digitalWrite(S_LATCH, b.HIGH, onLatchHigh);
\r
149 function onLatchHigh(x) {
\r
150 b.digitalWrite(S_LATCH, b.LOW);
\r
153 function onWriteByte(x) {
\r
154 if(x.event == 'callback') {
\r
155 b.i2cReadBytes(port, 1, 6, onReadBytes);
\r
159 function onReadBytes(x) {
\r
160 if(x.event == 'callback') {
\r
164 if(nX >= 128) nX = -((nX^0xFF)+1);
\r
165 if(nY >= 128) nY = -((nY^0xFF)+1);
\r
166 if(nZ >= 128) nZ = -((nZ^0xFF)+1);
\r
171 $('#nX').html(nX);
\r
172 $('#nY').html(nY);
\r
173 $('#nZ').html(nZ);
\r
177 // Handle data back from button
\r
178 function onDigitalRead(x) {
\r
179 buttonStatus = (x.value == b.LOW) ? 1 : 0;
\r
180 $('#buttonStatus').html(buttonStatus);
\r
188 <p>buttonStatus = <span id="buttonStatus"></span></p>
\r
189 <p>sliderStatus = <span id="sliderStatus"></span></p>
\r
190 <p>nX = <span id="nX"></span></p>
\r
191 <p>nY = <span id="nY"></span></p>
\r
192 <p>nZ = <span id="nZ"></span></p>
\r
193 <div style="clear:both;"></div></body>
194 <pseudoparent idref="2892" prototyperef="Page"/>
195 <http_referer>http://beagleboard.org/support/BoneScript/processingjs/accelerometer/edit</http_referer>
196 <http_host>beagleboard.org</http_host>
197 <user>jkridner@gmail.com</user>