/* main.c - Application main entry point */

/*
 * Copyright (c) 2015-2016 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr/types.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <sys/printk.h>
#include <sys/byteorder.h>
#include <zephyr.h>
#include <drivers/gpio.h>

#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/conn.h>
#include <bluetooth/uuid.h>
#include <bluetooth/gatt.h>
#include <bluetooth/services/bas.h>
#include <bluetooth/services/hrs.h>
const struct device *latch_dev;
const struct device *gpio_0_dev;

//static const struct bt_data ad[] = {
//	BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
//	BT_DATA_BYTES(BT_DATA_UUID16_ALL,
//		      BT_UUID_16_ENCODE(BT_UUID_HRS_VAL),
//		      BT_UUID_16_ENCODE(BT_UUID_BAS_VAL),
//		      BT_UUID_16_ENCODE(BT_UUID_DIS_VAL))
//};

//static void connected(struct bt_conn *conn, uint8_t err)
//{
//	if (err) {
//		printk("Connection failed (err 0x%02x)\n", err);
//	} else {
//		printk("Connected\n");
//	}
//}

//static void disconnected(struct bt_conn *conn, uint8_t reason)
//{
//	printk("Disconnected (reason 0x%02x)\n", reason);
//}

//static struct bt_conn_cb conn_callbacks = {
//	.connected = connected,
//	.disconnected = disconnected,
//};

//static void bt_ready(void)
//{
//	int err;

//	printk("Bluetooth initialized\n");

//	err = bt_le_adv_start(BT_LE_ADV_CONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0);
//	if (err) {
//		printk("Advertising failed to start (err %d)\n", err);
//		return;
//	}

//	printk("Advertising successfully started\n");
//}

//static void auth_cancel(struct bt_conn *conn)
//{
//	char addr[BT_ADDR_LE_STR_LEN];

//	bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));

//	printk("Pairing cancelled: %s\n", addr);
//}

//static struct bt_conn_auth_cb auth_cb_display = {
//	.cancel = auth_cancel,
//};

//static void bas_notify(void)
//{
//	uint8_t battery_level = bt_bas_get_battery_level();

//	battery_level--;

//	if (!battery_level) {
//		battery_level = 100U;
//	}

//	bt_bas_set_battery_level(battery_level);
//}

//static void hrs_notify(void)
//{
//	static uint8_t heartrate = 90U;

//	/* Heartrate measurements simulation */
//	heartrate++;
//	if (heartrate == 160U) {
//		heartrate = 90U;
//	}

//	bt_hrs_notify(heartrate);
//}

void main(void)
{
	int err;

	//err = bt_enable(NULL);
	//if (err) {
	//	printk("Bluetooth init failed (err %d)\n", err);
	//	return;
	//}

	//bt_ready();

	//bt_conn_cb_register(&conn_callbacks);
	//bt_conn_auth_cb_register(&auth_cb_display);


                 latch_dev = device_get_binding("GPIO_1");
            gpio_0_dev = device_get_binding("GPIO_0");

        gpio_pin_configure(latch_dev,0, GPIO_INPUT| GPIO_PULL_UP);
        gpio_pin_configure(latch_dev,1, GPIO_INPUT| GPIO_PULL_UP);
        gpio_pin_configure(latch_dev,2, GPIO_INPUT | GPIO_PULL_UP);
        gpio_pin_configure(latch_dev,3, GPIO_INPUT | GPIO_PULL_UP);
        gpio_pin_configure(latch_dev,4, GPIO_INPUT | GPIO_PULL_UP);
        gpio_pin_configure(latch_dev,5, GPIO_INPUT | GPIO_PULL_UP);
        gpio_pin_configure(latch_dev,6, GPIO_INPUT | GPIO_PULL_UP);
        gpio_pin_configure(latch_dev,7, GPIO_INPUT | GPIO_PULL_UP);
        gpio_pin_configure(gpio_0_dev,28, GPIO_INPUT | GPIO_PULL_UP);


	/* Implement notification. At the moment there is no suitable way
	 * of starting delayed work so we do it here
	 */
	while (1) {
		k_sleep(K_SECONDS(1));

		/* Heartrate measurements simulation */
//		hrs_notify();

		/* Battery level simulation */
	//	bas_notify();

      //  gpio_port_clear_bits(latch_dev,0);

                      int testval0 = gpio_pin_get(latch_dev,0);
                      int testval1 = gpio_pin_get(latch_dev,1);
                      int testval2 = gpio_pin_get(latch_dev,2);
                      int testval3 = gpio_pin_get(latch_dev,3);
                      int testval4 = gpio_pin_get(latch_dev,4);
                      int testval5 = gpio_pin_get(latch_dev,5);
                      int testval6 = gpio_pin_get(latch_dev,6);
                      int testval7 = gpio_pin_get(latch_dev,7);

                    
                      int testval28 = gpio_pin_get(gpio_0_dev,28);

                      printk("gpio 1 0 %d%d%d%d%d%d%d%d  gpio 0 28: %d\n",testval0,testval1,testval2,testval3,testval4,testval5,testval6,testval7,testval28);

                  
			k_msleep(10);

	}
}
