Skip to content
krabipacket.h 2.26 KiB
Newer Older
#ifndef KRABIPACKET_H
#define KRABIPACKET_H

#include <stdint.h>

Alexandre Manoury's avatar
Alexandre Manoury committed
#define KRABIPACKET_MAXSIZE 32
#define KRABIPACKET_SUFFIX_SIZE 1

#ifndef ROBOTHW
    #include <QByteArray>
#endif

class KrabiPacket
{
Alexandre Manoury's avatar
Alexandre Manoury committed
public:
    enum PACKET_TYPE {
        PING_NULL = 0,
        PING_SET = 1,

        REMOTE_MOD_SET = 2,
        REMOTE_MOD_RESET = 3,

        REMOTE_CONTROL_SET = 4,
        REMOTE_CONTROL_RESET = 5,

        LOG_NORMAL = 10,
        LOG_DEBUG = 11,

        WATCH_VARIABLE = 20,
        WATCH_VARIABLE_TIMED = 21,
        WATCH_REQUIRE_ONCE = 23,
        WATCH_SET = 24,
        WATCH_RESET = 25,

        WATCH_DESELECT_ALL = 27,
Alexandre Manoury's avatar
Alexandre Manoury committed

        SET_ODOMETRIE = 30,
        SET_PID_LIN = 32,
        SET_PID_ANG = 33,

        ASSERV_RESULT = 38,
Alexandre Manoury's avatar
Alexandre Manoury committed

        TIME_RESET = 40,
        TIME_SYNC = 41,

        RUN_PID_TEST = 50,
        RUN_GOTO = 51,

        STOP = 100
Alexandre Manoury's avatar
Alexandre Manoury committed
    };

    enum W_TABLE {
        W_NULL,
        W_POSITION,
        W_SPEED,
        W_SPEED_TARGET,
        W_ODOMETRIE,
        W_PID_LIN,
        W_PID_ANG,
        MAX_WATCHES
Alexandre Manoury's avatar
Alexandre Manoury committed
    };

    enum DATA_TYPE {
        DATA_INT8,
        DATA_INT16,
        DATA_INT32,
        DATA_FLOAT,
        DATA_DOUBLE,
        DATA_STRING
    };

    KrabiPacket(uint8_t* data, uint8_t size);
    KrabiPacket(uint8_t id = PING_NULL, W_TABLE watch = W_NULL);
#ifndef ROBOTHW
    KrabiPacket(QByteArray data);
#endif

Arnaud Cadot's avatar
Arnaud Cadot committed
    void addData(void* data, uint8_t size);
Alexandre Manoury's avatar
Alexandre Manoury committed
    void copyData(void* dest, uint8_t size);

    // args
    template<typename T>
    void add(T data)
    {
        addData(&data, sizeof(T));
    }

    template<typename T>
    T get()
    {
        T d;
        copyData(&d, sizeof(T));
        return d;
    }

    template<typename T>
    bool isReadable()
    {
        return mCursor + sizeof(T) < mLength;
    }

Arnaud Cadot's avatar
Arnaud Cadot committed
    void addString(char *data);
Alexandre Manoury's avatar
Alexandre Manoury committed
    char* getString();

    void setId(uint8_t id);
    uint8_t id();
Alexandre Manoury's avatar
Alexandre Manoury committed
    uint8_t length();
    uint8_t* data();
Alexandre Manoury's avatar
Alexandre Manoury committed
    bool checkValidity();
Alexandre Manoury's avatar
Alexandre Manoury committed
#ifndef ROBOTHW
    QByteArray dataByteArray();
#endif
Alexandre Manoury's avatar
Alexandre Manoury committed
    bool isValid();
Alexandre Manoury's avatar
Alexandre Manoury committed
private:
    uint8_t mPacket[KRABIPACKET_MAXSIZE], mPacketSecurized[KRABIPACKET_MAXSIZE];
    uint8_t mId, mCursor, mLength, mLengthSecurized;
    bool mValid;
Alexandre Manoury's avatar
Alexandre Manoury committed
    void securize();
    void unsecurize();
};

#endif // KRABIPACKET_H