/*************************** (C) COPYRIGHT 2017 CXHY ****************************
 * 文件名  ：ElectronicLock.c
 * 描  述  ：电子锁控制驱动程序
 * 版  本  ：V1.0
 * 作  者  ：XuZhongPing
 * 日  期  ：2017/05/06 
*********************************************************************************/
#include "ElectronicLock.h"
#include "RFID_IC.h"
#include "timer.h"

#define ELE_LOCK1_PeriphClock		RCC_APB2Periph_GPIOC
#define ELE_LOCK1_PORT					GPIOC
#define ELE_LOCK1_PIN						GPIO_Pin_2

#define ELE_LOCK2_PeriphClock		RCC_APB2Periph_GPIOC
#define ELE_LOCK2_PORT					GPIOC
#define ELE_LOCK2_PIN						GPIO_Pin_3

#define ELE_LOCK3_PeriphClock		RCC_APB2Periph_GPIOA
#define ELE_LOCK3_PORT					GPIOA
#define ELE_LOCK3_PIN						GPIO_Pin_0

#define ELE_LOCK4_PeriphClock		RCC_APB2Periph_GPIOA
#define ELE_LOCK4_PORT					GPIOA
#define ELE_LOCK4_PIN						GPIO_Pin_1

#define ELE_LOCK5_PeriphClock		RCC_APB2Periph_GPIOB
#define ELE_LOCK5_PORT					GPIOB
#define ELE_LOCK5_PIN						GPIO_Pin_13

#define ELE_LOCK6_PeriphClock		RCC_APB2Periph_GPIOB
#define ELE_LOCK6_PORT					GPIOB
#define ELE_LOCK6_PIN						GPIO_Pin_14


uint8_t lockStatus_1 = 0;
uint8_t lockStatus_2 = 0;
uint8_t lockStatus_3 = 0;
uint8_t lockStatus_4 = 0;
uint8_t lockStatus_5 = 0;
uint8_t lockStatus_6 = 0;

uint8_t isUnlockingMark = 0;

/*
***********************************************************************
*	函 数 名: ElectronicLock_Init
*	功能说明: 电子锁控制IO口初始化
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void ElectronicLock_Init(void)
{
 	GPIO_InitTypeDef  GPIO_InitStructure;
 	
 	RCC_APB2PeriphClockCmd(ELE_LOCK1_PeriphClock, ENABLE);	 					//使能端口时钟
	RCC_APB2PeriphClockCmd(ELE_LOCK2_PeriphClock, ENABLE);
	RCC_APB2PeriphClockCmd(ELE_LOCK3_PeriphClock, ENABLE);
	RCC_APB2PeriphClockCmd(ELE_LOCK4_PeriphClock, ENABLE);
	RCC_APB2PeriphClockCmd(ELE_LOCK5_PeriphClock, ENABLE);
	RCC_APB2PeriphClockCmd(ELE_LOCK6_PeriphClock, ENABLE);
	
	//第一路电磁锁IO口配置
 	GPIO_InitStructure.GPIO_Pin = ELE_LOCK1_PIN;							
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 							
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 							
 	GPIO_Init(ELE_LOCK1_PORT, &GPIO_InitStructure);					 					
	
 	GPIO_ResetBits(ELE_LOCK1_PORT, ELE_LOCK1_PIN);	 	

	//第二路电磁锁IO口配置
	GPIO_InitStructure.GPIO_Pin = ELE_LOCK2_PIN;							
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 							
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 							
 	GPIO_Init(ELE_LOCK2_PORT, &GPIO_InitStructure);	
	
	GPIO_ResetBits(ELE_LOCK2_PORT, ELE_LOCK2_PIN);
	
	//第三路电磁锁IO口配置
	GPIO_InitStructure.GPIO_Pin = ELE_LOCK3_PIN;							
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 							
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 							
 	GPIO_Init(ELE_LOCK3_PORT, &GPIO_InitStructure);	
	
	GPIO_ResetBits(ELE_LOCK3_PORT, ELE_LOCK3_PIN);
	
	//第四路电磁锁IO口配置
	GPIO_InitStructure.GPIO_Pin = ELE_LOCK4_PIN;							
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 							
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 							
 	GPIO_Init(ELE_LOCK4_PORT, &GPIO_InitStructure);	
	
	GPIO_ResetBits(ELE_LOCK4_PORT, ELE_LOCK4_PIN);
	
	//第五路电磁锁IO口配置
	GPIO_InitStructure.GPIO_Pin = ELE_LOCK5_PIN;							
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 							
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 							
 	GPIO_Init(ELE_LOCK5_PORT, &GPIO_InitStructure);	
	
	GPIO_ResetBits(ELE_LOCK5_PORT, ELE_LOCK5_PIN);
	
	//第六路电磁锁IO口配置
	GPIO_InitStructure.GPIO_Pin = ELE_LOCK6_PIN;							
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 							
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 							
 	GPIO_Init(ELE_LOCK6_PORT, &GPIO_InitStructure);	
	
	GPIO_ResetBits(ELE_LOCK6_PORT, ELE_LOCK6_PIN);
}

/*
***********************************************************************
*	函 数 名: Unlock
*	功能说明: 开锁
*	形    参：num -> 哪一路电子锁
*	返 回 值: 无
***********************************************************************
*/
void Unlock(uint8_t num)
{
	switch(num)
	{
		case 1:
			GPIO_SetBits(ELE_LOCK1_PORT, ELE_LOCK1_PIN);
			lockStatus_1 = 1;
			break;
			
		case 2:
			GPIO_SetBits(ELE_LOCK2_PORT, ELE_LOCK2_PIN);
			lockStatus_2 = 1;
			break;
			
		case 3:
			GPIO_SetBits(ELE_LOCK3_PORT, ELE_LOCK3_PIN);
			lockStatus_3 = 1;
			break;
			
		case 4:
			GPIO_SetBits(ELE_LOCK4_PORT, ELE_LOCK4_PIN);
			lockStatus_4 = 1;
			break;
			
		case 5:
			GPIO_SetBits(ELE_LOCK5_PORT, ELE_LOCK5_PIN);
			lockStatus_5 = 1;
			break;
			
		case 6:
			GPIO_SetBits(ELE_LOCK6_PORT, ELE_LOCK6_PIN);
			lockStatus_6 = 1;
			break;
			
		default:
			printf("\r\n没有此通道电磁锁！-> %d\r\n", num);
			break;
	}
}

