# include <stdio.h>
# include <stdlib.h>
# include <math.h>
# include <stdbool.h>
# define LOW_COOK_WINDOW_SIZE 20
# define LOW_COOK_SLOPE_THRESHOLD 0.12f
# define LOW_COOK_SAMPLE_INTERVAL 10
# define LOW_COOK_DEFAULT_TIME_DIFFERENCE 10.0f
# define MIN_TIME_DIFFERENCE 1.0f
# define MAX_TIME_DIFFERENCE ( LOW_COOK_WINDOW_SIZE - 1 )
typedef struct
{ float slope; float data[ LOW_COOK_WINDOW_SIZE] ; unsigned char count; bool slope_event; unsigned char tim_count; unsigned char state; unsigned char slope_status;
} low_cook_heating_system_t ;
low_cook_heating_system_t low_cooksystem;
void low_cook_init_heating_system ( low_cook_heating_system_t * system)
{ system-> slope = 0.0f ; system-> tim_count = 0 ; system-> state = 0 ; system-> count = 0 ; system-> slope_event = false; system-> slope_status = 1 ; for ( unsigned char i = 0 ; i < LOW_COOK_WINDOW_SIZE; i++ ) { system-> data[ i] = 0.0f ; }
}
void low_cook_add_sample ( low_cook_heating_system_t * system, float temperature)
{ if ( ++ system-> tim_count < LOW_COOK_SAMPLE_INTERVAL) { return ; } system-> tim_count = 0 ; if ( system-> count < LOW_COOK_WINDOW_SIZE) { system-> data[ system-> count++ ] = temperature; } else { for ( unsigned char i = 1 ; i < LOW_COOK_WINDOW_SIZE; i++ ) { system-> data[ i - 1 ] = system-> data[ i] ; } system-> data[ LOW_COOK_WINDOW_SIZE - 1 ] = temperature; }
}
float low_cook_calculate_slope ( low_cook_heating_system_t * system, float time_difference)
{ if ( time_difference < MIN_TIME_DIFFERENCE || time_difference > MAX_TIME_DIFFERENCE) { system-> slope_status = 2 ; return 0.0f ; } unsigned char interval_samples = ( unsigned char ) ( time_difference) ; if ( system-> count < LOW_COOK_WINDOW_SIZE || interval_samples >= system-> count) { system-> slope_status = 1 ; return 0.0f ; } float current = system-> data[ system-> count - 1 ] ; float previous = system-> data[ system-> count - 1 - interval_samples] ; system-> slope_status = 0 ; return ( current - previous) / time_difference;
} void low_cook_update_slope ( float time_difference)
{ low_cook_add_sample ( & low_cooksystem, stheat. temperature) ; low_cooksystem. slope = low_cook_calculate_slope ( & low_cooksystem, time_difference) ; if ( ( low_cooksystem. slope_status == 0 ) && ( stheat. temperature > 35 ) ) { low_cooksystem. slope_event = ( fabsf ( low_cooksystem. slope) < LOW_COOK_SLOPE_THRESHOLD) ; }
}