#include <zephyr.h>
#include <device.h>
#include <devicetree.h>
#include <logging/log.h>
#include <logging/log_ctrl.h>
#include <drivers/gpio.h>
#include <hal/nrf_gpio.h>

#include "hw_driver.h"

#define CONSOLE_LABEL DT_LABEL(DT_CHOSEN(zephyr_console))
#include <pm/pm.h>

#define MODULE hw_driver
#define LOG_LEVEL 3
LOG_MODULE_REGISTER(MODULE);

/* pwr3v3_5 device alias */
const struct device *pwr3v3_5;
#define PWR3V3_5_NODE DT_ALIAS(pwr3v3_5)
#if DT_NODE_HAS_STATUS(PWR3V3_5_NODE, okay)
#define PWR3V3_5	DT_GPIO_LABEL(PWR3V3_5_NODE, gpios)
#define PWR3V3_5_PIN	DT_GPIO_PIN(PWR3V3_5_NODE, gpios)
#define PWR3V3_5_FLAGS	DT_GPIO_FLAGS(PWR3V3_5_NODE, gpios)
#else
#error "pwr3v3_5 devicetree alias is not defined"
#define PWR3V3_5	""
#define PWR3V3_5_PIN	0
#define PWR3V3_5_FLAGS	0
#endif

/* vdd_sens device alias */
const struct device *vdd_sens;
#define VDD_SENS_NODE DT_ALIAS(vdd_sens)
#if DT_NODE_HAS_STATUS(VDD_SENS_NODE, okay)
#define VDD_SENS	DT_GPIO_LABEL(VDD_SENS_NODE, gpios)
#define VDD_SENS_PIN	DT_GPIO_PIN(VDD_SENS_NODE, gpios)
#define VDD_SENS_FLAGS	DT_GPIO_FLAGS(VDD_SENS_NODE, gpios)
#else
#error "vdd_sens devicetree alias is not defined"
#define VDD_SENS	""
#define VDD_SENS_PIN	0
#define VDD_SENS_FLAGS	0
#endif

/* mux device alias */
const struct device *mux;
#define MUX_NODE DT_ALIAS(mux)
#if DT_NODE_HAS_STATUS(MUX_NODE, okay)
#define MUX	DT_GPIO_LABEL(MUX_NODE, gpios)
#define MUX_PIN	DT_GPIO_PIN(MUX_NODE, gpios)
#define MUX_FLAGS	DT_GPIO_FLAGS(MUX_NODE, gpios)
#else
#error "mux devicetree alias is not defined"
#define MUX	""
#define MUX_PIN	0
#define MUX_FLAGS	0
#endif

/* tof_1v8 device alias */
const struct device *tof_1v8;
#define TOF1V8_NODE DT_ALIAS(tof_1v8)
#if DT_NODE_HAS_STATUS(TOF1V8_NODE, okay)
#define TOF1V8	DT_GPIO_LABEL(TOF1V8_NODE, gpios)
#define TOF1V8_PIN	DT_GPIO_PIN(TOF1V8_NODE, gpios)
#define TOF1V8_FLAGS	DT_GPIO_FLAGS(TOF1V8_NODE, gpios)
#else
#error "tof_1v8 devicetree alias is not defined"
#define TOF1V8	""
#define TOF1V8_PIN	0
#define TOF1V8_FLAGS	0
#endif

/* nox device alias */
const struct device *nox;
#define NOX_NODE DT_ALIAS(nox)
#if DT_NODE_HAS_STATUS(NOX_NODE, okay)
#define NOX	DT_GPIO_LABEL(NOX_NODE, gpios)
#define NOX_PIN	DT_GPIO_PIN(NOX_NODE, gpios)
#define NOX_FLAGS	DT_GPIO_FLAGS(NOX_NODE, gpios)
#else
#error "nox devicetree alias is not defined"
#define NOX	""
#define NOX_PIN	0
#define NOX_FLAGS	0
#endif

