 /**
 * Copyright (c) 2014 - 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 "mode_select.h"

static modes_wban_t current_indication;

void mode_indication(modes_wban_t indicate, bsp_indication_t previous)
{
    // Switch-off
    nrf_drv_pwm_uninit(&m_pwm1);
    bsp_board_leds_off();
    nrf_gpio_pin_clear(MOTOR_PIN);
    bsp_indication_set(BSP_INDICATE_IDLE);
    // Set current WBAN indication
    current_indication = indicate;

    switch (indicate)
    {
        case MODE_READY:
            bsp_indication_set(previous);
            break;

        case MODE_ON:
            bsp_board_led_on(BSP_BOARD_LED_0);
            bsp_board_led_on(BSP_BOARD_LED_1);
            break;

        case MODE_PWM: ;
            nrf_drv_pwm_config_t const config1 =
            {
                .output_pins =
                {
                    BSP_LED_0 | NRF_DRV_PWM_PIN_INVERTED, // channel 0
                    BSP_LED_1 | NRF_DRV_PWM_PIN_INVERTED, // channel 1
                    MOTOR_PIN | NRF_DRV_PWM_PIN_INVERTED, // channel 2
                    NRF_DRV_PWM_PIN_NOT_USED              // channel 3
                },
                .irq_priority = APP_IRQ_PRIORITY_LOWEST,
                .base_clock   = NRF_PWM_CLK_125kHz,
                .count_mode   = NRF_PWM_MODE_UP,
                .top_value    = 10000,
                .load_mode    = NRF_PWM_LOAD_INDIVIDUAL,
                .step_mode    = NRF_PWM_STEP_AUTO
            };
            APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm1, &config1, NULL));

            // This array cannot be allocated on stack (hence "static") and it must
            // be in RAM (hence no "const", though its content is not changed).
            static nrf_pwm_values_individual_t /*const*/ seq1_values[] =
            {
                {      0, 0,    0|0x8000, 0 },
                {  10000, 0, 1000|0x8000, 0 },
                {      0, 0,    0|0x8000, 0 },
                {  10000, 0, 1000|0x0000, 0 },
                {      0, 0,    0|0x8000, 0 },
                {      0, 0,    0|0x8000, 0 },
                {      0, 0,    0|0x8000, 0 },
                {      0, 0,    0|0x8000, 0 }    
        
            };

            nrf_pwm_sequence_t const seq1 =
            {
                .values.p_individual = seq1_values,
                .length          = NRF_PWM_VALUES_LENGTH(seq1_values),
                .repeats         = 1,
                .end_delay       = 10
            };

            (void)nrf_drv_pwm_simple_playback(&m_pwm1, &seq1, 1,
                                              NRF_DRV_PWM_FLAG_LOOP);


            break;

        case MODE_MOT:
            bsp_board_led_on(BSP_BOARD_LED_1);
            nrf_gpio_pin_set(MOTOR_PIN);
            break;

        default:
            break;
    }
}


bool timeslot_indicate(void)
{
    if(current_indication == MODE_READY)
    {
        return true;
    }
    else
    {
        return false;
    }
}
