Skip to content

Commit 4e45e83

Browse files
committed
Added live Sphero SPRK+ simulation for position and color.
1 parent cad4911 commit 4e45e83

File tree

12 files changed

+113
-22
lines changed

12 files changed

+113
-22
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"documentation": "jsdoc src -r -c documentation.json -d docs",
8282
"launch": "echo Lauching app. Please wait ... && npm run launch:linux --silent || npm run launch:mac --silent || npm run launch:win-experimental --silent",
8383
"launch:linux": "google-chrome --load-and-launch:app=$PWD/dist/chrome_os --enable-logging --v=1 --no-first-run",
84-
"launch:mac": "\"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome\" --load-and-launch:app=$PWD/dist/chrome_os --enable-logging --v=1 --no-first-run",
84+
"launch:mac": "\"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome\" --load-and-launch-app=$PWD/dist/chrome_os --enable-logging --v=1 --no-first-run",
8585
"launch:nw": "nw dist/nw_app",
8686
"launch:web": "http-server dist/web_app",
8787
"launch:win": "echo && \"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\" --profile-directory=Default --app-id=becloognjehhioodmnimnehjcibkloed --enable-logging --v=1 --no-first-run",

src/blocks/sphero/sprk_plus/javascript.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ Blockly.JavaScript['sphero_sprk_plus_roll_time'] = function(block) {
6868
block, 'time', Blockly.JavaScript.ORDER_ATOMIC);
6969
let speed = Blockly.JavaScript.valueToCode(
7070
block, 'speed', Blockly.JavaScript.ORDER_ATOMIC);
71-
let heading = parseInt(Blockly.JavaScript.valueToCode(
72-
block, 'heading', Blockly.JavaScript.ORDER_ATOMIC));
71+
let heading = Blockly.JavaScript.valueToCode(
72+
block, 'heading', Blockly.JavaScript.ORDER_ATOMIC);
7373
return 'sprkPlus.rollTime(' + time + ', ' + speed + ', ' + heading +
7474
', true);\n';
7575
};

