/*
 * signals.c
 *
 *  Created on: Jan 28, 2022
 *      Author: xavie
 */
#include "signals.h"

#define STSOP_LATTITUDE           ((float) 49.229393 )  /*!< default latitude position */
#define STSOP_LONGITUDE           ((float) 6.1033393 )  /*!< default longitude position */
#define MAX_GPS_POS               ((int32_t) 8388607 )  /*!< 2^23 - 1 */


// System variables read from main unit
float meas_case_temperature;						// Main unit case temperature
float meas_valves_current;							// Current consumption of active valves
float meas_drives_current;							// Current consumption of active drives
float meas_battery_voltage;							// Vbat voltage
uint16_t VREF;										// ADC reference voltage

// System variables read from CAN-BUS
// Temperature unit is degree Celsius
float gh_outsideTemperature[AIR_SENSORS];			// Outside temperature	(eg. full sun, shade)
float gh_insideTemperature[AIR_SENSORS];			// Inside temperature - Used to control cooling system
uint8_t gh_insideHumidity[AIR_SENSORS];				// Inside relative humidity
float gh_solarIrradiance[AIR_SENSORS];				// Solar irradiance inside the greenhouse */
float gh_waterTankTemperature[WATER_SENSORS];		// Water tank temperature
uint8_t gh_waterTankLevel[WATER_SENSORS];				// Water tank level in percent
float gh_soilTemperature[SOIL_SENSORS];	// Soil temperature - Used to control heating cable or sprinklers

float gh_averageRainDropRate[AIR_SENSORS];			// Average rain drop rate in mm/h
float gh_averageRainDropSize[AIR_SENSORS];			// Average rain drop size in mm
float gh_averageRainDropSpeed[AIR_SENSORS];			// Average rain drop speed in m/s
uint16_t gh_rainDropCount[AIR_SENSORS];				// Total rain drop count

// Moisture unit is percent
float gh_soilMoisture[SOIL_SENSORS];	// Soil moisture - Used to control sprinklers
// Vibes unit is scale from 0(no vibes) to 5(very strong vibes)
uint8_t gh_soilVibesAiSimilarity[SOIL_SENSORS];		// Soil vibration detection AI similarity

// Atmospheric Pressure unit is hecto Pascal
uint16_t gh_atmPressure[AIR_SENSORS];	// Atmospheric pressure

// Wind speed unit is meter/second
uint16_t gh_windSpeed[AIR_SENSORS];		// Wind speed
// Wind direction unit is degree - 0 is north, 180 is south
uint16_t gh_windDirection[AIR_SENSORS];	// Wind direction
// Max wind speed in Beaufort
uint8_t gh_windMaxBeaufort[AIR_SENSORS];
// Current rain condition
uint8_t gh_rainingCondition[AIR_SENSORS];
bool gh_dropDetection[AIR_SENSORS];				// Rainy day  0=No, 1=Yes


float getAvgFloatValue( float tab[], uint16_t size )
{
float sum = 0;
uint16_t i=0, z=0;

	while( i < size )
	{
		float t = tab[i++];
		if( t == 0 ) z++;
		sum += t;
	}
	if( size == z )
		return 0;
	else
		return sum / (size-z);
}


// MCU_S_ANEMOMETER
float getOutsideTemperature( dev_anemometer s )
{
	return s.MCU_OutsideTemp - 40;
}

uint16_t getAtmPressure( dev_anemometer s )
{
	return (s.MCU_AtmPressure + 920 );
}

float getWindSpeed( dev_anemometer s )
{
	return (s.MCU_WindSpeed * 0.1F );
}

uint8_t getWindDirection( dev_anemometer s )
{
	return (s.MCU_WindDir );
}

uint8_t getWindBeaufort( dev_anemometer s )
{
	return (s.MCU_MaxBeaufort );
}

// MCU_S_WATERTANK
uint8_t getWaterTankLevel( dev_watertank s )
{
	return ( s.MCU_WaterTankLevel );
}

bool getRain( dev_watertank s )
{
	return s.MCU_Rain;
}

// MCU_S_PYRANOMETER
float getSolarIrradiance( dev_pyranometer s )
{
	return s.MCU_SolarIrradiance * 0.1F;
}

// MCU_S_THERMOMETER
float getInsideTemperature( dev_thermometer s )
{
	return s.MCU_InsideTemp - 40;
}

uint8_t getInsideRelativeHumidity( dev_thermometer s )
{
	return s.MCU_InsideHumidity;
}

// MCU_S_RAINGAUGE
uint8_t getRainingCondition( dev_rain_gauge s )
{
	return s.MCU_RainingCondition;
}

// MCU_S_SUBSONICGAUGE
uint8_t getSubsonicAiSimilarity( dev_subsonic_gauge s )
{
	return s.MCU_AiSimilarity;
}

// MCU_S_DISDROMETER
float getAvgRainDropRate( dev_disdrometer s )
{
	return s.MCU_AvgRainDropRate * 0.01F; // mm/h;
}

float getAvgRainDropSize(dev_disdrometer s) {
	return s.MCU_AvgRainDropSize * 0.01F; // mm
}

float getAvgRainDropSpeed(dev_disdrometer s) {
	return s.MCU_AvgRainDropSpeed * 0.01F; // m/s
}

uint16_t getRainDropCount(dev_disdrometer s) {
	return s.MCU_RainDropCount;
}

int32_t  EnvSensors_Read(LoRaWAN_sensor_t *sensor_data)
{
  /* USER CODE BEGIN EnvSensors_Read */
  sensor_data->outsideAirTemperature = gh_outsideTemperature[0];
  sensor_data->greenHouseAirTemperature = gh_insideTemperature[0];
  sensor_data->greenHouseHumidity = gh_insideHumidity[0];
  sensor_data->atmosphericPressure = gh_atmPressure[0];
  sensor_data->waterLevel = gh_waterTankLevel[0];
  sensor_data->dropDetection = gh_dropDetection[0];
  sensor_data->solarIrradiance = gh_solarIrradiance[0];
  //sensor_data->maxBeaufort = gh_windMaxBeaufort[0];
  sensor_data->windDirection = gh_windDirection[0];
  sensor_data->windSpeed = gh_windSpeed[0];
  sensor_data->avgRainDropRate = gh_averageRainDropRate[0];


  sensor_data->latitude  = (int32_t)((STSOP_LATTITUDE  * MAX_GPS_POS) / 90);
  sensor_data->longitude = (int32_t)((STSOP_LONGITUDE  * MAX_GPS_POS) / 180);

  return 0;
  /* USER CODE END EnvSensors_Read */
}

int32_t  EventSensors_Read(LoRaWAN_event_sensor_t *sensor_data)
{
  /* USER CODE BEGIN EventSensors_Read */
  sensor_data->subsonic_AI_similarity = gh_soilVibesAiSimilarity[0];

  return 0;
  /* USER CODE END EnvSensors_Read */
}

