Skip to content

Commit d1024b8

Browse files
committed
Improved existing mBot implementation.
Added error message after 20sec of unsuccessful connection but only for manual connections. Automated connection have no declear timeout, but throttle the re-tries.
1 parent 6d3d3dc commit d1024b8

File tree

45 files changed

+544
-1596
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+544
-1596
lines changed

src/mode/default/mod.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ cwc.mode.default.ApiTypes;
3535

3636
/**
3737
* @typedef {cwc.mode.lego.ev3.Connection|
38-
* cwc.mode.makeblock.mbot.Connection|
39-
* cwc.mode.makeblock.mbotRanger.Connection|
38+
* cwc.mode.makeblock.mBot.Connection|
39+
* cwc.mode.makeblock.mBotRanger.Connection|
4040
* cwc.mode.sphero.sphero2.Connection|
4141
* cwc.mode.sphero.bb8.Connection|
4242
* cwc.mode.sphero.sprkPlus.Connection|

src/mode/lego/ev3/connection.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,6 @@ cwc.mode.lego.ev3.Connection.prototype.init = function() {
8080
this.handleUpdateDevices_.bind(this));
8181
}
8282

83-
if (!this.connectMonitor) {
84-
this.connectMonitor = new goog.Timer(this.connectMonitorInterval);
85-
this.events_.listen(this.connectMonitor, goog.Timer.TICK,
86-
this.connect.bind(this));
87-
}
88-
89-
// Unload event
9083
let layoutInstance = this.helper.getInstance('layout');
9184
if (layoutInstance) {
9285
this.events_.listen(layoutInstance.getEventTarget(),
@@ -100,6 +93,12 @@ cwc.mode.lego.ev3.Connection.prototype.init = function() {
10093
false, this);
10194
}
10295

96+
if (!this.connectMonitor) {
97+
this.connectMonitor = new goog.Timer(this.connectMonitorInterval);
98+
this.events_.listen(this.connectMonitor, goog.Timer.TICK,
99+
this.connect.bind(this));
100+
}
101+
103102
this.connectMonitor.start();
104103
this.connect();
105104
};

src/mode/lego/wedo2/mod.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ cwc.mode.lego.weDo2.Mod = function(helper, enableBlockly = false) {
4141
/** @type {!cwc.mode.default.Mod} */
4242
this.mod = new cwc.mode.default.Mod(helper);
4343

44-
/** @type {!cwc.mode.makeblock.mbot.Monitor} */
44+
/** @type {!cwc.mode.lego.weDo2.Monitor} */
4545
this.monitor = new cwc.mode.lego.weDo2.Monitor(helper, this.connection);
4646

4747
/** @type {!cwc.renderer.external.WEDO2} */

src/mode/lego/wedo2/panel/monitor/monitor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ goog.require('goog.events.EventType');
2929

3030