/* nox_pre_heat device alias */
const struct device *nox_pre_heat;
#define NOX_PRE_HEAT_NODE DT_ALIAS(nox_pre_heat)
#if DT_NODE_HAS_STATUS(NOX_PRE_HEAT_NODE, okay)
#define NOX_PRE_HEAT	DT_GPIO_LABEL(NOX_PRE_HEAT_NODE, gpios)
#define NOX_PRE_HEAT_PIN	DT_GPIO_PIN(NOX_PRE_HEAT_NODE, gpios)
#define NOX_PRE_HEAT_FLAGS	DT_GPIO_FLAGS(NOX_PRE_HEAT_NODE, gpios)
#else
#error "nox_pre_heat devicetree alias is not defined"
#define NOX_PRE_HEAT	""
#define NOX_PRE_HEAT_PIN	0
#define NOX_PRE_HEAT_FLAGS	0
#endif

/* reed enable device alias */
const struct device *reed_en;
#define REED_EN_NODE DT_ALIAS(reed_en)
#if DT_NODE_HAS_STATUS(REED_EN_NODE, okay)
#define REED_EN	DT_GPIO_LABEL(REED_EN_NODE, gpios)
#define REED_EN_PIN	DT_GPIO_PIN(REED_EN_NODE, gpios)
#define REED_EN_FLAGS	DT_GPIO_FLAGS(REED_EN_NODE, gpios)
#else
#error "reed_en devicetree alias is not defined"
#define REED_EN	""
#define REED_EN_PIN	0
#define REED_EN_FLAGS	0
#endif

/* reed enable device alias */
const struct device *ble_rst;
#define BLE_RST_NODE DT_ALIAS(ble_rst)
#if DT_NODE_HAS_STATUS(BLE_RST_NODE, okay)
#define BLE_RST	DT_GPIO_LABEL(BLE_RST_NODE, gpios)
#define BLE_RST_PIN	DT_GPIO_PIN(BLE_RST_NODE, gpios)
#define BLE_RST_FLAGS	DT_GPIO_FLAGS(BLE_RST_NODE, gpios)
#else
#error "reed_en devicetree alias is not defined"
#define BLE_RST	""
#define BLE_RST_PIN	0
#define BLE_RST_FLAGS	0
#endif

/* gauge enable device alias */
const struct device *gauge;
#define GAUGE_NODE DT_ALIAS(gauge)
#if DT_NODE_HAS_STATUS(GAUGE_NODE, okay)
#define GAUGE	DT_GPIO_LABEL(GAUGE_NODE, gpios)
#define GAUGE_PIN	DT_GPIO_PIN(GAUGE_NODE, gpios)
#define GAUGE_FLAGS	DT_GPIO_FLAGS(GAUGE_NODE, gpios)
#else
#error "gauge devicetree alias is not defined"
#define GAUGE	""
#define GAUGE_PIN	0
#define GAUGE_FLAGS	0
#endif

/* display enable device alias */
const struct device *dsy_oe;
#define DSY_OE_NODE DT_ALIAS(dsy_oe)
#if DT_NODE_HAS_STATUS(DSY_OE_NODE, okay)
#define DSY_OE	DT_GPIO_LABEL(DSY_OE_NODE, gpios)
#define DSY_OE_PIN	DT_GPIO_PIN(DSY_OE_NODE, gpios)
#define DSY_OE_FLAGS	DT_GPIO_FLAGS(DSY_OE_NODE, gpios)
#else
#error "dsy_oe devicetree alias is not defined"
#define DSY_OE	""
#define DSY_OE_PIN	0
#define DSY_OE_FLAGS	0
#endif

#define VDD_SENS_USER_NUM	6

typedef enum
{
	VDD_SENS_NOT_REQUIRED = 0,
	VDD_SENS_REQUIRED = 1
} vdd_sense_req;

typedef enum
{
	TOF,
	BME680,
	MIC,
	EEPROM,
	BQ35100,
	SPS30
} vdd_sens_usr_id;

static vdd_sense_req vdd_sens_req_pending[VDD_SENS_USER_NUM] = {
	VDD_SENS_NOT_REQUIRED,	/* ToF */
	VDD_SENS_NOT_REQUIRED,	/* BME680 */
	VDD_SENS_NOT_REQUIRED,	/* MIC */
	VDD_SENS_NOT_REQUIRED,	/* EEPROM */
	VDD_SENS_NOT_REQUIRED,	/* BQ35100 */
	VDD_SENS_NOT_REQUIRED,	/* SPS30 */
};

