/**
 * Copyright (c) 2017 - 2019, Nordic Semiconductor ASA
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, 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.
 *
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>

#include "nrf.h"
#include "nrf_drv_usbd.h"
#include "nrf_drv_clock.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "nrf_drv_power.h"
#include "nrfx_saadc.h"

#include "app_error.h"

#include "boards.h"
#include "bsp.h"

#include "nrf_pwr_mgmt.h"
#include "nrf_drv_saadc.h"



#include "app_timer.h"
#include "kimia_usb_log.h"


APP_TIMER_DEF(m_light_timer_id);

static void light_timer_handler(void * p_context)
{
    (void)p_context;

    nrf_saadc_value_t adc_value;
    nrf_drv_saadc_sample_convert(0, &adc_value);

    uint32_t voltage_mv = adc_value * 3600 / 1024;
    KIMIA_USB_PRINT("ADC: %d, %lu mV\r\n", adc_value, (unsigned long)voltage_mv);

    if (voltage_mv < 1000)
    {
        bsp_board_led_on(BSP_BOARD_LED_0);
        bsp_board_led_on(BSP_BOARD_LED_1);
    }
    else
    {
        bsp_board_led_off(BSP_BOARD_LED_0);
        bsp_board_led_off(BSP_BOARD_LED_1);
    }
}


static void idle_state_handle(void)
{
    if (kimia_usb_log_process() == false)
    {
        nrf_pwr_mgmt_run();
    }
}

static void init_bsp(void)
{
    ret_code_t ret;
    ret = bsp_init(BSP_INIT_LEDS, 0);
    APP_ERROR_CHECK(ret);
}





void saadc_callback(nrf_drv_saadc_evt_t const * p_event)
{
    //empty callback
}


void saadc_init(void)
{
    ret_code_t err_code;

    err_code = nrf_drv_saadc_init(NULL, saadc_callback);
    APP_ERROR_CHECK(err_code);

    nrf_drv_saadc_channel_config_t channel_config =
        NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);

    nrf_drv_saadc_channel_init(0, &channel_config);
}




int main(void)
{
    ret_code_t ret;


    ret = nrf_drv_clock_init();
    APP_ERROR_CHECK(ret);
    
    nrf_drv_clock_lfclk_request(NULL);

    while(!nrf_drv_clock_lfclk_is_running())
    {
        /* Just waiting */
    }

    ret = app_timer_init();
    APP_ERROR_CHECK(ret);

    kimia_usb_log_init();
    init_bsp();
    saadc_init();

    app_timer_create(&m_light_timer_id,
                     APP_TIMER_MODE_REPEATED,
                     light_timer_handler);

    app_timer_start(m_light_timer_id, APP_TIMER_TICKS(1000), NULL);
    
    KIMIA_USB_PRINT("LAB3."); //print do usb zadziała tylko po jakimś czasie, użyjcie tej funkcji w timerze
    

    //Dont change this loop
    for (;;)
    {
         idle_state_handle();
    }
}
