/*
 * mock_tasks.c
 *
 *  Created on: Aug 23, 2024
 *      Author: jakubski
 */


#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

#include "mock_tasks.h"
#include "measurements.h"
#include "node-red-config.h"
#include "stdlib.h"

//extern RESMeasurements resMeasurements;
//extern SesnorsInfo sensorsInfo;
extern osMutexId_t resMeasurementsMutex;
extern osMutexId_t sensorsInfoMutex;

#ifdef USER_MOCKS
extern RNG_HandleTypeDef hrng;
float rndflt (float rng) {
    uint32_t rndVal = 0;
    HAL_RNG_GenerateRandomNumber (&hrng, &rndVal);
    return ((float)rndVal / (float)(RAND_MAX)) * rng;
}
#else
float rndflt (float rng)
{
	(void)rng;
	return 0.0;
}
#endif

void MockMeasurmetsTaskInit(void) {
	osThreadAttr_t osThreadAttrMockMeasTask = { 0 };
	osThreadAttrMockMeasTask.name       = "os_thread_mock_measurmets";
	osThreadAttrMockMeasTask.stack_size = configMINIMAL_STACK_SIZE * 2;
	osThreadAttrMockMeasTask.priority   = (osPriority_t)osPriorityNormal;
	osThreadNew (MockMeasurmetsTask, NULL, &osThreadAttrMockMeasTask);

}

void MockMeasurmetsTask (void* argument) {
	uint16_t counter = 0;
    while (pdTRUE) {
    	osMutexAcquire (resMeasurementsMutex, osWaitForever);
		resMeasurements.voltagePeak[0] = 60 + (0.01 * (counter % 100));
		resMeasurements.voltagePeak[1] = 61 + (0.01 * (counter % 100));
		resMeasurements.voltagePeak[2] = 62 + (0.01 * (counter % 100));
		resMeasurements.voltageRMS[0] = 46 + (0.01 * (counter % 100));
		resMeasurements.voltageRMS[1] = 47 + (0.01 * (counter % 100));
		resMeasurements.voltageRMS[2] = 48 + (0.01 * (counter % 100));
		resMeasurements.currentPeak[0] = 3 + (0.01 * (counter % 100));
		resMeasurements.currentPeak[1] = 4 + (0.01 * (counter % 100));
		resMeasurements.currentPeak[2] = 5 + (0.01 * (counter % 100));
		resMeasurements.currentRMS[0] = 1 + (0.01 * (counter % 100));
		resMeasurements.currentRMS[1] = 2 + (0.01 * (counter % 100));
		resMeasurements.currentRMS[2] = 3 + (0.01 * (counter % 100));
		resMeasurements.power[0] = resMeasurements.voltagePeak[0] * resMeasurements.currentRMS[0];
		resMeasurements.power[1] = resMeasurements.voltagePeak[1] * resMeasurements.currentRMS[1];
		resMeasurements.power[2] = resMeasurements.voltagePeak[2] * resMeasurements.currentRMS[2];
    	osMutexRelease(resMeasurementsMutex);

    	osMutexAcquire (sensorsInfoMutex, osWaitForever);
		sensorsInfo.pvTemperature[0] = 50 + (0.01 * (counter % 100));
		sensorsInfo.pvTemperature[1] = 51 + (0.01 * (counter % 100));
		sensorsInfo.fanVoltage = 12 + (0.01 * (counter % 100));
		sensorsInfo.pvEncoderX = 15 + (0.01 * (counter % 100));
		sensorsInfo.pvEncoderY = 10 + (0.01 * (counter % 100));
		sensorsInfo.motorXStatus = (counter % 100) > 50 ? 1 : 0;
		sensorsInfo.motorYStatus = (counter % 100) > 75 ? 1 : 0;
		sensorsInfo.motorXAveCurrent = 3 + (0.01 * (counter % 100));
		sensorsInfo.motorYAveCurrent = 3 + (0.01 * (counter % 100));
		sensorsInfo.motorXPeakCurrent = 6 + (0.01 * (counter % 100));
		sensorsInfo.motorYPeakCurrent = 6 + (0.01 * (counter % 100));
		sensorsInfo.limitXSwitchUp = (counter % 100) > 50 ? 1 : 0;
		sensorsInfo.limitXSwitchDown = (counter % 100) < 25 ? 1 : 0;
		sensorsInfo.limitXSwitchCenter = (counter % 100) > 35 ? 1 : 0;
		sensorsInfo.limitYSwitchUp = (counter % 100) > 50 ? 1 : 0;
		sensorsInfo.limitYSwitchDown = (counter % 100) < 25 ? 1 : 0;
		sensorsInfo.limitYSwitchCenter = (counter % 100) > 35 ? 1 : 0;
		sensorsInfo.powerSupplyFailMask = 0;
    	osMutexRelease(sensorsInfoMutex);
    	counter++;
        osDelay (pdMS_TO_TICKS (1000));
    }
}

