Skip to content

Commit d605651

Browse files
committed
Added basic gamepad support.
Added basic gamepad support for EV3 with the following controlls: RT = forward LT = backwards LS = left / right A= ping Need to add additional runner module for handling these events inside the runner sandbox like requested in #78.
1 parent 7a0aeb5 commit d605651

File tree

12 files changed

+435
-77
lines changed

12 files changed

+435
-77
lines changed

src/mode/ev3/monitor.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ goog.require('cwc.protocol.lego.ev3.RobotType');
2525
goog.require('cwc.protocol.lego.ev3.Robots');
2626
goog.require('cwc.soy.mode.ev3.Monitor');
2727
goog.require('cwc.utils.Events');
28+
goog.require('cwc.utils.Gamepad.Events');
2829
goog.require('cwc.utils.Helper');
2930

3031
goog.require('goog.events');
@@ -150,6 +151,7 @@ cwc.mode.ev3.Monitor.prototype.decorate = function() {
150151
}
151152

152153
this.addEventHandler_();
154+
this.addGamepadHandler_();
153155
this.addKeyHandler_();
154156
runnerInstance.enableMonitor(true);
155157
layoutInstance.refresh();
@@ -233,6 +235,34 @@ cwc.mode.ev3.Monitor.prototype.addEventHandler_ = function() {
233235
};
234236

235237

238+
/**
239+
* @private
240+
*/
241+
cwc.mode.ev3.Monitor.prototype.addGamepadHandler_ = function() {
242+
let eventHandler = this.helper.getInstance('gamepad').getEventHandler();
243+
this.events_.listen(eventHandler, cwc.utils.Gamepad.Events.Type.BUTTON[7],
244+
(e) => {
245+
this.api.movePower(e.data * 100);
246+
});
247+
this.events_.listen(eventHandler, cwc.utils.Gamepad.Events.Type.BUTTON[6],
248+
(e) => {
249+
this.api.movePower(-e.data * 100);
250+
});
251+
this.events_.listen(eventHandler, cwc.utils.Gamepad.Events.Type.AXIS[0],
252+
(e) => {
253+
if (e.data > 0.1 || e.data < -0.1) {
254+
this.api.rotatePower(e.data * 100);
255+
} else {
256+
this.api.rotatePower(0);
257+
}
258+
});
259+
this.events_.listen(eventHandler, cwc.utils.Gamepad.Events.Type.BUTTON[0],
260+
() => {
261+
this.api.playTone(3000, 200, 50);
262+
});
263+
};
264+
265+
236266
/**
237267
* @private
238268
*/

