Skip to content

Commit f79f682

Browse files
committed
Improved Gamepad integration.
1 parent 188ea9e commit f79f682

File tree

10 files changed

+120
-81
lines changed

10 files changed

+120
-81
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
},
2121
"devDependencies": {
2222
"closure-templates": "latest",
23-
"eslint": "^4.19.0",
23+
"eslint": "^4.19.1",
2424
"eslint-config-google": "^0.9.1",
2525
"http-server": "latest",
2626
"jasmine-core": "latest",

src/frameworks/internal/ev3/ev3.js

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ cwc.framework.Ev3 = function(code) {
4646
code(this);
4747
}.bind(this);
4848

49-
/** @type {!cwc.framework.Runner} */
50-
this.runner = new cwc.framework.Runner(this.code, this);
51-
5249
/** @type {Object} */
5350
this.deviceInfo = {};
5451

@@ -109,19 +106,23 @@ cwc.framework.Ev3 = function(code) {
109106
/** @type {!number} */
110107
this.mediumMotorSpeed = 250 / 60;
111108

109+
/** @private {!cwc.framework.Runner} */
110+
this.runner_ = new cwc.framework.Runner(this.code, this);
111+
112112
// External commands
113-
this.runner.addCommand('updateColorSensor', this.handleUpdateColorSensor_);
114-
this.runner.addCommand('updateDeviceInfo', this.handleUpdateDeviceInfo_);
115-
this.runner.addCommand('updateGyroSensor', this.handleUpdateGyroSensor_);
116-
this.runner.addCommand('updateIrSensor', this.handleUpdateIrSensor_);
117-
this.runner.addCommand('updateTouchSensor', this.handleUpdateTouchSensor_);
118-
this.runner.addCommand('updateUltrasonicSensor',
113+
this.runner_.addCommand('updateColorSensor', this.handleUpdateColorSensor_);
114+
this.runner_.addCommand('updateDeviceInfo', this.handleUpdateDeviceInfo_);
115+
this.runner_.addCommand('updateGyroSensor', this.handleUpdateGyroSensor_);
116+
this.runner_.addCommand('updateIrSensor', this.handleUpdateIrSensor_);
117+
this.runner_.addCommand('updateTouchSensor', this.handleUpdateTouchSensor_);
118+
this.runner_.addCommand('updateUltrasonicSensor',
119119
this.handleUpdateUltrasonicSensor_);
120-
this.runner.addCommand('updateRobotType', this.handleUpdateRobotType_);
121-
this.runner.addCommand('updateWheelDiameter',
120+
this.runner_.addCommand('updateRobotType', this.handleUpdateRobotType_);
121+
this.runner_.addCommand('updateWheelDiameter',
122122
this.handleUpdateWheelDiameter_);
123-
this.runner.addCommand('updateWheelWidth', this.handleUpdateWheelWidth_);
124-
this.runner.addCommand('updateWheelbase', this.handleUpdateWheelbase_);
123+
this.runner_.addCommand('updateWheelWidth', this.handleUpdateWheelWidth_);
124+
this.runner_.addCommand('updateWheelbase', this.handleUpdateWheelbase_);
125+
this.runner_.addCommand('__gamepad__', this.handleUpdateGamepad_);
125126
};
126127

127128

@@ -334,7 +335,7 @@ cwc.framework.Ev3.prototype.stopUltrasonicSensorEvent = function() {
334335
* @export
335336
*/
336337
cwc.framework.Ev3.prototype.drawImage = function(filename, opt_delay) {
337-
this.runner.send('drawImage', {'file': filename}, opt_delay);
338+
this.runner_.send('drawImage', {'file': filename}, opt_delay);
338339
};
339340

340341

@@ -348,7 +349,7 @@ cwc.framework.Ev3.prototype.drawImage = function(filename, opt_delay) {
348349
*/
349350
cwc.framework.Ev3.prototype.playTone = function(frequency, opt_duration,
350351
opt_volume, opt_delay) {
351-
this.runner.send('playTone', {
352+
this.runner_.send('playTone', {
352353
'frequency': frequency,
353354
'duration': opt_duration,
354355
'volume': opt_volume}, opt_delay);
@@ -364,7 +365,7 @@ cwc.framework.Ev3.prototype.playTone = function(frequency, opt_duration,
364365
*/
365366
cwc.framework.Ev3.prototype.playSound = function(filename, opt_volume,
366367
opt_delay) {
367-
this.runner.send('playSound', {
368+
this.runner_.send('playSound', {
368369
'file': filename,
369370
'volume': opt_volume}, opt_delay);
370371
};
@@ -378,7 +379,7 @@ cwc.framework.Ev3.prototype.playSound = function(filename, opt_volume,
378379
* @export
379380
*/
380381
cwc.framework.Ev3.prototype.moveServo = function(steps, opt_speed, opt_delay) {
381-
this.runner.send('moveServo', {
382+
this.runner_.send('moveServo', {
382383
'steps': steps,
383384
'speed': opt_speed}, opt_delay);
384385
};
@@ -394,7 +395,7 @@ cwc.framework.Ev3.prototype.moveServo = function(steps, opt_speed, opt_delay) {
394395
*/
395396
cwc.framework.Ev3.prototype.movePen = function(steps,
396397
opt_speed, opt_color, opt_delay) {
397-
this.runner.send('movePen', {
398+
this.runner_.send('movePen', {
398399
'steps': steps,
399400
'speed': opt_speed,
400401
'color': opt_color}, opt_delay);
@@ -415,7 +416,7 @@ cwc.framework.Ev3.prototype.moveSteps = function(steps, opt_speed, opt_delay) {
415416
let delay = /** @type {number|undefined} */ (
416417
opt_delay === true ? this.getDelay(steps, opt_speed) : opt_delay);
417418
let distance = Math.round((this.wheelCircumference * (steps/360)) / 10);
418-
this.runner.send('moveSteps', {
419+
this.runner_.send('moveSteps', {
419420
'distance': distance,
420421
'steps': steps,
421422
'speed': opt_speed}, delay);
@@ -435,7 +436,7 @@ cwc.framework.Ev3.prototype.customMoveSteps = function(steps, opt_ports,
435436
opt_speed, opt_delay) {
436437
let delay = /** @type {number|undefined} */ (
437438
opt_delay === true ? this.getDelay(steps, opt_speed) : opt_delay);
438-
this.runner.send('customMoveSteps', {
439+
this.runner_.send('customMoveSteps', {
439440
'steps': steps,
440441
'ports': opt_ports,
441442
'speed': opt_speed}, delay);
@@ -454,7 +455,7 @@ cwc.framework.Ev3.prototype.moveDistance = function(distance, opt_speed,
454455
let steps = Math.round((distance * 10 / this.wheelCircumference) * 360);
455456
let delay = /** @type {number|undefined} */ (
456457
opt_delay === true ? this.getDelay(steps, opt_speed) : opt_delay);
457-
this.runner.send('moveSteps', {
458+
this.runner_.send('moveSteps', {
458459
'distance': distance,
459460
'steps': steps,
460461
'speed': opt_speed}, delay);
@@ -471,7 +472,7 @@ cwc.framework.Ev3.prototype.moveDistance = function(distance, opt_speed,
471472
*/
472473
cwc.framework.Ev3.prototype.rotateSteps = function(steps,
473474
opt_speed, opt_ratio, opt_delay) {
474-
this.runner.send('rotateSteps', {
475+
this.runner_.send('rotateSteps', {
475476
'steps': steps,
476477
'speed': opt_speed,
477478
'ratio': opt_ratio}, opt_delay);
@@ -495,7 +496,7 @@ cwc.framework.Ev3.prototype.rotateAngle = function(angle,
495496
(rotateDistance * angle / this.wheelCircumference) * 360);
496497
let delay = /** @type {number|undefined} */ (
497498
opt_delay === true ? this.getDelay(steps, opt_speed) : opt_delay);
498-
this.runner.send('rotateSteps', {
499+
this.runner_.send('rotateSteps', {
499500
'angle': angle,
500501
'steps': steps,
501502
'speed': opt_speed}, delay);
@@ -518,7 +519,7 @@ cwc.framework.Ev3.prototype.customRotateAngle = function(angle,
518519
(rotateDistance * angle / this.wheelCircumference) * 360);
519520
let delay = /** @type {number|undefined} */ (
520521
opt_delay === true ? this.getDelay(steps, opt_speed) : opt_delay);
521-
this.runner.send('customRotateSteps', {
522+
this.runner_.send('customRotateSteps', {
522523
'angle': angle,
523524
'steps': steps,
524525
'ports': opt_ports,
@@ -533,7 +534,7 @@ cwc.framework.Ev3.prototype.customRotateAngle = function(angle,
533534
* @export
534535
*/
535536
cwc.framework.Ev3.prototype.movePower = function(power, opt_delay) {
536-
this.runner.send('movePower', {'power': power}, opt_delay);
537+
this.runner_.send('movePower', {'power': power}, opt_delay);
537538
};
538539

539540

@@ -546,7 +547,7 @@ cwc.framework.Ev3.prototype.movePower = function(power, opt_delay) {
546547
*/
547548
cwc.framework.Ev3.prototype.rotatePower = function(power, opt_power,
548549
opt_delay) {
549-
this.runner.send('rotatePower', {
550+
this.runner_.send('rotatePower', {
550551
'power': power,
551552
'opt_power': opt_power}, opt_delay);
552553
};
@@ -558,7 +559,7 @@ cwc.framework.Ev3.prototype.rotatePower = function(power, opt_power,
558559
* @export
559560
*/
560561
cwc.framework.Ev3.prototype.stop = function(opt_delay) {
561-
this.runner.send('stop', null, opt_delay);
562+
this.runner_.send('stop', null, opt_delay);
562563
};
563564

564565

@@ -568,7 +569,7 @@ cwc.framework.Ev3.prototype.stop = function(opt_delay) {
568569
* @export
569570
*/
570571
cwc.framework.Ev3.prototype.wait = function(time) {
571-
this.runner.send('wait', null, time);
572+
this.runner_.send('wait', null, time);
572573
};
573574

574575

@@ -578,7 +579,7 @@ cwc.framework.Ev3.prototype.wait = function(time) {
578579
* @export
579580
*/
580581
cwc.framework.Ev3.prototype.setColorSensorMode = function(mode, opt_delay) {
581-
this.runner.send('setColorSensorMode', {'mode': mode}, opt_delay);
582+
this.runner_.send('setColorSensorMode', {'mode': mode}, opt_delay);
582583
};
583584

584585

@@ -588,7 +589,7 @@ cwc.framework.Ev3.prototype.setColorSensorMode = function(mode, opt_delay) {
588589
* @export
589590
*/
590591
cwc.framework.Ev3.prototype.setIrSensorMode = function(mode, opt_delay) {
591-
this.runner.send('setIrSensorMode', {'mode': mode}, opt_delay);
592+
this.runner_.send('setIrSensorMode', {'mode': mode}, opt_delay);
592593
};
593594

594595

@@ -599,7 +600,7 @@ cwc.framework.Ev3.prototype.setIrSensorMode = function(mode, opt_delay) {
599600
*/
600601
cwc.framework.Ev3.prototype.setUltrasonicSensorMode = function(mode,
601602
opt_delay) {
602-
this.runner.send('setUltrasonicSensorMode', {'mode': mode}, opt_delay);
603+
this.runner_.send('setUltrasonicSensorMode', {'mode': mode}, opt_delay);
603604
};
604605

605606

@@ -610,7 +611,7 @@ cwc.framework.Ev3.prototype.setUltrasonicSensorMode = function(mode,
610611
* @export
611612
*/
612613
cwc.framework.Ev3.prototype.setLed = function(color, opt_mode, opt_delay) {
613-
this.runner.send('setLed', {
614+
this.runner_.send('setLed', {
614615
'color': color,
615616
'mode': opt_mode}, opt_delay);
616617
};
@@ -622,7 +623,7 @@ cwc.framework.Ev3.prototype.setLed = function(color, opt_mode, opt_delay) {
622623
* @export
623624
*/
624625
cwc.framework.Ev3.prototype.setStepSpeed = function(speed, opt_delay) {
625-
this.runner.send('setStepSpeed', {'speed': speed}, opt_delay);
626+
this.runner_.send('setStepSpeed', {'speed': speed}, opt_delay);
626627
};
627628

628629

@@ -706,6 +707,15 @@ cwc.framework.Ev3.prototype.handleUpdateWheelDiameter_ = function(data) {
706707
};
707708

708709

710+
/**
711+
* @param {Object} data
712+
* @private
713+
*/
714+
cwc.framework.Ev3.prototype.handleUpdateGamepad_ = function(data) {
715+
console.log(data);
716+
};
717+
718+
709719
/**
710720
* Sets the wheel diameter
711721
* @param {!number} data

src/frameworks/internal/runner/runner.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,6 @@ cwc.framework.Runner.prototype.setMonitor = function(monitor) {
156156
};
157157

158158

159-
/**
160-
* Sends the direct update confirmation to the runner..
161-
* @param {string=} data
162-
* @export
163-
*/
164-
cwc.framework.Runner.prototype.enableDirectUpdate = function(data = '') {
165-
console.log('Enable direct update.');
166-
this.send('__direct_update__', data);
167-
};
168-
169-
170159
/**
171160
* Handles the received messages and executes the predefined actions.
172161
* @param {Event} event

src/mode/lego/ev3/monitor.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,8 @@ cwc.mode.lego.ev3.Monitor.prototype.addGamepadHandler_ = function() {
259259
this.api.rotatePower(e.data * 100);
260260
});
261261
this.events_.listen(eventHandler, cwc.utils.Gamepad.Events.Type.BUTTON[0],
262-
(e) => {
263-
if (e === 1) {
264-
this.api.playTone(3000, 200, 50);
265-
}
262+
() => {
263+
this.api.playTone(3000, 200, 50);
266264
});
267265
};
268266

src/runner/runnerConnector.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
goog.provide('cwc.runner.Connector');
2121

2222
goog.require('cwc.utils.Events');
23+
goog.require('cwc.utils.Gamepad.Events.Type');
2324
goog.require('cwc.utils.Logger');
2425

2526
goog.require('goog.events');
@@ -340,6 +341,13 @@ cwc.runner.Connector.prototype.handleMessage_ = function(event) {
340341
*/
341342
cwc.runner.Connector.prototype.handleGamepad_ = function() {
342343
console.log('Enable Gamepad Support');
344+
let gamepadInstance = this.helper.getInstance('gamepad');
345+
let eventHandler = gamepadInstance.getEventHandler();
346+
this.events_.listen(eventHandler, cwc.utils.Gamepad.Events.Type.UPDATE,
347+
(e) => {
348+
console.log('Gamepad', e.data);
349+
this.send('__gamepad__', e.data);
350+
});
343351
};
344352

345353

src/utils/byte_tools.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,17 @@ cwc.utils.ByteTools.joinUint8Array = function(data1, data2) {
118118

119119

120120
/**
121-
* @param {ArrayBuffer|Uint8Array} data
122-
* @return {Uint8Array}
121+
* @param {!ArrayBuffer|Uint8Array} data
122+
* @return {!Uint8Array}
123123
*/
124124
cwc.utils.ByteTools.getUint8Array = function(data) {
125+
if (data instanceof Uint8Array) {
126+
return data;
127+
}
125128
if (data instanceof ArrayBuffer) {
126129
return new Uint8Array(data);
127130
}
128-
return data;
131+
console.error('Unable to read:', data);
129132
};
130133

131134

src/utils/dialog/dialog.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ cwc.utils.Dialog.prototype.showContent = function(title, content) {
195195
if (this.getDialog_()) {
196196
this.render(title, content, cwc.soy.Dialog.contentTemplate);
197197
let closeButton = goog.dom.getElement(this.prefix + 'close');
198-
closeButton.addEventListener('click', this.close.bind(this));
198+
closeButton.addEventListener('click', () => {
199+
this.close();
200+
});
199201
this.showModal();
200202
}
201203
};
@@ -218,7 +220,9 @@ cwc.utils.Dialog.prototype.showTemplate = function(title, template, values) {
218220
let contentNode = goog.dom.getElement(this.prefix + 'content');
219221
goog.soy.renderElement(contentNode, template, values);
220222
let closeButton = goog.dom.getElement(this.prefix + 'close');
221-
closeButton.addEventListener('click', this.close.bind(this));
223+
closeButton.addEventListener('click', () => {
224+
this.close();
225+
});
222226
this.showModal();
223227
}
224228
return this;
@@ -283,7 +287,9 @@ cwc.utils.Dialog.prototype.showAlert = function(title, content) {
283287
if (this.getDialog_()) {
284288
this.render(title, content, cwc.soy.Dialog.alertTemplate);
285289
let okButton = goog.dom.getElement(this.prefix + 'ok');
286-
okButton.addEventListener('click', this.close.bind(this));
290+
okButton.addEventListener('click', () => {
291+
this.close();
292+
});
287293
if (this.defaultCloseHandler_) {
288294
okButton.addEventListener('click', this.defaultCloseHandler_);
289295
}
@@ -312,7 +318,9 @@ cwc.utils.Dialog.prototype.showPrompt = function(title, content, optValue) {
312318
let inputField = goog.dom.getElement(this.prefix + 'input');
313319

314320
let okButton = goog.dom.getElement(this.prefix + 'ok');
315-
okButton.addEventListener('click', this.close.bind(this));
321+
okButton.addEventListener('click', () => {
322+
this.close();
323+
});
316324
if (this.defaultCloseHandler_) {
317325
okButton.addEventListener('click', this.defaultCloseHandler_);
318326
}
@@ -326,7 +334,9 @@ cwc.utils.Dialog.prototype.showPrompt = function(title, content, optValue) {
326334
});
327335

328336
let cancelButton = goog.dom.getElement(this.prefix + 'cancel');
329-
cancelButton.addEventListener('click', this.close.bind(this));
337+
cancelButton.addEventListener('click', () => {
338+
this.close();
339+
});
330340
cancelButton.addEventListener('click', reject);
331341
this.showModal();
332342
} else {
@@ -348,7 +358,9 @@ cwc.utils.Dialog.prototype.showActionCancel = function(title, content, action) {
348358
if (this.getDialog_()) {
349359
this.render(title, content, cwc.soy.Dialog.actionCancelTemplate, action);
350360
let actionButton = goog.dom.getElement(this.prefix + 'action');
351-
actionButton.addEventListener('click', this.close.bind(this));
361+
actionButton.addEventListener('click', () => {
362+
this.close();
363+
});
352364
if (this.defaultCloseHandler_) {
353365
actionButton.addEventListener('click', this.defaultCloseHandler_);
354366
}
@@ -357,7 +369,9 @@ cwc.utils.Dialog.prototype.showActionCancel = function(title, content, action) {
357369
});
358370

359371
let cancelButton = goog.dom.getElement(this.prefix + 'cancel');
360-
cancelButton.addEventListener('click', this.close.bind(this));
372+
cancelButton.addEventListener('click', () => {
373+
this.close();
374+
});
361375
cancelButton.addEventListener('click', function() {
362376
resolve(false);
363377
});

0 commit comments

Comments
 (0)