Repository navigation
Expand file tree
/
Copy pathcommands.js
More file actions
214 lines (188 loc) · 5.9 KB
/
Copy pathcommands.js
File metadata and controls
214 lines (188 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* @fileoverview Byte commands for the Sphero communication.
*
* @license Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author mbordihn@google.com (Markus Bordihn)
*/
goog.provide('cwc.protocol.sphero.Commands');
goog.require('cwc.protocol.sphero.Buffer');
goog.require('cwc.protocol.sphero.CallbackType');
goog.require('cwc.protocol.sphero.Command');
/**
* @constructor
* @struct
* @final
*/
cwc.protocol.sphero.Commands = function() {
/** @private {Object} */
this.cache_ = {};
};
/**
* Sets RGB LED color.
* @param {!number} red 0-255
* @param {!number} green 0-255
* @param {!number} blue 0-255
* @param {boolean=} opt_persistant
* @return {!ArrayBuffer}
* @export
*/
cwc.protocol.sphero.Commands.prototype.setRGB = function(red, green, blue,
opt_persistant) {
var buffer = new cwc.protocol.sphero.Buffer();
buffer.writeCommand(cwc.protocol.sphero.Command.RGB_LED.SET);
buffer.writeByte(red);
buffer.writeByte(green);
buffer.writeByte(blue);
buffer.writeByte(opt_persistant == false ? 0x00 : 0x01);
return buffer.readSigned();
};
/**
* Gets current RGB LED color.
* @return {!ArrayBuffer}
* @export
*/
cwc.protocol.sphero.Commands.prototype.getRGB = function() {
var buffer = new cwc.protocol.sphero.Buffer(
cwc.protocol.sphero.CallbackType.RGB);
buffer.writeCommand(cwc.protocol.sphero.Command.RGB_LED.GET);
return buffer.readSigned();
};
/**
* Sets back-light LED brightness.
* @param {!number} brightness 0-255
* @return {!ArrayBuffer}
* @export
*/
cwc.protocol.sphero.Commands.prototype.setBackLed = function(brightness) {
var buffer = new cwc.protocol.sphero.Buffer();
buffer.writeCommand(cwc.protocol.sphero.Command.BACK_LED);
buffer.writeByte(brightness);
return buffer.readSigned();
};
/**
* Sets heading.
* @param {!number} heading 0-359
* @return {!ArrayBuffer}
* @export
*/
cwc.protocol.sphero.Commands.prototype.setHeading = function(heading) {
var buffer = new cwc.protocol.sphero.Buffer();
buffer.writeCommand(cwc.protocol.sphero.Command.HEADING);
buffer.writeUInt(heading);
return buffer.readSigned();
};
/**
* Rolls the Sphero.
* @param {number} opt_speed 0-255
* @param {number=} opt_heading 0-359
* @param {boolean=} opt_state
* @return {!ArrayBuffer}
* @export
*/
cwc.protocol.sphero.Commands.prototype.roll = function(opt_speed, opt_heading,
opt_state) {
var buffer = new cwc.protocol.sphero.Buffer();
buffer.writeCommand(cwc.protocol.sphero.Command.ROLL);
buffer.writeByte(opt_speed, 50);
buffer.writeUInt(opt_heading, 0);
buffer.writeByte(opt_state === undefined ? 0x01 : (opt_state ? 0x01 : 0x00));
return buffer.readSigned();
};
/**
* Sets collision detection.
* @param {number=} opt_method 0x00 to disable this service.
* @param {number=} opt_threshold_x left/right axes
* @param {number=} opt_threshold_y front/back axes
* @param {number=} opt_speed_x
* @param {number=} opt_speed_y
* @param {number=} opt_interval in 10msec
* @export
*/
cwc.protocol.sphero.Commands.prototype.setColisionDetection = function(
opt_method, opt_threshold_x, opt_threshold_y, opt_speed_x, opt_speed_y,
opt_interval) {
var buffer = new cwc.protocol.sphero.Buffer();
buffer.writeCommand(cwc.protocol.sphero.Command.COLLISION_DETECTION);
buffer.writeByte(opt_method, 0x01);
buffer.writeByte(opt_threshold_x, 0x60);
buffer.writeByte(opt_threshold_y, 0x60);
buffer.writeByte(opt_speed_x, 0x60);
buffer.writeByte(opt_speed_y, 0x60);
buffer.writeByte(opt_interval, 0x0A);
return buffer.readSigned();
};
/**
* Sets montion timeout.
* @param {!number} timeout in msec
* @return {!ArrayBuffer}
* @export
*/
cwc.protocol.sphero.Commands.prototype.setMotionTimeout = function(timeout) {
var buffer = new cwc.protocol.sphero.Buffer();
buffer.writeCommand(cwc.protocol.sphero.Command.MOTION_TIMEOUT);
buffer.writeByte(timeout);
return buffer.readSigned();
};
/**
* @param {!boolean} enabled
* @return {!ArrayBuffer}
* @export
*/
cwc.protocol.sphero.Commands.prototype.boost = function(enabled) {
var buffer = new cwc.protocol.sphero.Buffer();
buffer.writeCommand(cwc.protocol.sphero.Command.BOOST);
buffer.writeByte(enabled ? 0x01 : 0x00);
return buffer.readSigned();
};
/**
* Puts the Sphero into sleep.
* @param {number=} opt_wakeup
* @param {number=} opt_macro
* @param {number=} opt_orb_basic
* @return {!ArrayBuffer}
* @export
*/
cwc.protocol.sphero.Commands.prototype.sleep = function(opt_wakeup, opt_macro,
opt_orb_basic) {
var buffer = new cwc.protocol.sphero.Buffer();
buffer.writeCommand(cwc.protocol.sphero.Command.SYSTEM.SLEEP);
buffer.writeByte(opt_wakeup, 0x00);
buffer.writeByte(opt_macro, 0x00);
buffer.writeByte(opt_orb_basic, 0x00);
return buffer.readSigned();
};
/**
* Reads the current Sphero location.
* @return {!ArrayBuffer}
* @export
*/
cwc.protocol.sphero.Commands.prototype.getLocation = function() {
var buffer = new cwc.protocol.sphero.Buffer(
cwc.protocol.sphero.CallbackType.LOCATION);
buffer.writeCommand(cwc.protocol.sphero.Command.LOCATION.GET);
return buffer.readSigned();
};
/**
* Reads current Sphero version.
* @return {!ArrayBuffer}
* @export
*/
cwc.protocol.sphero.Commands.prototype.getVersion = function() {
var buffer = new cwc.protocol.sphero.Buffer(
cwc.protocol.sphero.CallbackType.FIRMWARE);
buffer.writeCommand(cwc.protocol.sphero.Command.SYSTEM.VERSION);
return buffer.readSigned();
};