static void vdd_sens_request_regist(uint8_t idx)
{
	vdd_sens_req_pending[idx] = VDD_SENS_REQUIRED;
}

static void vdd_sens_request_unregist(uint8_t idx)
{
	vdd_sens_req_pending[idx] = VDD_SENS_NOT_REQUIRED;
}

static bool vdd_sens_is_req(void)
{
	bool ret = false;
	for(uint8_t i = 0; i < VDD_SENS_USER_NUM; i++ )
	{
		if( vdd_sens_req_pending[i] == VDD_SENS_REQUIRED){
			ret = true;
			break;
		}
	}
	return ret;
}

static hw_driver_err hw_driver_enable_i2c_sensors (vdd_sens_usr_id user_id)
{	
	hw_driver_err ret = ERRNO;

	vdd_sens_request_regist(user_id);

	if(!vdd_sens_is_req()){
		/* someone has already required VDD_SENS */
	} else {
		if ( gpio_pin_set(vdd_sens, VDD_SENS_PIN, 1) ){
			ret = INIT_ERR;
			LOG_ERR("Enable VDD_SENS Failed");
		}

		if(ret == ERRNO){
			LOG_INF("VDD_SENS Enabled");
			k_sleep(K_MSEC(100));
		}
	}

	return ret;
}

static hw_driver_err hw_driver_disable_i2c_sensors (vdd_sens_usr_id user_id)
{
	hw_driver_err ret = ERRNO;

	vdd_sens_request_unregist(user_id);

	if(vdd_sens_is_req()){
		/* someone requires VDD_SENS */
	} else {
		if ( gpio_pin_set(vdd_sens, VDD_SENS_PIN, 0) ){
			ret = INIT_ERR;
			LOG_ERR("Disable VDD_SENS Failed");
		}

		if(ret == ERRNO)
			LOG_INF("VDD_SENS Disabled");
	}
	
	return ret;
}

