#include "at32f403a_407.h"
#include "modbus_ao_params.h"
#include "settings_api.h"
#include "modbus_params.h"
#include "analog_output.h"


#if defined (MAO_4)


extern mb_param_t mb_param[];


//
uint16_t mb_init_ao_params(uint16_t i)
{
    uint16_t index = i;
    uint16_t addr = 0;  
  
    // Статус выходов (вкл./выкл.)
    mb_param[index].reg = 0x0500;
	mb_param[index].size = 1;
	mb_param[index].param = (uint8_t*)&settings.ao_state_bits;
	mb_param[index].set = mb_set_ao_state;
    mb_param[index].get = NULL;
    mb_param[index].check_handler = mb_check_dummy;
    
    index++;
    
    // Статус выходов в безопасном режиме (вкл./выкл.)
    mb_param[index].reg = 0x0501;
	mb_param[index].size = 1;
	mb_param[index].param = (uint8_t*)&settings.ao_state_save_bits;
	mb_param[index].set = mb_set_ao_state_save;
    mb_param[index].get = NULL;
    mb_param[index].check_handler = mb_check_dummy;
    
    index++;
    
    // Режим работы выходов
    mb_param[index].reg = 0x0502;
	mb_param[index].size = 1;
	mb_param[index].param = (uint8_t*)&settings.ao_mode_bits;
	mb_param[index].set = mb_set_ao_mode;
    mb_param[index].get = NULL;
    mb_param[index].check_handler = mb_check_dummy;
    
    index++;
    
    // Режим работы выходов
    mb_param[index].reg = 0x0503;
	mb_param[index].size = 1;
	mb_param[index].param = (uint8_t*)&settings.ao_mode_save_bits;
	mb_param[index].set = mb_set_ao_mode_save;
    mb_param[index].get = NULL;
    mb_param[index].check_handler = mb_check_dummy;
    
    index++;
    
    // Состояние питания выходных каналов
    mb_param[index].reg = 0x0504;
	mb_param[index].size = 1;
	mb_param[index].param = (uint8_t*)&ao_power;
	mb_param[index].set = NULL;
    mb_param[index].get = NULL;
    mb_param[index].check_handler = mb_check_dummy;
    
    index++;
    
    // ---------------------------------------------------------------------- //
    
    // Значения выходов. Регистры 0x0510 - 0x0513.
    addr = 0x0510;
    for (int i = 0; i < AO_NUMBER; i++)
    {
        mb_param[index].reg = addr;
        mb_param[index].size = 1;
        mb_param[index].param = (uint8_t*)&settings.ao[i].value;
        mb_param[index].set = mb_set_ao_value;
        mb_param[index].get = NULL;
        mb_param[index].check_handler = mb_check_dummy;
        
        addr += 1;
        index++;
    }
    
    // Значения выходов в безопасном режиме. Регистры 0x0420 - 0x0423.
    addr = 0x0520;
    for (int i = 0; i < AO_NUMBER; i++)
    {
        mb_param[index].reg = addr;
        mb_param[index].size = 1;
        mb_param[index].param = (uint8_t*)&settings.ao_save[i].value;
        mb_param[index].set = NULL;
        mb_param[index].get = NULL;
        mb_param[index].check_handler = mb_check_dummy;
        
        addr += 1;
        index++;
    }
        
    return index;
}



// -------------------------------------------------------------------------- //
//                          Установка параметров
// -------------------------------------------------------------------------- //

//
mb_delay_action_t mb_set_ao_state(void)
{
    ao_set();
    return MB_SAVE_SETTINGS;
}

//
mb_delay_action_t mb_set_ao_state_save(void)
{
    ao_set_save();
    return MB_SAVE_SETTINGS;
}

//
mb_delay_action_t mb_set_ao_mode(void)
{
    ao_set_mode();
    return MB_SAVE_SETTINGS;
}

//
mb_delay_action_t mb_set_ao_mode_save(void)
{
    ao_set_mode_save();
    return MB_SAVE_SETTINGS;
}

//
mb_delay_action_t mb_set_ao_value(void)
{
    ao_set_value_flag();
}



#endif