/*!
 ** Modified by Scitech Korea Inc., Changwoo Park
 * @file DFRobot_GP8403.h
 * @brief This is a method description file for the DAC module
 * @copyright	Copyright (c) 2021 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @license The MIT License (MIT)
 * @author [TangJie](jie.tang@dfrobot.com)
 * @version V1.0
 * @date 2022-03-07
 * @url https://github.com/DFRobot/DFRobot_Microphone
 */
#ifndef _DFROBOT_GP8403_H_
#define _DFROBOT_GP8403_H

#ifndef TwoWire_h
#include "Wire.h"
#endif

#define GP8302_STORE_TIMING_HEAD            0x02  ///< Store function timing start head
#define GP8302_STORE_TIMING_ADDR            0x10  ///< The first address for entering store timing
#define GP8302_STORE_TIMING_CMD1            0x03  ///< The command 1 to enter store timing
#define GP8302_STORE_TIMING_CMD2            0x00  ///< The command 2 to enter store timing
#define GP8302_STORE_TIMING_DELAY           10    ///< Store procedure interval delay time: 10ms, more than 7ms
#define GP8302_STORE_TIMING_DELAY           10    ///< Store procedure interval delay time: 10ms, more than 7ms
#define I2C_CYCLE_TOTAL                     5     ///< Total I2C communication cycle
#define I2C_CYCLE_BEFORE                    1     ///< The first half cycle 2 of the total I2C communication cycle
#define I2C_CYCLE_AFTER                     2     ///< The second half cycle 3 of the total I2C communication cycle


//#define ENABLE_DBG //!< Open the macro and you can see the detailed procedure of the program
#ifdef ENABLE_DBG
#define DBG(...) {Serial.print("[");Serial.print(__FUNCTION__); Serial.print("(): "); Serial.print(__LINE__); Serial.print(" ] "); Serial.println(__VA_ARGS__);}
#else
#define DBG(...)
#endif
#define GP8302_CONFIG_CURRENT_REG 0x02
#define OUTPUT_RANGE             0X01
class DFRobot_GP8403
{
public:
	/**
	 * @enum eOutPutRange_t
	 * @brief Analog voltage output range select
	 */
	typedef enum{
    eOutputRange5V = 0X00,
    eOutputRange10V = 0X11,
  }eOutPutRange_t;
	/**
	 * @brief DFRobot_GP8403 constructor
	 * @param pWire I2C object
	 * @param addr I2C address
	 */
		DFRobot_GP8403(TwoWire *pWire = &Wire,uint8_t addr = 0x58);
	/**
   * @fn begin
   * @brief Initialize the module
   */
  uint8_t begin(void);

	/**
   * @fn setDACOutRange
   * @brief Set DAC output range
   * @param range DAC output range
   * @return NONE
   */
  void setDACOutRange(eOutPutRange_t range);
    
  /**
   * @fn setDACOutVoltage
   * @brief Set output DAC voltage of different channels
   * @param data The voltage value to be output
   * @param channel Output channel. 0: channel 0; 1: channel 1; 2: all the channels
   * @return NONE
   */
  void setDACOutVoltage(uint16_t data,uint8_t channel);
  /**
   * @brief Save the set voltage in the chip
   */
	void store(void);

protected:
  void startSignal(void);
  void stopSignal(void);
  uint8_t recvAck(uint8_t ack);
  uint8_t sendByte(uint8_t data, uint8_t ack = 0, uint8_t bits = 8, bool flag = true);
  
private:
/**
  * @fn writeReg
  * @brief   Write register value through IIC bus
  * @param reg  Register address 8bits
  * @param pBuf Storage cache to write data in
  * @param size The length of data to be written
  */
  void writeReg(uint8_t reg, void *pBuf, size_t size);
  TwoWire *_pWire;
	uint8_t _addr;
	uint16_t voltage = 0;
  int _scl= SCL;
  int _sda = SDA;
  void sendData(uint16_t data, uint8_t channel);
};



#endif
