Программный модуль для утюга RI-C250S


ПО является неотъемлемой частью утюга RI-C250S, отдельно потребителю не поставляется и эксплуатируется только в составе устройства.

Фрагмент исходного кода

////////////////////////////////////////////////////////////////

//RI-C250S

//v1.9

//state_mashine.c

////////////////////////////////////////////////////////////////

/*==============================================================
                          include
==============================================================*/


#include "state_mashine.h"
#include "test_service.h"
#include "child_protection.h"
/********* DEFINES **************************************************************/

/********* MACROS ***************************************************************/

/********* GLOBAL VARIABLES *****************************************************/
bool orientation_blocked_flag = false;
StatusHandlerTypeDef		statusHandler;
extern bool 				orientationChangedFlag;
extern bool 				startupFlag;
extern bool					childProtectionOnFlag;
extern bool					pairingStartFlag;
/* timers */
extern app_timer_id_t		pairing_start_timer_id;
extern app_timer_id_t		unpairing_start_timer_id;
extern app_timer_id_t		overheat_protection_timer_id;
extern app_timer_id_t		indication_timer_id;
extern app_timer_id_t		unpairing_check_timer_id;
extern app_timer_id_t		clear_safe_mode_timer_id;
/********* LOCAL VARIABLES ******************************************************/
/*static*/ uint8_t s1 = 0;
/*static*/ uint8_t s2 = 0;
/*static*/ uint8_t orientation = 4;
/*static*/ uint8_t or1, or2, or3;
uint8_t debug;

/*static*/ bool 				unpairingBlockingFlag = false;
static uint8_t 				indState = 0;
static uint8_t 				unpairingData = 1;
static uint8_t 				orientationCheckingStage;
/*static*/ uint8_t 				safeModeIndicationState = 0;
extern app_timer_id_t		safeModeIndicationTimerId;
	uint8_t ledState;
/********* FUNCTION PROTOTYPES **************************************************/

/********* FUNCTIONS IMPLEMENTATION *********************************************/

/********* FUNCTION PROTOTYPES **************************************************/
static void  change_orientation(OrientationTypeDef orient);
/********* FUNCTIONS IMPLEMENTATION *********************************************/
void lock_orientation(void)
{
    orientation_blocked_flag = true;
}

void unlock_orientation(void)
{
    orientation_blocked_flag = false;
}
void orientation_service(void)
{
APP_TIMER_TICKS(OVERHEATING_HORIZONTAL_PROTECTION_TIMER_MS,0), NULL);

	if( !orientationChangedFlag ) return;
	
	
	switch ( orientation ) 
	{
		case 0:
			
			change_orientation(HORIZONTAL);
		
		break;
				
		case 1:	
			
			change_orientation(VERTICAL_UP);
		
		break;
			
		case 2:
			
			change_orientation(VERTICAL_DOWN);		
	
		break;
		
		case 3:
			
			change_orientation(THIRD_STATE);
		
		break;
		
		default:
			break;
		
	}				/* -----  end switch  ----- */		
	
	state_maschine();
}

void state_maschine(void)
{
	if( !orientationChangedFlag)return;
	
	app_timer_stop(pairing_start_timer_id);
	app_timer_stop(overheat_protection_timer_id);
	app_timer_stop(indication_timer_id);
	app_timer_stop(unpairing_start_timer_id);
	app_timer_stop(unpairing_check_timer_id);
	app_timer_stop(clear_safe_mode_timer_id);
	unpairingBlockingFlag = true;
	
	if( startupFlag != true	)
	{
		if(childProtectionOnFlag != true)
		{
			led_off(); 
			buz_off();
		}
	}
	else
	{
		led_on();
		buz_on();
		app_timer_start(unpairing_start_timer_id, APP_TIMER_TICKS(500,0), NULL);	// 
		startupFlag = false;
		
		if( is_child_prot_flag_set() != 0 )
		{	
			clear_child_protect();
			
			childProtectionOnFlag = false;
		}
		
		if( is_child_prot_list_empty() != 0)
		{
			childProtectionOnFlag = true;
			
			nrf_gpio_pin_set(RELAY_PIN);
		}		
	}
	
	switch (statusHandler.currOrient)
	{    

		case VERTICAL_DOWN:
			
			app_timer_start(pairing_start_timer_id, APP_TIMER_TICKS(PAIRING_START_TIMER_MS,0),NULL);	
		
			app_timer_start(overheat_protection_timer_id, APP_TIMER_TICKS(OVERHEATING_VERTICAL_DOWN_PROTECTION_TIMER_MS,0),NULL);	
		
		break;
		
		case THIRD_STATE:
			
			if(!unpairingBlockingFlag)return;
		
			app_timer_start(unpairing_check_timer_id, APP_TIMER_TICKS(UNPAIRING_DURATION_TIMER_MS, 0), NULL);
		
			app_timer_start(clear_safe_mode_timer_id, APP_TIMER_TICKS(CLEAR_SAFE_MODE_TIMER_MS, 0), NULL);
		
			unpairingBlockingFlag = false;
			
			nrf_gpio_pin_set(RELAY_PIN);
		
			statusHandler.state = OFF;
		
		break;
		
		case HORIZONTAL:
			
			heater_on();
		
			app_timer_start(overheat_protection_timer_id, APP_TIMER_TICKS(OVERHEATING_HORIZONTAL_PROTECTION_TIMER_MS,0), NULL);
		
		break;
        
		case VERTICAL_UP:	
			
			heater_on();
		
			app_timer_start(overheat_protection_timer_id, APP_TIMER_TICKS(OVERHEATING_VERTICAL_UP_PROTECTION_TIMER_MS,0), NULL);
		
			app_timer_stop(pairing_start_timer_id);
		
		break;
	
	}	
	
	orientationChangedFlag = false;	
}

