#pragma once

#include "config.h"
#include "buttons.h"
#include <stdint.h>

#define FXP_SCALING 4000
#define DIV_ROUND(a,b) (((a)+(b)/2)/(b))

static const int32_t MIN_FREQ1 = 7000;
static const int32_t MAX_FREQ1 = 30000;
static const int32_t MIN_FREQ3 = 1000000;
static const int32_t MAX_FREQ3 = 3000000;
static const int32_t MIN_D1D2 = 0.005 * FXP_SCALING;//0.5%
static const int32_t MAX_D1D2 = 0.495 * FXP_SCALING;//49.5%
static const int32_t MAX_PH3 = 3000;


typedef enum {
    ENABLE_FORCE_NONE = 0,
    ENABLE_FORCE_ENABLE,
    ENABLE_FORCE_DISABLE
} enable_force_t;

typedef struct {
    uint32_t f1;//Hz
    uint32_t _period1;//steps
    uint32_t D1;//4000/100%
    uint32_t _duty1;//steps
    uint32_t D2;//4000/100%
    uint32_t _duty2;//steps

    uint32_t Ph2;//4000steps/360*
    uint32_t _phase2;//steps

    uint32_t Ph3;//4000steps/360*
    uint32_t f3;//Hz
    uint32_t _period3;//steps
    uint32_t D3;//*0.1%
    uint32_t N3;//n

    enable_force_t ena;
} tweaked_params_s;

typedef struct {
	union {
		uint32_t data[14];
		tweaked_params_s regs;
	};
} tweaked_params_union_s;

void init_params(tweaked_params_s * pParams);
uint32_t validate_generation_values(tweaked_params_s * pParams);
void update_three_signal_values(tweaked_params_s * pParams, wb_mailbox_button_regs_t * pButtons);
