-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpheroRequest.py
231 lines (157 loc) · 3.92 KB
/
SpheroRequest.py
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# coding: utf-8
import struct
import SpheroResponse
class Request(object):
SOP1 = 0xFF
SOP2 = 0xFF
did = 0x00
cid = 0x00
fmt = None
def __init__(self, seq=0x00, *data):
self.seq = seq
self.data = data
if not self.fmt:
self.fmt = '%sB' % len(self.data)
def __str__(self):
return self.bytes
def checksum(self):
body = self.packet_header() + self.packet_body()
body = struct.unpack('%sB' % len(body), body)
return struct.pack('B', ~(sum(body[2:]) % 256) & 0xFF)
@property
def bytes(self):
return self.packet_header() + self.packet_body() + self.checksum()
def packet_header(self):
return struct.pack('6B', *self.header())
def packet_body(self):
if not self.data:
return ''
return struct.pack(self.fmt, *self.data)
@property
def dlen(self):
return struct.calcsize(self.fmt) + 1
def header(self):
return [self.SOP1, self.SOP2, self.did, self.cid, self.seq, self.dlen]
def response(self, header, body):
name = self.__class__.__name__.split('.')[-1]
klass = getattr(SpheroResponse, name, SpheroResponse.Response)
return klass(header, body)
class Core(Request):
did = 0x00
class Ping(Core):
cid = 0x01
class GetVersion(Core):
cid = 0x02
class SetDeviceName(Core):
cid = 0x10
class GetBluetoothInfo(Core):
cid = 0x11
class GetAutoReconnect(Core):
cid = 0x12
class SetAutoReconnect(Core):
cid = 0x13
class GetPowerState(Core):
cid = 0x20
class SetPowerNotification(Core):
cid = 0x21
class Sleep(Core):
cid = 0x22
class GetVoltageTripPoints(Core):
cid = 0x23
class SetVoltageTripPoints(Core):
cid = 0x24
class SetInactivityTimeout(Core):
cid = 0x25
class JumpToBootloader(Core):
cid = 0x30
class PerformLevel1Diagnostics(Core):
cid = 0x40
class PerformLevel2Diagnostics(Core):
cid = 0x41
class ClearCounters(Core):
cid = 0x42
class SetTimeValue(Core):
cid = 0x50
class PollPacketTimes(Core):
cid = 0x51
#Sphero Commands
class Sphero(Request):
did = 0x02
class SetHeading(Sphero):
cid = 0x01
fmt = '!H'
class SetStabilization(Sphero):
cid = 0x02
class SetRotationRate(Sphero):
cid = 0x03
class SetApplicationConfigurationBlock(Sphero):
cid = 0x04
class GetApplicationConfigurationBlock(Sphero):
cid = 0x05
class ReenableDemoMode(Sphero):
cid = 0x06
class GetChassisId(Sphero):
cid = 0x07
class SetChassisId(Sphero):
cid = 0x08
class SelfLevel(Sphero):
cid = 0x09
class SetVDL(Sphero):
cid = 0x0A
class SetDataStreaming(Sphero):
cid = 0x11
class ConfigureCollisionDetection(Sphero):
cid = 0x12
class Locator(Sphero):
cid = 0x13
class SetAccelerometer(Sphero):
cid = 0x14
class ReadLocator(Sphero):
cid=0x15
class SetRGB(Sphero):
cid = 0x20
class SetBackLEDOutput(Sphero):
cid = 0x21
class GetRGB(Sphero):
cid = 0x22
class Roll(Sphero):
fmt = '!BHB' #Speed, heading, state
cid = 0x30
class SetBoostWithTime(Sphero):
cid = 0x31
class SetRawMotorValues(Sphero):
cid = 0x33
class SetMotionTimeout(Sphero):
cid = 0x34
class SetOptionFlags(Sphero):
cid = 0x35
class GetOptionFlags(Sphero):
cid = 0x36
class GetConfigurationBlock(Sphero):
cid = 0x40
class GetDeviceMode(Sphero):
cid = 0x42
class RunMacro(Sphero):
cid = 0x50
class SaveTemporaryMacro(Sphero):
cid = 0x51
class ReinitMacro(Sphero):
cid = 0x54
class AbortMacro(Sphero):
cid = 0x55
class GetMacroStatus(Sphero):
cid = 0x56
class SetMacroParameter(Sphero):
cid = 0x57
class AppendMacroChunk(Sphero):
cid = 0x58
class EraseOrbbasicStorage(Sphero):
cid = 0x60
class AppendOrbbasicFragment(Sphero):
cid = 0x61
class RunOrbbasicProgram(Sphero):
cid = 0x62
class AbortOrbbasicProgram(Sphero):
cid = 0x63
class AnswerInput(Sphero):
cid = 0x64