3131
goog.scope(function() {
32-
const Events = goog.require('cwc.lib.protocol.lego.weDo2.Events');
32+
const Events = goog.module.get('cwc.lib.protocol.lego.weDo2.Events');
3333

3434
/**
3535
* @constructor

src/mode/makeblock/mbot/connection.js

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,27 @@
1717
*
1818
* @author [email protected] (Yu Wang)
1919
*/
20-
goog.provide('cwc.mode.makeblock.mbot.Connection');
20+
goog.provide('cwc.mode.makeblock.mBot.Connection');
2121

22-
goog.require('cwc.protocol.makeblock.mbot.Api');
22+
goog.require('cwc.lib.protocol.makeblock.mBot.Api');
23+
goog.require('cwc.lib.protocol.bluetoothChrome.profile.Device');
24+
goog.require('cwc.lib.protocol.makeblock.mBot.Events');
2325
goog.require('cwc.utils.Events');
2426

2527
goog.require('goog.Timer');
2628

2729

30+
goog.scope(function() {
31+
const Api = goog.module.get('cwc.lib.protocol.makeblock.mBot.Api');
32+
const BluetoothProfile =
33+
goog.module.get('cwc.lib.protocol.bluetoothChrome.profile.Device');
34+
const Events = goog.module.get('cwc.lib.protocol.makeblock.mBot.Events');
35+
2836
/**
2937
* @constructor
3038
* @param {!cwc.utils.Helper} helper
3139
*/
32-
cwc.mode.makeblock.mbot.Connection = function(helper) {
40+
cwc.mode.makeblock.mBot.Connection = function(helper) {
3341
/** @type {string} */
3442
this.name = 'mBot Connection';
3543

@@ -39,32 +47,41 @@ cwc.mode.makeblock.mbot.Connection = function(helper) {
3947
/** @type {!cwc.utils.Helper} */
4048
this.helper = helper;
4149

42-
/** @type {!cwc.protocol.makeblock.mbot.Api} */
43-
this.api_ = new cwc.protocol.makeblock.mbot.Api();
44-
4550
/** @type {goog.Timer} */
4651
this.connectMonitor = null;
4752

4853
/** @type {number} */
4954
this.connectMonitorInterval = 5000;
5055

56+
/** @type {!cwc.protocol.makeblock.mBot.Api} */
57+
this.api_ = new Api();
58+
59+
/** @private {!goog.events.EventTarget} */
60+
this.apiEvents_ = this.api_.getEventTarget();
61+
5162
/** @private {!cwc.utils.Events} */
5263
this.events_ = new cwc.utils.Events(this.name);
64+
65+
/** @private {!cwc.lib.protocol.bluetoothChrome.profile.Device} */
66+
this.device_ = BluetoothProfile.MAKEBLOCK_MBOT_RANGER;
5367
};
5468

5569

5670
/**
5771
* Connects the mbot unit.
5872
* @export
5973
*/
60-
cwc.mode.makeblock.mbot.Connection.prototype.init = function() {
61-
if (!this.connectMonitor) {
62-
this.connectMonitor = new goog.Timer(this.connectMonitorInterval);
63-
this.events_.listen(this.connectMonitor, goog.Timer.TICK,
64-
this.connect.bind(this));
74+
cwc.mode.makeblock.mBot.Connection.prototype.init = function() {
75+
this.handleConnecting_({
76+
'data': 'Connecting Sphero 2.0',
77+
'source': 1,
78+
});
79+
80+
if (this.apiEvents_) {
81+
this.events_.listen(this.apiEvents_,
82+
Events.Type.CONNECT, this.handleConnecting_.bind(this));
6583
}
6684

67-
// Unload event
6885
let layoutInstance = this.helper.getInstance('layout');
6986
if (layoutInstance) {
7087
this.events_.listen(layoutInstance.getEventTarget(),
@@ -78,36 +95,40 @@ cwc.mode.makeblock.mbot.Connection.prototype.init = function() {
7895
false, this);
7996
}
8097

98+
if (!this.connectMonitor) {
99+
this.connectMonitor = new goog.Timer(this.connectMonitorInterval);
100+
this.events_.listen(this.connectMonitor, goog.Timer.TICK,
101+
this.connect.bind(this));
102+
}
103+
81104
this.connectMonitor.start();
82105
this.connect();
83106
};
84107

85108

86109
/**
87-
* Connects the mbot ball.
110+
* Connects the mBot robot.
88111
* @param {Event=} opt_event
89112
* @export
90113
*/
91-
cwc.mode.makeblock.mbot.Connection.prototype.connect = function(opt_event) {
114+
cwc.mode.makeblock.mBot.Connection.prototype.connect = function(opt_event) {
92115
let bluetoothInstance = this.helper.getInstance('bluetoothChrome');
93116
if (!bluetoothInstance) {
94117
return;
95118
}
96119
if (!this.isConnected()) {
97-
bluetoothInstance.autoConnectDevice(this.autoConnectName, function(device) {
98-
if (device) {
99-
this.api_.connect(device);
100-
}
101-
}.bind(this));
120+
bluetoothInstance.autoConnectDevice(this.autoConnectName,
121+
this.api_.connect.bind(this.api_));
102122
}
123+
this.api_.monitor(true);
103124
};
104125

105126

106127
/**
107128
* Stops the current executions.
108129
* @export
109130
*/
110-
cwc.mode.makeblock.mbot.Connection.prototype.stop = function() {
131+
cwc.mode.makeblock.mBot.Connection.prototype.stop = function() {
111132
let previewInstance = this.helper.getInstance('preview');
112133
if (previewInstance) {
113134
previewInstance.stop();
@@ -118,10 +139,9 @@ cwc.mode.makeblock.mbot.Connection.prototype.stop = function() {
118139

119140
/**
120141
* Resets the connection.
121-
* @param {Event=} opt_event
122142
* @export
123143
*/
124-
cwc.mode.makeblock.mbot.Connection.prototype.reset = function(opt_event) {
144+
cwc.mode.makeblock.mBot.Connection.prototype.reset = function() {
125145
if (this.isConnected()) {
126146
this.api_.reset();
127147
}
@@ -132,36 +152,36 @@ cwc.mode.makeblock.mbot.Connection.prototype.reset = function(opt_event) {
132152
* @return {boolean}
133153
* @export
134154
*/
135-
cwc.mode.makeblock.mbot.Connection.prototype.isConnected = function() {
155+
cwc.mode.makeblock.mBot.Connection.prototype.isConnected = function() {
136156
return this.api_.isConnected();
137157
};
138158

139159

140160
/**
141-
* @return {!cwc.protocol.makeblock.mbot.Api}
161+
* @return {!cwc.protocol.makeblock.mBot.Api}
142162
* @export
143163
*/
144-
cwc.mode.makeblock.mbot.Connection.prototype.getApi = function() {
164+
cwc.mode.makeblock.mBot.Connection.prototype.getApi = function() {
145165
return this.api_;
146166
};
147167

148168

149169
/**
150170
* @return {goog.events.EventTarget}
151171
*/
152-
cwc.mode.makeblock.mbot.Connection.prototype.getEventTarget = function() {
172+
cwc.mode.makeblock.mBot.Connection.prototype.getEventTarget = function() {
153173
return this.api_.getEventTarget();
154174
};
155175

156176

157177
/**
158178
* Cleans up the event listener and any other modification.
159179
*/
160-
cwc.mode.makeblock.mbot.Connection.prototype.cleanUp = function() {
180+
cwc.mode.makeblock.mBot.Connection.prototype.cleanUp = function() {
161181
if (this.connectMonitor) {
162182
this.connectMonitor.stop();
163183
}
164-
this.api_.monitor(false);
184+
this.api_.cleanUp();
165185
this.stop();
166186
this.events_.clear();
167187
};
@@ -171,9 +191,23 @@ cwc.mode.makeblock.mbot.Connection.prototype.cleanUp = function() {
171191
* @param {Event|Object} e
172192
* @private
173193
*/
174-
cwc.mode.makeblock.mbot.Connection.prototype.handlePreviewStatus_ = function(
194+
cwc.mode.makeblock.mBot.Connection.prototype.handleConnecting_ = function(e) {
195+
let message = e.data;
196+
let step = e.source;
197+
let title = 'Connecting' + this.device_.name;
198+
let connectScreenInstance = this.helper.getInstance('connectScreen');
199+
connectScreenInstance.showConnectingStep(title, message, step);
200+
};
201+
202+
203+
/**
204+
* @param {Event|Object} e
205+
* @private
206+
*/
207+
cwc.mode.makeblock.mBot.Connection.prototype.handlePreviewStatus_ = function(
175208
e) {
176209
if (e.data === cwc.ui.PreviewState.STOPPED) {
177210
this.stop();
178211
}
179212
};
213+
});

src/mode/makeblock/mbot/events.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
*
1818
* @author [email protected] (Markus Bordihn)
1919
*/
20-
goog.provide('cwc.mode.makeblock.mbot.SensorEvents');
21-
22-
goog.require('cwc.protocol.makeblock.mbot.Events');
20+
goog.provide('cwc.mode.makeblock.mBot.SensorEvents');
2321

2422

2523
/**
2624
* @enum {string}
2725
*/
28-
cwc.mode.makeblock.mbot.SensorEvents = cwc.protocol.makeblock.mbot.Events.Type;
26+
cwc.mode.makeblock.mBot.SensorEvents = {
27+
BUTTON_PRESSED: 'BUTTON_PRESSED',
28+
LIGHTNESS_SENSOR: 'CHANGED_LIGHTNESS',
29+
LINEFOLLOWER_SENSOR: 'CHANGED_LINEFOLLOWER',
30+
ULTRASONIC_SENSOR: 'CHANGED_ULTRASONIC',
31+
};

src/mode/makeblock/mbot/mod.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
*
1818
* @author [email protected] (Yu Wang)
1919
*/
20-
goog.provide('cwc.mode.makeblock.mbot.Mod');
20+
goog.provide('cwc.mode.makeblock.mBot.Mod');
2121

2222
goog.require('cwc.mode.default.Mod');
23-
goog.require('cwc.mode.makeblock.mbot.Connection');
24-
goog.require('cwc.mode.makeblock.mbot.Control');
25-
goog.require('cwc.mode.makeblock.mbot.Monitor');
26-
goog.require('cwc.mode.makeblock.mbot.SensorEvents');
27-
goog.require('cwc.mode.makeblock.mbot.Simulation');
23+
goog.require('cwc.mode.makeblock.mBot.Connection');
24+
goog.require('cwc.mode.makeblock.mBot.Control');
25+
goog.require('cwc.mode.makeblock.mBot.Monitor');
26+
goog.require('cwc.mode.makeblock.mBot.SensorEvents');
27+
goog.require('cwc.mode.makeblock.mBot.Simulation');
2828
goog.require('cwc.renderer.external.makeblock.MBot');
2929
goog.require('cwc.soy.mbot.Blocks');
3030

@@ -34,27 +34,27 @@ goog.require('cwc.soy.mbot.Blocks');
3434
* @param {!cwc.utils.Helper} helper
3535
* @param {boolean=} enableBlockly
3636
*/
37-
cwc.mode.makeblock.mbot.Mod = function(helper, enableBlockly = false) {
37+
cwc.mode.makeblock.mBot.Mod = function(helper, enableBlockly = false) {
3838
/** @type {boolean} */
3939
this.enableBlockly = enableBlockly;
4040

41-
/** @type {!cwc.mode.makeblock.mbot.Connection} */
42-
this.connection = new cwc.mode.makeblock.mbot.Connection(helper);
41+
/** @type {!cwc.mode.makeblock.mBot.Connection} */
42+
this.connection = new cwc.mode.makeblock.mBot.Connection(helper);
4343

44-
/** @type {!cwc.mode.makeblock.mbot.SensorEvents} */
45-
this.events = cwc.mode.makeblock.mbot.SensorEvents;
44+
/** @type {!cwc.mode.makeblock.mBot.SensorEvents} */
45+
this.events = cwc.mode.makeblock.mBot.SensorEvents;
4646

4747
/** @type {!cwc.mode.default.Mod} */
4848
this.mod = new cwc.mode.default.Mod(helper);
4949

50-
/** @type {!cwc.mode.makeblock.mbot.Control} */
51-
this.control = new cwc.mode.makeblock.mbot.Control(helper, this.connection);
50+
/** @type {!cwc.mode.makeblock.mBot.Control} */
51+
this.control = new cwc.mode.makeblock.mBot.Control(helper, this.connection);
5252

53-
/** @type {!cwc.mode.makeblock.mbot.Monitor} */
54-
this.monitor = new cwc.mode.makeblock.mbot.Monitor(helper, this.connection);
53+
/** @type {!cwc.mode.makeblock.mBot.Monitor} */
54+
this.monitor = new cwc.mode.makeblock.mBot.Monitor(helper, this.connection);
5555

5656
/** @type {!cwc.mode.lego.ev3.Simulation} */
57-
this.simulation = new cwc.mode.makeblock.mbot.Simulation(helper);
57+
this.simulation = new cwc.mode.makeblock.mBot.Simulation(helper);
5858

5959
/** @type {!cwc.renderer.external.makeblock.MBot} */
6060
this.renderer = new cwc.renderer.external.makeblock.MBot(helper);
@@ -64,7 +64,7 @@ cwc.mode.makeblock.mbot.Mod = function(helper, enableBlockly = false) {
6464
/**
6565
* Decorates the different parts of the modification.
6666
*/
67-
cwc.mode.makeblock.mbot.Mod.prototype.decorate = function() {
67+
cwc.mode.makeblock.mBot.Mod.prototype.decorate = function() {
6868
if (this.enableBlockly) {
6969
this.mod.enableBlockly(cwc.soy.mbot.Blocks.toolbox);
7070
}

0 commit comments

Comments
 (0)