#include "at32f403a_407.h"
#include "output.h"
#include "FreeRTOS.h"
#include "task.h"
#include "settings_api.h"
#include "io_utils.h"
#include <stdio.h>


out_t outputs[DO_NUMBER] = {
    {GPIOC, GPIO_PINS_12, 0, 0, 0, false, 0, 0},   // -
    {GPIOD, GPIO_PINS_2,  0, 0, 0, false, 0, 0},   // -
    {GPIOE, GPIO_PINS_6,  0, 0, 0, false, 0, 0},   // TMR9_CH2 (remap)
    {GPIOC, GPIO_PINS_1,  0, 0, 0, false, 0, 0},   // -
    {GPIOC, GPIO_PINS_11, 0, 0, 0, false, 0, 0},   // -
    {GPIOD, GPIO_PINS_3,  0, 0, 0, false, 0, 0},   // -
    {GPIOE, GPIO_PINS_5,  0, 0, 0, false, 0, 0},   // TMR9_CH1 (remap)
    {GPIOC, GPIO_PINS_2,  0, 0, 0, false, 0, 0}    // -
};


simple_gpio_t load_sens[DO_NUMBER] = {
    {GPIOD, GPIO_PINS_0},
    {GPIOD, GPIO_PINS_1},
    {GPIOC, GPIO_PINS_13},
    {GPIOC, GPIO_PINS_0},
    {GPIOC, GPIO_PINS_10},
    {GPIOD, GPIO_PINS_4},
    {GPIOE, GPIO_PINS_4},
    {GPIOC, GPIO_PINS_3}
};



//
void out_gpio_init(out_t *out, uint8_t index)
{
    gpio_init_type gpio_init_struct;

    
    // Выход
    if (out->mode == 0)
    {
        gpio_default_para_init(&gpio_init_struct);
        
        gpio_init_struct.gpio_out_type       = GPIO_OUTPUT_PUSH_PULL;  
        gpio_init_struct.gpio_pull           = GPIO_PULL_NONE;  
        gpio_init_struct.gpio_mode           = GPIO_MODE_OUTPUT;  
        gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
        gpio_init_struct.gpio_pins           = out->pin;
        gpio_init(out->port, &gpio_init_struct); 
    }
    
    do_update(out, index) ;
}

//
void do_set_common(void)
{
    bool flag = false;
    
    for (int i = 0; i < DO_NUMBER; i++)
    {
        // 
        if ((settings.do_bits & (1 << i)) != (output_state_bit & (1 << i))) {
            settings.do_bits = output_state_bit;
            flag = true;
        } 
        else if ((settings.do_mode_bits & (1 << i)) != (output_mode_bit & (1 << i))) {
            settings.do_mode_bits ^= (1 << i);
            settings.do_mode_bits = output_mode_bit;
            flag = true;
        }  
        else if (settings.do_pwm[i] != output_pwm[i]) {
            settings.do_pwm[i] = output_pwm[i];
            flag = true;
        }
        else if (settings.do_pwm_save[i] != output_pwm_save[i]) {
            settings.do_pwm_save[i] = output_pwm_save[i];
            flag = true;
        }
        else if (settings.do_pwm_period [i] != output_pwm_period[i]) {
            settings.do_pwm_period[i] = output_pwm_period[i];
            flag = true;
        }
        else if (settings.do_pwm_period_save[i] != output_pwm_period_save[i]) {
            settings.do_pwm_period_save[i] = output_pwm_period_save[i];
            flag = true;
        }
 
        if (flag)
            do_update(&outputs[i], i);
    }
}

//
void do_set(void)
{
    if (output_state_bit == settings.do_bits) 
        return;
    
    // Состояние выхода/выходов изменилось
    for (int i = 0; i < DO_NUMBER; i++)
    {
        if ((settings.do_bits & (1 << i)) != (output_state_bit & (1 << i))) 
        {
            do_update(&outputs[i], i);
        }
    }
    
    // Сохраним новое значение выходов в настройках
    settings.do_bits = output_state_bit;
}

// Установка значения на выходе 
void do_update(out_t *out, uint8_t i)
{
    // Режим ШИМ
    if (settings.do_mode_bits & (1 << i)) {
        // Безопасный режим включен
        if (save_mode_get())
            do_set_pwm(settings.do_pwm_period_save[i], settings.do_pwm_save[i], i);
        else
            do_set_pwm(settings.do_pwm_period[i], settings.do_pwm[i], i);
    }
    // Режим обычного выхода
    else {
        out->mode = 0;
        // Безопасный режим включен
        if (save_mode_get())
            do_set_out(out, settings.do_save_bits & (1 << i));
        else
            do_set_out(out, settings.do_bits & (1 << i));
    }
}

