#ifndef __TX_H
#define __TX_H

#include <stdint.h>
#include <arm_math.h>

#define CW_TONE_INCREMENT	0.15
#define CW_TONE_VOLUME		2

#define TX_TYPE_NONE            0
#define TX_TYPE_DAC             1
#define TX_TYPE_SI5351          3

// #define TX_TYPE			TX_TYPE_SI5351
#define TX_TYPE                 TX_TYPE_DAC


// TX ADC
#define TX_ADC_DIVISOR          7680
#define TX_ADC_SAMPLE_RATE	(CLOCK/TX_ADC_DIVISOR) // 21875
#define TX_ADC_DIMENSIONE_BUFFER	(64)
#define TX_ADC_BUFFER_RATE	(TX_ADC_SAMPLE_RATE/TX_ADC_DIMENSIONE_BUFFER)

// TX IF

/*
#define TX_AUDIO_FILTER_INTERPOLATION_FACTOR (16)
#define TX_AUDIO_FILTER_BLOCK_SIZE (1024)
#define TX_AUDIO_FILTER_TAP_NUM (128)
*/

#define TX_FILTRO_AUDIO_FATTORE_INTERPOLAZIONE (16)
#define TX_FILTRO_AUDIO_DIMENSIONE_BLOCCO (64)
#define TX_FILTRO_AUDIO_NUMERO_TAP (128)

// filtro audio
#define TX_FILTRO_AUDIO_TAP_NUM    128
#define TX_FILTRO_AUDIO_FREQ_DEF   (1250)
#define TX_FILTRO_AUDIO_FREQ_MAX   (TX_DAC_SAMPLE_RATE/2)
#define TX_FILTRO_AUDIO_BANDA_DEF     (2000)
#define TX_FILTRO_AUDIO_BANDA_MAX     (TX_DAC_SAMPLE_RATE/2)
#define TX_FILTRO_AUDIO_BETA_DEF   (16)
#define TX_FILTRO_AUDIO_BLOCK_SIZE  TX_DAC_BUFFER_SIZE


#define TX_FILTRO_RF_FATTORE_INTERPOLAZIONE (16)
#define TX_FILTRO_RF_DIMENSIONE_BLOCCO (1024)
#define TX_FILTRO_RF_NUMERO_TAP (128)

// TX DAC
#define TX_DAC_SAMPLE_RATE      (CLOCK/480) // 350000
#define TX_DAC_BUFFER_SIZE      (1024)
#define TX_DAC_BUFFER_RATE      (TX_DAC_SAMPLE_RATE/TX_DAC_BUFFER_SIZE) // 341,796


#define TX_NCO_FREQUENCY        (TX_ADC_SAMPLE_RATE/((0xFFFFFFFF)/NCO2_INCREMENT)) // 1750

#define TX_SMETER_SCALE         2

// variabili

extern uint8_t tx_signal, tx_signal_last;
extern q31_t tx_nco1_increment;

// buffer
// TX ADC
extern volatile uint8_t tx_adc_buffer_pronto;
extern uint16_t tx_adc_buffer[TX_ADC_DIMENSIONE_BUFFER];

// TX filtro audio

extern arm_fir_instance_q31 tx_filtro_audio_struttura;
extern q31_t tx_filtro_audio_stato[TX_FILTRO_AUDIO_DIMENSIONE_BLOCCO + TX_FILTRO_AUDIO_NUMERO_TAP - 1];
extern int32_t tx_filtro_audio_coefficienti[TX_FILTRO_AUDIO_NUMERO_TAP];

// TX DAC
extern volatile uint8_t tx_dac_buffer_mezzo_vuoto, tx_dac_buffer_toggle;
extern q31_t tx_dac_buffer[2][TX_DAC_BUFFER_SIZE];

// TX IF
extern q31_t tx_I_buffer[TX_ADC_DIMENSIONE_BUFFER], tx_Q_buffer[TX_ADC_DIMENSIONE_BUFFER];

// funzioni

// filtro audio

extern void tx_filtra_audio(q31_t *out, uint16_t dimensione, uint16_t *in);

extern arm_fir_instance_q31 tx_audio_filter_I_struct, tx_audio_filter_Q_struct;
extern q31_t tx_audio_filter_I_state[TX_FILTRO_AUDIO_DIMENSIONE_BLOCCO + TX_FILTRO_AUDIO_NUMERO_TAP - 1];
extern q31_t tx_audio_filter_Q_state[TX_FILTRO_AUDIO_DIMENSIONE_BLOCCO + TX_FILTRO_AUDIO_NUMERO_TAP - 1];
extern int32_t tx_audio_filter_coeffs[TX_FILTRO_AUDIO_NUMERO_TAP];

// mixer

void tx_mixer(q31_t *out, uint16_t dimensione_out, q31_t *I_in, q31_t *Q_in, int32_t incremento_nco);

// modulatori

void modulatore_dc(q31_t *out_I, q31_t *out_Q, uint16_t dimensione, q31_t *in);
void modulatore_ssb(q31_t *out_I, q31_t *out_Q, uint16_t dimensione, q31_t *in, q31_t nco_freq);
void modulatore_am(q31_t *out_I, q31_t *out_Q, uint16_t dimensione, q31_t *in);

uint16_t u12_sine(void);
void genera_tono(q31_t *buffer, uint16_t buffer_dimensione);
void set_tx_nco1_freq(int32_t freq);

#endif
