12 changed files with 968 additions and 735 deletions
-
5.clang-format
-
55APP/key.c
-
1APP/key.h
-
194APP/key2.c
-
69APP/key2.h
-
30APP/light.c
-
8APP/light.h
-
29APP/main.c
-
43APP/pwm.c
-
15APP/pwm.h
-
1014project_ozone/Listings/project_o.map
-
216project_ozone/project_o.uvgui.admin
@ -0,0 +1,5 @@ |
|||||
|
# Defines the Chromium style for automatic reformatting. |
||||
|
# http://clang.llvm.org/docs/ClangFormatStyleOptions.html |
||||
|
Language: Cpp |
||||
|
BasedOnStyle: Google |
||||
|
ColumnLimit: 120 |
@ -0,0 +1,194 @@ |
|||||
|
#include "key.h" |
||||
|
static zkey_t s_keys[] = { |
||||
|
ZKEY_INIT("timerkey", port_gpio_get_timer_key_state), |
||||
|
ZKEY_INIT("gearskey", port_gpio_get_gears_key_state), |
||||
|
ZKEY_INIT("intervalkey", port_gpio_get_interval_key_state), |
||||
|
ZKEY_INIT("switchkey", port_gpio_get_switch_key_state), |
||||
|
}; |
||||
|
zkey_module_t key_module = ZMODULE_INIT(s_keys, onkey); |
||||
|
|
||||
|
void zkey_init(zkey_module_t *module) { |
||||
|
/** |
||||
|
* @brief 按键初始化,三个状态的初始化,之前的,现在的,之后的 |
||||
|
* |
||||
|
*/ |
||||
|
s_module = module; |
||||
|
s_inited = true; //标志位说明按键初始化完成 |
||||
|
|
||||
|
for (int i = 0; i < s_module->nkey; i++) { |
||||
|
s_module->keys[i].cur_state = zks_keep; |
||||
|
s_module->keys[i].last_io_state = s_module->keys[i].get_key_state(); |
||||
|
s_module->keys[i].after_filter_state = s_module->keys[i].get_key_state(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void zkey_do_loop_in_each_period(void *_null) { /** |
||||
|
* @brief 循环查询按键的状态 |
||||
|
* |
||||
|
*/ |
||||
|
if (!s_inited) |
||||
|
return; |
||||
|
for (int i = 0; i < s_module->nkey; i++) //开始对每个按键进行查询 |
||||
|
{ |
||||
|
zkey_process_each(&s_module->keys[i]); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void zkey_process_each(zkey_t *each) { |
||||
|
/** |
||||
|
* @brief 按键是否发生变换,没变换的时候进行时间记录 |
||||
|
* |
||||
|
* @param each |
||||
|
*/ |
||||
|
each->keep_state_count++; |
||||
|
|
||||
|
bool now_io_state = each->get_key_state(); //获取现在的按键io状态 |
||||
|
if (each->currentstatekeep_count < 1000) { |
||||
|
each->currentstatekeep_count++; |
||||
|
} |
||||
|
if (now_io_state != each->last_real_state) //发生变换 |
||||
|
{ |
||||
|
each->currentstatekeep_count = 0; |
||||
|
} |
||||
|
if (each->currentstatekeep_count >= 1) //按键状态保持时间大于20ms,更新按键状态 |
||||
|
{ |
||||
|
each->after_filter_state = now_io_state; |
||||
|
} |
||||
|
each->last_real_state = now_io_state; |
||||
|
zkey_process_each_after_filter(each, each->after_filter_state); |
||||
|
} |
||||
|
|
||||
|
void zkey_process_each_after_filter(zkey_t *each, bool now_io_state) { |
||||
|
if (now_io_state != each->last_io_state) { |
||||
|
if (now_io_state) { |
||||
|
each->keep_state_count = 0; |
||||
|
each->hasProcessed = false; |
||||
|
each->cur_state = zks_rising_edge; |
||||
|
s_module->onkey(each, each->cur_state); |
||||
|
} else { |
||||
|
each->cur_state = zks_falling_edge; |
||||
|
s_module->onkey(each, each->cur_state); |
||||
|
each->keep_state_count = 0; |
||||
|
} |
||||
|
each->last_io_state = now_io_state; |
||||
|
} else { |
||||
|
each->cur_state = zks_keep; |
||||
|
if (now_io_state) { |
||||
|
s_module->onkey(each, each->cur_state); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
//############################### 原本在main中的方法 以下都是 |
||||
|
//###############################// |
||||
|
extern uint32_t g_frequency; |
||||
|
extern uint32_t g_duty[4]; |
||||
|
extern int g_ozen_gears; |
||||
|
extern uint32_t g_count_down_begin_sys_time; |
||||
|
extern int time_key_press_frequency; //每按下4次进行清0 |
||||
|
extern uint32_t g_ozone_work_time; |
||||
|
//##############新的############ |
||||
|
extern pwm_message_t pwm_message; |
||||
|
extern rgb_message_t rgb_message; |
||||
|
shutdown_before_message_t shutdown_before_message; |
||||
|
void process_switchkey(void) { |
||||
|
static uint8_t switchkey_press_conut = 0; |
||||
|
switchkey_press_conut++; |
||||
|
switch (switchkey_press_conut) { |
||||
|
case 1: //开机后工作模式为关机前的工作模式,无定时无间歇 |
||||
|
pwm_module_set_pwm_output_2( |
||||
|
shutdown_before_message.hardware_frequency, shutdown_before_message.hardware_duty, |
||||
|
shutdown_before_message.large_period, shutdown_before_message.large_duty); |
||||
|
light_module_set_rgb_mode(rgb_message.turn_off_the_mode_before_rgb); |
||||
|
port_fan_set(true); |
||||
|
break; |
||||
|
case 2: //关机 |
||||
|
switchkey_press_conut = 0; |
||||
|
shutdown_before_message.hardware_duty=pwm_message.s_hardware_duty; |
||||
|
shutdown_before_message.hardware_frequency=pwm_message.s_hardware_frequency; |
||||
|
shutdown_before_message.large_duty=pwm_message.s_large_duty; |
||||
|
shutdown_before_message.large_period=pwm_message.s_large_period; |
||||
|
light_module_set_rgb_mode(RGB_CLOSE); |
||||
|
light_module_set_timing_light_mode(CLOSE_ALL_LED); |
||||
|
pwm_module_set_pwm_output_2(1,100,0,0); |
||||
|
port_fan_set(false); |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void onkey(zkey_t *key, zkey_state_t key_state) { |
||||
|
/** |
||||
|
* @brief 判断每个按键触发的状态 |
||||
|
* |
||||
|
* @param key |
||||
|
* @param key_state |
||||
|
*/ |
||||
|
if (get_ozone_starting_up_state()) { //开机中 |
||||
|
if (key == &s_keys[0] && //定时按键 |
||||
|
key->cur_state == zks_keep && //长按 |
||||
|
!key->hasProcessed && //没有被处理过 |
||||
|
key->keep_state_count >= |
||||
|
POWER_KEY_TRIGGER_TIME / KEY_SCAN_PERIOD) //按下持续时间大于3s |
||||
|
{ |
||||
|
key->hasProcessed = true; |
||||
|
// printf("key0 zks_keep\r\n"); |
||||
|
// stop_ozone_work(); |
||||
|
} else if (key == &s_keys[0] && //定时按键 |
||||
|
key->cur_state == zks_falling_edge && //下降沿触发 |
||||
|
!key->hasProcessed && |
||||
|
key->keep_state_count <= |
||||
|
POWER_KEY_TRIGGER_TIME / KEY_SCAN_PERIOD) //小于3s |
||||
|
{ |
||||
|
key->hasProcessed = true; |
||||
|
printf("key0 zks_falling_edge\r\n"); |
||||
|
// set_ozone_work_time(); |
||||
|
// set_timing_time(running_time.timing_time_state); |
||||
|
} else if (key == &s_keys[1] && //高低档位选择 |
||||
|
key->cur_state == zks_rising_edge && // |
||||
|
!key->hasProcessed && // |
||||
|
key->keep_state_count <= |
||||
|
POWER_KEY_TRIGGER_TIME / KEY_SCAN_PERIOD) { |
||||
|
key->hasProcessed = true; |
||||
|
// key_control_switch_gears(g_switch_gears); |
||||
|
printf("key1 zks_rising_edge\r\n"); |
||||
|
// set_ozen_gears(g_ozen_gears); |
||||
|
} else if (key == &s_keys[2] && //间歇时间选择 |
||||
|
key->cur_state == zks_rising_edge && // |
||||
|
!key->hasProcessed && // |
||||
|
key->keep_state_count <= |
||||
|
POWER_KEY_TRIGGER_TIME / KEY_SCAN_PERIOD) { |
||||
|
key->hasProcessed = true; |
||||
|
printf("key2 zks_rising_edge\r\n"); |
||||
|
// set_interval_time(); |
||||
|
// set_interval_time(running_time.interval_time_state); |
||||
|
} |
||||
|
// else if (key == &s_keys[3] && //开关 |
||||
|
// key->cur_state == zks_rising_edge && // |
||||
|
// !key->hasProcessed && // |
||||
|
// key->keep_state_count <= |
||||
|
// POWER_KEY_TRIGGER_TIME / KEY_SCAN_PERIOD) { |
||||
|
// key->hasProcessed = true; |
||||
|
// printf("key3 zks_rising_edge\r\n"); |
||||
|
// process_switch_key(); |
||||
|
// // set_interval_time(running_time.interval_time_state); |
||||
|
// } |
||||
|
} |
||||
|
// else { |
||||
|
if (key == &s_keys[3] && //开关 |
||||
|
key->cur_state == zks_rising_edge && // |
||||
|
!key->hasProcessed && // |
||||
|
key->keep_state_count <= POWER_KEY_TRIGGER_TIME / KEY_SCAN_PERIOD) { |
||||
|
key->hasProcessed = true; |
||||
|
printf("key zks_rising_edge\r\n"); |
||||
|
// process_switch_key(); |
||||
|
process_switchkey(); |
||||
|
// set_interval_time(running_time.interval_time_state); |
||||
|
} |
||||
|
// } |
||||
|
} |
||||
|
void port_key_state(void) { |
||||
|
static uint32_t keylastprocess = 0; |
||||
|
if (port_haspassedms(keylastprocess) > 20) { |
||||
|
keylastprocess = get_sys_ticket(); |
||||
|
zkey_do_loop_in_each_period(NULL); |
||||
|
} |
||||
|
} |
@ -0,0 +1,69 @@ |
|||||
|
#pragma once |
||||
|
#include "port.h" |
||||
|
#include "ozone_work.h" |
||||
|
#include "pwm.h" |
||||
|
#define POWER_KEY_TRIGGER_TIME 3000 |
||||
|
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) |
||||
|
#define ZKEY_INIT(_name, _get_key_state) \ |
||||
|
{ .name = _name, .get_key_state = _get_key_state } |
||||
|
#define ZMODULE_INIT(_keys, _onkey) /**/ \ |
||||
|
{ /**/ \ |
||||
|
.keys = _keys, /**/ \ |
||||
|
.nkey = ARRAY_SIZE(_keys), /**/ \ |
||||
|
.onkey = _onkey /**/ \ |
||||
|
} |
||||
|
|
||||
|
typedef enum { |
||||
|
// mode1 |
||||
|
zks_keep, |
||||
|
zks_rising_edge, //上升沿按键按下从0-1 |
||||
|
zks_falling_edge, //下降沿按键抬起1-0 |
||||
|
|
||||
|
// mode2 |
||||
|
zks_trigger_event, //触发 |
||||
|
zks_longtime_trigger_event, //长时间触发 |
||||
|
} zkey_event_t; |
||||
|
typedef zkey_event_t zkey_state_t; |
||||
|
|
||||
|
typedef bool (*get_key_state_t)(void); |
||||
|
|
||||
|
typedef struct { |
||||
|
const char *name; |
||||
|
get_key_state_t get_key_state; |
||||
|
bool last_io_state; |
||||
|
|
||||
|
zkey_state_t cur_state; //当前的状态 |
||||
|
uint32_t keep_state_count; /*按键当前状态保持了多久*/ |
||||
|
|
||||
|
bool hasProcessed; /*useful for user*/ |
||||
|
bool after_filter_state; |
||||
|
|
||||
|
uint32_t currentstatekeep_count; //消抖使用 |
||||
|
bool last_real_state; //消抖使用 |
||||
|
|
||||
|
} zkey_t; |
||||
|
|
||||
|
static uint16_t s_key_press_state; |
||||
|
static uint16_t s_key_long_press_time_ms = 3000; //长按判断时间 |
||||
|
#define KEY_SCAN_PERIOD 20 //扫描周期20ms |
||||
|
|
||||
|
typedef struct { |
||||
|
zkey_t *keys; |
||||
|
int nkey; |
||||
|
void (*onkey)(zkey_t *key, zkey_state_t key_state); |
||||
|
} zkey_module_t; |
||||
|
|
||||
|
static zkey_module_t *s_module; |
||||
|
static bool s_inited; |
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
|
||||
|
void zkey_init(zkey_module_t *module); |
||||
|
void zkey_do_loop_in_each_period(void *_null); |
||||
|
void zkey_process_each(zkey_t *each); |
||||
|
void zkey_process_each_after_filter(zkey_t *each, bool now_io_state); |
||||
|
void read_key_state(void); |
||||
|
void onkey(zkey_t *key, zkey_state_t key_state); |
||||
|
void port_key_state(void); |
1014
project_ozone/Listings/project_o.map
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
216
project_ozone/project_o.uvgui.admin
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
Write
Preview
Loading…
Cancel
Save
Reference in new issue