
//definice HW: pripojeni knihovny pro komunikaci SPI
#include "stpm18spi.h"



#define STPM_NUMBER		4


//zapne ladeni
#define DEBUG_STPM		1
//zapne vypis surovych dat pro dane ID obvodu, DEBUG_STPM musi byt 1
#define DEBUG_STPM_ID	0b00000010

//definice typu

typedef struct {
	signed long dec;
	signed long frac;
} stpmEnergyCounter;

typedef struct {
	float freq;
	float iRMS;
	float uRMS;
	float iMax;
	float uMax;

	float actPwr;
	float appPwr;
	float reaPwr;
	float t1Pwr;

	stpmEnergyCounter actEnergy;
	stpmEnergyCounter appEnergy;
	stpmEnergyCounter reaEnergy;
	stpmEnergyCounter t1Energy;

} stpmResultType;

typedef struct {
	float calU;
	float calI;
} stpmCalibrationType;

extern stpmResultType stpmResult[STPM_NUMBER];
extern stpmCalibrationType stpmCal[STPM_NUMBER];


//pomocne funkce pro zpracovani dat
float getUCrestFactor(stpmResultType * stres);
float getICrestFactor(stpmResultType * stres);
float getPowerFactor(stpmResultType * stres);

#define TD_ACT	1
#define TD_APP	1
#define TD_REA	2
#define TD_T1	4

float getEnergyWs(unsigned char id, stpmEnergyCounter sid, unsigned char typDat);
float getEnergyWh(unsigned char id, stpmEnergyCounter sid, unsigned char typDat);

// inicializace knihovny
void initSTPM(void);
void initDefaultCalibrations(void);

// funkce vycte data z obvodu elektromeru
// nutno volat periodicky po max. 500 ms
void processSTPM(unsigned long deltaUs);

// funkce pripravi obsah struktury stpmResult[id]
void computeSTPMResults(unsigned char id);

// funkce smaze uMax, iMax z pameti prubezneho mereni
#define REI_ALL	255
void reinitializeMaxMeasurements(unsigned char id);

//funkce nuluje mereni z daneho obvodu vc. nacitane energie
void initializeRealData(unsigned char id);