hw_driver_err hw_driver_init (void)
{
	hw_driver_err ret = ERRNO;

	pwr3v3_5 = device_get_binding(PWR3V3_5);
	if (pwr3v3_5 == NULL) {
		ret = INIT_ERR;
		LOG_ERR("Device PWR3V3_5 binding error");
	}

	vdd_sens = device_get_binding(VDD_SENS);
	if (vdd_sens == NULL) {
		ret = INIT_ERR;
		LOG_ERR("Device VDD_SENS binding error");
	}

	mux = device_get_binding(MUX);
	if (mux == NULL) {
		ret = INIT_ERR;
		LOG_ERR("Device MUX binding error");
	}

	tof_1v8 = device_get_binding(TOF1V8);
	if (tof_1v8 == NULL) {
		ret = INIT_ERR;
		LOG_ERR("Device TOF1V8 binding error");
	}

	nox = device_get_binding(NOX);
	if (nox == NULL) {
		ret = INIT_ERR;
		LOG_ERR("Device NOX binding error");
	}

	nox_pre_heat = device_get_binding(NOX_PRE_HEAT);
	if (nox_pre_heat == NULL) {
		ret = INIT_ERR;
		LOG_ERR("Device NOX_PRE_HEAT binding error");
	}

	reed_en = device_get_binding(REED_EN);
	if (reed_en == NULL) {
		ret = INIT_ERR;
		LOG_ERR("Device REED.EN binding error");
	}

	ble_rst = device_get_binding(BLE_RST);
	if (ble_rst == NULL) {
		ret = INIT_ERR;
		LOG_ERR("Device BLE_RST binding error");
	}
	
	gauge = device_get_binding(GAUGE);
	if (gauge == NULL) {
		ret = INIT_ERR;
		LOG_ERR("Device GAUGE binding error");
	}

	dsy_oe = device_get_binding(DSY_OE);
	if (dsy_oe == NULL) {
		ret = INIT_ERR;
		LOG_ERR("Device DSY_OE binding error");
	}

	ret = gpio_pin_configure(pwr3v3_5, PWR3V3_5_PIN, GPIO_OUTPUT_ACTIVE | PWR3V3_5_FLAGS);
	if (ret < 0) {
		ret = CONFIG_ERR;
		LOG_ERR("GPIO %s config error",PWR3V3_5);
	}

	ret = gpio_pin_configure(vdd_sens, VDD_SENS_PIN, GPIO_OUTPUT_ACTIVE | VDD_SENS_FLAGS);
	if (ret < 0) {
		ret = CONFIG_ERR;
		LOG_ERR("GPIO %s config error",VDD_SENS);
	}

	ret = gpio_pin_configure(mux, MUX_PIN, GPIO_OUTPUT_INACTIVE | MUX_FLAGS);
	if (ret < 0) {
		ret = CONFIG_ERR;
		LOG_ERR("GPIO %s config error",MUX);
	}

	ret = gpio_pin_configure(tof_1v8, TOF1V8_PIN, GPIO_OUTPUT_ACTIVE | TOF1V8_FLAGS);
	if (ret < 0) {
		ret = CONFIG_ERR;
		LOG_ERR("GPIO %s config error",TOF1V8);
	}

	ret = gpio_pin_configure(nox, NOX_PIN, GPIO_OUTPUT_INACTIVE | NOX_FLAGS);
	if (ret < 0) {
		ret = CONFIG_ERR;
		LOG_ERR("GPIO %s config error",NOX);
	}

	ret = gpio_pin_configure(nox_pre_heat, NOX_PRE_HEAT_PIN, GPIO_OUTPUT_INACTIVE | NOX_PRE_HEAT_FLAGS);
	if (ret < 0) {
		ret = CONFIG_ERR;
		LOG_ERR("GPIO %s config error",NOX_PRE_HEAT);
	}

	ret = gpio_pin_configure(reed_en, REED_EN_PIN, GPIO_OUTPUT_ACTIVE );//| REED_EN_FLAGS);
	if (ret < 0) {
		ret = CONFIG_ERR;
		LOG_ERR("GPIO %s config error",REED_EN);
	}

	ret = gpio_pin_configure(ble_rst, BLE_RST_PIN, GPIO_OUTPUT_INACTIVE | BLE_RST_FLAGS);
	if (ret < 0) {
		ret = CONFIG_ERR;
		LOG_ERR("GPIO %s config error",BLE_RST);
	}

	ret = gpio_pin_configure(gauge, GAUGE_PIN, GPIO_OUTPUT_INACTIVE | GAUGE_FLAGS);
	if (ret < 0) {
		ret = CONFIG_ERR;
		LOG_ERR("GPIO %s config error",GAUGE);
	}

	ret = gpio_pin_configure(dsy_oe, DSY_OE_PIN, GPIO_OUTPUT_INACTIVE | DSY_OE_FLAGS);
	if (ret < 0) {
		ret = CONFIG_ERR;
		LOG_ERR("GPIO %s config error",DSY_OE);
	}

	k_sleep(K_MSEC(100));
	return ret;
}

hw_driver_err hw_driver_enable_sps30 (void)
{
	hw_driver_err ret = ERRNO;
	ret = hw_driver_enable_i2c_sensors(SPS30);

	if(ret != ERRNO){
		LOG_ERR("I2C Enable Failed");
	} else {
		LOG_INF("I2C Enabled");
		k_sleep(K_MSEC(500));
	}

	if ( gpio_pin_set(pwr3v3_5, PWR3V3_5_PIN, 1) ){
		ret = INIT_ERR;
		LOG_ERR("Enable SPS30 Failed");
	}

	if(ret == ERRNO){
		LOG_INF("SPS30 Enabled");
		k_sleep(K_MSEC(100));
	}

	return ret;
}

hw_driver_err hw_driver_disable_sps30 (void)
{
	hw_driver_err ret = ERRNO;
	ret = hw_driver_disable_i2c_sensors(SPS30);
	
	if(ret != ERRNO){
		LOG_ERR("I2C Disable Failed");
	} else {
		LOG_INF("I2C Disabled");
		k_sleep(K_MSEC(500));
	}

	if ( gpio_pin_set(pwr3v3_5, PWR3V3_5_PIN, 0) ){
		ret = INIT_ERR;
		LOG_ERR("Disable SPS30 Failed");
	}

	if(ret == ERRNO)
		LOG_INF("SPS30 Disabled");

	return ret;
}

