Skip to content

Commit

Permalink
adding queueing
Browse files Browse the repository at this point in the history
  • Loading branch information
devnook committed Dec 5, 2014
1 parent 5e012a1 commit 0fd7bbc
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 20 deletions.
47 changes: 44 additions & 3 deletions app/blockly-ui/blockly-ui.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,23 @@
},

setColor: function(color) {
this.sendRequest_('/color', { color: color });

var COLOURS = {
'#ff0000': 'red',
'#4169e1': 'royalblue',
'#00ff7f': 'springgreen',
'#ffff00': 'yellow',
'#191970': 'midnightblue',
'#c71585': 'mediumvioletred',
'#adff2f': 'greenyellow'

};


colorName = COLOURS[color];
console.log(color, colorName)

this.sendRequest_('/color', { color: colorName });
},

sendRequest_: function(url, content) {
Expand Down Expand Up @@ -149,7 +165,7 @@

#blockly-wrapper {
width: 60%;
height: 480px;
height: 560px;
}

#editor {
Expand All @@ -160,7 +176,7 @@

#code-wrapper {
width: 40%;
height: 480px;
height: 560px;
background-color: #fff;
}

Expand Down Expand Up @@ -223,6 +239,7 @@
<block type="sphero_roll"></block>
<block type="sphero_turn"></block>
<block type="sphero_set_color"></block>
<block type="colour_picker"></block>
</xml>