//
void do_set_mode(void)
{
    if (output_mode_bit == settings.do_mode_bits)
        return;
  
    // Состояние выхода/выходов изменилось
    for (int i = 0; i < DO_NUMBER; i++)
    {
        if ((settings.do_mode_bits & (1 << i)) != (output_mode_bit & (1 << i)))
        {
            settings.do_mode_bits ^= (1 << i);
            do_update(&outputs[i], i);
        }
    }
  
    settings.do_mode_bits = output_mode_bit;
}

//#define PWM_PERIOD_TEST     20
//#define PWM_DUTY_TEST       10
void do_set_pwm(uint16_t period, uint16_t duty, uint8_t index)
{
    uint16_t duty_calc;
  
    if (duty == 0)
        duty_calc = 0;
    else
        duty_calc = (duty*period/100 < 1) ? 1 : duty*period/100;
    
    outputs[index].pwm_flag = false;
    outputs[index].pwm_period_cnt = 0;
    outputs[index].pwm_duty_cnt = 0;
    outputs[index].pwm_period = period;
    outputs[index].pwm_duty = duty_calc;
    outputs[index].mode = 1;
}

//
void do_set_out(out_t *out, uint8_t val)
{
    if (val) {
        gpio_bits_set(GPIOB, GPIO_PINS_15);
        gpio_bits_set(out->port, out->pin);
    }
    else {
        gpio_bits_reset(GPIOB, GPIO_PINS_15);
        gpio_bits_reset(out->port, out->pin);
    }
}

//
void out_as_pwm(void)
{
    uint32_t timer_period;
    uint16_t pwm_pulse;
    crm_clocks_freq_type crm_clocks_freq_struct = {0};
    tmr_output_config_type tmr_output_struct;
    
    crm_periph_clock_enable(CRM_TMR9_PERIPH_CLOCK, TRUE);
    
    crm_clocks_freq_get(&crm_clocks_freq_struct);
    
    //timer_period = (crm_clocks_freq_struct.sclk_freq / 10 ) - 1;
    
    timer_period = 24000 - 1;
    pwm_pulse = 125*timer_period/1000;
    
    tmr_base_init(TMR9, 24000 - 1, 1000 - 1);
    tmr_cnt_dir_set(TMR9, TMR_COUNT_UP);

    tmr_output_default_para_init(&tmr_output_struct);
    tmr_output_struct.oc_mode = TMR_OUTPUT_CONTROL_PWM_MODE_A;
    tmr_output_struct.oc_output_state = TRUE;
    tmr_output_struct.oc_polarity = TMR_OUTPUT_ACTIVE_HIGH;
    tmr_output_struct.oc_idle_state = TRUE;
    
    tmr_output_channel_config(TMR9, TMR_SELECT_CHANNEL_1, &tmr_output_struct);
    tmr_channel_value_set(TMR9, TMR_SELECT_CHANNEL_1, pwm_pulse);
    
    tmr_flag_clear(TMR9, TMR_OVF_FLAG);
    nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
    nvic_irq_enable(TMR1_BRK_TMR9_IRQn, 5, 0);
    
    //tmr_interrupt_enable(TMR9, TMR_OVF_INT, TRUE);
    tmr_interrupt_enable(TMR9, TMR_C1_INT, TRUE);
    
    tmr_counter_enable(TMR9, TRUE);
}

// Таймер для выходов в режиме PWM. Частота 10Гц.
void out_pwm_tim_init(void)
{
    uint16_t prescaler_value = 0;
    uint16_t timer_period = 1000 - 1;
    gpio_init_type gpio_init_struct;
    
    gpio_default_para_init(&gpio_init_struct);
        
    gpio_init_struct.gpio_pins = GPIO_PINS_15;
    gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
    gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
    gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
    gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
    gpio_init(GPIOB, &gpio_init_struct);

    crm_periph_clock_enable(CRM_TMR9_PERIPH_CLOCK, TRUE);
    
    prescaler_value = (uint16_t)(system_core_clock / 10000) - 1;
    tmr_base_init(TMR9, timer_period, prescaler_value);
    
    tmr_cnt_dir_set(TMR9, TMR_COUNT_UP);
    tmr_clock_source_div_set(TMR9, TMR_CLOCK_DIV1);
    
    nvic_irq_enable(TMR1_BRK_TMR9_IRQn, 5, 0);

    tmr_interrupt_enable(TMR9, TMR_OVF_INT, TRUE);
    
    tmr_counter_enable(TMR9, TRUE);
}



