/* main.c -- MQTT client example
*
* Copyright (c) 2014-2015, Tuan PM <tuanpm at live dot com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "esp_common.h"
#include "driver/uart.h"
#include "driver/spi_interface.h"
#include "gpio.h"

#include "log/esp_log.h"
#include "gdbstub/gdbstub.h"

#include "hal/lsm6ds3.h"
#include "hal/max17043.h"
#include "hal/lps25hb.h"
#include "hal/ws2812.h"

#include "user_config.h"

static const char* TAG = "main.c";

// void wifi_handle_event_cb(System_Event_t *evt)
// {
//     switch(evt->event)
//     {
//       case EVENT_STAMODE_CONNECTED:
//         break;
//       case EVENT_STAMODE_DISCONNECTED:
//         break;
//       case EVENT_STAMODE_AUTHMODE_CHANGE:
//         break;
//       case EVENT_STAMODE_GOT_IP:
//         break;
//       case EVENT_SOFTAPMODE_PROBEREQRECVED:
//         break;
//       case EVENT_SOFTAPMODE_STACONNECTED:
//         break;
//       case EVENT_SOFTAPMODE_STADISCONNECTED:
//         break;
//       default:
//         break;
//     }
// }

#define DEBOUNCEDELAY 		10 		// ms

#define SHDN_MUX 	PERIPHS_IO_MUX_GPIO4_U	// GPIO 4
#define PBA_MUX	 	PERIPHS_IO_MUX_GPIO5_U	// GPIO 5
#define PBB_MUX  	PERIPHS_IO_MUX_GPIO2_U	// GPIO 2
#define STATLED_MUX	PERIPHS_IO_MUX_GPIO2_U	// GPIO 0

#define SHDN_FUNC		FUNC_GPIO4
#define PBA_FUNC 		FUNC_GPIO5
#define PBB_FUNC 		FUNC_GPIO2
#define STATLED_FUNC 	FUNC_GPIO0

#define SHDN 		4
#define PBA 		5
#define PBB 		2
#define STATLED		0

 void ICACHE_FLASH_ATTR app_init(void)
{
    uart_init(BIT_RATE_115200, BIT_RATE_115200, DISABLE_UART1);
    ESP_LOGI(TAG, "Starting LSM6DS3 Demo\n");
    ESP_LOGI(TAG, "WiFi Testing \n");

    //while(true)
    {
        LSM6DS3_Enable_I2C_Bridge(1);
        uint16_t x = max17043_getVoltage();
        ESP_LOGI(TAG, "MAX17043 Voltage %d", x);
        uint8_t y;
        LPS25HB_Get_DeviceID(&y);
        ESP_LOGI(TAG, "DEVICEID %x", y);
        // uint8_t i;
        // for(i=0;i<8;i++)
        //     ESP_LOGI(TAG, "RX SPI Value: %x", data[i]);
        //os_delay_us(1000000);
        system_soft_wdt_feed();
    }

    //while(true)
    {
        ESP_LOGI(TAG, "Setting WS2812B");
        PIN_FUNC_SELECT(STATLED_MUX, STATLED_FUNC);
        gpio_output_set(BIT(SHDN), BIT(STATLED), (BIT(SHDN)|BIT(STATLED)), (BIT(PBA)|BIT(PBB)));

        WS2812_SetColor(WS2812_ORANGE, WS2812_PULSE);

    }

  // wifi_set_opmode(STATION_MODE);
  // wifi_station_ap_number_set(MAX_APS);
  // wifi_station_set_auto_connect(true);
  //
  // wifi_station_get_ap_info()
  // wifi_station_ap_change()
  // wifi_staiton_get_current_ap_id()
  // wifi_set_sleep_type(MODEM_SLEEP_T);
  // wifi_enable_gpio_wakeup()
  //
  // if (wifi_station_get_config())
  //   os_printf("zero wifi_config\n");
  // else
  //   os_printf("there is a stored config");
}

void user_init(void)
{
    wifi_set_opmode(STATION_MODE);
    wifi_station_ap_number_set(5);
    system_init_done_cb(app_init);
}