/**
* @file PWR_BTN_ATY.c
*
* @param Project DEVICE_GENERAL_ATY_LIB
*
* @author ATY
*
* @copyright
* - Copyright 2017 - 2026 MZ-ATY
* - This code follows:
* - MZ-ATY Various Contents Joint Statement -
*
* https://mengze.top/MZ-ATY_VCJS
* - CC 4.0 BY-NC-SA -
*
* https://creativecommons.org/licenses/by-nc-sa/4.0/
* - Your use will be deemed to have accepted the terms of this statement.
*
* @brief functions of power button for C platform
*
* @version
* - 1_01_251118 > ATY
* -# Preliminary version, first Release
********************************************************************************
*/
#ifndef __PWR_BTN_ATY_C
#define __PWR_BTN_ATY_C
#include "PWR_BTN_ATY.h"
#define PWR_BTN_ATY_TAG "\r\n[PWR_BTN_ATY] "
/******************************* For user *************************************/
/******************************************************************************/
/**
* @brief turn on power in soft
*
* @param dev device
* @return uint8_t
* @note ioSet pull down, output low when start
* ioGet pull up
*/
uint8_t PWR_BTN_On(struct PWR_BTN_ATY_Dev* dev){
uint16_t i = 0;
__ATY_LOCK(dev);
// make little delay before ioSet, 0.5ms at least after main start
while(i < 3000) i++;
dev->ioSet(__ATY_HL_H);
printf_ATY_D("%sPWR_BTN_On.", PWR_BTN_ATY_TAG);
__ATY_UNLOCK(dev);
return 0;
}
/**
* @brief turn off power in soft
*
* @param dev device
* @return uint8_t
*/
uint8_t PWR_BTN_Off(struct PWR_BTN_ATY_Dev* dev){
__ATY_LOCK(dev);
dev->ioSet(__ATY_HL_L);
printf_ATY_D("%sPWR_BTN_Off.", PWR_BTN_ATY_TAG);
if(dev->powerOffCallback != NULL)
dev->powerOffCallback();
__ATY_UNLOCK(dev);
return 0;
}
/**
* @brief process power button status with IO IT type
*
* @param dev device
* @return uint8_t
* @note put this function at timer IT callback
* button long press time count depend on cycle time
*/
uint8_t PWR_BTN_Status_withIT(struct PWR_BTN_ATY_Dev* dev){
__ATY_LOCK(dev);
if(dev->PWR_BTN_LongPressCount != 0){
dev->PWR_BTN_LongPressCount++;
}
if(dev->PWR_BTN_LongPressCount > dev->PWR_BTN_LongPressCountMax){
dev->PWR_BTN_LongPressCount = 0;
if(dev->ioGet() == __ATY_HL_L){
__ATY_UNLOCK(dev);
PWR_BTN_Off(dev);
__ATY_LOCK(dev);
}
}
__ATY_UNLOCK(dev);
return 0;
}
uint8_t __PWR_BTN_INIT_Flag = 1;
/**
* @brief process power button status with IO cycle read type
*
* @param dev device
* @return uint8_t
* @note put this function at timer IT callback
* button long press time count depend on cycle time
*/
uint8_t PWR_BTN_Status_withCycle(struct PWR_BTN_ATY_Dev* dev){
__ATY_LOCK(dev);
if(dev->ioGet() == __ATY_HL_L){
if(__PWR_BTN_INIT_Flag == 0){
dev->PWR_BTN_LongPressCount++;
if(dev->PWR_BTN_LongPressCount > dev->PWR_BTN_LongPressCountMax){
dev->PWR_BTN_LongPressCount = 0;
__ATY_UNLOCK(dev);
PWR_BTN_Off(dev);
__ATY_LOCK(dev);
}
}
}
else{
dev->PWR_BTN_LongPressCount = 0;
__PWR_BTN_INIT_Flag = 0;
}
__ATY_UNLOCK(dev);
return 0;
}
#endif /* __PWR_BTN_ATY_C */
/************************************ etc *************************************/
/* init */
// Power Button ----------------------------------------------------------------
// #include "PWR_BTN_ATY.h"
// void PWR_BTN_1_IO_SET(uint8_t level){
// if(level)
// HAL_GPIO_WritePin(SYS_PWR_GPIO_Port, SYS_PWR_Pin, GPIO_PIN_SET);
// else
// HAL_GPIO_WritePin(SYS_PWR_GPIO_Port, SYS_PWR_Pin, GPIO_PIN_RESET);
// }
// uint8_t PWR_BTN_1_IO_GET(void){
// if(HAL_GPIO_ReadPin(SYS_OFF_GPIO_Port, SYS_OFF_Pin) == GPIO_PIN_SET)
// return 1;
// else
// return 0;
// }
// void PWR_BTN_1_powerOffCallback(void){
// // turn off board
// LED_ATY_t_1.ledBlinkType = 0x00;
// }
// struct PWR_BTN_ATY_Dev PWR_BTN_ATY_t_1 = {
// .PWR_BTN_LongPressCountMax = 20,
// .PWR_BTN_LongPressCount = 0,
// .ioSet = PWR_BTN_1_IO_SET,
// .ioGet = PWR_BTN_1_IO_GET,
// .powerOffCallback = PWR_BTN_1_powerOffCallback,
// .lock = __ATY_UNLOCKED
// };
// // if use IT type
// void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
// if(GPIO_Pin == SYS_OFF_Pin){
// // raise and fall
// if(HAL_GPIO_ReadPin(SYS_OFF_GPIO_Port, SYS_OFF_Pin) == GPIO_PIN_SET)
// PWR_BTN_ATY_t_1.PWR_BTN_LongPressCount = 0;
// else
// PWR_BTN_ATY_t_1.PWR_BTN_LongPressCount = 1;
// }
// }
/* use */
// PWR_BTN_Status_withIT(&PWR_BTN_ATY_t_1); // put at 1ms cycle
// // or
// PWR_BTN_Status_withCycle(&PWR_BTN_ATY_t_1); // put at 1ms cycle
/******************************************************************************/
/******************************** End Of File *********************************/