Hardware Reference
In-Depth Information
Discussion
Wow! What is happening here? The JavaScript code is running on the browser, but when
you get to a BoneScript statement such as
b.analogRead(SLIDER,
onSliderRead);
, the browser does a remote procedure call (RPC) to the Bone to read
the value. The value is returned to the browser, and it continues running. This is a very
powerful setup. The same BoneScript code that runs on the Bone and interacts with the
hardware can also run on a remote browser and
still interact with the Bone
!
Let's take a close look at the code in
Example 6-4
.
Example 6-4. BoneScript running in a browser and reading BeagleBone Black hardware
(jQueryDemo.js)
setTargetAddress
(
'192.168.7.2'
, {
initialized
:
run
});
function
run
() {
var
b
=
require
(
'bonescript'
);
var
SLIDER
=
'P9_36'
;
var
BUTTON
=
'P8_19'
;
b
.
pinMode
(
BUTTON
,
b
.
INPUT
);
getSliderStatus
();
function
getSliderStatus
() {
b
.
analogRead
(
SLIDER
,
onSliderRead
);
}
function
onSliderRead
(
x
) {
if
(!
x
.
err
) {
$
(
'#sliderStatus'
).
html
(
x
.
value
.
toFixed
(
3
));
}
getButtonStatus
()
}
function
getButtonStatus
() {
b
.
digitalRead
(
BUTTON
,
onButtonRead
);
}
function
onButtonRead
(
x
) {
if
(!
x
.
err
) {
$
(
'#buttonStatus'
).
html
(
x
.
value
);