i2c  1.4.2
RoadNarrows Robotics I2C Package
basetypes.py
Go to the documentation of this file.
1 #
2 # Package: RoadNarrows Robotics I2C
3 #
4 # Module: rnr.basetypes
5 #
6 # Link: https://roadnarrows.com
7 #
8 # File: basetypes.py
9 #
10 
11 ## \file
12 ##
13 ## $LastChangedDate: 2016-01-28 14:19:12 -0700 (Thu, 28 Jan 2016) $
14 ## $Rev: 4278 $
15 ##
16 ## \brief RoadNarrows Robotics I2C types module.
17 ##
18 ## \author: Robin Knight (robin.knight@roadnarrows.com)
19 ##
20 ## \copyright
21 ## \h_copy 2016-2017. RoadNarrows LLC.\n
22 ## http://www.roadnarrows.com\n
23 ## All Rights Reserved
24 #
25 # @EulaBegin@
26 #
27 # Permission is hereby granted, without written agreement and without
28 # license or royalty fees, to use, copy, modify, and distribute this
29 # software and its documentation for any purpose, provided that
30 # (1) The above copyright notice and the following two paragraphs
31 # appear in all copies of the source code and (2) redistributions
32 # including binaries reproduces these notices in the supporting
33 # documentation. Substantial modifications to this software may be
34 # copyrighted by their authors and need not follow the licensing terms
35 # described here, provided that the new terms are clearly indicated in
36 # all files where they apply.
37 #
38 # IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
39 # OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
40 # PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
41 # DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
42 # EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
43 # THE POSSIBILITY OF SUCH DAMAGE.
44 #
45 # THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
46 # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
47 # FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
48 # "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
49 # PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
50 #
51 # @EulaEnd@
52 #
53 
54 import rnr.i2ccore as i2ccore
55 
56 #--
57 def sizeof(bsObj):
58  """ Size of object in bytes. Size is contextual by object type. """
59  return bsObj.__sizeof__()
60 ##
61 
62 
63 
64 #------------------------------------------------------------------------------
65 # CLASS: ByteBuf
66 #------------------------------------------------------------------------------
67 class ByteBuf:
68  """ RNR Byte Buffer Class.
69 
70  The ByteBuf class provides a wrapper around the byte_t* SWIG object.
71  The byte_t is typedef'ed in C as an unsigned char. Byte buffers are used
72  in packing and unpacking buffers in function calls.
73 
74  Note: There does not seem to be any way to advance the pointer of the
75  underlining byte_t* SWIG object, so a copy from/to a python byte
76  list (buffer) is required.
77  """
78 
79  #--
80  def __init__(self, size=256):
81  """ Initialize byte buffer instance.
82 
83  Parameters:
84  size - Number of bytes of the underlining fixed sized SWIG
85  object.
86  """
87  ## internal swig object
88  self._swigObj = i2ccore.new_byteArray(size)
89 
90  ## internal swig object fixed buffer size
91  self._size = size
92 
93  ## python byte string
94  self.buf = ''
95 
96  #--
97  def __del__(self):
98  """ Delete byte buffer instance. """
99  i2ccore.delete_byteArray(self._swigObj)
100  ##
101 
102  #--
103  @staticmethod
104  def Constructor(seq):
105  """ Conversion constructor.
106 
107  Parameters:
108  seq - Sequence of values to convert. Sequence type can be an
109  integer [0,255], a string, or a list of elements of
110  type integer, string, list.
111 
112  Return:
113  New initialized ByteBuf().
114  """
115  buf = ByteBuf.bytestring(seq)
116  bytebuf = ByteBuf(size=len(buf))
117  bytebuf.buf = buf
118  return bytebuf
119 
120  #--
121  @staticmethod
122  def Clone(buf):
123  """ Smart clone constructor.
124 
125  If buf is a ByteBuf instance, then simply return buf.
126  Otherwise convert buf to a ByteBuf instance.
127 
128  The underlining swig object contents are not touched, so swig copies
129  will need to be done prior to or after calling a byte_t* core routine.
130 
131  Parameters:
132  buf - Either a ByteBuf instance or a sequence of values to convert.
133  Sequence type can be an integer [0,255], a string, or a list
134  of elements of type integer, string, list.
135 
136  Return:
137  Existing or new ByteBuf().
138  """
139  if isinstance(buf, ByteBuf):
140  return buf
141  else:
142  return ByteBuf.Constructor(buf)
143  ##
144 
145  #--
146  def __getitem__(self, i):
147  """ x.__getitem__(i) <==> x[i] """
148  return self.buf.__getitem__(i)
149  ##
150 
151  #--
152  def __getslice__(self, i, j):
153  """ x.__getslice__(i, j) <==> x[i:j] """
154  return self.buf.__getslice__(i, j)
155  ##
156 
157  #--
158  def __iadd__(self, y):
159  """ x.__iadd__(y) <==> x+=y """
160  z = ByteBuf.bytestring(y)
161  self.buf.__iadd__(z)
162  return self
163  ##
164 
165  #--
166  def __imul__(self, y):
167  """ x.__imul__(y) <==> x*=y """
168  self.buf.__imul__(y)
169  return self
170  ##
171 
172  #--
173  def __iter__(self):
174  """ x.__iter__() <==> iter(x) """
175  return self.buf.__iter__()
176  ##
177 
178  #--
179  def __len__(self):
180  """ x.__len__() <==> len(x)
181 
182  Number of bytes used in buffer.
183  """
184  return len(self.buf)
185  ##
186 
187  #--
188  def __str__(self):
189  """ x.__str__() <==> str(x) <==> print x """
190  return self.buf.__str__()
191  ##
192 
193  #--
194  def __setitem__(self, i, y):
195  """ x.__setitem__(i, y) <==> x[i]=y """
196  z = self.byte(y)
197  self.buf.__setitem__(i, z)
198  return self
199  ##
200 
201  #--
202  def __setslice__(self, i, j, y):
203  """ x.__setslice__(i, j, y) <==> x[i:j]=y """
204  z = ByteBuf.bytestring(y)
205  self.buf.__setslice__(i, j, z)
206  return self
207  ##
208 
209  #--
210  def __sizeof__(self):
211  """ x.__sizeof__() -- size of swig object byte buffer, in bytes. """
212  return self._size
213  ##
214 
215  #--
216  def avail(self):
217  """ Available bytes in fixed swig object buffer. """
218  return self._size - len(self.buf)
219  ##
220 
221  #--
222  def copyFromSwigObj(self, n, i=0, j=0):
223  """ Copy swig object buffer to python buffer.
224 
225  Parameters:
226  n - Number of bytes to copy.
227  i - Starting source index in swig object buffer.
228  j - Starting destination index in python buffer.
229  """
230  assert i+n <= self._size, "index out of range"
231  l = len(self.buf)
232  if l < j+n:
233  self.buf += chr(0) * (j + n - l)
234  s = ''
235  for k in xrange(n):
236  s += chr(i2ccore.byteArray_getitem(self._swigObj, i+k))
237  self.buf = self.buf[:j] + s
238  ##
239 
240  #--
241  def copyToSwigObj(self, n, i=0, j=0):
242  """ Copy python buffer to instance swig object buffer.
243 
244  Parameters:
245  n - Number of bytes to copy.
246  i - Starting source index in python buffer.
247  j - Starting destination index in swig object buffer.
248  """
249  assert i+n <= len(self.buf), "index out of range"
250  assert j+n <= self._size, "index out of range"
251  for k in xrange(n):
252  i2ccore.byteArray_setitem(self._swigObj, j+k, ord(self.buf[i+k]))
253  ##
254 
255  #--
256  def getSwigObj(self):
257  """ Return raw swig object. """
258  return self._swigObj
259  ##
260 
261  #--
262  def getBuffer(self):
263  """ Return python buffer. """
264  return self.buf
265  ##
266 
267  #--
268  def sizeof(self):
269  """ Size of fixed swig object byte buffer, in bytes. """
270  return self.__sizeof__()
271  ##
272 
273  #--
274  @staticmethod
275  def byte(val):
276  """ Static method to convert a value into a byte.
277 
278  Parameters:
279  val - Value to convert. Value type can be an integer [0,255],
280  a string of length 1, or a list of length 1 of element
281  type of integer, string, list.
282 
283  Return:
284  On success, returns converted byte value.
285  On failure, a BotSenseError exception is raised.
286  """
287  if type(val) == int:
288  assert val <= 255, "integer too big"
289  return val
290  elif type(val) == str:
291  assert len(val) == 1, "string too long"
292  return ord(val[0])
293  elif type(val) == list:
294  assert len(val) == 1, "too many list eleements"
295  return ByteBuf.byte(val[0])
296  else:
297  assert 0, "unsupported type"
298  ##
299 
300  #--
301  @staticmethod
302  def bytelist(seq):
303  """ Static method to convert a value sequence into a byte list.
304 
305  Parameters:
306  seq - Sequence of values to convert. Sequence type can be an
307  integer [0,255], a string, or a list of elements of
308  type integer, string, list.
309 
310  Return:
311  On success, returns converted byte value list.
312  On failure, a BotSenseError exception is raised.
313  """
314  if seq is None:
315  return []
316  elif type(seq) == int:
317  outbuf = [ ByteBuf.byte(seq) ]
318  return outbuf
319  elif type(seq) == str or type(seq) == list:
320  outbuf = []
321  for val in seq:
322  outbuf += [ ByteBuf.byte(val) ]
323  return outbuf
324  else:
325  assert 0, "unsupported type"
326  ##
327 
328  #--
329  @staticmethod
330  def bytec(val):
331  """ Static method to convert a value into a byte character string.
332 
333  Parameters:
334  val - Value to convert. Value type can be an integer [0,255],
335  a string of length 1, or a list of length 1 of element
336  type of integer, string, list.
337 
338  Return:
339  On success, returns converted byte value.
340  On failure, a BotSenseError exception is raised.
341  """
342  if type(val) == int:
343  assert val <= 255, "integer too big"
344  return chr(val)
345  elif type(val) == str:
346  assert len(val) == 1, "string too long"
347  return val[0]
348  elif type(val) == list:
349  assert len(val) == 1, "too many list elements"
350  return ByteBuf.bytec(val[0])
351  else:
352  assert 0, "unsupported type"
353  ##
354 
355  #--
356  @staticmethod
357  def bytestring(seq):
358  """ Static method to convert a value sequence into a byte string.
359 
360  Parameters:
361  seq - Sequence of values to convert. Sequence type can be an
362  integer [0,255], a string, or a list of elements of
363  type integer, string, list.
364 
365  Return:
366  On success, returns converted byte value list.
367  On failure, a BotSenseError exception is raised.
368  """
369  if seq is None:
370  return ''
371  elif type(seq) == int:
372  return ByteBuf.bytec(seq)
373  elif type(seq) == str or type(seq) == list:
374  outbuf = ''
375  for val in seq:
376  outbuf += ByteBuf.bytec(val)
377  return outbuf
378  else:
379  assert 0, "unsupported type"
380 
def __init__(self, size=256)
Definition: basetypes.py:80
def __setslice__(self, i, j, y)
Definition: basetypes.py:202
_size
internal swig object fixed buffer size
Definition: basetypes.py:91
def __imul__(self, y)
Definition: basetypes.py:166
def copyToSwigObj(self, n, i=0, j=0)
Definition: basetypes.py:241
def __setitem__(self, i, y)
Definition: basetypes.py:194
def __getitem__(self, i)
Definition: basetypes.py:146
def __iadd__(self, y)
Definition: basetypes.py:158
def __sizeof__(self)
Definition: basetypes.py:210
def __getslice__(self, i, j)
Definition: basetypes.py:152
def copyFromSwigObj(self, n, i=0, j=0)
Definition: basetypes.py:222
RoadNarrows Robotics Swigged Core Python Interface Module.
Definition: i2ccore.py:1
buf
python byte string
Definition: basetypes.py:94
_swigObj
internal swig object
Definition: basetypes.py:88
def getSwigObj(self)
Definition: basetypes.py:256