hw_driver_err hw_driver_mux_sps30 (void)
{
	hw_driver_err ret = ERRNO;
	nrf_gpio_cfg_output(MUX_PIN);
	k_sleep(K_MSEC(100));
	if ( gpio_pin_set(mux, MUX_PIN, 1) ){
		ret = INIT_ERR;
		LOG_ERR("Enable MUX SPS30 UART Failed");
	}

	if(ret == ERRNO){
		LOG_INF("SPS30 UART MUX Enabled");
		k_sleep(K_MSEC(200));
	}
	return ret;
}

hw_driver_err hw_driver_mux_ble (void)
{
	hw_driver_err ret = ERRNO;
	nrf_gpio_cfg_output(MUX_PIN);
	k_sleep(K_MSEC(100));
	if ( gpio_pin_set(mux, MUX_PIN, 0) ){
		ret = INIT_ERR;
		LOG_ERR("Enable MUX BLE UART Failed");
	}

	if(ret == ERRNO)
		LOG_INF("BLE UART MUX Enabled");

	return ret;
}

hw_driver_err hw_driver_enable_bme680 (void)
{
	hw_driver_err ret = ERRNO;
	ret = hw_driver_enable_i2c_sensors(BME680);
	if(ret != ERRNO){
		LOG_ERR("BME680 Enable Failed");
	} else {
		LOG_INF("BME680 Enabled");
		k_sleep(K_MSEC(500));
	}
	return ret;
}

hw_driver_err hw_driver_enable_ToF (void)
{
	hw_driver_err ret = ERRNO;
	//VDD_SENS
	ret = hw_driver_enable_i2c_sensors(TOF);
	if(ret != ERRNO){
		LOG_ERR("TOF (vdd_sens) Enable Failed");
		return ret;
	} else {
		LOG_INF("TOF (vdd_sens) Enabled");
		k_sleep(K_MSEC(100));
	}
	//TOF_SHUT
	if ( gpio_pin_set(tof_1v8, TOF1V8_PIN, 1) ){
		ret = INIT_ERR;
		LOG_ERR("Enable TOF 1V8 Failed");
	}

	if(ret == ERRNO){
		LOG_INF("TOF 1V8 Enabled");
		k_sleep(K_MSEC(100));
	}

	return ret;
}

hw_driver_err hw_driver_enable_EEPROM (void)
{
	hw_driver_err ret = ERRNO;
	ret = hw_driver_enable_i2c_sensors(EEPROM);
	if(ret != ERRNO){
		LOG_ERR("EEPROM Enable Failed");
	} else {
		LOG_INF("EEPROM Enabled");
		k_sleep(K_MSEC(100));
	}
	return ret;
}

hw_driver_err hw_driver_enable_Mic (void)
{
	hw_driver_err ret = ERRNO;
	ret = hw_driver_enable_i2c_sensors(MIC);
	if(ret != ERRNO)
		LOG_ERR("MIC Enable Failed");
	else {
		LOG_INF("MIC Enabled");
		k_sleep(K_MSEC(100));
	}

	return ret;
}

hw_driver_err hw_driver_enable_NOx (void)
{
	hw_driver_err ret = ERRNO;
	if ( gpio_pin_set(mux, NOX_PIN, 1) ){
		ret = INIT_ERR;
		LOG_ERR("Enable NOx Sensor Failed");
	}

	if(ret == ERRNO){
		LOG_INF("NOx Sensor Enabled");
		k_sleep(K_MSEC(100));
	}

	return ret;
}

hw_driver_err hw_driver_enable_NOx_pre_heat (void)
{
	hw_driver_err ret = ERRNO;
	if ( gpio_pin_set(nox_pre_heat, NOX_PRE_HEAT_PIN, 1) ){
		ret = INIT_ERR;
		LOG_ERR("Enable NOX_PRE_HEAT Failed");
	}

	if(ret == ERRNO){
		LOG_INF("NOX_PRE_HEAT_PIN Enabled");
		k_sleep(K_MSEC(100));
	}

	return ret;
}

hw_driver_err hw_driver_enable_reed (void)
{
	hw_driver_err ret = ERRNO;
	if ( gpio_pin_set(reed_en, REED_EN_PIN, 1) ){
		ret = INIT_ERR;
		LOG_ERR("Enable REED in Sensor Failed");
	}

	if(ret == ERRNO){
		LOG_INF("REED In Sensor Enabled");
		k_sleep(K_MSEC(100));
	}

	return ret;
}

