# automatically generated by the FlatBuffers compiler, do not modify

# namespace: dv

import flatbuffers
from .util import import_numpy

np = import_numpy()

class EventPacket(object):
    __slots__ = ['_tab']

    @classmethod
    def GetRootAsEventPacket(cls, buf, offset):
        n = flatbuffers.encode.Get(flatbuffers.packer.uoffset, buf, offset)
        x = EventPacket()
        x.Init(buf, n + offset)
        return x

    # EventPacket
    def Init(self, buf, pos):
        self._tab = flatbuffers.table.Table(buf, pos)

    # EventPacket
    def Events(self, j):
        o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
        if o != 0:
            x = self._tab.Vector(o)
            x += flatbuffers.number_types.UOffsetTFlags.py_type(j) * 16
            from dv.fb.Event import Event
            obj = Event()
            obj.Init(self._tab.Bytes, x)
            return obj
        return None

    # EventPacket
    def EventsLength(self):
        o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
        if o != 0:
            return self._tab.VectorLen(o)
        return 0

    def EventsBufferAsNumpy(self):
        o = flatbuffers.number_types.UOffsetTFlags.py_type(self._tab.Offset(4))
        offset = self._tab.Vector(4)
        count = self._tab.VectorLen(4)
        if np is None:
            raise RuntimeError("Numpy has to be installed for this")

        np_dtype = np.dtype({
            'names': ['timestamp', 'x', 'y', 'polarity', '_p1', '_p2'],
            'formats': ['<i8', '<i2', '<i2', '<i1', '<i2', '<i1']
        })
        return np.frombuffer(self._tab.Bytes, dtype=np_dtype, count=count, offset=offset)

def EventPacketStart(builder): builder.StartObject(1)
def EventPacketAddEvents(builder, events): builder.PrependUOffsetTRelativeSlot(0, flatbuffers.number_types.UOffsetTFlags.py_type(events), 0)
def EventPacketStartEventsVector(builder, numElems): return builder.StartVector(16, numElems, 8)
def EventPacketEnd(builder): return builder.EndObject()