src/frameworks/turtle/turtle.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ cwc.framework.Turtle.prototype.addListener = function() {
9696
this.listener_['turnto'] = this.handleTurnto_.bind(this);
9797
this.listener_['play'] = this.handlePlay_.bind(this);
9898

99+
// Custom commands
100+
this.listener_['moveToXY'] = this.handleMoveToXY_.bind(this);
101+
99102
// Methods below happen in an instant, but line up in the animation queue.
100103
this.listener_['home'] = this.handleHome_.bind(this);
101104
this.listener_['pen'] = this.handlePen_.bind(this);
@@ -213,12 +216,24 @@ cwc.framework.Turtle.prototype.handleJump_ = function(x, y) {
213216

214217
/**
215218
* Absolute motion on page.
216-
* @param {number} x
217-
* @param {number} y
219+
* @param {Object} data
220+
* @private
221+
*/
222+
cwc.framework.Turtle.prototype.handleMoveto_ = function(data) {
223+
this.turtleTarget['moveto']({
224+
'pageX': data['x'],
225+
'pageY': data['y'],
226+
});
227+
};
228+
229+
230+
/**
231+
* X/Y motion on page.
232+
* @param {Object} data
218233
* @private
219234
*/
220-
cwc.framework.Turtle.prototype.handleMoveto_ = function(x, y) {
221-
this.turtleTarget['moveto']({pageX: x, pageY: y});
235+
cwc.framework.Turtle.prototype.handleMoveToXY_ = function(data) {
236+
this.turtleTarget['moveto'](data['x'], data['y']);
222237
};
223238

224239

src/mode/sphero/sprk_plus/mod.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ cwc.mode.sphero.sprkPlus.Mod = function(helper, enableBlockly = false) {
6060
this.monitor = new cwc.mode.sphero.sprkPlus.Monitor(helper, this.connection);
6161

6262
/** @type {!cwc.mode.sphero.sprkPlus.Simulation} */
63-
this.simulation = new cwc.mode.sphero.sprkPlus.Simulation(helper);
63+
this.simulation = new cwc.mode.sphero.sprkPlus.Simulation(helper,
64+
this.connection);
6465

6566
/** @type {!cwc.renderer.external.sphero.SprkPlus} */
6667
this.renderer = new cwc.renderer.external.sphero.SprkPlus(helper);

src/mode/sphero/sprk_plus/panel/simulation/command.js renamed to src/mode/sphero/sprk_plus/simulation/command.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @fileoverview Simulation Commands for the Sphero SPRK+ unit.
33
*
4-
* @license Copyright 2015 The Coding with Chrome Authors.
4+
* @license Copyright 2018 The Coding with Chrome Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -36,7 +36,10 @@ cwc.mode.sphero.sprkPlus.SimulationCommand = function(turtle) {
3636
this.heading_ = 0;
3737

3838
/** @private {number} */
39-
this.speed_ = 40;
39+
this.scale_ = 1;
40+
41+
/** @private {number} */
42+
this.speed_ = 10;
4043
};
4144

4245

@@ -45,9 +48,8 @@ cwc.mode.sphero.sprkPlus.SimulationCommand = function(turtle) {
4548
*/
4649
cwc.mode.sphero.sprkPlus.SimulationCommand.prototype['__handshake__'] =
4750
function() {
48-
this.speed_ = 40;
49-
this.turtle.action('speed', 2);
50-
this.turtle.action('scale', 1);
51+
this.turtle.action('scale', this.scale_);
52+
this.turtle.action('speed', this.speed_);
5153
this.turtle.reset();
5254
};
5355

@@ -72,3 +74,29 @@ cwc.mode.sphero.sprkPlus.SimulationCommand.prototype['roll'] = function(data) {
7274
this.turtle.action('rt', angle);
7375
this.turtle.action('fd', speed);
7476
};
77+
78+
79+
/**
80+
* @param {!Object} data
81+
*/
82+
cwc.mode.sphero.sprkPlus.SimulationCommand.prototype['setRGB'] = function(
83+
data) {
84+
let hexColor = '#' +
85+
Number(data['red']).toString(16).padStart(2, 0) +
86+
Number(data['green']).toString(16).padStart(2, 0) +
87+
Number(data['blue']).toString(16).padStart(2, 0);
88+
if (hexColor === '#000000') {
89+
this.turtle.action('pen', null);
90+
} else {
91+
this.turtle.action('pen', hexColor);
92+
}
93+
};
94+
95+
96+
/**
97+
* @param {!Object} data
98+
*/
99+
cwc.mode.sphero.sprkPlus.SimulationCommand.prototype['position'] = function(
100+
data) {
101+
this.turtle.action('moveToXY', {'x': data['x'], 'y': data['y']});
102+
};

src/mode/sphero/sprk_plus/panel/simulation/simulation.js renamed to src/mode/sphero/sprk_plus/simulation/simulation.js

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,24 @@ goog.require('cwc.ui.Turtle');
2525
goog.require('cwc.utils.Events');
2626

2727

28+
goog.scope(function() {
29+
const Events = goog.module.get('cwc.lib.protocol.sphero.sprkPlus.Events');
30+
2831
/**
2932
* @param {!cwc.utils.Helper} helper
33+
* @param {!cwc.mode.sphero.sprkPlus.Connection} connection
3034
* @constructor
3135
*/
32-
cwc.mode.sphero.sprkPlus.Simulation = function(helper) {
36+
cwc.mode.sphero.sprkPlus.Simulation = function(helper, connection) {
3337
/** @type {string} */
3438
this.name = 'Sphero SPRK+ Simulation';
3539

3640
/** @type {string} */
3741
this.prefix = helper.getPrefix('sphero-sprk-plus-simulation');
3842

43+
/** @type {!cwc.mode.sphero.sprkPlus.Connection} */
44+
this.connection = connection;
45+
3946
/** @type {Element} */
4047
this.node = null;
4148

@@ -63,6 +70,12 @@ cwc.mode.sphero.sprkPlus.Simulation = function(helper) {
6370
/** @type {!cwc.ui.Turtle} */
6471
this.turtle = new cwc.ui.Turtle(helper, this.sprite);
6572

73+
/** @private {Object} */
74+
this.cache_ = {};
75+
76+
/** @type {boolean} */
77+
this.connected_ = false;
78+
6679
/** @type {!cwc.mode.sphero.SimulationCommand} */
6780
this.commands_ = new cwc.mode.sphero.sprkPlus.SimulationCommand(this.turtle);
6881

@@ -95,6 +108,11 @@ cwc.mode.sphero.sprkPlus.Simulation.prototype.decorate = function(node) {
95108
this.events_.listen(previewInstance.getEventTarget(),
96109
cwc.MessengerEvents.Type.COMMAND, this.handleCommand_);
97110
}
111+
112+
// Roboter event
113+
let connectionEvent = this.connection.getEventTarget();
114+
this.events_.listen(
115+
connectionEvent, Events.Type.POSITION, this.handlePosition_);
98116
};
99117

100118

@@ -115,5 +133,34 @@ cwc.mode.sphero.sprkPlus.Simulation.prototype.handleCommand_ = function(e) {
115133
if (typeof this.commands_[e.data['name']] === 'undefined') {
116134
return;
117135
}
118-
this.commands_[e.data['name']](e.data['value']);
136+
let commandName = e.data['name'];
137+
if (commandName === '__handshake__') {
138+
this.cache_ = {};
139+
this.connected_ = false;
140+
}
141+
// Exclude specific commands if device is connected.
142+
if (this.connected_) {
143+
switch (commandName) {
144+
case 'roll':
145+
return;
146+
}
147+
}
148+
this.commands_[commandName](e.data['value']);
149+
};
150+
151+
152+
/**
153+
* @param {!Event} e
154+
* @private
155+
*/
156+
cwc.mode.sphero.sprkPlus.Simulation.prototype.handlePosition_ = function(e) {
157+
if (this.cache_['position'] &&
158+
this.cache_['position']['x'] === e.data['x'] &&
159+
this.cache_['position']['y'] === e.data['y']) {
160+
return;
161+
}
162+
this.connected_ = true;
163+
this.commands_['position'](e.data);
164+
this.cache_['position'] = e.data;
119165
};
166+
});

src/ui/blockly/blockly_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ goog.provide('cwc.ui.BlocklyConfig');
2121

2222

2323
/**
24-
* @emum {string|objct<string>|array<string>}
24+
* @enum {string|objct<string>|array<string>}
2525
*/
2626
cwc.ui.BlocklyConfig = {
2727
'path': '../external/blockly/',

src/ui/editor/editor_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ goog.provide('cwc.ui.EditorConfig');
2121

2222

2323
/**
24-
* @emum {string|objct<string>|array<string>}
24+
* @enum {string|objct<string>|array<string>}
2525
*/
2626
cwc.ui.EditorConfig = {
2727
'autoCloseBrackets': true,

0 commit comments

Comments
 (0)