Package nxt :: Module system

Source Code for Module nxt.system

  1  # nxt.system module -- LEGO Mindstorms NXT system telegrams 
  2  # Copyright (C) 2006  Douglas P Lau 
  3  # Copyright (C) 2009  Marcus Wanner 
  4  # 
  5  # This program is free software: you can redistribute it and/or modify 
  6  # it under the terms of the GNU General Public License as published by 
  7  # the Free Software Foundation, either version 3 of the License, or 
  8  # (at your option) any later version. 
  9  # 
 10  # This program is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  # GNU General Public License for more details. 
 14   
 15  'Use for communications regarding the NXT filesystem and such ***ADVANCED USERS ONLY***' 
 16   
17 -def _create(opcode):
18 'Create a simple system telegram' 19 from telegram import Telegram 20 return Telegram(False, opcode)
21
22 -def _create_with_file(opcode, fname):
23 tgram = _create(opcode) 24 tgram.add_filename(fname) 25 return tgram
26
27 -def _create_with_handle(opcode, handle):
28 tgram = _create(opcode) 29 tgram.add_u8(handle) 30 return tgram
31
32 -def open_read(opcode, fname):
33 return _create_with_file(opcode, fname)
34
35 -def _parse_open_read(tgram):
36 tgram.check_status() 37 handle = tgram.parse_u8() 38 n_bytes = tgram.parse_u32() 39 return (handle, n_bytes)
40
41 -def open_write(opcode, fname, n_bytes):
42 tgram = _create_with_file(opcode, fname) 43 tgram.add_u32(n_bytes) 44 return tgram
45
46 -def _parse_open_write(tgram):
47 tgram.check_status() 48 handle = tgram.parse_u8() 49 return handle
50
51 -def read(opcode, handle, n_bytes):
52 tgram = _create_with_handle(opcode, handle) 53 tgram.add_u16(n_bytes) 54 return tgram
55
56 -def _parse_read(tgram):
57 tgram.check_status() 58 handle = tgram.parse_u8() 59 n_bytes = tgram.parse_u16() 60 data = tgram.parse_string() 61 return (handle, n_bytes, data)
62
63 -def write(opcode, handle, data):
64 tgram = _create_with_handle(opcode, handle) 65 tgram.add_string(len(data), data) 66 return tgram
67
68 -def _parse_write(tgram):
69 tgram.check_status() 70 handle = tgram.parse_u8() 71 n_bytes = tgram.parse_u16() 72 return (handle, n_bytes)
73
74 -def close(opcode, handle):
75 return _create_with_handle(opcode, handle)
76
77 -def _parse_close(tgram):
78 tgram.check_status() 79 handle = tgram.parse_u8() 80 return handle
81
82 -def delete(opcode, fname):
83 return _create_with_file(opcode, fname)
84
85 -def _parse_delete(tgram):
86 tgram.check_status() 87 handle = tgram.parse_u8() 88 fname = tgram.parse_string() 89 return (handle, fname)
90
91 -def find_first(opcode, fname):
92 return _create_with_file(opcode, fname)
93
94 -def _parse_find(tgram):
95 tgram.check_status() 96 handle = tgram.parse_u8() 97 fname = tgram.parse_string(20) 98 n_bytes = tgram.parse_u32() 99 return (handle, fname, n_bytes)
100
101 -def find_next(opcode, handle):
102 return _create_with_handle(opcode, handle)
103
104 -def get_firmware_version(opcode):
105 return _create(opcode)
106
107 -def _parse_get_firmware_version(tgram):
108 tgram.check_status() 109 prot_minor = tgram.parse_u8() 110 prot_major = tgram.parse_u8() 111 prot_version = (prot_major, prot_minor) 112 fw_minor = tgram.parse_u8() 113 fw_major = tgram.parse_u8() 114 fw_version = (fw_major, fw_minor) 115 return (prot_version, fw_version)
116
117 -def open_write_linear(opcode, fname, n_bytes):
118 tgram = _create_with_file(opcode, fname) 119 tgram.add_u32(n_bytes) 120 return tgram
121
122 -def open_read_linear(opcode, fname):
123 return _create_with_file(opcode, fname)
124
125 -def _parse_open_read_linear(tgram):
126 tgram.check_status() 127 n_bytes = tgram.parse_u32() 128 return n_bytes
129
130 -def open_write_data(opcode, fname, n_bytes):
131 tgram = _create_with_file(opcode, fname) 132 tgram.add_u32(n_bytes) 133 return tgram
134
135 -def open_append_data(opcode, fname):
136 return _create_with_file(opcode, fname)
137
138 -def _parse_open_append_data(tgram):
139 tgram.check_status() 140 handle = tgram.parse_u8() 141 n_bytes = tgram.parse_u32() 142 return (handle, n_bytes)
143
144 -def request_first_module(opcode, mname):
145 return _create_with_file(opcode, mname)
146
147 -def _parse_request_module(tgram):
148 tgram.check_status() 149 handle = tgram.parse_u8() 150 mname = tgram.parse_string(20) 151 mod_id = tgram.parse_u32() 152 mod_size = tgram.parse_u32() 153 mod_iomap_size = tgram.parse_u16() 154 return (handle, mname, mod_id, mod_size, mod_iomap_size)
155
156 -def request_next_module(opcode, handle):
157 return _create_with_handle(opcode, handle)
158
159 -def close_module_handle(opcode, handle):
160 return _create_with_handle(opcode, handle)
161
162 -def read_io_map(opcode, mod_id, offset, n_bytes):
163 tgram = _create(opcode) 164 tgram.add_u32(mod_id) 165 tgram.add_u16(offset) 166 tgram.add_u16(n_bytes) 167 return tgram
168
169 -def _parse_read_io_map(tgram):
170 tgram.check_status() 171 mod_id = tgram.parse_u32() 172 n_bytes = tgram.parse_u16() 173 contents = tgram.parse_string() 174 return (mod_id, n_bytes, contents)
175
176 -def write_io_map(opcode, mod_id, offset, content):
177 tgram = _create(opcode) 178 tgram.add_u32(mod_id) 179 tgram.add_u16(offset) 180 tgram.add_u16(len(content)) 181 tgram.add_string(len(content), content) 182 return tgram
183
184 -def _parse_write_io_map(tgram):
185 tgram.check_status() 186 mod_id = tgram.parse_u32() 187 n_bytes = tgram.parse_u16() 188 return (mod_id, n_bytes)
189
190 -def boot(opcode):
191 # Note: this command is USB only (no Bluetooth) 192 tgram = _create(opcode) 193 tgram.add_string(19, "Let's dance: SAMBA\0") 194 return tgram
195
196 -def _parse_boot(tgram):
197 tgram.check_status() 198 resp = tgram.parse_string() 199 # Resp should be 'Yes\0' 200 return resp
201
202 -def set_brick_name(opcode, bname):
203 tgram = _create(opcode) 204 # FIXME: validate brick name 205 tgram.add_string(len(bname), bname) 206 return tgram
207
208 -def _parse_set_brick_name(tgram):
209 tgram.check_status()
210
211 -def get_device_info(opcode):
212 return _create(opcode)
213
214 -def _parse_get_device_info(tgram):
215 tgram.check_status() 216 name = tgram.parse_string(15) 217 a0 = tgram.parse_u8() 218 a1 = tgram.parse_u8() 219 a2 = tgram.parse_u8() 220 a3 = tgram.parse_u8() 221 a4 = tgram.parse_u8() 222 a5 = tgram.parse_u8() 223 a6 = tgram.parse_u8() 224 # FIXME: what is a6 for? 225 address = '%02X:%02X:%02X:%02X:%02X:%02X' % (a0, a1, a2, a3, a4, a5) 226 signal_strength = tgram.parse_u32() 227 user_flash = tgram.parse_u32() 228 return (name, address, signal_strength, user_flash)
229
230 -def delete_user_flash(opcode):
231 return _create(opcode)
232
233 -def _parse_delete_user_flash(tgram):
234 tgram.check_status()
235
236 -def poll_command_length(opcode, buf_num):
237 tgram = _create(opcode) 238 tgram.add_u8(buf_num) 239 return tgram
240
241 -def _parse_poll_command_length(tgram):
242 buf_num = tgram.parse_u8() 243 tgram.check_status() 244 n_bytes = tgram.parse_u8() 245 return (buf_num, n_bytes)
246
247 -def poll_command(opcode, buf_num, n_bytes):
248 tgram = _create(opcode) 249 tgram.add_u8(buf_num) 250 tgram.add_u8(n_bytes) 251 return tgram
252
253 -def _parse_poll_command(tgram):
254 buf_num = tgram.parse_u8() 255 tgram.check_status() 256 n_bytes = tgram.parse_u8() 257 command = tgram.parse_string() 258 return (buf_num, n_bytes, command)
259
260 -def bluetooth_factory_reset(opcode):
261 # Note: this command is USB only (no Bluetooth) 262 return _create(opcode)
263
264 -def _parse_bluetooth_factory_reset(tgram):
265 tgram.check_status()
266 267 OPCODES = { 268 0x80: (open_read, _parse_open_read), 269 0x81: (open_write, _parse_open_write), 270 0x82: (read, _parse_read), 271 0x83: (write, _parse_write), 272 0x84: (close, _parse_close), 273 0x85: (delete, _parse_delete), 274 0x86: (find_first, _parse_find), 275 0x87: (find_next, _parse_find), 276 0x88: (get_firmware_version, _parse_get_firmware_version), 277 0x89: (open_write_linear, _parse_open_write), 278 0x8A: (open_read_linear, _parse_open_read_linear), 279 0x8B: (open_write_data, _parse_open_write), 280 0x8C: (open_append_data, _parse_open_append_data), 281 0x90: (request_first_module, _parse_request_module), 282 0x91: (request_next_module, _parse_request_module), 283 0x92: (close_module_handle, _parse_close), 284 0x94: (read_io_map, _parse_read_io_map), 285 0x95: (write_io_map, _parse_write_io_map), 286 0x97: (boot, _parse_boot), 287 0x98: (set_brick_name, _parse_set_brick_name), 288 0x9B: (get_device_info, _parse_get_device_info), 289 0xA0: (delete_user_flash, _parse_delete_user_flash), 290 0xA1: (poll_command_length, _parse_poll_command_length), 291 0xA2: (poll_command, _parse_poll_command), 292 0xA4: (bluetooth_factory_reset, _parse_bluetooth_factory_reset), 293 } 294