/**
 * @file sx1262.h
 * @brief Header file for the SX1276 transceiver class.
 *
 * This file provides the `Sx1262` class implementation, to control the SX126
 * transceiver. It includes methods for configuring the device, reading/writing
 * registers, and managing the FIFO buffer.
 *
 * @details The class is currently designed to use the SX126x in BFSK
 * configuration, but it supports many others digital modulations.
 * The SX1262 is commonly used in LoRa communication systems.
 * This class tries to abstract the hardware-level details and offers an API 
 * to work with the SX1276 on ESP32 platforms.
 *
 * @author Julia CABARBAYE (Student, ENSIL-ENSCI [ELT], France)
 * @author Adrien CHEVRIER (Student, ENSIL-ENSCI [ELT], France)
 *
 * @date Created on: 2024-11-25
 * @date Last updated: 2025-01-05
 *
 * @see { @link https://www.semtech.fr/products/wireless-rf/lora-connect/sx1276 }
 * for detailed information about the SX1276.  
 */

#ifndef SX1262_H
#define SX1262_H

#include <stdint.h>
#include <Arduino.h>
#include <SPI.h>
#include <Adafruit_ST7735.h>

#include "sx1262regs.h"

/**
 * @namespace sx1262
 * @brief Contains constants related to the SX1276 transceiver.
 *
 * This namespace holds static constants such as clock frequency, FIFO size,
 * and default read/write commands.
 */
namespace sx1262
{
  constexpr double radio_clk = 32E6;     ///< SX1276 clock frequency in Hz.
  constexpr double radio_div = 524288.0; ///< Divider for frequency calculations (2^19).
  constexpr uint8_t fifo_len = 64;       ///< FIFO size in bytes
  constexpr uint8_t fifo_read = 0x00;    ///< Command to read from FIFO.
  constexpr uint8_t fifo_write = 0xFF;   ///< Command to write to FIFO.
}

/**
 * @class Sx1262
 * @brief A class to manage the Sx1262 transceiver.
 *
 * This class provides an interface to interact with the Sx1262 transceiver,
 * including methods for frequency configuration, reading/writing registers, 
 * handling FIFO operations, and managing pin states.
 */
class Sx1262
{
  public:
    /**
     * @brief Constructor for the Sx1262 class.
     *
     * @param rst Reset pin.
     * @param sclk SPI clock pin.
     * @param mosi SPI MOSI pin.
     * @param miso SPI MISO pin.
     * @param cs Chip select pin.
     * @param dio1 Digital Input/Output pin DIO1.
     * @param busy Digital Input/Output pin BUSY.
     */
    Sx1262(
      uint8_t rst,
      uint8_t sclk,
      uint8_t mosi,
      uint8_t miso,
      uint8_t cs,
      uint8_t dio1,
      uint8_t busy
    ) : 
      _rst(rst),
      _sclk(sclk),
      _mosi(mosi),
      _miso(miso),
      _cs(cs),
      _dio1(dio1),
      _busy(busy)
    { }


    void beginSPI() const;
    uint8_t getRst() const;
    uint8_t getDio(uint8_t n = 0) const;
    uint8_t getSclk() const;
    uint8_t getMosi() const;
    uint8_t getMiso() const;
    uint8_t getCs() const;
    uint8_t getBusy() const;

    void reset() const;

    uint8_t regRead(uint16_t reg) const;
    void regWrite(uint16_t reg, uint8_t val) const;
    void setOpMode(uint8_t opcode, uint8_t *payload, uint8_t size) const;
    uint8_t getStatusCommand(uint8_t opcode) const;

    void SetSleep() const;
    void SetStandby() const;
    void SetFS() const;
    void SetPacketType(uint8_t type) const;
    void GetPacketType() const;
    void SetRfFrequency(uint32_t freq) const;
            
    void freqUpdate(uint32_t freq,uint8_t debug) const;
    void SetPaConfig(uint8_t paDutyCycle, uint8_t hpMax, uint8_t deviceSel, uint8_t paLut) const;
    void SetTxParams(uint8_t power, uint8_t rampTime) const;
    void SetBufferBaseAddress(uint8_t txBaseAddress, uint8_t rxBaseAddress) const;
    void WriteBuffer(uint8_t *buffer, uint8_t size) const;

    void SetModulationParams(uint32_t bitrate, uint8_t pulseShape, uint8_t bandwidht, uint32_t fdev) const;
    void SetPacketParams(uint16_t preambleLength, uint8_t PreambleDetectorLength, uint8_t SyncWordLength, uint8_t AddrComp, uint8_t PacketType, uint8_t PayloadLength, uint8_t CRCType) const;
    void SetDIO3AsTCXOCtrl(uint8_t tcxoVoltage, uint8_t timeout) const;
    void SetTx() const;


  private:
    uint8_t _rst;  ///< Reset pin.
    uint8_t _sclk; ///< SPI clock pin.
    uint8_t _mosi; ///< SPI MOSI pin.
    uint8_t _miso; ///< SPI MISO pin.
    uint8_t _cs;   ///< Chip select pin.
    uint8_t _dio1; ///< DIO1 pin.
    uint8_t _dio2; ///< DIO2 pin.
    uint8_t _dio3; ///< DIO3 pin.
    uint8_t _busy; ///< BUSY pin.
};

#endif //SX1262_H