inline void pwm_proc(void)
{
    for (int i = 0; i < DO_NUMBER; i++)
    {
        if (outputs[i].mode)    // режим PWM
        {
            if (outputs[i].pwm_period_cnt == outputs[i].pwm_period /*PWM_PERIOD_TEST*/) {
                outputs[i].pwm_period_cnt = 0;
            }
            if (outputs[i].pwm_period_cnt == 0) {
                outputs[i].pwm_flag = false;
                gpio_bits_set(outputs[i].port, outputs[i].pin);
                //gpio_bits_set(GPIOB, GPIO_PINS_15);
            }
            if (outputs[i].pwm_duty_cnt == outputs[i].pwm_duty /*PWM_DUTY_TEST*/) {
                outputs[i].pwm_duty_cnt = 0;
                gpio_bits_reset(outputs[i].port, outputs[i].pin);
                //gpio_bits_reset(GPIOB, GPIO_PINS_15);
                outputs[i].pwm_flag = true;
            }
            outputs[i].pwm_period_cnt++;
            if (outputs[i].pwm_flag == false) {
                outputs[i].pwm_duty_cnt++;
            }
        }
    }
}

//
void load_sens_init(simple_gpio_t *sens)
{
    gpio_init_type gpio_init_struct;
    
    gpio_default_para_init(&gpio_init_struct);
        
    gpio_init_struct.gpio_pull           = GPIO_PULL_NONE;  
    gpio_init_struct.gpio_mode           = GPIO_MODE_INPUT;  
    gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
    gpio_init_struct.gpio_pins           = sens->pin;
    gpio_init(sens->port, &gpio_init_struct); 
    
}

//
void out_test(void)
{
    for (int i = 0; i < DO_NUMBER; i++)
    {
        outputs[i].port->odt ^= outputs[i].pin;
        
        printf("LOAD_1: %u LOAD_2: %u LOAD_3: %u LOAD_4: %u LOAD_5: %u LOAD_6: %u LOAD_7: %u LOAD_8: %u\r\n", 
           gpio_input_data_bit_read(load_sens[0].port, load_sens[0].pin),
           gpio_input_data_bit_read(load_sens[1].port, load_sens[1].pin),
           gpio_input_data_bit_read(load_sens[2].port, load_sens[2].pin),
           gpio_input_data_bit_read(load_sens[3].port, load_sens[3].pin),
           gpio_input_data_bit_read(load_sens[4].port, load_sens[4].pin),
           gpio_input_data_bit_read(load_sens[5].port, load_sens[5].pin),
           gpio_input_data_bit_read(load_sens[6].port, load_sens[6].pin), 
           gpio_input_data_bit_read(load_sens[7].port, load_sens[7].pin));
        
        vTaskDelay(500);
    }  
}

//
void load_test(void)
{
    printf("LOAD_1: %u LOAD_2: %u LOAD_3: %u LOAD_4: %u LOAD_5: %u LOAD_6: %u LOAD_7: %u LOAD_8: %u\r\n", 
           gpio_input_data_bit_read(load_sens[0].port, load_sens[0].pin),
           gpio_input_data_bit_read(load_sens[1].port, load_sens[1].pin),
           gpio_input_data_bit_read(load_sens[2].port, load_sens[2].pin),
           gpio_input_data_bit_read(load_sens[3].port, load_sens[3].pin),
           gpio_input_data_bit_read(load_sens[4].port, load_sens[4].pin),
           gpio_input_data_bit_read(load_sens[5].port, load_sens[5].pin),
           gpio_input_data_bit_read(load_sens[6].port, load_sens[6].pin), 
           gpio_input_data_bit_read(load_sens[7].port, load_sens[7].pin));
}


//
void TMR1_BRK_TMR9_IRQHandler(void)
{
    if(tmr_flag_get(TMR9, TMR_OVF_FLAG) != RESET)
    {
        tmr_flag_clear(TMR9, TMR_OVF_FLAG);
        //GPIOB->odt ^= GPIO_PINS_15;
        pwm_proc();
    }
}

#if 0
void TMR1_BRK_TMR9_IRQHandler(void)
{
    static uint32_t cnt1 = 0;
    static uint32_t cnt2 = 0;
    
    if (tmr_flag_get(TMR9, TMR_OVF_FLAG) != RESET)
    {
        tmr_flag_clear(TMR9, TMR_OVF_FLAG);
        cnt1++;
        printf("Cnt1 %u\r\n", cnt1);
    }
    else if (tmr_flag_get(TMR9, TMR_C1_INT) != RESET)
    {
        tmr_flag_clear(TMR9, TMR_C1_INT);
        cnt2++;
        printf("Cnt2 %u\r\n", cnt2);
    }
}
#endif
