Program Listing for File imu9.h

Return to documentation for file (include/libcaer/events/imu9.h)

#ifndef LIBCAER_EVENTS_IMU9_H_
#define LIBCAER_EVENTS_IMU9_H_

#include "common.h"

#ifdef __cplusplus
extern "C" {
#endif

PACKED_STRUCT(struct caer_imu9_event {
    uint32_t info;
    int32_t timestamp;
    float accel_x;
    float accel_y;
    float accel_z;
    float gyro_x;
    float gyro_y;
    float gyro_z;
    float temp;
    float comp_x;
    float comp_y;
    float comp_z;
});

typedef struct caer_imu9_event *caerIMU9Event;
typedef const struct caer_imu9_event *caerIMU9EventConst;

PACKED_STRUCT(struct caer_imu9_event_packet {
    struct caer_event_packet_header packetHeader;
    struct caer_imu9_event events[];
});

typedef struct caer_imu9_event_packet *caerIMU9EventPacket;
typedef const struct caer_imu9_event_packet *caerIMU9EventPacketConst;

static inline caerIMU9EventPacket caerIMU9EventPacketAllocate(
    int32_t eventCapacity, int16_t eventSource, int32_t tsOverflow) {
    return ((caerIMU9EventPacket) caerEventPacketAllocate(eventCapacity, eventSource, tsOverflow, IMU9_EVENT,
        sizeof(struct caer_imu9_event), offsetof(struct caer_imu9_event, timestamp)));
}

static inline caerIMU9EventPacket caerIMU9EventPacketFromPacketHeader(caerEventPacketHeader header) {
    if (caerEventPacketHeaderGetEventType(header) != IMU9_EVENT) {
        return (NULL);
    }

    return ((caerIMU9EventPacket) header);
}

static inline caerIMU9EventPacketConst caerIMU9EventPacketFromPacketHeaderConst(caerEventPacketHeaderConst header) {
    if (caerEventPacketHeaderGetEventType(header) != IMU9_EVENT) {
        return (NULL);
    }

    return ((caerIMU9EventPacketConst) header);
}

static inline caerIMU9Event caerIMU9EventPacketGetEvent(caerIMU9EventPacket packet, int32_t n) {
    // Check that we're not out of bounds.
    if (n < 0 || n >= caerEventPacketHeaderGetEventCapacity(&packet->packetHeader)) {
        caerLogEHO(CAER_LOG_CRITICAL, "IMU9 Event",
            "Called caerIMU9EventPacketGetEvent() with invalid event offset %" PRIi32
            ", while maximum allowed value is %" PRIi32 ".",
            n, caerEventPacketHeaderGetEventCapacity(&packet->packetHeader) - 1);
        return (NULL);
    }

    // Return a pointer to the specified event.
    return (packet->events + n);
}

static inline caerIMU9EventConst caerIMU9EventPacketGetEventConst(caerIMU9EventPacketConst packet, int32_t n) {
    // Check that we're not out of bounds.
    if (n < 0 || n >= caerEventPacketHeaderGetEventCapacity(&packet->packetHeader)) {
        caerLogEHO(CAER_LOG_CRITICAL, "IMU9 Event",
            "Called caerIMU9EventPacketGetEventConst() with invalid event offset %" PRIi32
            ", while maximum allowed value is %" PRIi32 ".",
            n, caerEventPacketHeaderGetEventCapacity(&packet->packetHeader) - 1);
        return (NULL);
    }

    // Return a pointer to the specified event.
    return (packet->events + n);
}

static inline int32_t caerIMU9EventGetTimestamp(caerIMU9EventConst event) {
    return (I32T(le32toh(U32T(event->timestamp))));
}

static inline int64_t caerIMU9EventGetTimestamp64(caerIMU9EventConst event, caerIMU9EventPacketConst packet) {
    return (I64T((U64T(caerEventPacketHeaderGetEventTSOverflow(&packet->packetHeader)) << TS_OVERFLOW_SHIFT)
                 | U64T(caerIMU9EventGetTimestamp(event))));
}

static inline void caerIMU9EventSetTimestamp(caerIMU9Event event, int32_t timestamp) {
    if (timestamp < 0) {
        // Negative means using the 31st bit!
        caerLogEHO(CAER_LOG_CRITICAL, "IMU9 Event", "Called caerIMU9EventSetTimestamp() with negative value!");
        return;
    }

    event->timestamp = I32T(htole32(U32T(timestamp)));
}

static inline bool caerIMU9EventIsValid(caerIMU9EventConst event) {
    return (GET_NUMBITS32(event->info, VALID_MARK_SHIFT, VALID_MARK_MASK));
}

static inline void caerIMU9EventValidate(caerIMU9Event event, caerIMU9EventPacket packet) {
    if (!caerIMU9EventIsValid(event)) {
        SET_NUMBITS32(event->info, VALID_MARK_SHIFT, VALID_MARK_MASK, 1);

        // Also increase number of events and valid events.
        // Only call this on (still) invalid events!
        caerEventPacketHeaderSetEventNumber(
            &packet->packetHeader, caerEventPacketHeaderGetEventNumber(&packet->packetHeader) + 1);
        caerEventPacketHeaderSetEventValid(
            &packet->packetHeader, caerEventPacketHeaderGetEventValid(&packet->packetHeader) + 1);
    }
    else {
        caerLogEHO(CAER_LOG_CRITICAL, "IMU9 Event", "Called caerIMU9EventValidate() on already valid event.");
    }
}

static inline void caerIMU9EventInvalidate(caerIMU9Event event, caerIMU9EventPacket packet) {
    if (caerIMU9EventIsValid(event)) {
        CLEAR_NUMBITS32(event->info, VALID_MARK_SHIFT, VALID_MARK_MASK);

        // Also decrease number of valid events. Number of total events doesn't change.
        // Only call this on valid events!
        caerEventPacketHeaderSetEventValid(
            &packet->packetHeader, caerEventPacketHeaderGetEventValid(&packet->packetHeader) - 1);
    }
    else {
        caerLogEHO(CAER_LOG_CRITICAL, "IMU9 Event", "Called caerIMU9EventInvalidate() on already invalid event.");
    }
}

static inline float caerIMU9EventGetAccelX(caerIMU9EventConst event) {
    return (leflttoh(event->accel_x));
}

static inline void caerIMU9EventSetAccelX(caerIMU9Event event, float accelX) {
    event->accel_x = htoleflt(accelX);
}

static inline float caerIMU9EventGetAccelY(caerIMU9EventConst event) {
    return (leflttoh(event->accel_y));
}

static inline void caerIMU9EventSetAccelY(caerIMU9Event event, float accelY) {
    event->accel_y = htoleflt(accelY);
}

static inline float caerIMU9EventGetAccelZ(caerIMU9EventConst event) {
    return (leflttoh(event->accel_z));
}

static inline void caerIMU9EventSetAccelZ(caerIMU9Event event, float accelZ) {
    event->accel_z = htoleflt(accelZ);
}

static inline float caerIMU9EventGetGyroX(caerIMU9EventConst event) {
    return (leflttoh(event->gyro_x));
}

static inline void caerIMU9EventSetGyroX(caerIMU9Event event, float gyroX) {
    event->gyro_x = htoleflt(gyroX);
}

static inline float caerIMU9EventGetGyroY(caerIMU9EventConst event) {
    return (leflttoh(event->gyro_y));
}

static inline void caerIMU9EventSetGyroY(caerIMU9Event event, float gyroY) {
    event->gyro_y = htoleflt(gyroY);
}

static inline float caerIMU9EventGetGyroZ(caerIMU9EventConst event) {
    return (leflttoh(event->gyro_z));
}

static inline void caerIMU9EventSetGyroZ(caerIMU9Event event, float gyroZ) {
    event->gyro_z = htoleflt(gyroZ);
}

static inline float caerIMU9EventGetTemp(caerIMU9EventConst event) {
    return (leflttoh(event->temp));
}

static inline void caerIMU9EventSetTemp(caerIMU9Event event, float temp) {
    event->temp = htoleflt(temp);
}

static inline float caerIMU9EventGetCompX(caerIMU9EventConst event) {
    return (leflttoh(event->comp_x));
}

static inline void caerIMU9EventSetCompX(caerIMU9Event event, float compX) {
    event->comp_x = htoleflt(compX);
}

static inline float caerIMU9EventGetCompY(caerIMU9EventConst event) {
    return (leflttoh(event->comp_y));
}

static inline void caerIMU9EventSetCompY(caerIMU9Event event, float compY) {
    event->comp_y = htoleflt(compY);
}

static inline float caerIMU9EventGetCompZ(caerIMU9EventConst event) {
    return (leflttoh(event->comp_z));
}

static inline void caerIMU9EventSetCompZ(caerIMU9Event event, float compZ) {
    event->comp_z = htoleflt(compZ);
}

#define CAER_IMU9_ITERATOR_ALL_START(IMU9_PACKET)                                                     \
    for (int32_t caerIMU9IteratorCounter = 0;                                                         \
         caerIMU9IteratorCounter < caerEventPacketHeaderGetEventNumber(&(IMU9_PACKET)->packetHeader); \
         caerIMU9IteratorCounter++) {                                                                 \
        caerIMU9Event caerIMU9IteratorElement = caerIMU9EventPacketGetEvent(IMU9_PACKET, caerIMU9IteratorCounter);

#define CAER_IMU9_CONST_ITERATOR_ALL_START(IMU9_PACKET)                                               \
    for (int32_t caerIMU9IteratorCounter = 0;                                                         \
         caerIMU9IteratorCounter < caerEventPacketHeaderGetEventNumber(&(IMU9_PACKET)->packetHeader); \
         caerIMU9IteratorCounter++) {                                                                 \
        caerIMU9EventConst caerIMU9IteratorElement                                                    \
            = caerIMU9EventPacketGetEventConst(IMU9_PACKET, caerIMU9IteratorCounter);

#define CAER_IMU9_ITERATOR_ALL_END }

#define CAER_IMU9_ITERATOR_VALID_START(IMU9_PACKET)                                                                \
    for (int32_t caerIMU9IteratorCounter = 0;                                                                      \
         caerIMU9IteratorCounter < caerEventPacketHeaderGetEventNumber(&(IMU9_PACKET)->packetHeader);              \
         caerIMU9IteratorCounter++) {                                                                              \
        caerIMU9Event caerIMU9IteratorElement = caerIMU9EventPacketGetEvent(IMU9_PACKET, caerIMU9IteratorCounter); \
        if (!caerIMU9EventIsValid(caerIMU9IteratorElement)) {                                                      \
            continue;                                                                                              \
        } // Skip invalid IMU9 events.

#define CAER_IMU9_CONST_ITERATOR_VALID_START(IMU9_PACKET)                                             \
    for (int32_t caerIMU9IteratorCounter = 0;                                                         \
         caerIMU9IteratorCounter < caerEventPacketHeaderGetEventNumber(&(IMU9_PACKET)->packetHeader); \
         caerIMU9IteratorCounter++) {                                                                 \
        caerIMU9EventConst caerIMU9IteratorElement                                                    \
            = caerIMU9EventPacketGetEventConst(IMU9_PACKET, caerIMU9IteratorCounter);                 \
        if (!caerIMU9EventIsValid(caerIMU9IteratorElement)) {                                         \
            continue;                                                                                 \
        } // Skip invalid IMU9 events.

#define CAER_IMU9_ITERATOR_VALID_END }

#define CAER_IMU9_REVERSE_ITERATOR_ALL_START(IMU9_PACKET)                                                         \
    for (int32_t caerIMU9IteratorCounter = caerEventPacketHeaderGetEventNumber(&(IMU9_PACKET)->packetHeader) - 1; \
         caerIMU9IteratorCounter >= 0; caerIMU9IteratorCounter--) {                                               \
        caerIMU9Event caerIMU9IteratorElement = caerIMU9EventPacketGetEvent(IMU9_PACKET, caerIMU9IteratorCounter);
#define CAER_IMU9_CONST_REVERSE_ITERATOR_ALL_START(IMU9_PACKET)                                                   \
    for (int32_t caerIMU9IteratorCounter = caerEventPacketHeaderGetEventNumber(&(IMU9_PACKET)->packetHeader) - 1; \
         caerIMU9IteratorCounter >= 0; caerIMU9IteratorCounter--) {                                               \
        caerIMU9EventConst caerIMU9IteratorElement                                                                \
            = caerIMU9EventPacketGetEventConst(IMU9_PACKET, caerIMU9IteratorCounter);

#define CAER_IMU9_REVERSE_ITERATOR_ALL_END }

#define CAER_IMU9_REVERSE_ITERATOR_VALID_START(IMU9_PACKET)                                                        \
    for (int32_t caerIMU9IteratorCounter = caerEventPacketHeaderGetEventNumber(&(IMU9_PACKET)->packetHeader) - 1;  \
         caerIMU9IteratorCounter >= 0; caerIMU9IteratorCounter--) {                                                \
        caerIMU9Event caerIMU9IteratorElement = caerIMU9EventPacketGetEvent(IMU9_PACKET, caerIMU9IteratorCounter); \
        if (!caerIMU9EventIsValid(caerIMU9IteratorElement)) {                                                      \
            continue;                                                                                              \
        } // Skip invalid IMU9 events.

#define CAER_IMU9_CONST_REVERSE_ITERATOR_VALID_START(IMU9_PACKET)                                                 \
    for (int32_t caerIMU9IteratorCounter = caerEventPacketHeaderGetEventNumber(&(IMU9_PACKET)->packetHeader) - 1; \
         caerIMU9IteratorCounter >= 0; caerIMU9IteratorCounter--) {                                               \
        caerIMU9EventConst caerIMU9IteratorElement                                                                \
            = caerIMU9EventPacketGetEventConst(IMU9_PACKET, caerIMU9IteratorCounter);                             \
        if (!caerIMU9EventIsValid(caerIMU9IteratorElement)) {                                                     \
            continue;                                                                                             \
        } // Skip invalid IMU9 events.

#define CAER_IMU9_REVERSE_ITERATOR_VALID_END }

#ifdef __cplusplus
}
#endif

#endif /* LIBCAER_EVENTS_IMU9_H_ */