#pragma once

//	----------------------------------------------------------------------------
//	Definitions from Gimbal code
//	----------------------------------------------------------------------------

#include "gimbalDefs.h"
#include "gimbalDef.h"

//	----------------------------------------------------------------------------
//	Constants
//	----------------------------------------------------------------------------

	#define BODEMAX 500
	#define FILTMAX 5
	#define FILTBODEMAX 500

	#define IMU_BUF_POINTS 2048
	#define IMU_BUF_AXES 6
	#define IMU_BUF_COUNTx (IMU_BUF_POINTS*IMU_BUF_AXES)
	#define IMU_BUF_BYTES (4*IMU_BUF_COUNTx)

//	----------------------------------------------------------------------------
//	gimbalState
//	----------------------------------------------------------------------------

class gimbalState
{
public:

	// Name

	char name[80];
	int nameLen;

	// Bode results

	float gain[NiAXES3][BODEMAX];
	float phase[NiAXES3][BODEMAX];
	float freqs[NiAXES3][BODEMAX];
	int nbode[NiAXES3];

	// Open Loop results

	float gainOpen[NiAXES3][BODEMAX];
	float phaseOpen[NiAXES3][BODEMAX];
	float freqsOpen[NiAXES3][BODEMAX];
	int numOpen[NiAXES3];

	// Shake results

	float gainShake[NiAXES3][BODEMAX];
	float phaseShake[NiAXES3][BODEMAX];
	float freqsShake[NiAXES3][BODEMAX];
	float jitterShake[NiAXES3][BODEMAX];
	int nShake[NiAXES3];

	// PID Parameters & HS Filters

	tunings_t par;
	hsFilterState_t hsFilterState[NiAXES3][FILTMAX];
	int numfiltpar[NiAXES3];
	int numfilt[NiAXES3];
	float filterphase[NiAXES3][FILTMAX+1][FILTBODEMAX];
	float filtergains[NiAXES3][FILTMAX+1][FILTBODEMAX];
	float filterfreqs[NiAXES3][FILTMAX+1][FILTBODEMAX];

	// Step Response

	float stepsamples[NiAXES3][IMU_BUF_POINTS];
	int numstep[NiAXES3];

	// FFT Samples

	float ffts[NiAXES3][IMU_BUF_POINTS];
	int numffts[NiAXES3];

public:
	gimbalState(char *name = NULL);
	~gimbalState(void);
public:
	void init(void);
	void copy(gimbalState in);
	void saveState(FILE *sfile);
	void readState(FILE *sfile);
	int match1(char *s, char *arrayName, int *index1, int max1, int *index0, int max0, char *elementName);
	int match2(char *s, char *arrayName, int *index0, int max0);
	int match3(char *s, char *arrayName, int *index0, int max0, char *elementName);
	int match5(char *s, char *arrayName, int *index1, int max1, int *index0, int max0);
	int getnumfilt(int axis);
	void defaultSettings(void);
	void hsFilterNotify();
};