hw_driver_err hw_driver_enable_bq35100 (void)
{
	hw_driver_err ret = ERRNO;

	ret = hw_driver_enable_i2c_sensors(BQ35100);
	if(ret != ERRNO){
		LOG_ERR("BQ35100 (vdd_sens) Enable Failed");
		return ret;
	} else {
		LOG_INF("BQ35100 (vdd_sens) Enabled");
		k_sleep(K_MSEC(1000));
	}

	if ( gpio_pin_set(gauge, GAUGE_PIN, 1) ){
		ret = INIT_ERR;
		LOG_ERR("Enable GAUGE Failed");
	}

	if(ret == ERRNO){
		LOG_INF("GAUGE Enabled");
		k_sleep(K_MSEC(1000));
	}

	return ret;
}

hw_driver_err hw_driver_disable_bme680 (void)
{
	hw_driver_err ret = ERRNO;
	ret = hw_driver_disable_i2c_sensors(BME680);
	if(ret != ERRNO)
		LOG_ERR("BME680 Disable Failed");
	else
		LOG_INF("BME680 Disabled");
	return ret;
}

hw_driver_err hw_driver_disable_ToF (void)
{
	hw_driver_err ret = ERRNO;

	hw_driver_disable_i2c_sensors(TOF);

	if ( gpio_pin_set(tof_1v8, TOF1V8_PIN, 0) ){
		ret = INIT_ERR;
		LOG_ERR("Disable TOF Failed");
	}

	if(ret == ERRNO)
		LOG_INF("TOF Disabled");

	return ret;
}

hw_driver_err hw_driver_disable_EEPROM (void)
{
	hw_driver_err ret = ERRNO;
	ret = hw_driver_disable_i2c_sensors(EEPROM);
	if(ret != ERRNO)
		LOG_ERR("EEPROM Disable Failed");
	else
		LOG_INF("EEPROM Disabled");
	return ret;
}

hw_driver_err hw_driver_disable_Mic (void)
{
	hw_driver_err ret = ERRNO;
	ret = hw_driver_disable_i2c_sensors(MIC);
	if(ret != ERRNO)
		LOG_ERR("MIC Disable Failed");
	else
		LOG_INF("MIC Disabled");
	return ret;
}

hw_driver_err hw_driver_disable_NOx (void)
{
	hw_driver_err ret = ERRNO;
	if ( gpio_pin_set(mux, NOX_PIN, 0) ){
		ret = INIT_ERR;
		LOG_ERR("Disable NOx Sensor Failed");
	}

	if(ret == ERRNO)
		LOG_INF("NOx Sensor Disabled");

	return ret;
}

hw_driver_err hw_driver_disable_NOx_pre_heat (void)
{
	hw_driver_err ret = ERRNO;
	if ( gpio_pin_set(nox_pre_heat, NOX_PRE_HEAT_PIN, 0) ){
		ret = INIT_ERR;
		LOG_ERR("Disable NOX_PRE_HEAT Failed");
	}

	if(ret == ERRNO)
		LOG_INF("NOX_PRE_HEAT_PIN Disabled");

	return ret;
}

hw_driver_err hw_driver_disable_reed (void)
{
	hw_driver_err ret = ERRNO;
	if ( gpio_pin_set(reed_en, REED_EN_PIN, 0) ){
		ret = INIT_ERR;
		LOG_ERR("Disable REED in Sensor Failed");
	}

	if(ret == ERRNO)
		LOG_INF("REED In Sensor Disabled");

	return ret;
}

hw_driver_err hw_driver_disable_bq35100 (void)
{
	hw_driver_err ret = ERRNO;
	//VDD_SENS
	hw_driver_disable_i2c_sensors(BQ35100);
	//TOF_SHUT
	if ( gpio_pin_set(gauge, GAUGE_PIN, 0) ){
		ret = INIT_ERR;
		LOG_ERR("Disable BQ35100 Failed");
	}

	if(ret == ERRNO)
		LOG_INF("BQ35100 Disabled");

	return ret;
}