void clear_safe_mode(void)
{
	
	app_timer_stop(safeModeIndicationTimerId);
	
	led_off();
	
	safeModeIndicationState = 0;
	
	buz_on();
	
	led_on();
	
	app_timer_start(unpairing_start_timer_id, APP_TIMER_TICKS(500,0), NULL);	// 
	
	set_child_protect_clear_flag();
	
	app_timer_start(indication_timer_id, APP_TIMER_TICKS(INDICATION_TIMER_MS,0), NULL);
}

static void change_orientation(OrientationTypeDef orient)
{
	statusHandler.prevOrient = statusHandler.currOrient;
	
	statusHandler.currOrient = orient;
}

void heater_on(void)
{
	if( childProtectionOnFlag )return;
	
	statusHandler.state = ON;
//	lock_orientation();
	nrf_gpio_pin_clear(RELAY_PIN);	
//    unlock_orientation();
}

void heater_off(void)
{
	if(statusHandler.state != ON ) return;
	lock_orientation();
	
	app_timer_stop(overheat_protection_timer_id);
	
	statusHandler.state = OFF;
	nrf_gpio_pin_set(RELAY_PIN);
	
	if( childProtectionOnFlag != true )
	{
		buz_on();		
		
		app_timer_start(unpairing_start_timer_id, APP_TIMER_TICKS(UNPAIRING_START_TIMER_MS,0), NULL);	
		
		app_timer_start(indication_timer_id, APP_TIMER_TICKS(INDICATION_TIMER_MS,0), NULL);
	}
    else
    {
        unlock_orientation();
    }
    
}

void indication(void *p_context)
{
	switch(indState)
	{
		case 0:
			led_on();
			++indState;
			break;
		
		case 1:
			led_off();
			++indState;
		break;
		
	}
	
	if( indState > 1 )
	{
		indState = 0;
	}
}

uint8_t check_orientation(void *p_context)
{
	uint8_t s1 = 1;
	uint8_t s2 = 1;
	uint16_t timeout = 40;	
	static uint8_t orient;
	static uint8_t buffer;
	if( orientation_blocked_flag )return orient;
	if( startupFlag == true)
	{
		buffer = 0xFF;
	}
	else
	{
		buffer = orient;
	}
	
	while(timeout-- > 0)
	{
		if( pin_get_with_timeout(S1_PIN) == 0 )
		{
			s1 = 0;
		}
		
		if( pin_get_with_timeout(S2_PIN) == 0 )
		{
			s2 = 0;
		}
		nrf_delay_ms(1);
	}
	
	orient = (s1 << 1) | (s2);
	
	if( (orient != buffer 					 &&
		 orient != statusHandler.prevOrient) ||
		(orient != statusHandler.currOrient))
	{
		orientationChangedFlag = true;
		orientation = orient;
	}
	
}

uint8_t pin_get_with_timeout(uint8_t pinNumber)
{
	uint16_t timeout = 50;
	uint8_t buffer = 0xAA;
	
	while(timeout-- > 0)
	{
		buffer <<= 1;
		buffer |= (nrf_gpio_pin_read(pinNumber) ? 0x01 : 0x00);
		
		if( buffer == 0xFF)
		{
			return 1;
		}
		
		if( buffer == 0x00 )
		{
			return 0;
		}
		
		nrf_delay_us(1);
	}
	
	return 0;
	
}

void safety_mode_indication_service(void)
{

	
	if( childProtectionOnFlag 	!= true 	||
		pairingStartFlag	 	== true	)
		{
			app_timer_stop(safeModeIndicationTimerId);
//			led_off();
			safeModeIndicationState = 0;
			return;			
		}
	
	switch( safeModeIndicationState)
	{
		case 0:
			app_timer_stop(indication_timer_id);
			app_timer_stop(overheat_protection_timer_id);
			led_on();
			ledState = 0;
			app_timer_start(safeModeIndicationTimerId, APP_TIMER_TICKS(300,0), &ledState);				

		break;
		
		case 1:
			ledState = 1;
			app_timer_start(safeModeIndicationTimerId, APP_TIMER_TICKS(300,0), &ledState);			
		break;
		
		case 2:
			ledState = 2;
			app_timer_start(safeModeIndicationTimerId, APP_TIMER_TICKS(300,0), &ledState);				
		break;
		
		case 3:
			ledState = 3;
			app_timer_start(safeModeIndicationTimerId, APP_TIMER_TICKS(300,0), &ledState);				
		break;
		
		case 4:
			ledState = 4;
			app_timer_start(safeModeIndicationTimerId, APP_TIMER_TICKS(300,0), &ledState);				
		break;			
		
		
	}
}

void safe_mode_blink(void *p_context)
{
	switch(*(uint8_t *)p_context)
	{
		case 0:
			led_off();
			safeModeIndicationState = 1;				
		break;
		
		case 1:
			led_on();
			safeModeIndicationState = 2;
		break;
		
		case 2:
			led_off();
			safeModeIndicationState = 3;		
		break;
		
		case 3:
			led_on();
			safeModeIndicationState = 4;		
		break;

		case 4:
			led_off();
			app_timer_start(safeModeIndicationTimerId, APP_TIMER_TICKS(3000, 0), (uint8_t *)1);
			safeModeIndicationState = 1;			
		break;
	}
}

Made on
Tilda