1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 from time import sleep
16 from nxt.error import FileNotFound, ModuleNotFound
17 from nxt.telegram import OPCODES, Telegram
18
20 def poll(self, *args, **kwargs):
21 ogram = poll_func(opcode, *args, **kwargs)
22 self.sock.send(str(ogram))
23 igram = Telegram(opcode=opcode, pkt=self.sock.recv())
24 return parse_func(igram)
25 return poll
26
36
38 'Main object for NXT Control'
39
40 __metaclass__ = _Meta
41
44
46 self.play_tone(frequency, duration)
47 sleep(duration / 1000.0)
48
50 'Context manager to find files on a NXT brick'
51
53 self.brick = brick
54 self.pattern = pattern
55 self.handle = None
56
59
61 if self.handle:
62 self.brick.close(self.handle)
63
65 self.handle, fname, size = self.brick.find_first(self.pattern)
66 yield (fname, size)
67 while True:
68 try:
69 handle, fname, size = self.brick.find_next(
70 self.handle)
71 yield (fname, size)
72 except FileNotFound:
73 break
74
76 'Context manager to read a file on a NXT brick'
77
81
83 self.handle, self.size = self.brick.open_read(self.fname)
84 return self
85
88
90 rem = self.size
91 bsize = self.brick.sock.bsize
92 while rem > 0:
93 handle, bsize, data = self.brick.read(self.handle,
94 min(bsize, rem))
95 yield data
96 rem -= len(data)
97
99 'Context manager to write a file to a NXT brick'
100
102 self.brick = brick
103 self.fname = fname
104 self.fil = fil
105 fil.seek(0, 2)
106 self.size = fil.tell()
107 fil.seek(0)
108
110 self.handle = self.brick.open_write(self.fname, self.size)
111 return self
112
115
117 rem = self.size
118 bsize = self.brick.sock.bsize
119 while rem > 0:
120 b = min(bsize, rem)
121 handle, size = self.brick.write(self.handle,
122 self.fil.read(b))
123 yield size
124 rem -= b
125
127 'Context manager to lookup modules on a NXT brick'
128
130 self.brick = brick
131 self.pattern = pattern
132 self.handle = None
133
136
138 if self.handle:
139 self.brick.close(self.handle)
140
142 self.handle, mname, mid, msize, miomap_size = \
143 self.brick.request_first_module(self.pattern)
144 yield (mname, mid, msize, miomap_size)
145 while True:
146 try:
147 handle, mname, mid, msize, miomap_size = \
148 self.brick.request_next_module(
149 self.handle)
150 yield (mname, mid, msize, miomap_size)
151 except ModuleNotFound:
152 break
153