/*******************************************************************************
 * @file	mcp4725_i2c.h
 * @author	CandyPops Co.
 * @version	V1.0.0
 * @date	2022-09-05
 * @brief   
 ******************************************************************************/
#include "sdk_common.h"
			
#include <stdbool.h>
#include "nrf.h"
#include "nrf_drv_gpiote.h"
#ifndef _MCP4725_I2C_H_
#define _MCP4725_I2C_H_

#define MCP4725_I2C_SDA_PIN    	NRF_GPIO_PIN_MAP(1,15)//cj edit
#define MCP4725_I2C_SCL_PIN    	NRF_GPIO_PIN_MAP(1,14) //cd eidt 

#define SDA_H()					nrf_gpio_pin_set(MCP4725_I2C_SDA_PIN) 							// SDA pin high
#define SDA_L()					nrf_gpio_pin_clear(MCP4725_I2C_SDA_PIN) 						// SDA pin low
#define SCL_H()					nrf_gpio_pin_set(MCP4725_I2C_SCL_PIN) 							// SCL pin high
#define SCL_L()					nrf_gpio_pin_clear(MCP4725_I2C_SCL_PIN) 						// SCL pin low
#define SDA_OUT()				nrf_gpio_cfg_output(MCP4725_I2C_SDA_PIN) 						// SDA pin output mode
#define SCL_OUT()				nrf_gpio_cfg_output(MCP4725_I2C_SCL_PIN) 						// SCL pin output mode
#define SDA_IN()				nrf_gpio_cfg_input(MCP4725_I2C_SDA_PIN, NRF_GPIO_PIN_NOPULL) 	// SDA pin input mode
#define SDA_READ()				nrf_gpio_pin_read(MCP4725_I2C_SDA_PIN) 							// SDA pin read
#define SCL_READ()				nrf_gpio_pin_read(MCP4725_I2C_SCL_PIN) 							// SCL pin read
#define i2c_delay()				nrf_delay_us(1)													// delay time for 400khz clock  1
#define i2c_clock()				{ i2c_delay(); SCL_H(); i2c_delay(); SCL_L(); }
#define i2c_start()				{ SDA_H(); i2c_delay(); SCL_H(); i2c_delay(); SDA_L(); i2c_delay(); SCL_L(); i2c_delay();}
#define i2c_stop()				{ SDA_L(); i2c_delay(); SCL_H(); i2c_delay(); SDA_H(); i2c_delay(); }

#define TWI_WRITE				0x00
#define TWI_READ				0x01

#define ACK						true
#define NACK					false

#define DAC_write				false
#define EEPROM_write			true

// constants
#define MCP4725_MAXVALUE        4095

// registerMode
#define MCP4725_DAC             0x40
#define MCP4725_DACEEPROM       0x60

// reset & wake up
#define MCP4725_GC_RESET        0x06
#define MCP4725_GC_WAKEUP       0x09

// powerDown Mode - TODO ENUM?
#define MCP4725_PDMODE_NORMAL   0x00
#define MCP4725_PDMODE_1K       0x01
#define MCP4725_PDMODE_100K     0x02
#define MCP4725_PDMODE_500K     0x03

void mcp4725_i2c_initialize(void);
void mcp4725_writeFastMode(const uint16_t value);
void mcp4725_generalCall(const uint8_t gc);
void mcp4725_init(void);
void mcp4725_powerOnReset(void);
void mcp4725_powerOnWakeUp(void);
void mcp4725_PowerDownMode(void);

/* * * * * * * * * * * * * * * * * * * * */

uint16_t mcp4725_readDAC(void);
bool mcp4725_ready(void);
uint16_t mcp4725_readDAC(void);
bool mcp4725_ready(void);

void mcp4725_writeRegisterMode(const uint16_t value, uint8_t reg);
void mcp4725_readRegister(uint8_t* buffer, const uint8_t length);
void mcp4725_setValue(const uint16_t value);
uint16_t mcp4725_getValue(void);
void mcp4725_setPercentage(float percentage);
void mcp4725_writeDAC(const uint16_t value, const bool EEPROM);
uint16_t mcp4725_readEEPROM(void);
void mcp4725_writePowerDownMode(const uint8_t PDM, const bool EEPROM);
uint8_t mcp4725_readPowerDownModeEEPROM(void);
uint8_t mcp4725_readPowerDownModeDAC(void);



void DS3930_write(uint8_t id, uint8_t addr, uint8_t wdata);
uint8_t DS3930_read(uint8_t id, uint8_t addr, uint8_t* rdata);


#endif /* !_MCP4725_I2C_H_ */

