"""LCM type definitions
This file automatically generated by lcm.
DO NOT MODIFY BY HAND!!!!
"""

try:
    import cStringIO.StringIO as BytesIO
except ImportError:
    from io import BytesIO
import struct

class oculus_sonar_t(object):
    __slots__ = ["utime", "nBeams", "nRanges", "range", "image_size", "image", "bearings"]

    __typenames__ = ["int64_t", "int32_t", "int32_t", "double", "int32_t", "byte", "int16_t"]

    __dimensions__ = [None, None, None, None, None, ["image_size"], ["nBeams"]]

    def __init__(self):
        self.utime = 0
        self.nBeams = 0
        self.nRanges = 0
        self.range = 0.0
        self.image_size = 0
        self.image = b""
        self.bearings = []

    def encode(self):
        buf = BytesIO()
        buf.write(oculus_sonar_t._get_packed_fingerprint())
        self._encode_one(buf)
        return buf.getvalue()

    def _encode_one(self, buf):
        buf.write(struct.pack(">qiidi", self.utime, self.nBeams, self.nRanges, self.range, self.image_size))
        buf.write(bytearray(self.image[:self.image_size]))
        buf.write(struct.pack('>%dh' % self.nBeams, *self.bearings[:self.nBeams]))

    def decode(data):
        if hasattr(data, 'read'):
            buf = data
        else:
            buf = BytesIO(data)
        if buf.read(8) != oculus_sonar_t._get_packed_fingerprint():
            raise ValueError("Decode error")
        return oculus_sonar_t._decode_one(buf)
    decode = staticmethod(decode)

    def _decode_one(buf):
        self = oculus_sonar_t()
        self.utime, self.nBeams, self.nRanges, self.range, self.image_size = struct.unpack(">qiidi", buf.read(28))
        self.image = buf.read(self.image_size)
        self.bearings = struct.unpack('>%dh' % self.nBeams, buf.read(self.nBeams * 2))
        return self
    _decode_one = staticmethod(_decode_one)

    def _get_hash_recursive(parents):
        if oculus_sonar_t in parents: return 0
        tmphash = (0x826975d8961d653) & 0xffffffffffffffff
        tmphash  = (((tmphash<<1)&0xffffffffffffffff) + (tmphash>>63)) & 0xffffffffffffffff
        return tmphash
    _get_hash_recursive = staticmethod(_get_hash_recursive)
    _packed_fingerprint = None

    def _get_packed_fingerprint():
        if oculus_sonar_t._packed_fingerprint is None:
            oculus_sonar_t._packed_fingerprint = struct.pack(">Q", oculus_sonar_t._get_hash_recursive([]))
        return oculus_sonar_t._packed_fingerprint
    _get_packed_fingerprint = staticmethod(_get_packed_fingerprint)

    def get_hash(self):
        """Get the LCM hash of the struct"""
        return struct.unpack(">Q", oculus_sonar_t._get_packed_fingerprint())[0]