/*
***********************************************************************
*	函 数 名: Locked
*	功能说明: 上锁
*	形    参：num -> 哪一路电子锁
*	返 回 值: 无
***********************************************************************
*/
void Locked(uint8_t num)
{
	switch(num)
	{
		case 1:
			GPIO_ResetBits(ELE_LOCK1_PORT, ELE_LOCK1_PIN);
			lockStatus_1 = 0;
			break;
			
		case 2:
			GPIO_ResetBits(ELE_LOCK2_PORT, ELE_LOCK2_PIN);
			lockStatus_2 = 0;
			break;
			
		case 3:
			GPIO_ResetBits(ELE_LOCK3_PORT, ELE_LOCK3_PIN);
			lockStatus_3 = 0;
			break;
			
		case 4:
			GPIO_ResetBits(ELE_LOCK4_PORT, ELE_LOCK4_PIN);
			lockStatus_4 = 0;
			break;
			
		case 5:
			GPIO_ResetBits(ELE_LOCK5_PORT, ELE_LOCK5_PIN);
			lockStatus_5 = 0;
			break;
			
		case 6:
			GPIO_ResetBits(ELE_LOCK6_PORT, ELE_LOCK6_PIN);
			lockStatus_6 = 0;
			break;
			
		default:
			printf("\r\n没有此通道电磁锁！-> %d\r\n", num);
			break;
	}
}

/*
***********************************************************************
*	函 数 名: ElectronicLockCtrlTask
*	功能说明: 电子锁控制任务
*	形    参：无
*	返 回 值: 无
***********************************************************************
*/
void ElectronicLockCtrlTask(void)
{
	static uint8_t eleLockNum = 1;
	static uint8_t EleLockStatus = 0x00;
	
	static timer waitingTimer;
	static uint8_t timer_ok = 0;
	
	if(timer_ok == 0)
	{
		timer_ok = 1;
		timer_set(&waitingTimer, 2);
	}
	
	switch(EleLockStatus)
	{
		case 0x00:
			if(timer_expired(&waitingTimer))
			{
				Unlock(eleLockNum);
				
				printf("\r\n开锁 -> %d\r\n", eleLockNum);
				timer_restart(&waitingTimer);
				EleLockStatus = 0x01;
			}	
			break;
			
		case 0x01:
			if(timer_expired(&waitingTimer))
			{
				Locked(eleLockNum);
				printf("\r\n上锁 -> %d\r\n", eleLockNum);
				
				eleLockNum++;
				if(eleLockNum >= 7)
				{
					eleLockNum = 1;
				}
				timer_restart(&waitingTimer);
				EleLockStatus = 0x00;
				break;
			}
			break;
			
		default:
			break;
		
	}
}


 
/******************** (C) COPYRIGHT 2017 CXHY *****END OF FILE*******************/ 
