Skip to content

Commit

Permalink
also mirobot
Browse files Browse the repository at this point in the history
  • Loading branch information
forresto committed Oct 25, 2014
1 parent c6348e0 commit 14f0f7e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 47 deletions.
22 changes: 15 additions & 7 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,24 @@
</style>

<!-- Libs -->
<script src="http://d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="js/rawinflate.js"></script>
<script src="js/rawdeflate.js"></script>
<script src="./js/ace/ace.js" type="text/javascript" charset="utf-8"></script>
<script src="./js/rawinflate.js"></script>
<script src="./js/rawdeflate.js"></script>
<script src="./js/mirobot.js"></script>
<!-- App -->
<script src="turtle-svg.js"></script>
<script src="./turtle-svg.js"></script>
<script src="./turtle-mirobot.js"></script>

</head>
<body>

<div id="editor">// Press CMD + ENTER to run.
for(var i=0; i&lt;50000; i++) {
turnLeft(0.1-Math.random()*0.2);
moveForward(1);
var points = 5;
for (var i=0; i&lt;points; i++) {
moveForward(50);
turnLeft(1/points);
moveForward(50);
turnRight(2*(1/points));
}
</div>

Expand Down Expand Up @@ -158,6 +163,9 @@
<div>
<button id="export">Export SVG</button> <button id="export-png" style="display:none;">PNG</button>
</div>
<div>
<button id="mirobot">Send to Mirobot</button>
</div>
</div>

</div>
Expand Down
1 change: 0 additions & 1 deletion js/worker-javascript.js

This file was deleted.

37 changes: 37 additions & 0 deletions turtle-mirobot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
window.TurtleMirobot = function(options) {
if (!options || !options.address || !options.commands) {
return;
}

var commands = []
var paths = options.commands;
for (var i = 0, len=paths.length; i<len; i++) {
path = paths[i];
if (path.commands && path.commands.length) {
commands = commands.concat(path.commands);
}
}

var index = 0
var next = function (status, msg) {
console.log(arguments);
if (status !== 'complete') {
console.log(' a');
return;
}
if (commands[index]) {
console.log('sending '+index, commands[index]);
mb.send(commands[index], next);
index++;
}
};
var nextInABit = function (status, msg) {
console.log("f");
setTimeout(function(){
next('complete');
}, 500);
};

var mb = new Mirobot(options.address, nextInABit);

};
51 changes: 13 additions & 38 deletions turtle-svg-worker.js → turtle-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,28 @@ var _turn = function(){
};

var turnRight, r;
turnRight = r = function(angle){
turnRight = r = function (angle) {
if (isNaN(angle)) { return; }

turnLeft(-angle);
angle = -angle;
_currentAngle += angle;
_currentAngle = _currentAngle%1;
_currentPath.commands.push({cmd:'right', arg:0-angle*360});
_turn();
};
var turnLeft, l;
turnLeft = l = function(angle){
turnLeft = l = function (angle) {
if (isNaN(angle)) { return; }

_currentAngle += angle;
_currentAngle = _currentAngle%1;
_currentPath.commands.push({cmd:'left', arg:angle*360});
_turn();
};

// Absolute turn
var turnTo, t;
turnTo = t = function(angle){
turnTo = t = function (angle) {
if (isNaN(angle)) { return; }

_currentAngle = angle;
Expand All @@ -62,10 +67,12 @@ turnTo = t = function(angle){
var penUp, u;
penUp = u = function(){
_pen = false;
_currentPath.commands.push({cmd:'penup'});
};
var penDown, d;
penDown = d = function(){
_pen = true;
_currentPath.commands.push({cmd:'pendown'});
};

// Set color and make a new path
Expand All @@ -80,6 +87,7 @@ var color = function(color, fill) {
var newPath = {
color: color,
d: "M " + _position.x + " " + _position.y + " ",
commands: [],
fill: fill
};
_paths.push(newPath);
Expand Down Expand Up @@ -111,6 +119,7 @@ moveForward = f = function (distance) {

_currentPath.d += _pen ? "l " : "m ";
_currentPath.d += x + " " + y + " ";
_currentPath.commands.push({cmd:'forward', arg:distance});
_moveCount++;
};

Expand Down Expand Up @@ -151,37 +160,3 @@ var lineBy = function (x, y) {
_currentPath.d += "l " + x + " " + y + " ";
_moveCount++;
};

// Worker setup
self.onmessage = function(e) {

_resetTurtle();

try {
eval(e.data);

// Build SVG string
var svg = '<svg id="turtle-svg" xmlns="http://www.w3.org/2000/svg" version="1.1" width="'+
Math.ceil(_max.x) + '" height="' +
Math.ceil(_max.y) +'">'+"\n";
for (var i=0; i<_paths.length; i++) {
var path = _paths[i];
svg += ' <path id="turtle-path-'+ i +'" '+
'stroke="' + path.color + '" '+
'd="' + path.d + '" '+
'fill="' + (path.fill ? path.fill : 'none') + '" vector-effect="non-scaling-stroke" />' + "\n";
}
svg += '</svg>';

self.postMessage({
svg: svg,
code: e.data
});

// Terminate self
self.close();
} catch (error) {
// err
self.postMessage("");
}
};
16 changes: 15 additions & 1 deletion turtle-svg.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
window.onload = function(){

ace.config.set("workerPath", "js");
ace.config.set("workerPath", "./js/ace");
var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
var session = editor.getSession();
Expand All @@ -21,6 +21,7 @@ window.onload = function(){
var svgContainer = document.getElementById("svg-container");
var svgImage = document.getElementById("svg-image");
var info = document.getElementById("info");
var mirobot = document.getElementById("mirobot");

var testingCode;
var testCodeStart;
Expand All @@ -38,6 +39,8 @@ window.onload = function(){
var workerError = false;
var loadingSVG = false;

var currentParsedData;

var labels = {
run: "Run",
cancel: "Abort",
Expand Down Expand Up @@ -94,6 +97,7 @@ window.onload = function(){
applyButton.disabled = true;
// calculate + show the amount of time that was required to complete SVG creation
svgGenTime = Date.now() - testCodeStart;
currentParsedData = e.data;
setSVG(e.data, showStats);
}

Expand Down Expand Up @@ -150,6 +154,16 @@ window.onload = function(){
}
};

mirobot.onclick = function(){
if (!currentParsedData || !currentParsedData.paths) {
return;
}
mirobotConnection = new TurtleMirobot({
address: 'ws://10.10.100.254:8899/websocket',
commands: currentParsedData.paths
});
};

var jshintOK = function() {
var annotations = editor.getSession().getAnnotations();
for (var key in annotations) {
Expand Down

0 comments on commit 14f0f7e

Please sign in to comment.