Skip to content

Commit b32074b

Browse files
committed
Added Sphero SPRK+ position event.
1 parent 5d044ff commit b32074b

File tree

14 files changed

+813
-45
lines changed

14 files changed

+813
-45
lines changed

package-lock.json

Lines changed: 612 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/blocks/sphero/sprk_plus/blocks.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,24 @@ Blockly.Blocks['sphero_sprk_plus_collision'] = {
321321
this.setTooltip(i18t('Detect collision.'));
322322
},
323323
};
324+
325+
326+
/**
327+
* Position change.
328+
*/
329+
Blockly.Blocks['sphero_sprk_plus_position_change'] = {
330+
init: function() {
331+
this.setHelpUrl('');
332+
this.setColour(260);
333+
this.appendDummyInput()
334+
.appendField(Blockly.BlocksTemplate.point())
335+
.appendField(i18t('on position change'));
336+
this.appendStatementInput('CODE')
337+
.appendField(i18t('@@BLOCKS__DO'))
338+
.setAlign(Blockly.ALIGN_CENTRE);
339+
this.setPreviousStatement(true);
340+
this.setNextStatement(true);
341+
this.setTooltip(i18t('Detect position change.'));
342+
},
343+
};
344+

src/blocks/sphero/sprk_plus/javascript.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ Blockly.JavaScript['sphero_sprk_plus_roll_time'] = function(block) {
8080
*/
8181
Blockly.JavaScript['sphero_sprk_plus_stabilization'] = function(block) {
8282
let enable = block.getFieldValue('enable') === 'enable' ? true : false;
83-
return 'sphero.stabilization(' + enable + ');\n';
83+
return 'sphero.setStabilization(' + enable + ');\n';
8484
};
8585

8686

@@ -145,12 +145,24 @@ Blockly.JavaScript['sphero_sprk_plus_stop'] = function(block) {
145145

146146

147147
/**
148-
* Gyro sensor change.
148+
* Collision change.
149149
* @param {!Blockly.block} block
150150
* @return {string}
151151
*/
152152
Blockly.JavaScript['sphero_sprk_plus_collision'] = function(block) {
153153
let statements_code = Blockly.JavaScript.statementToCode(block, 'CODE');
154-
return 'var collisionEvent = function(data) {\n' +
154+
return 'var collisionEvent = function() {\n' +
155155
statements_code + '};\nsphero.onCollision(collisionEvent);\n';
156156
};
157+
158+
159+
/**
160+
* Position change.
161+
* @param {!Blockly.block} block
162+
* @return {string}
163+
*/
164+
Blockly.JavaScript['sphero_sprk_plus_position_change'] = function(block) {
165+
let statements_code = Blockly.JavaScript.statementToCode(block, 'CODE');
166+
return 'var positionEvent = function(position_x, position_y) {\n' +
167+
statements_code + '};\nsphero.onPositionChange(positionEvent);\n';
168+
};

src/blocks/sphero/sprk_plus/toolbox.soy

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,47 @@
6969

7070
<category name="Events" colour="120">
7171
<block type="sphero_sprk_plus_collision"></block>
72+
<block type="sphero_sprk_plus_position_change">
73+
<value name="CODE">
74+
<block type="controls_if">
75+
<value name="IF0">
76+
<block type="logic_operation">
77+
<field name="OP">OR</field>
78+
<value name="A">
79+
<block type="logic_compare">
80+
<field name="OP">GTE</field>
81+
<value name="A">
82+
<block type="variables_get">
83+
<field name="VAR">position_x</field>
84+
</block>
85+
</value>
86+
<value name="B">
87+
<block type="math_number">
88+
<field name="NUM">100</field>
89+
</block>
90+
</value>
91+
</block>
92+
</value>
93+
<value name="B">
94+
<block type="logic_compare">
95+
<field name="OP">GTE</field>
96+
<value name="A">
97+
<block type="variables_get">
98+
<field name="VAR">position_y</field>
99+
</block>
100+
</value>
101+
<value name="B">
102+
<block type="math_number">
103+
<field name="NUM">100</field>
104+
</block>
105+
</value>
106+
</block>
107+
</value>
108+
</block>
109+
</value>
110+
</block>
111+
</value>
112+
</block>
72113
</category>
73114

74115
<sep></sep>

src/frameworks/sphero/sphero.js

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,31 @@ cwc.framework.Sphero = function() {
3939
/** @type {!function(?)} */
4040
this.collisionEvent = function() {};
4141

42+
/** @type {!Object} */
43+
this.data = {
44+
position: {
45+
x: 0,
46+
y: 0,
47+
},
48+
};
49+
50+
/** @private {} */
51+
this.events_ = {
52+
position: function() {},
53+
collision: function() {},
54+
};
55+
4256
/** @private {!cwc.framework.Messenger} */
4357
this.messenger_ = new cwc.framework.Messenger()
4458
.setListenerScope(this)
4559
.addListener('__EVENT__COLLISION', this.handleCollision_)
4660
.addListener('__EVENT__changed_values', function(e) {
4761
console.log('Changed values event', e);
4862
})
63+
.addListener('__EVENT__POSITION', this.handlePositionChange_)
64+
.addListener('__EVENT__RGB', function(e) {
65+
console.log('RGB', e);
66+
})
4967
.addListener('__EVENT__changed_speed', function(e) {
5068
console.log('Changed speed event', e);
5169
});
@@ -62,7 +80,7 @@ cwc.framework.Sphero = function() {
6280
* @param {boolean=} enable
6381
* @export
6482
*/
65-
cwc.framwork.Sphero.prototype.setStabilization = function(enable) {
83+
cwc.framework.Sphero.prototype.setStabilization = function(enable) {
6684
this.messenger_.send('setStabilization', {'enable': enable});
6785
};
6886

@@ -181,7 +199,18 @@ cwc.framework.Sphero.prototype.sleep = function() {
181199
*/
182200
cwc.framework.Sphero.prototype.onCollision = function(func) {
183201
if (goog.isFunction(func)) {
184-
this.collisionEvent = func;
202+
this.events_.collision = func;
203+
}
204+
};
205+
206+
207+
/**
208+
* @param {!Function} func
209+
* @export
210+
*/
211+
cwc.framework.Sphero.prototype.onPositionChange = function(func) {
212+
if (goog.isFunction(func)) {
213+
this.events_.position = func;
185214
}
186215
};
187216

@@ -191,7 +220,17 @@ cwc.framework.Sphero.prototype.onCollision = function(func) {
191220
* @private
192221
*/
193222
cwc.framework.Sphero.prototype.handleCollision_ = function(data) {
194-
this.collisionEvent(data);
223+
this.events_.collision(data);
224+
};
225+
226+
227+
/**
228+
* @param {event} e
229+
* @private
230+
*/
231+
cwc.framework.Sphero.prototype.handlePositionChange_ = function(e) {
232+
this.data.position = e.data;
233+
this.events_.position(e.data.x, e.data.y);
195234
};
196235

197236

src/mode/sphero/sprk_plus/events.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
* @enum {string}
2525
*/
2626
cwc.mode.sphero.sprkPlus.SensorEvents = {
27-
CHANGED_POSITION: 'CHANGED_POSITION',
28-
CHANGED_SPEED: 'CHANGED_SPEED',
29-
CHANGED_VELOCITY: 'CHANGED_VELOCITY',
3027
COLLISION: 'COLLISION',
28+
POSITION: 'POSITION',
29+
RGB: 'RGB',
3130
};

src/mode/sphero/sprk_plus/panel/control/control.gss

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,16 @@
3333
#{$prefix}sphero-sprk-plus-control-commands > .icon_button {
3434
padding: 5px;
3535
}
36+
37+
38+
#{$prefix}sphero-sprk-plus-control-color {
39+
display: inline-block;
40+
padding: 5px;
41+
}
42+
43+
#{$prefix}sphero-sprk-plus-control-color-picker {
44+
background: transparent;
45+
border: 0;
46+
height: 36px;
47+
width: 36px;
48+
}

src/mode/sphero/sprk_plus/panel/control/control.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ goog.require('goog.ui.KeyboardShortcutHandler');
3535
*/
3636
cwc.mode.sphero.sprkPlus.Control = function(helper, connection) {
3737
/** @type {string} */
38-
this.name = 'Sphero 2.0 Control';
38+
this.name = 'Sphero SPRK+ Control';
3939

4040
/** @type {string} */
4141
this.prefix = helper.getPrefix('sphero-sprk-plus-control');
@@ -90,6 +90,18 @@ cwc.mode.sphero.sprkPlus.Control.prototype.decorate = function(node) {
9090
* @private
9191
*/
9292
cwc.mode.sphero.sprkPlus.Control.prototype.addEventHandler_ = function() {
93+
// Set Color
94+
this.events_.listen('color-picker', goog.events.EventType.CHANGE,
95+
function(e) {
96+
let color = /^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(
97+
e.target.value);
98+
this.api.exec('setRGB', {
99+
'red': parseInt(color[1], 16),
100+
'green': parseInt(color[2], 16),
101+
'blue': parseInt(color[3], 16),
102+
});
103+
});
104+
93105
// Movements
94106
this.events_.listen('move-left', goog.events.EventType.CLICK, function() {
95107
this.api.exec('roll', {'speed': 50, 'heading': 270});

src/mode/sphero/sprk_plus/panel/control/control.soy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
</div>
3838

3939
<div id="{$prefix}commands">
40+
<div id="{$prefix}color">
41+
<input type="color" id="{$prefix}color-picker" name="color" value="#ffffff" />
42+
</div>
43+
<div class="mdl-tooltip" for="{$prefix}color">
44+
Change the color of the LED.
45+
</div>
4046
<div id="{$prefix}flash" class="icon_36px icon_button">flare</div>
4147
<div class="mdl-tooltip" for="{$prefix}flash">
4248
Let the connected unit flash.

src/mode/sphero/sprk_plus/panel/monitor/monitor.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ cwc.mode.sphero.sprkPlus.Monitor = function(helper, connection) {
6060
/** @type {boolean} */
6161
this.prepared = false;
6262

63-
/** @private {!cwc.protocol.lego.ev3.Api} */
64-
this.api_ = this.connection.getApi();
65-
6663
/** @private {!cwc.utils.Events} */
6764
this.events_ = new cwc.utils.Events(this.name, this.prefix, this);
6865
};
@@ -85,11 +82,8 @@ cwc.mode.sphero.sprkPlus.Monitor.prototype.decorate = function(node) {
8582
this.nodeCache['rgb'] = goog.dom.getElement(this.prefix + 'rgb-value');
8683
this.nodeCache['position'] =
8784
goog.dom.getElement(this.prefix + 'position-value');
88-
this.nodeCache['port_2'] = goog.dom.getElement(this.prefix + 'port_2-value');
89-
this.nodeCache['gyroscope'] = goog.dom.getElement(
90-
this.prefix + 'gyroscope-value');
91-
this.nodeCache['motion_sensor'] = goog.dom.getElement(
92-
this.prefix + 'motion_sensor-value');
85+
this.nodeCache['collision'] =
86+
goog.dom.getElement(this.prefix + 'collision-value');
9387

9488
// Event Handler
9589
let eventTarget = this.connection.getEventTarget();
@@ -107,6 +101,14 @@ cwc.mode.sphero.sprkPlus.Monitor.prototype.decorate = function(node) {
107101
`x:${e.data.x} cm, y:${e.data.y} cm`;
108102
});
109103

104+
this.events_.listen(eventTarget,
105+
Events.Type.COLLISION, () => {
106+
this.nodeCache['collision'].firstChild.nodeValue = 'true';
107+
setTimeout(() => {
108+
this.nodeCache['collision'].firstChild.nodeValue = 'false';
109+
}, 100);
110+
});
111+
110112
// Unload event
111113
let layoutInstance = this.helper.getInstance('layout');
112114
if (layoutInstance) {

0 commit comments

Comments
 (0)