Skip to content

Commit eee5269

Browse files
committed
Added status page for BLE parring/connection status.
TODO: Adding correct data buffer handling for fragments in the BLE interface.
1 parent 6b54832 commit eee5269

File tree

6 files changed

+70
-22
lines changed

6 files changed

+70
-22
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"karma-chrome-launcher": "^2.2.0",
1515
"karma-closure": "^0.1.3",
1616
"npm-check-updates": "^2.14.1",
17-
"nw": "^0.29.0",
17+
"nw": "^0.29.1",
1818
"nw-builder": "^3.5.1",
1919
"rimraf": "^2.6.2"
2020
},

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,25 +108,22 @@ cwc.protocol.sphero.v1.Api.prototype.connect = function(device) {
108108

109109
if (!this.prepared) {
110110
console.log('Preparing Sphero bluetooth LE api for', device.getId());
111-
this.eventHandler.dispatchEvent(
112-
cwc.protocol.sphero.v1.Events.connecting(
113-
'Connecting device ...', 1));
111+
this.eventHandler.dispatchEvent(cwc.protocol.sphero.v1.Events.connect(
112+
'Connecting device ...', 1));
114113
this.device = device;
115114

116115
// Enable Developer mode.
117116
this.device.sendRaw(
118117
new TextEncoder('utf-8').encode('011i3'),
119118
'22bb746f-2bbd-7554-2d6f-726568705327', () => {
120-
this.eventHandler.dispatchEvent(
121-
cwc.protocol.sphero.v1.Events.connecting(
119+
this.eventHandler.dispatchEvent(cwc.protocol.sphero.v1.Events.connect(
122120
'Enable developer mode ...', 2));
123121
});
124122

125123
// Power on device.
126124
this.device.sendRaw(
127125
new Uint8Array([0x07]), '22bb746f-2bb2-7554-2d6f-726568705327', () => {
128-
this.eventHandler.dispatchEvent(
129-
cwc.protocol.sphero.v1.Events.connecting(
126+
this.eventHandler.dispatchEvent(cwc.protocol.sphero.v1.Events.connect(
130127
'Power on device. Waiting until device wakes up ...', 2));
131128
});
132129

@@ -135,9 +132,8 @@ cwc.protocol.sphero.v1.Api.prototype.connect = function(device) {
135132
new Uint8Array([0x01]), '22bb746f-2bbf-7554-2d6f-726568705327', () => {
136133
this.prepare();
137134
this.runTest();
138-
this.eventHandler.dispatchEvent(
139-
cwc.protocol.sphero.v1.Events.connecting(
140-
'Connected ...', 3));
135+
this.eventHandler.dispatchEvent(cwc.protocol.sphero.v1.Events.connect(
136+
'Ready ...', 3));
141137
});
142138
}
143139
return true;
@@ -367,7 +363,7 @@ cwc.protocol.sphero.v1.Api.prototype.runTest = function() {
367363
this.setBackLed(25);
368364
this.setBackLed(0);
369365

370-
this.setRGB(255, 0, 0);
366+
this.setRGB(64, 64, 64);
371367
this.roll(0, 180);
372368
};
373369

@@ -459,11 +455,23 @@ cwc.protocol.sphero.v1.Api.prototype.parseCollisionData_ = function(data) {
459455
* @private
460456
*/
461457
cwc.protocol.sphero.v1.Api.prototype.handleData_ = function(buffer) {
462-
if (!this.verifiyChecksum_(buffer)) {
458+
let data = new Uint8Array(buffer);
459+
console.log('handleData', buffer, data);
460+
if (data[0] !== 0xFF) {
461+
console.error('Data fragment ...');
462+
return;
463+
}
464+
if (!this.verifiyChecksum_(data)) {
463465
console.error('Checksum error ...');
464466
return;
465467
}
466-
console.log('handleData', buffer);
468+
if (data[1] === cwc.protocol.sphero.v1.ResponseType.ACKNOWLEDGEMENT) {
469+
this.handleAcknowledged_(data);
470+
} else if (data[1] === cwc.protocol.sphero.v1.ResponseType.ASYNCHRONOUS) {
471+
this.handleAsync_(data);
472+
} else {
473+
console.error('Data error ...');
474+
}
467475
};
468476

469477

@@ -473,9 +481,6 @@ cwc.protocol.sphero.v1.Api.prototype.handleData_ = function(buffer) {
473481
* @private
474482
*/
475483
cwc.protocol.sphero.v1.Api.prototype.handleAcknowledged_ = function(buffer) {
476-
if (!this.verifiyChecksum_(buffer)) {
477-
return;
478-
}
479484
let type = buffer[3];
480485
let len = buffer[4];
481486
let data = buffer.slice(5, buffer.length -1);
@@ -499,9 +504,6 @@ cwc.protocol.sphero.v1.Api.prototype.handleAcknowledged_ = function(buffer) {
499504
* @private
500505
*/
501506
cwc.protocol.sphero.v1.Api.prototype.handleAsync_ = function(buffer) {
502-
if (!this.verifiyChecksum_(buffer)) {
503-
return;
504-
}
505507
let message = buffer[2];
506508
let len = buffer[4];
507509
let data = buffer.slice(5, buffer.length -1);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ cwc.protocol.sphero.v1.CommandType = {
8484
};
8585

8686

87+
/**
88+
* @enum {!Object.<number>|number}
89+
*/
90+
cwc.protocol.sphero.v1.ResponseType = {
91+
ACKNOWLEDGEMENT: 0xFF,
92+
ASYNCHRONOUS: 0xFE,
93+
};
94+
95+
8796
/**
8897
* @enum {number}
8998
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ cwc.protocol.sphero.v1.Events.collision = function(data) {
8585
* @return {!cwc.utils.EventData}
8686
* @final
8787
*/
88-
cwc.protocol.sphero.v1.Events.connecting = function(data, step) {
88+
cwc.protocol.sphero.v1.Events.connect = function(data, step) {
8989
return new cwc.utils.EventData(
9090
cwc.protocol.sphero.v1.Events.Type.CONNECTING, data, step);
9191
};

src/ui/connect_screen/bluetooth/connect_screen.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,11 @@ 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.close_();
111+
this.showConnectingStep(
112+
'Pairing Device', 'Pairing device' + device.name, 1);
112113
resolve(bluetoothDevice);
114+
}).catch(() => {
115+
this.close_();
113116
});
114117
});
115118
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"format": "Coding with Chrome File Format 3",
3+
"content": {
4+
"blockly": {
5+
"content": "<xml xmlns=\"http://www.w3.org/1999/xhtml\">\n <variables></variables>\n <block type=\"sphero_collision\" x=\"133\" y=\"126\">\n <statement name=\"CODE\">\n <block type=\"sphero_rgb\">\n <value name=\"colour\">\n <block type=\"colour_picker\">\n <field name=\"COLOUR\">#ff0000</field>\n </block>\n </value>\n <next>\n <block type=\"sphero_rgb\">\n <value name=\"colour\">\n <block type=\"colour_picker\">\n <field name=\"COLOUR\">#33ff33</field>\n </block>\n </value>\n </block>\n </next>\n </block>\n </statement>\n </block>\n</xml>",
6+
"name": "blockly",
7+
"size": 642,
8+
"type": "application/blockly+xml",
9+
"version": 1
10+
},
11+
"__javascript__": {
12+
"content": "",
13+
"name": "__javascript__",
14+
"size": 151,
15+
"type": "application/javascript",
16+
"version": 1
17+
}
18+
},
19+
"files": {},
20+
"flags": {
21+
"__editor__": {}
22+
},
23+
"frameworks": {},
24+
"history": "",
25+
"metadata": {
26+
"__default__": {
27+
"author": "Markus Bordihn",
28+
"title": "Collision example",
29+
"version": "1.1"
30+
}
31+
},
32+
"mode": "sphero_sprk_plus_blockly",
33+
"ui": "blockly"
34+
}

0 commit comments

Comments
 (0)