RefStateType REF_GetCalibData(void){ FRTOS1_taskENTER_CRITICAL(); SensorCalibMinMax = *(SensorCalibT*)NVMC_GetReflectanceData(); if ((void*)&SensorCalibMinMax == NULL){ SHELL_SendString("No calibration data found"); FRTOS1_taskEXIT_CRITICAL(); return (RefStateType) REF_STATE_NOT_CALIBRATED; } else{ SHELL_SendString("Calibration data loaded"); FRTOS1_taskEXIT_CRITICAL(); return (RefStateType) REF_STATE_READY; } return (RefStateType) REF_STATE_INIT; }
static void REF_StateMachine(void) { int i; switch (refState) { case REF_STATE_INIT: #if PL_HAS_CONFIG_NVM /* If Calibration Data are saved, read it */ for (i = 0; i<REF_NOF_SENSORS;i++){ SensorCalibMinMax.minVal[i] = *((uint16_t*)NVMC_GetReflectanceData()+i); SensorCalibMinMax.minVal[i+REF_NOF_SENSORS] = *((uint16_t*)NVMC_GetReflectanceData()+(i+REF_NOF_SENSORS)); } SHELL_SendString((unsigned char*)"INFO: Loaded calibration data from FLASH...Gordon.\r\n"); refState = REF_STATE_READY; #else SHELL_SendString((unsigned char*)"INFO: No calibration data present.\r\n"); refState = REF_STATE_NOT_CALIBRATED; break; #endif case REF_STATE_NOT_CALIBRATED: REF_MeasureRaw(SensorRaw); if (EVNT_EventIsSet(EVNT_REF_START_STOP_CALIBRATION)) { EVNT_ClearEvent(EVNT_REF_START_STOP_CALIBRATION); refState = REF_STATE_START_CALIBRATION; break; } break; case REF_STATE_START_CALIBRATION: SHELL_SendString((unsigned char*)"start calibration...\r\n"); for(i=0;i<REF_NOF_SENSORS;i++) { SensorCalibMinMax.minVal[i] = MAX_SENSOR_VALUE; SensorCalibMinMax.maxVal[i] = 0; SensorCalibrated[i] = 0; } refState = REF_STATE_CALIBRATING; break; case REF_STATE_CALIBRATING: REF_CalibrateMinMax(SensorCalibMinMax.minVal, SensorCalibMinMax.maxVal, SensorRaw); if (EVNT_EventIsSet(EVNT_REF_START_STOP_CALIBRATION)) { EVNT_ClearEvent(EVNT_REF_START_STOP_CALIBRATION); refState = REF_STATE_STOP_CALIBRATION; } break; case REF_STATE_STOP_CALIBRATION: /* Save Calibration Data */ NVMC_SaveReflectanceData(&SensorCalibMinMax, sizeof(SensorCalibMinMax)); SHELL_SendString((unsigned char*)"...stopping calibration. Files written to Flash\r\n"); refState = REF_STATE_READY; break; case REF_STATE_READY: REF_Measure(); if (EVNT_EventIsSet(EVNT_REF_START_STOP_CALIBRATION)) { EVNT_ClearEvent(EVNT_REF_START_STOP_CALIBRATION); refState = REF_STATE_START_CALIBRATION; } break; } /* switch */ }
static void REF_StateMachine(void) { int i; switch (refState) { case REF_STATE_INIT: #if PL_CONFIG_EST { SensorCalibT *ptr; ptr = (SensorCalibT*)NVMC_GetReflectanceData(); if (ptr!=NULL) { /* valid data */ SensorCalibMinMax = *ptr; refState = REF_STATE_READY; } else { refState = REF_STATE_NOT_CALIBRATED; } } #else SHELL_SendString((unsigned char*)"INFO: No calibration data present.\r\n"); refState = REF_STATE_NOT_CALIBRATED; #endif break; case REF_STATE_NOT_CALIBRATED: REF_MeasureRaw(SensorRaw); /*! \todo Add a new event to your event module...*/ #if REF_START_STOP_CALIB if (FRTOS1_xSemaphoreTake(REF_StartStopSem, 0)==pdTRUE) { refState = REF_STATE_START_CALIBRATION; } #endif break; case REF_STATE_START_CALIBRATION: SHELL_SendString((unsigned char*)"start calibration...\r\n"); for(i=0;i<REF_NOF_SENSORS;i++) { SensorCalibMinMax.minVal[i] = MAX_SENSOR_VALUE; SensorCalibMinMax.maxVal[i] = 0; SensorCalibrated[i] = 0; } refState = REF_STATE_CALIBRATING; break; case REF_STATE_CALIBRATING: REF_CalibrateMinMax(SensorCalibMinMax.minVal, SensorCalibMinMax.maxVal, SensorRaw); #if PL_CONFIG_HAS_BUZZER startBuzzer(Middle,200); #endif #if REF_START_STOP_CALIB if (FRTOS1_xSemaphoreTake(REF_StartStopSem, 0)==pdTRUE) { refState = REF_STATE_STOP_CALIBRATION; } #endif break; case REF_STATE_STOP_CALIBRATION: SHELL_SendString((unsigned char*)"...stopping calibration.\r\n"); #if PL_CONFIG_EST if (NVMC_SaveReflectanceData(&SensorCalibMinMax, sizeof(SensorCalibMinMax))!=ERR_OK) { SHELL_SendString((unsigned char*)"Flashing calibration data FAILED!\r\n"); } else { SHELL_SendString((unsigned char*)"Stored calibration data.\r\n"); } #endif refState = REF_STATE_READY; break; case REF_STATE_READY: REF_Measure(); #if REF_START_STOP_CALIB if (FRTOS1_xSemaphoreTake(REF_StartStopSem, 0)==pdTRUE) { refState = REF_STATE_START_CALIBRATION; } #endif break; } /* switch */ }