hw_driver_err hw_driver_enable_display(void)
{
	hw_driver_err ret = ERRNO;
	nrf_gpio_cfg_output(DSY_OE_PIN);
	if ( gpio_pin_set(dsy_oe, DSY_OE_PIN, 1) ){
		ret = INIT_ERR;
		LOG_ERR("Enable DISPLAY Failed");
	}

	if(ret == ERRNO){
		LOG_INF("DISPLAY Enabled");
		k_sleep(K_MSEC(100));
	}

	return ret;
}

hw_driver_err hw_driver_disable_display (void)
{
	hw_driver_err ret = ERRNO;
	nrf_gpio_cfg_output(DSY_OE_PIN);
	if ( gpio_pin_set(dsy_oe, DSY_OE_PIN, 0) ){
		ret = INIT_ERR;
		LOG_ERR("Disable DISPLAY Failed");
	}

	if(ret == ERRNO){
		LOG_INF("DISPLAY Disabled");
	}

	return ret;
}

#define	MUX_CH_PIN	1

void hw_driver_sleep(void)
{
	hw_driver_disable_NOx_pre_heat();
	hw_driver_disable_bme680();
	hw_driver_disable_bq35100();
	hw_driver_disable_EEPROM();
	hw_driver_disable_Mic();    
	hw_driver_disable_NOx();
	hw_driver_disable_sps30();
	hw_driver_mux_ble();

	//hw_driver_disable_ToF();

	//hw_driver_enable_ToF();
	//hw_driver_enable_reed();
	hw_driver_disable_NOx_pre_heat();
	LOG_WRN("-------------------------------- ALL PINS SET IN HI-Z ---------------------------------------");
	nrf_gpio_cfg_input(MUX_CH_PIN, NRF_GPIO_PIN_NOPULL);
	
	//nrf_gpio_cfg_input(5, NRF_GPIO_PIN_NOPULL);
	//nrf_gpio_cfg_input(6, NRF_GPIO_PIN_NOPULL);
	nrf_gpio_cfg_input(13, NRF_GPIO_PIN_NOPULL);
	nrf_gpio_cfg_input(14, NRF_GPIO_PIN_NOPULL);
	//nrf_gpio_cfg_input(15, NRF_GPIO_PIN_NOPULL);
	//nrf_gpio_cfg_input(16, NRF_GPIO_PIN_NOPULL);
	//nrf_gpio_cfg_input(17, NRF_GPIO_PIN_NOPULL);
	nrf_gpio_cfg_input(DSY_OE_PIN, NRF_GPIO_PIN_NOPULL);	//DSY_OE
	//nrf_gpio_cfg_input(19, NRF_GPIO_PIN_NOPULL);
	//nrf_gpio_cfg_input(20, NRF_GPIO_PIN_NOPULL);
	//nrf_gpio_cfg_input(21, NRF_GPIO_PIN_NOPULL);
	//nrf_gpio_cfg_input(22, NRF_GPIO_PIN_NOPULL);
	//nrf_gpio_cfg_input(28, NRF_GPIO_PIN_NOPULL);
	//nrf_gpio_cfg_input(29, NRF_GPIO_PIN_NOPULL);
	nrf_gpio_cfg_input(30, NRF_GPIO_PIN_NOPULL);
	nrf_gpio_cfg_input(31, NRF_GPIO_PIN_NOPULL);

	nrf_gpio_cfg_input(24, NRF_GPIO_PIN_NOPULL);
	nrf_gpio_cfg_input(25, NRF_GPIO_PIN_NOPULL);

	nrf_gpio_cfg_output(20);//AT25SL !WP
	nrf_gpio_pin_set(20);

	// nrf_gpio_cfg_output(19);
	// nrf_gpio_pin_clear(19);

	nrf_gpio_cfg_output(21);//AT25SL !HOLD
	nrf_gpio_pin_set(21);

	nrf_gpio_cfg_output(22);//AT25SL CS
	nrf_gpio_pin_set(22);

	nrf_gpio_cfg_output(23);//ADXL CS
	nrf_gpio_pin_set(23);
}

void hw_driver_suspend_uart(void)
{
	const struct device *cons = device_get_binding(CONSOLE_LABEL);
	pm_device_state_set(cons, PM_DEVICE_STATE_SUSPENDED);
	LOG_WRN("UART 0 Disabled");
}

void hw_driver_resume(void)
{
	
}
