Skip to content

Instantly share code, notes, and snippets.

@sfentress
Created May 21, 2012 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sfentress/2763673 to your computer and use it in GitHub Desktop.
Save sfentress/2763673 to your computer and use it in GitHub Desktop.
Arduino Data
// [KCPT]
// The dgApi object contains everything needed to communicate back to the DG application.
dgApi = {};
// For now we go through 'DG.currGameController.doCommand'.
// Very shortly a development branch will be merged to the trunk
// which simplifies that to 'DG.doCommand'.
dgApi.gameController = window.parent.DG.currGameController;
dgApi.doCommand = dgApi.gameController && dgApi.gameController.doCommand;
// Mainly for debugging -- turns off creation of cases in DG
dgApi.isEnabled = true;
// Cases can be grouped into "Runs". Feel free to change to a better name.
// I'm not sure where the best place to end a run is -- starting and stopping
// the simulation doesn't seem like an appropriate place to end a run for instance.
dgApi.runCount = 0;
/**
Initializes the communication between the model and DG.
Passes information about the names of collection, names and types of attributes, etc.
Quotes follow DG convention of using double quotes for user-focused strings and
single quotes for strings used by the code. In general, changing a double-quoted string
will affect what the user sees but not affect the operation of the program, whereas
changing a single-quoted string can change the behavior of the program. Thus, changing
the name of the game from "Simple Atoms Model" won't break anything, but changing
'initGame' to something else will prevent the proper initialization from occurring.
*/
dgApi.initGame = function() {
if( !dgApi.doCommand) return;
dgApi.doCommand.call( dgApi.gameController, {
action: 'initGame',
args: {
name: "Arduino Data",
dimensions: { width: 525, height: 625 },
collections: [
{ // parent collection -- Runs
name: "Runs",
attrs: [ { name: "run", type: 'numeric', description: "The run number", precision: 0 }
],
childAttrName: "run"
},
{ // Child collection -- Steps
name: "Steps",
attrs: [ { name: "step", type: 'numeric', description: "Current step", precision: 0 },
{ name: "a0Reading", type: 'numeric', description: "Reading from pin A0", precision: 2 }
],
// default attributes for initial plot
defaults: {
xAttr: "step",
yAttr: "a0Reading"
}
}
]
}
});
};
/**
Begins a "Run", i.e. the collection of a set of related cases (Steps).
Client should call endRun() when the Run is complete.
*/
dgApi.beginRun = function() {
if( !dgApi.doCommand) return;
var result = dgApi.doCommand.call( dgApi.gameController, {
action: 'openCase',
args: {
collection: "Runs",
// increment the runCount when we start a run
values: [ ++dgApi.runCount ]
}
});
// Returns the ID of the parent case, which should be passed
// in subsequent calls to create the child cases.
if( result.success)
dgApi.openRunID = result.caseID;
return result;
};
/**
Ends a "Run". This is not currently called as I wasn't sure when to do so.
*/
dgApi.endRun = function() {
if( !dgApi.doCommand) return;
dgApi.doCommand.call( dgApi.gameController, {
action: 'closeCase',
args: {
collection: "Runs",
// Passes the case ID returned by the call to 'openCase'
caseID: dgApi.openRunID
}
});
// Null out the openRunID so it can't get used inadvertently.
dgApi.openRunID = null;
};
/**
Creates a DG case corresponding to the current state of the model.
The model state supported currently is the five values returned by model.getStats(),
but additional properties could be added as well. To do so, the corresponding
attribute descriptions should be added to the 'initGame' call, and the code should
be changed here to pass the corresponding value in the array passed to doCommand.
@param {Object} iModel -- The current model state
*/
dgApi.addTick = function( data) {
if( !dgApi.isEnabled || !dgApi.doCommand) return;
dgApi.doCommand.call( dgApi.gameController, {
action: 'createCase',
args: {
collection: "Steps",
parent: dgApi.openRunID,
values: [ data.step, data.a0Reading ]
}
});
};
// [/KCPT]
<html>
<head>
<script src="http://mbostock.github.com/d3/d3.v2.js?2.8.1"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="dgApi.js"></script>
<script type="text/javascript">
$(function() {
var graph = null,
formatter = null,
dataSet = [],
graphPin = null;
dgApi.initGame();
dgApi.beginRun();
$(function() {
formatter = d3.format("2.2f");
$('#run-button').click(function() {
setInterval(getData, 250);
$('#run-button').html("Running...")
.attr("disabled", true)
.unbind();
});
});
function getData() {
$.getJSON("http://169.254.1.1/&callback=?", window.arduinoEthernetComCallback);
/* comment-out the above and uncomment the code below to generate random data */
// r = formatter(40 + (Math.random() * 50));
// callback('{"A0":' + r + ',"A1":30,"A2":20,"A3":10,"A4":30,"A5":20}');
};
var step = 0;
window.arduinoEthernetComCallback = function(jsonData) {
data = JSON.parse(jsonData);
for (pin in data) {
var val = data[pin],
voltage = (val * 5.0) / 1024.0,
conv = $("#" + pin + "-conversion").val(),
convValue = convert(voltage, conv);
$("#" + pin).html(val);
$("#" + pin + "-volts").html(formatter(voltage));
$("#" + pin + "-convValue").html(convValue ? formatter(convValue) : "");
}
console.log("{step:"+step+", a0Reading: "+data.A0+"}");
step++;
dgApi.addTick({step: step, a0Reading: data.A0});
};
function convert(value, type) {
return convos[type](value);
};
// set of conversion functions
var convos = {
none: function(volts) {
return "";
},
L35: function(volts) {
return volts * 100;
},
TMP36: function(volts) {
return (volts - 0.5) * 100;
},
A1301: function(volts) {
return ((volts - 2.5) / 2.5) * 1000;
},
};
});
</script>
</head>
<body>
<button id="run-button">Start Reading</button>
<div>
The current readings are:
<div id="values">
<table>
<tr>
<th>
Pin
</th>
<th>
Value
</th>
<th>
Volts
</th>
<th>
Conversion
</th>
<th>
Converted
</th>
<tr>
<th>A0</th>
<td id="A0" class="value"></td>
<td id="A0-volts" class="volts"></td>
<td>
<select id="A0-conversion">
<option value="none"></option>
<option value="L35">L35 temperature</option>
<option value="TMP36">TMP36 temperature</option>
<option value="A1301">A1301 Hall Effect</option>
</select>
</td>
<td id="A0-convValue">
</td>
</tr>
<tr>
<th>A1</th>
<td id="A1"></td>
<td id="A1-volts" class="volts"></td>
<td>
<select id="A1-conversion">
<option value="none"></option>
<option value="L35">L35 temperature</option>
<option value="TMP36">TMP36 temperature</option>
<option value="A1301">A1301 Hall Effect</option>
</select>
</td>
<td id="A1-convValue">
</td>
</tr>
<tr>
<th>A2</th>
<td id="A2"></td>
<td id="A2-volts" class="volts"></td>
<td>
<select id="A2-conversion">
<option value="none"></option>
<option value="L35">L35 temperature</option>
<option value="TMP36">TMP36 temperature</option>
<option value="A1301">A1301 Hall Effect</option>
</select>
</td>
<td id="A2-convValue">
</td>
</tr>
<tr>
<th>A3</th>
<td id="A3"></td>
<td id="A3-volts" class="volts"></td>
<td>
<select id="A3-conversion">
<option value="none"></option>
<option value="L35">L35 temperature</option>
<option value="TMP36">TMP36 temperature</option>
<option value="A1301">A1301 Hall Effect</option>
</select>
</td>
<td id="A3-convValue">
</td>
</tr>
<tr>
<th>A4</th>
<td id="A4"></td>
<td id="A4-volts" class="volts"></td>
<td>
<select id="A4-conversion">
<option value="none"></option>
<option value="L35">L35 temperature</option>
<option value="TMP36">TMP36 temperature</option>
<option value="A1301">A1301 Hall Effect</option>
</select>
</td>
<td id="A4-convValue">
</td>
</tr>
<tr>
<th>A5</th>
<td id="A5"></td>
<td id="A5-volts" class="volts"></td>
<td>
<select id="A5-conversion">
<option value="none"></option>
<option value="L35">L35 temperature</option>
<option value="TMP36">TMP36 temperature</option>
<option value="A1301">A1301 Hall Effect</option>
</select>
</td>
<td id="A5-convValue">
</td>
</tr>
</table>
</div>
</div>
</body>
</html>​
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment