#ifndef __LT8920_H
#define __LT8920_H


#define REGISTER_READ       0b10000000  //bin
#define REGISTER_WRITE      0b00000000  //bin
#define REGISTER_MASK       0b01111111  //bin

#define R_CHANNEL           7
#define CHANNEL_RX_BIT      7
#define CHANNEL_TX_BIT      8
#define CHANNEL_MASK        0b01111111  //bin
#define DEFAULT_CHANNEL     0x31

#define R_CURRENT           9
#define CURRENT_POWER_SHIFT 12
#define CURRENT_POWER_MASK  0b1111000000000000
#define CURRENT_GAIN_SHIFT  7
#define CURRENT_GAIN_MASK   0b0000011110000000

#define R_DATARATE          44
#define DATARATE_MASK       0xFF00
#define DATARATE_1MBPS      0x0101
#define DATARATE_250KBPS    0x0401
#define DATARATE_125KBPS    0x0801
#define DATARATE_62KBPS     0x1001

#define R_SYNCWORD1         36
#define R_SYNCWORD2         37
#define R_SYNCWORD3         38
#define R_SYNCWORD4         39

#define R_PACKETCONFIG      41
#define PACKETCONFIG_CRC_ON             0x8000
#define PACKETCONFIG_SCRAMBLE_ON        0x4000
#define PACKETCONFIG_PACK_LEN_ENABLE    0x2000
#define PACKETCONFIG_FW_TERM_TX         0x1000
#define PACKETCONFIG_AUTO_ACK           0x0800
#define PACKETCONFIG_PKT_FIFO_POLARITY  0x0400

#define R_STATUS            48
#define STATUS_CRC_BIT      15


#define R_FIFO              50
#define R_FIFO_CONTROL      52



class LT8920
{
  public:
    /** Data Data */
    enum DataRate
    {
      LT8920_1MBPS,     /** default transmit rate */
      LT8920_250KBPS,   /** 250 Kpbs, only on lt8910 */
      LT8920_125KBPS,   /** 125 Kbps, only on lt8910 */
      LT8920_62KBPS,     /** 62 Kbps, only on lt8910 */
      LT8920_ERROR
    };

  private:
    uint8_t _pin_chipselect;
    uint8_t _pin_pktflag;
    uint8_t _pin_reset;
    uint8_t _channel;

  public:
    /** Construct a new instance
     * @param cs Chip Select, this is the SLAVE SELECT pin. Please note that it is a different location than the NRF24L01+ SS pin.
     * @param pkt PKT_flag input signal pin. Comparable to the NRF24L01+ IRQ pin.
     * @param rst RESET pin. Use 0 to disable.
    */
    LT8920(void);

    /** Configures the module for initial use */
    void begin();

    /**
    * @param channel has significant 7 bits
    */
    void setChannel(uint8_t channel);
    /** Retrieve the current channel */
    uint8_t getChannel();

    /** Set power and gain
    * @param power 0-0xf
    * @param gain 0-0xf
    */
    void setCurrentControl(uint8_t power, uint8_t gain);

    /** Sets the data rate
    * @param rate the transmission/reception speed
    * @returns true when the data rate was succesfully changed.
    */
    bool setDataRate(DataRate rate);

    /** Returns the data rate
    * @returns the active data rate.
    */
    DataRate getDataRate();

    /** Read the value of a register
    * @param reg
    */
    uint16_t readRegister(uint8_t reg);

    /** Writes to a register
    * @param reg The register to write to
    * @param data 16bits of data, MSB first */
    uint8_t writeRegister(uint8_t reg, uint16_t data);

    /** Writes to a register, one byte at a time.
    * This is a convenience function, because the LT8920 uses 16 bits registers with all 16 bits written at once.
    * @param reg The register to write to
    * @param high bits 15..8
    * @param low bits 7..0  */
    uint8_t writeRegister2(uint8_t reg, uint8_t high, uint8_t low);

	void dump_register(uint8_t reg);
	
    /** Put the LT8920 module to sleep mode.
     * In contrast to POWER DOWN, this mode will keep the register values
     * Any SPI call will awaken the module automatically */
    void sleep();

    /** Dumps debug information on the selected port
    * @param stream the output stream to use as output, eg. `whatsUp(&Serial);`
    */
    //void whatsUp(Stream &stream);
    void whatsUp(void);

    /** Signals a packet ready in the FIFO queue */
    bool available();

    /** Read a packet into the buffer
    * @param buffer a pointer to a buffer large enough to hold the packet
    * @param maxBuffer the maximum size of the buffer. Any bytes left over in the buffer will be dropped.
    * @returns the size of the packet that was read, -1 for CRC error
    */
    int read(uint8_t *buffer, size_t maxBuffer);

    /** Switch the module to RX mode */
    void startListening();

    /** Sets the internal clock divider
   * @param clock
   */
    void setClock(uint8_t clock);

    /** Sends a packet
    * This call blocks until the packet was sent and the Tx Buffer has been completed
    * @param data
    * @param packetSize
    * @returns true when the packet was sent, or false when the packetSize was invalid.
    */
    bool sendPacket(uint8_t *data, size_t packetSize);

    /** Set Syncword
    * @param syncWord 64 bits of sync word settings.
    * @see setSyncwordLength
    */
    void setSyncWord(uint64_t syncWord);

    /** Sets the length of the sync word used
     * @param length:
     *          11 = 64bits, 10 = 48 bits, 01 = 32 bits, 00 = 16 bits
     * @see Check the datasheet for which bits are actually used.
     */
    void setSyncWordLength(uint8_t length);

    /** Scan the signal strength for one or more channels
    * @param buffer points to a buffer to store the signal strengths, at least num_channels
    * @param start_channel starting channel to scan, where Frequency = 2402 + channel
    * @param num_channels number of channels to scan in this batch, for example scanning 4 channels
    *                     with start 2480 will scan 2480, 2481, 2482, 2483
    */
    void scanRSSI(uint16_t *buffer, uint8_t start_channel, uint8_t num_channels);

    /** retrieve the analog signal strength for the current channel */
    uint8_t getRSSI();
};


#define _BV(bit) (1 << (bit))

#define bitRead(value, bit) (((value) >> (bit)) & 0x01)

#endif