/*
 * 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"

extern RESMeasurements resMeasurements[SLAVES_COUNT];
extern SesnorsInfo sensorsInfo[SLAVES_COUNT];
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 };
//	osThreadId_t osThreadMockMeasurmetsTaskId;
	osThreadAttrMockMeasTask.name       = "os_thread_mock_measurmets";
	osThreadAttrMockMeasTask.stack_size = configMINIMAL_STACK_SIZE * 2;
	osThreadAttrMockMeasTask.priority   = (osPriority_t)osPriorityNormal;
//	osThreadMockMeasurmetsTaskId = osThreadNew (MockMeasurmetsTask, NULL, &osThreadAttrMockMeasTask);
	osThreadNew (MockMeasurmetsTask, NULL, &osThreadAttrMockMeasTask);

}

void MockMeasurmetsTask (void* argument) {
	uint8_t boardNumber = 0;
	uint16_t counter = 0;
    while (pdTRUE) {
		for( boardNumber = 0; boardNumber < SLAVES_COUNT; boardNumber++)
		{
			osMutexAcquire (resMeasurementsMutex, osWaitForever);
			RESMeasurements *resMeas = &resMeasurements[boardNumber];
			resMeas->voltagePeak[0] = 60 + (0.01 * (counter % 100));
			resMeas->voltagePeak[1] = 61 + (0.01 * (counter % 100));
			resMeas->voltagePeak[2] = 62 + (0.01 * (counter % 100));
			resMeas->voltageRMS[0] = 46 + (0.01 * (counter % 100));
			resMeas->voltageRMS[1] = 47 + (0.01 * (counter % 100));
			resMeas->voltageRMS[2] = 48 + (0.01 * (counter % 100));
			resMeas->currentPeak[0] = 3 + (0.01 * (counter % 100));
			resMeas->currentPeak[1] = 4 + (0.01 * (counter % 100));
			resMeas->currentPeak[2] = 5 + (0.01 * (counter % 100));
			resMeas->currentRMS[0] = 1 + (0.01 * (counter % 100));
			resMeas->currentRMS[1] = 2 + (0.01 * (counter % 100));
			resMeas->currentRMS[2] = 3 + (0.01 * (counter % 100));
			resMeas->power[0] = resMeas->voltagePeak[0] * resMeas->currentRMS[0];
			resMeas->power[1] = resMeas->voltagePeak[1] * resMeas->currentRMS[1];
			resMeas->power[2] = resMeas->voltagePeak[2] * resMeas->currentRMS[2];
			osMutexRelease(resMeasurementsMutex);

			osMutexAcquire (sensorsInfoMutex, osWaitForever);
			SesnorsInfo* sensors = &sensorsInfo[boardNumber];
			sensors->pvTemperature[0] = 50 + (0.01 * (counter % 100));
			sensors->pvTemperature[1] = 51 + (0.01 * (counter % 100));
			sensors->fanVoltage = 12 + (0.01 * (counter % 100));
			sensors->pvEncoder = 15 + (0.01 * (counter % 100));
			sensors->motorXStatus = (counter % 100) > 50 ? 1 : 0;
			sensors->motorYStatus = (counter % 100) > 75 ? 1 : 0;
			sensors->motorXAveCurrent = 3 + (0.01 * (counter % 100));
			sensors->motorYAveCurrent = 3 + (0.01 * (counter % 100));
			sensors->motorXPeakCurrent = 6 + (0.01 * (counter % 100));
			sensors->motorYPeakCurrent = 6 + (0.01 * (counter % 100));
			sensors->limitSwitchUp = (counter % 100) > 50 ? 1 : 0;
			sensors->limitSwitchDown = (counter % 100) < 25 ? 1 : 0;
			sensors->limitSwitchCenter = (counter % 100) > 35 ? 1 : 0;
			sensors->powerSupplyFailMask = 0;
			osMutexRelease(sensorsInfoMutex);
		}
    	counter++;
        osDelay (pdMS_TO_TICKS (1000));
    }
}

