"""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

import compas_lcmtypes.stdlcm.header_t

class gamepad_t(object):
    __slots__ = ["header", "button_a", "button_b", "button_x", "button_y", "button_select", "button_start", "button_trigger_r", "button_trigger_l", "axis_r", "axis_l", "trigger_r", "trigger_l", "hats"]

    __typenames__ = ["compas_lcmtypes.stdlcm.header_t", "boolean", "boolean", "boolean", "boolean", "boolean", "boolean", "boolean", "boolean", "double", "double", "double", "double", "double"]

    __dimensions__ = [None, None, None, None, None, None, None, None, None, [2], [2], None, None, [2]]

    def __init__(self):
        self.header = compas_lcmtypes.stdlcm.header_t()
        self.button_a = False
        self.button_b = False
        self.button_x = False
        self.button_y = False
        self.button_select = False
        self.button_start = False
        self.button_trigger_r = False
        self.button_trigger_l = False
        self.axis_r = [ 0.0 for dim0 in range(2) ]
        self.axis_l = [ 0.0 for dim0 in range(2) ]
        self.trigger_r = 0.0
        self.trigger_l = 0.0
        self.hats = [ 0.0 for dim0 in range(2) ]

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

    def _encode_one(self, buf):
        assert self.header._get_packed_fingerprint() == compas_lcmtypes.stdlcm.header_t._get_packed_fingerprint()
        self.header._encode_one(buf)
        buf.write(struct.pack(">bbbbbbbb", self.button_a, self.button_b, self.button_x, self.button_y, self.button_select, self.button_start, self.button_trigger_r, self.button_trigger_l))
        buf.write(struct.pack('>2d', *self.axis_r[:2]))
        buf.write(struct.pack('>2d', *self.axis_l[:2]))
        buf.write(struct.pack(">dd", self.trigger_r, self.trigger_l))
        buf.write(struct.pack('>2d', *self.hats[:2]))

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

    def _decode_one(buf):
        self = gamepad_t()
        self.header = compas_lcmtypes.stdlcm.header_t._decode_one(buf)
        self.button_a = bool(struct.unpack('b', buf.read(1))[0])
        self.button_b = bool(struct.unpack('b', buf.read(1))[0])
        self.button_x = bool(struct.unpack('b', buf.read(1))[0])
        self.button_y = bool(struct.unpack('b', buf.read(1))[0])
        self.button_select = bool(struct.unpack('b', buf.read(1))[0])
        self.button_start = bool(struct.unpack('b', buf.read(1))[0])
        self.button_trigger_r = bool(struct.unpack('b', buf.read(1))[0])
        self.button_trigger_l = bool(struct.unpack('b', buf.read(1))[0])
        self.axis_r = struct.unpack('>2d', buf.read(16))
        self.axis_l = struct.unpack('>2d', buf.read(16))
        self.trigger_r, self.trigger_l = struct.unpack(">dd", buf.read(16))
        self.hats = struct.unpack('>2d', buf.read(16))
        return self
    _decode_one = staticmethod(_decode_one)

    def _get_hash_recursive(parents):
        if gamepad_t in parents: return 0
        newparents = parents + [gamepad_t]
        tmphash = (0xa2323731c07047db+ compas_lcmtypes.stdlcm.header_t._get_hash_recursive(newparents)) & 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 gamepad_t._packed_fingerprint is None:
            gamepad_t._packed_fingerprint = struct.pack(">Q", gamepad_t._get_hash_recursive([]))
        return gamepad_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", gamepad_t._get_packed_fingerprint())[0]

