Skip to content

Commit 8498b4f

Browse files
committed
Added Sphero 2.0 connect screen.
1 parent 16187eb commit 8498b4f

File tree

7 files changed

+62
-235
lines changed

7 files changed

+62
-235
lines changed

src/mode/lego/ev3/connection.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ cwc.mode.lego.ev3.Connection = function(helper) {
6161
*/
6262
cwc.mode.lego.ev3.Connection.prototype.init = function() {
6363
this.handleConnecting_({
64-
'data': 'Connection EV3',
64+
'data': 'Connecting EV3',
6565
'source': 1,
6666
});
6767
if (this.apiEvents_) {
@@ -86,11 +86,8 @@ cwc.mode.lego.ev3.Connection.prototype.init = function() {
8686
cwc.mode.lego.ev3.Connection.prototype.connect = function() {
8787
if (!this.isConnected()) {
8888
let bluetoothInstance = this.helper.getInstance('bluetooth', true);
89-
bluetoothInstance.autoConnectDevice(this.autoConnectName, function(device) {
90-
if (device) {
91-
this.api_.connect(device);
92-
}
93-
}.bind(this));
89+
bluetoothInstance.autoConnectDevice(this.autoConnectName,
90+
this.api_.connect.bind(this.api_));
9491
}
9592
this.api_.monitor(true);
9693
};

src/mode/sphero/classic/connection.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ cwc.mode.sphero.classic.Connection = function(helper) {
4848
/** @private {!cwc.protocol.sphero.classic.Api} */
4949
this.api_ = new cwc.protocol.sphero.classic.Api();
5050

51+
/** @private {!goog.events.EventTarget} */
52+
this.apiEvents_ = this.api_.getEventHandler();
53+
5154
/** @private {!cwc.utils.Events} */
5255
this.events_ = new cwc.utils.Events(this.name);
5356
};
@@ -58,6 +61,16 @@ cwc.mode.sphero.classic.Connection = function(helper) {
5861
* @export
5962
*/
6063
cwc.mode.sphero.classic.Connection.prototype.init = function() {
64+
this.handleConnecting_({
65+
'data': 'Connecting Sphero 2.0',
66+
'source': 1,
67+
});
68+
if (this.apiEvents_) {
69+
this.events_.listen(this.apiEvents_,
70+
cwc.protocol.sphero.v1.Events.Type.CONNECT,
71+
this.handleConnecting_.bind(this));
72+
}
73+
6174
if (!this.connectMonitor) {
6275
this.connectMonitor = new goog.Timer(this.connectMonitorInterval);
6376
this.events_.listen(this.connectMonitor, goog.Timer.TICK,
@@ -76,11 +89,8 @@ cwc.mode.sphero.classic.Connection.prototype.init = function() {
7689
cwc.mode.sphero.classic.Connection.prototype.connect = function(opt_event) {
7790
if (!this.isConnected()) {
7891
let bluetoothInstance = this.helper.getInstance('bluetooth', true);
79-
bluetoothInstance.autoConnectDevice(this.autoConnectName, function(device) {
80-
if (device) {
81-
this.api_.connect(device);
82-
}
83-
}.bind(this));
92+
bluetoothInstance.autoConnectDevice(this.autoConnectName,
93+
this.api_.connect.bind(this.api_));
8494
}
8595
this.api_.monitor(true);
8696
};
@@ -147,3 +157,16 @@ cwc.mode.sphero.classic.Connection.prototype.cleanUp = function() {
147157
this.stop();
148158
this.events_.clear();
149159
};
160+
161+
162+
/**
163+
* @param {Event} e
164+
* @private
165+
*/
166+
cwc.mode.sphero.classic.Connection.prototype.handleConnecting_ = function(e) {
167+
let message = e.data;
168+
let step = e.source;
169+
let title = 'Connecting Sphero 2.0';
170+
let connectScreenInstance = this.helper.getInstance('connectScreen');
171+
connectScreenInstance.showConnectingStep(title, message, step);
172+
};

src/protocol/robots/lego/ev3/api.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ cwc.protocol.lego.ev3.Api = function() {
9999
* @export
100100
*/
101101
cwc.protocol.lego.ev3.Api.prototype.connect = function(device) {
102-
if (!device || !device.isConnected()) {
102+
if (!device) {
103+
return false;
104+
} else if (!device.isConnected()) {
103105
console.error('EV3 unit is not ready yet...');
104106
return false;
105107
}

src/protocol/robots/sphero/classic/api.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,22 @@ cwc.protocol.sphero.classic.Api = function() {
105105
* @export
106106
*/
107107
cwc.protocol.sphero.classic.Api.prototype.connect = function(device) {
108-
if (!device || !device.isConnected()) {
108+
if (!device) {
109+
return false;
110+
} else if (!device.isConnected()) {
109111
console.error('Sphero ball is not ready yet...');
110112
return false;
111113
}
112114

113115
if (!this.prepared) {
114-
console.log('Preparing Sphero bluetooth api for', device.getAddress());
116+
console.log('Preparing Sphero classic api for', device.getAddress());
117+
this.eventHandler.dispatchEvent(cwc.protocol.sphero.classic.Events.connect(
118+
'Prepare Sphero Classic api for' + device.getAddress(), 2));
115119
this.device = device;
116120
this.prepare();
117121
this.runTest();
122+
this.eventHandler.dispatchEvent(cwc.protocol.sphero.classic.Events.connect(
123+
'Ready ...', 3));
118124
}
119125
return true;
120126
};

src/protocol/robots/sphero/classic/events.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ goog.require('cwc.utils.EventData');
2828
*/
2929
cwc.protocol.sphero.classic.Events.Type = {
3030
CHANGED_LOCATION: 'changed_devices',
31-
CHANGED_VELOCITY: 'changed_values',
3231
CHANGED_SPEED: 'changed_speed',
32+
CHANGED_VELOCITY: 'changed_values',
3333
COLLISION: 'collision',
34+
CONNECT: 'connect',
3435
};
3536

3637

@@ -76,3 +77,15 @@ cwc.protocol.sphero.classic.Events.collision = function(data) {
7677
return new cwc.utils.EventData(
7778
cwc.protocol.sphero.classic.Events.Type.COLLISION, data);
7879
};
80+
81+
82+
/**
83+
* @param {string} data
84+
* @param {number} step
85+
* @return {!cwc.utils.EventData}
86+
* @final
87+
*/
88+
cwc.protocol.sphero.classic.Events.connect = function(data, step) {
89+
return new cwc.utils.EventData(
90+
cwc.protocol.sphero.classic.Events.Type.CONNECT, data, step);
91+
};

src/protocol/robots/sphero/v1/api.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
2-
* @fileoverview Handles the communication with the Sphero Classic unit.
2+
* @fileoverview Handles the communication with the Sphero v1 unit.
33
*
4-
* This api allows to read and control the Lego Mindstorm Sphero sensors and
5-
* actors over an Bluetooth connection.
4+
* This api allows to read and control the Sphero sensors and actors over an
5+
* Bluetooth connection.
66
*
77
* @license Copyright 2018 The Coding with Chrome Authors.
88
*
@@ -23,7 +23,7 @@
2323
goog.provide('cwc.protocol.sphero.v1.Api');
2424

2525
goog.require('cwc.protocol.sphero.v1.CallbackType');
26-
goog.require('cwc.protocol.sphero.v1.Commands');
26+
goog.require('cwc.protocol.sphero.classic.Commands');
2727
goog.require('cwc.protocol.sphero.v1.Events');
2828
goog.require('cwc.protocol.sphero.v1.MessageType');
2929
goog.require('cwc.protocol.sphero.v1.Monitoring');
@@ -44,8 +44,8 @@ cwc.protocol.sphero.v1.Api = function() {
4444
/** @type {boolean} */
4545
this.prepared = false;
4646

47-
/** @type {!cwc.protocol.sphero.v1.Commands} */
48-
this.commands = new cwc.protocol.sphero.v1.Commands();
47+
/** @type {!cwc.protocol.sphero.classic.Commands} */
48+
this.commands = new cwc.protocol.sphero.classic.Commands();
4949

5050
/** @type {cwc.protocol.sphero.v1.Monitoring} */
5151
this.monitoring = new cwc.protocol.sphero.v1.Monitoring(this);

src/protocol/robots/sphero/v1/commands.js

Lines changed: 0 additions & 214 deletions
This file was deleted.

0 commit comments

Comments
 (0)