src/ui/builder.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ goog.require('cwc.ui.SettingScreen');
6363
goog.require('cwc.ui.connectScreen.Screens');
6464
goog.require('cwc.utils.Dialog');
6565
goog.require('cwc.utils.Events');
66+
goog.require('cwc.utils.Gamepad');
6667
goog.require('cwc.utils.Helper');
6768
goog.require('cwc.utils.I18n');
6869
goog.require('cwc.utils.Logger');
@@ -286,6 +287,9 @@ cwc.ui.Builder.prototype.loadUI = function() {
286287
this.setProgress('Prepare helpers ...', 40);
287288
this.prepareHelper();
288289

290+
this.setProgress('Gamepad support ...', 44);
291+
this.prepareGamepad();
292+
289293
this.setProgress('Prepare addons ...', 45);
290294
this.prepareAddons();
291295

@@ -434,7 +438,7 @@ cwc.ui.Builder.prototype.prepareSerial = function() {
434438
*/
435439
cwc.ui.Builder.prototype.prepareServer = function() {
436440
let serverInstance = new cwc.server.Server(this.helper);
437-
if (this.helper.checkChromeFeature('sockets.tcpServer') && serverInstance) {
441+
if (this.helper.checkChromeFeature('sockets.tcpServer')) {
438442
serverInstance.prepare();
439443
}
440444
this.helper.setInstance('server', serverInstance);
@@ -447,10 +451,7 @@ cwc.ui.Builder.prototype.prepareServer = function() {
447451
*/
448452
cwc.ui.Builder.prototype.prepareDebug_ = function() {
449453
let debugInstance = new cwc.ui.Debug(this.helper);
450-
if (debugInstance) {
451-
debugInstance.prepare();
452-
}
453-
this.helper.setInstance('debug', debugInstance);
454+
this.helper.setInstance('debug', debugInstance).prepare();
454455
};
455456

456457

@@ -460,10 +461,7 @@ cwc.ui.Builder.prototype.prepareDebug_ = function() {
460461
*/
461462
cwc.ui.Builder.prototype.prepareExperimental_ = function() {
462463
let experimentalInstance = new cwc.ui.Experimental(this.helper);
463-
if (experimentalInstance) {
464-
experimentalInstance.prepare();
465-
}
466-
this.helper.setInstance('experimental', experimentalInstance);
464+
this.helper.setInstance('experimental', experimentalInstance).prepare();
467465
};
468466

469467

@@ -472,15 +470,12 @@ cwc.ui.Builder.prototype.prepareExperimental_ = function() {
472470
*/
473471
cwc.ui.Builder.prototype.prepareDialog = function() {
474472
let dialogInstance = new cwc.utils.Dialog();
475-
if (dialogInstance) {
476-
dialogInstance.setDefaultCloseHandler(
477-
function() {
478-
this.helper.getInstance('navigation').hide();
479-
}.bind(this)
480-
);
481-
dialogInstance.prepare();
482-
}
483-
this.helper.setInstance('dialog', dialogInstance);
473+
dialogInstance.setDefaultCloseHandler(
474+
function() {
475+
this.helper.getInstance('navigation').hide();
476+
}.bind(this)
477+
);
478+
this.helper.setInstance('dialog', dialogInstance).prepare();
484479
};
485480

486481

@@ -514,6 +509,12 @@ cwc.ui.Builder.prototype.prepareHelper = function() {
514509
};
515510

516511

512+
cwc.ui.Builder.prototype.prepareGamepad = function() {
513+
let gamepadInstance = new cwc.utils.Gamepad();
514+
this.helper.setInstance('gamepad', gamepadInstance).prepare();
515+
};
516+
517+
517518
/**
518519
* Load additional oauth2 helpers.
519520
*/

src/ui/connect_screen/bluetooth/connect_screen.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ cwc.ui.connectScreen.Bluetooth.prototype.requestDevice = function(device) {
108108
});
109109
this.events_.listen('search-button', goog.events.EventType.CLICK, () => {
110110
bluetoothInstance.requestDevice(device).then((bluetoothDevice) => {
111-
this.showConnectingStep(
112-
'Pairing Device', 'Pairing device' + device.name, 1);
111+
this.helper.getInstance('connectScreen')
112+
.showConnectingStep(
113+
'Pairing Device', 'Pairing device' + device.name, 1);
113114
resolve(bluetoothDevice);
114115
}).catch(() => {
115116
this.close_();

src/ui/menubar/menubar.gss

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#{$prefix}menubar-account-body,
2424
#{$prefix}menubar-bluetooth-body,
2525
#{$prefix}menubar-close,
26+
#{$prefix}menubar-gamepad-body,
2627
#{$prefix}menubar-help,
2728
#{$prefix}menubar-maximize-body,
2829
#{$prefix}menubar-minimize,
@@ -38,10 +39,8 @@
3839
padding-left: 5px;
3940
}
4041

41-
#{$prefix}menubar-account-logout-disabled {
42-
opacity: 0.7;
43-
}
44-
45-
#{$prefix}menubar-bluetooth-disabled {
46-
opacity: 0.7;
42+
#{$prefix}menubar-account-logout-disabled,
43+
#{$prefix}menubar-bluetooth-disabled,
44+
#{$prefix}menubar-gamepad {
45+
opacity: 0.5;
4746
}

0 commit comments

Comments
 (0)