#include <Arduino.h>
#include <Wire.h>

#ifndef __GT1151_H
#define __GT1151_H


#define GT1151_RESET_PIN 02
#define GT1151_INT_PIN 15 //RTC_GPIO13



#define GT1151_ADDRESS 0x14
#define GT1151_ADDRESS_28 0x14
#define GT1151_ADDRESS_BA  0x5D

#define CT_MAX_TOUCH 5

#define GT1151_REG_CMD     0x8040

#define GT1151_REG_DATA    0x8140
#define GT1151_REG_ID      0x8140

#define GT1151_COORD_ADDR  0x814E
#define GT1151_POINTS_ADDR 0x814F

//121x250
struct GTPoint {
    uint8_t unused;
    uint8_t id;
    uint16_t x;
    uint16_t y;
    uint16_t a;
};

typedef struct {
    bool holding;
    bool pressing;
    GTPoint points[CT_MAX_TOUCH];
    int16_t dx,dy;
} GT1151_Dev;


class GT1151 {
public:
    GT1151_Dev dev;

    GT1151(TwoWire* i2c);

    void update(void);
    void begin(void);
    void Reset(void);
    void Gesture(void);
    void setHandler(void (*handler)(int8_t, GTPoint*));

private:
    void (*touchHandler)(int8_t, GTPoint*);
    TwoWire* wire;


    void armIRQ(void);
    void onIRQ(void);
    int16_t readInput(GTPoint* points);

    void ReadVersion(void);
    uint8_t Scan(void);

    bool WriteRegister(uint16_t addr, uint8_t data);
    bool ReadRegister(uint16_t addr,uint8_t data[],uint8_t length);
};

#endif