Expand Down Expand Up @@ -267,6 +284,30 @@
attached: function() {
Blockly.HSV_SATURATION = 1;
Blockly.HSV_VALUE = 0.75;

var COLOURS = {
'#ff0000': 'red',
'#4169e1': 'royalblue',
'#00ff7f': 'springgreen',
'#ffff00': 'yellow',
'#191970': 'midnightblue',
'#c71585': 'mediumvioletred',
'#adff2f': 'greenyellow'

};

Blockly.FieldColour.COLOURS = [
'#FF0000', // red
'#4169E1', // royalblue
'#00FF7F', // springgreen
'#FFFF00', // yellow
'#191970', // midnightblue
'#c71585', // mediumvioletred
'#adff2f' // greenyellow

];


//configureBlockColor('text_print', 340); // pink
//configureBlockColor('math_number', 330); // blue
console.log(Blockly.Blocks.text.init)
Expand Down
9 changes: 6 additions & 3 deletions app/blockly-ui/sphero_blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ Blockly.Blocks['sphero_set_color'] = {
this.setHelpUrl('http://www.example.com/');
this.setColour(330);
this.appendDummyInput()
.appendField("set color to")
.appendField(new Blockly.FieldAngle("270"), "color");
.appendField("set color");
this.appendValueInput("color");
this.setInputsInline(true);
this.setPreviousStatement(true);
this.setNextStatement(true);
this.setTooltip('');
Expand All @@ -69,8 +70,10 @@ Blockly.Blocks['sphero_set_color'] = {

Blockly.JavaScript['sphero_set_color'] = function(block) {
var angle_color = block.getFieldValue('color');
var color = Blockly.JavaScript.valueToCode(
block, 'color', Blockly.JavaScript.ORDER_ATOMIC);
// TODO: Assemble JavaScript into code variable.
var code = 'color = ' + angle_color + ';\n' +
var code = 'color = ' + color + ';\n' +
'ball.setColor(color);\n';
return code;
};
Expand Down
70 changes: 60 additions & 10 deletions sphero/sphero_driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ var ROBOT_NAME = 'Purple with joy';
var spheroReady = false;

Cylon.robot({
connection: { name: 'sphero', adaptor: 'sphero', port: '/dev/cu.Sphero-RRP-AMP-SPP' },
device: { name: 'Sphero-RRP', driver: 'sphero' },
//connection: { name: 'sphero', adaptor: 'sphero', port: '/dev/cu.Sphero-RRP-AMP-SPP' },
//device: { name: 'Sphero-RRP', driver: 'sphero' },
connection: {
name: 'sphero',
adaptor: 'sphero',
port: '/dev/tty.Sphero-RYR-AMP-SPP-6'
},
device: { name: 'Sphero-RYR', driver: 'sphero' },

name: ROBOT_NAME,

Expand All @@ -23,14 +29,16 @@ Cylon.robot({

work: function(my) {
var white = true;
console.log('start working')
console.log(white)
every((1).second(), function(){
var color = my.readMyColor();
if (white) {
my.sphero.setColor('white');
} else {
my.sphero.setColor(color);
}
white = !white
white = !white;
});
},

Expand All @@ -41,6 +49,7 @@ Cylon.robot({
setColor: function(color) {
console.log("Sphero changing to color " + color);
this.myColor = color;
this.stop();
},

turn: function(direction) {
Expand All @@ -55,7 +64,9 @@ Cylon.robot({
}

if (this.myAngle >= 360) this.myAngle -= 360;
console.log("Sphero turning " + direction + ", will roll at " + this.myAngle + " degrees");
console.log("Sphero turning " + direction + ", will roll at " +
this.myAngle + " degrees");
this.stop();
},

roll: function(units, direction) {
Expand All @@ -71,16 +82,24 @@ Cylon.robot({
this.sphero.roll(this.mySpeed, rollAngle, 1);
after((units).seconds(), function() {
console.log("Sphero stopping after " + units + " seconds");
// this.stop();
});
this.stop();
}.bind(this));
},

stop: function() {
console.log("Sphero stopping");
this.sphero.stop();
queue.shift();

if (queue.length) {
queue[0].handler.apply(Cylon.robots[ROBOT_NAME], queue[0].params);
}

}
});

var queue = [];


function spheroStart() {
Cylon.robots[ROBOT_NAME].on('ready', function() {
Expand All @@ -94,15 +113,46 @@ function spheroStop() {
}

function spheroRoll(units, direction) {
Cylon.robots[ROBOT_NAME].roll(units, direction);
//Cylon.robots[ROBOT_NAME].roll(units, direction);
console.log(JSON.stringify(queue))

queue.push({
'handler': Cylon.robots[ROBOT_NAME].roll,
'params': [units, direction]
});
console.log('spheroRoll', queue);
if (queue.length == 1) {
queue[0].handler.apply(Cylon.robots[ROBOT_NAME], queue[0].params);
}

}

function spheroSetColor(color) {
Cylon.robots[ROBOT_NAME].setColor(color);
//Cylon.robots[ROBOT_NAME].setColor(color);

queue.push({
'handler': Cylon.robots[ROBOT_NAME].setColor,
'params': [color]
});
console.log('spheroSetColor queue', queue);
if (queue.length == 1) {
queue[0].handler.apply(Cylon.robots[ROBOT_NAME], queue[0].params);
}

}

function spheroTurn(direction) {
Cylon.robots[ROBOT_NAME].turn(direction)
//Cylon.robots[ROBOT_NAME].turn(direction)

queue.push({
'handler': Cylon.robots[ROBOT_NAME].turn,
'params': [direction]
});
console.log('spheroturn queue', queue);
if (queue.length == 1) {
queue[0].handler.apply(Cylon.robots[ROBOT_NAME], queue[0].params);
}

}

function restart() {
Expand All @@ -113,4 +163,4 @@ function restart() {

function isReady() {
return spheroReady;
}
}
8 changes: 4 additions & 4 deletions sphero/sphero_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var router = new Director.http.Router({
startSpheroDriver();
function startSpheroDriver() {
var port = Number(process.argv[2]);
// SpheroDriver.start();
SpheroDriver.start();
var server = Http.createServer(spheroServerDispatch);
console.log("Listening on port " + port)
server.listen(port);
Expand Down Expand Up @@ -62,21 +62,21 @@ function spheroRoll() {
var units = params.units;
var direction = params.direction;
console.log("Roll " + units + " units in direction " + direction);
// SpheroDriver.roll(units, direction);
SpheroDriver.roll(units, direction);
endOk(this.res);
}

function spheroColor() {
var color = this.req.body.color;
console.log("Set color to " + color);
// SpheroDriver.setColor(color);
SpheroDriver.setColor(color);
endOk(this.res);
}

function spheroTurn() {
var direction = this.req.body.direction;
console.log("Turning to the " + direction);
// SpheroDriver.turn(direction)
SpheroDriver.turn(direction)
endOk(this.res);
}

Expand Down

0 comments on commit 0fd7bbc

Please sign in to comment.