-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmy_key.c
138 lines (126 loc) · 3.37 KB
/
my_key.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "my_key.h"
#include "my_led.h"
#include "my_motor.h"
my_key_t my_key =
{
.key_mode = 0,
.iskeypre_f = false,
.key_Pre_cnt = 0,
.key_mode_change_f = false,
};
extern volatile uint32_t systick;//上电工作时间
uint8_t charger_flag = NO_CHARGE; // 0-no charger; 1-charging; 2-charge full
//APP按键事件回调函数
void button_event_handler(uint8_t pin_no, uint8_t button_action)
{
switch (pin_no)//判断键值
{
case MY_BUTTON_1://是BUTTON_1按键
if(systick < 6)//刚上电,不允许开机
return;
if(work_flag == WORK_FLAG_CHARING)//充电不允许开机
{
return;
}
if(my_motor.is_renmender)//振动提醒期间屏蔽按键
return;
if(button_action == APP_BUTTON_PUSH)//按键按下事件
{
my_key.iskeypre_f = true;
if(work_flag == WORK_FLAG_TSET)//工作在测试模式,再按退出
{
work_flag = WORK_FLAG_POWER_OFF;
return;
}
if(my_ble.is_ble_con)
{
my_ble.is_ble_con = false;
my_key.key_mode--;
}
if(working_sec <= 5)//5s以内按键切换模式
{
my_key.key_mode_change_f = true;
working_sec = 0; //从零开始计算工作时间
my_key.key_mode++;
if(my_key.key_mode > LED_NUM)
my_key.key_mode = 1;
my_motor.mode = my_key.key_mode;
//m_motor_mode = key_mode;
}
else
{
working_sec = 0; //从零开始计算工作时间
my_key.key_mode--;
work_flag = WORK_FLAG_POWER_OFF;
return;
}
work_flag = WORK_FLAG_WORKING;
}
if(button_action == APP_BUTTON_RELEASE)//按键释放事件
{
my_key.iskeypre_f = false;
my_key.key_Pre_cnt = 0;
}
break;
case CHG_IN_PIN_NO://插入充电器,电池开始充电
if(button_action == APP_BUTTON_PUSH)//按键按下事件
{
if(work_flag == WORK_FLAG_WORKING)
{//检测到充电暂停工作
my_key.key_mode--;
Led_all_Off();
m_motor_stop();
}
work_flag = WORK_FLAG_CHARING;
charge_flag = CHARGING;
Led_softblink_on();
NRF_LOG_INFO("Charging.\r\n");
}
if(button_action == APP_BUTTON_RELEASE)//充电器拔下或充满
{
//if(STB_IN_RD)
{
charge_flag = NO_CHARGE;
work_flag = WORK_FLAG_POWER_OFF;
Led_softblink_off();
NRF_LOG_INFO("Charge is not\r\n");
}
}
break;
/*case STB_IN_PIN_NO://电池电量充满
if(button_action == APP_BUTTON_PUSH)//按键按下事件
{
work_flag = WORK_FLAG_CHARING;
//LED_POWER_ON;
LED_MODE_OFF;
NRF_LOG_INFO("Charge is over.\r\n");
}
if(button_action == APP_BUTTON_RELEASE)//按键释放事件
{
work_flag = WORK_FLAG_POWER_OFF;
LED_POWER_OFF;
NRF_LOG_INFO("Charge is not\r\n");
}*/
// break;
default:
return;
}
}
void buttons_init(void)
{
ret_code_t err_code;
//app按键数组必须定义为static类型,因为按键处理模块需要保存指向该数组的指针
static app_button_cfg_t buttons[] =
{
{MY_BUTTON_1, APP_BUTTON_ACTIVE_LOW, NRF_GPIO_PIN_NOPULL, button_event_handler},
{CHG_IN_PIN_NO, APP_BUTTON_ACTIVE_LOW, NRF_GPIO_PIN_NOPULL, button_event_handler},//充电指示脚
{STB_IN_PIN_NO, APP_BUTTON_ACTIVE_LOW, NRF_GPIO_PIN_NOPULL, button_event_handler},//充满指示脚
};
//初始化APP按键
err_code = app_button_init(buttons, ARRAY_SIZE(buttons),
BUTTON_DETECTION_DELAY);
APP_ERROR_CHECK(err_code);
//使能APP按键
err_code = app_button_enable();
APP_ERROR_CHECK(err_code);
}