From db9c0d679d64e774157d2b53a84e73e94d5ede04 Mon Sep 17 00:00:00 2001 From: kisslorand Date: Sat, 9 Sep 2023 12:17:09 +0300 Subject: [PATCH] Relax OS_Time IRQHandler --- TFT/src/User/API/Printing.c | 2 +- TFT/src/User/API/UI/TouchProcess.c | 209 ++--------------------------- TFT/src/User/API/UI/TouchProcess.h | 10 +- TFT/src/User/Configuration.h | 4 +- TFT/src/User/os_timer.c | 94 ++++--------- TFT/src/User/os_timer.h | 15 --- 6 files changed, 42 insertions(+), 292 deletions(-) diff --git a/TFT/src/User/API/Printing.c b/TFT/src/User/API/Printing.c index b4360cf7f4..ce09749316 100644 --- a/TFT/src/User/API/Printing.c +++ b/TFT/src/User/API/Printing.c @@ -132,7 +132,7 @@ uint32_t getPrintExpectedTime(void) return infoPrinting.expectedTime; } -void updatePrintTime(void) +void updatePrintTime(void) // Handle in interrupt { if (infoPrinting.printing && !infoPrinting.paused) { diff --git a/TFT/src/User/API/UI/TouchProcess.c b/TFT/src/User/API/UI/TouchProcess.c index 99127be033..0a6bf92baa 100644 --- a/TFT/src/User/API/UI/TouchProcess.c +++ b/TFT/src/User/API/UI/TouchProcess.c @@ -26,8 +26,10 @@ #define F TSC_Para[5] #define K TSC_Para[6] +#define TOUCH_DEBOUNCE_MS 20 // 20ms + int32_t TSC_Para[7]; -static volatile bool touchScreenIsPress = false; +static volatile uint8_t touchCountdown = TOUCH_DEBOUNCE_MS; bool touchSound = true; void TS_Get_Coordinates(uint16_t *x, uint16_t *y) @@ -142,30 +144,17 @@ uint16_t Key_value(uint8_t total_rect, const GUI_RECT *menuRect) return IDLE_TOUCH; } -void loopTouchScreen(void) // Handle in interrupt +void checkTouchScreen(void) // Handle in interrupt { - static uint8_t touch; - if (!XPT2046_Read_Pen()) - { - if (touch >= 20) // 20ms - { - touchScreenIsPress = true; - } - else - { - touch++; - } - } + if (XPT2046_Read_Pen() == LOW) + touchCountdown -= !!touchCountdown; else - { - touchScreenIsPress = false; - touch = 0; - } + touchCountdown = TOUCH_DEBOUNCE_MS; } -uint8_t isPress(void) +bool isPress(void) { - return touchScreenIsPress; + return (touchCountdown == 0); } void (*TSC_ReDrawIcon)(uint8_t position, uint8_t is_press) = NULL; @@ -177,7 +166,7 @@ uint16_t KEY_GetValue(uint8_t total_rect, const GUI_RECT* menuRect) uint16_t key_return = IDLE_TOUCH; - if (touchScreenIsPress) + if (isPress()) { if (firstPress) { @@ -207,181 +196,3 @@ uint16_t KEY_GetValue(uint8_t total_rect, const GUI_RECT* menuRect) } return key_return; } - -typedef enum -{ - NO_CLICK, - FIRST_CLICK, - FIRST_RELEASE, - SECOND_CLICK, - LONG_PRESS, -} KEY_STATUS; - -#define KEY_DOUOBLE_SPACE 15 //锟洁长时锟斤拷锟节碉拷锟斤拷锟斤拷锟斤拷卸锟轿?双锟斤拷 -#define KEY_LONG_PRESS_START 200 //锟斤拷锟斤拷锟斤拷锟斤拷每锟绞硷拷卸锟轿? 锟斤拷锟斤拷 锟斤拷值 - -#define KEY_LONG_PRESS_SPACE_MAX 10 //锟斤拷锟斤拷时 锟筋长锟斤拷梅锟斤拷锟揭伙拷渭锟街? -#define KEY_LONG_PRESS_SPACE_MIN 2 //����ʱ ��̶�÷���һ�μ�ֵ - -//uint16_t KEY_GetValue(uint8_t total_rect, const GUI_RECT *menuRect) -//{ -// uint16_t key_return = NO_TOUCH; - -// static uint16_t first_key = NO_TOUCH; -// static uint32_t first_time = 0; -// static uint8_t long_press_space = KEY_LONG_PRESS_SPACE_MAX; - -// static KEY_STATUS nowStatus = NO_CLICK; //������ǰ��״̬ - -// if (touchScreenIsPress) -// { -// switch (nowStatus) -// { -// case NO_CLICK: -// nowStatus = FIRST_CLICK; -// first_key = Key_value(total_rect, menuRect); -// first_time = OS_GetTime(); -// break; - -// case FIRST_CLICK: -// if (OS_GetTime() - first_time > KEY_LONG_PRESS_START) -// { -// nowStatus = LONG_PRESS; -// first_key |= KEY_LONG_CLICK; -// } -// break; - -// case FIRST_RELEASE: -// if (first_key == Key_value(total_rect, menuRect)) -// { -// nowStatus = SECOND_CLICK; -// first_key |= KEY_DOUBLE_CLICK; -// } -// else -// { -// nowStatus = NO_CLICK; -// } -// break; - -// case SECOND_CLICK: -// if (OS_GetTime() - first_time > KEY_LONG_PRESS_START) -// { -// nowStatus = LONG_PRESS; -// first_key |= KEY_LONG_CLICK; -// } -// break; - -// case LONG_PRESS: -// if (OS_GetTime() - first_time > long_press_space) -// { -// if (long_press_space > KEY_LONG_PRESS_SPACE_MIN) -// long_press_space--; -// first_time = OS_GetTime(); -// key_return = first_key; -// } -// break; - -// default: -// break; -// } -// } -// else -// { -// switch (nowStatus) -// { -// case FIRST_CLICK: -// nowStatus = FIRST_RELEASE; -// break; - -// case FIRST_RELEASE: -// if (OS_GetTime() - first_time > KEY_DOUOBLE_SPACE) -// { -// nowStatus = NO_CLICK; -// key_return = first_key; -// } -// break; - -// case SECOND_CLICK: -// nowStatus = NO_CLICK; -// key_return = first_key; -// break; - -// case LONG_PRESS: -// nowStatus = NO_CLICK; -// key_return = first_key | KEY_LONG_RELEASE; -// break; - -// default: -// break; -// } -// } -// return key_return; -//} - -uint16_t KNOB_GetRV(GUI_RECT *knob) -{ - uint16_t key_return = IDLE_TOUCH; - uint16_t x = 0, y = 0; - - static uint16_t oldx = 0, oldy = 0; - static uint32_t mytime; - - if (touchScreenIsPress && OS_GetTimeMs() > mytime) - { - mytime = OS_GetTimeMs() + 10; - TS_Get_Coordinates(&x, &y); - if (x > knob->x0 && x < knob->x1 && y > knob->y0 && y < knob->y1) - { - if (x > oldx + 5) - { - if (oldy > (knob->y0 + knob->y1) / 2) - { - key_return = KNOB_DEC; - } - else - { - key_return = KNOB_INC; - } - } - else if (x < oldx - 5) - { - if (oldy > (knob->y0 + knob->y1) / 2) - { - key_return = KNOB_INC; - } - else - { - key_return = KNOB_DEC; - } - } - if (y > oldy + 5) - { - if (x > (knob->x0 + knob->x1) / 2) - { - key_return = KNOB_INC; - } - else - { - key_return = KNOB_DEC; - } - } - else if (y < oldy - 5) - { - if (x > (knob->x0 + knob->x1) / 2) - { - key_return = KNOB_DEC; - } - else - { - key_return = KNOB_INC; - } - } - } - } - if (key_return != IDLE_TOUCH) - { - oldx = x; - oldy = y; - } - return key_return; -} diff --git a/TFT/src/User/API/UI/TouchProcess.h b/TFT/src/User/API/UI/TouchProcess.h index 3bdb6452ed..7ec41c31a4 100644 --- a/TFT/src/User/API/UI/TouchProcess.h +++ b/TFT/src/User/API/UI/TouchProcess.h @@ -25,20 +25,14 @@ enum KNOB_DEC, }; -#define KEY_CLICK 0x0000 // The key is defined as uint16_t , 16 (uint16_t 16bit)-3 (3 bits flag) = 13 bit, so uint16_t supports a maximum of 2 ^ 13 = 8192 key values -#define KEY_DOUBLE_CLICK 0x2000 // The third bit is used to identify the double-click action -#define KEY_LONG_RELEASE 0x4000 // The second bit is used to identify the release action after a long press -#define KEY_LONG_CLICK 0x8000 // The first bit is used to identify the long press action - extern bool touchSound; void TSC_Calibration(void); -uint8_t isPress(void); +bool isPress(void); uint16_t KEY_GetValue(uint8_t total_rect, const GUI_RECT *menuRect); uint16_t Key_value(uint8_t total_rect, const GUI_RECT *menuRect); -uint16_t KNOB_GetRV(GUI_RECT *knob); -void loopTouchScreen(void); +void checkTouchScreen(void); // WARNING, TIMER INTERRUPT ROUTINE CALLED ONCE A MILLISECOND extern void (*TSC_ReDrawIcon)(uint8_t position, uint8_t is_press); extern void TS_Get_Coordinates(uint16_t *x, uint16_t *y); diff --git a/TFT/src/User/Configuration.h b/TFT/src/User/Configuration.h index efc9266f7a..ba58123521 100644 --- a/TFT/src/User/Configuration.h +++ b/TFT/src/User/Configuration.h @@ -1117,7 +1117,7 @@ * Monitoring Debug * Uncomment/Enable to monitor/show system resources usage in Monitoring menu. */ -#define DEBUG_MONITORING // Default: commented (disabled) +//#define DEBUG_MONITORING // Default: commented (disabled) /** * Generic Debug @@ -1431,7 +1431,7 @@ * Uncomment to enable a progress bar with 10% markers. * Comment to enable a standard progress bar. */ -#define MARKED_PROGRESS_BAR // Default: commented (disabled) +//#define MARKED_PROGRESS_BAR // Default: commented (disabled) /** * Live Text Common Color Layout (Status Screen menu) diff --git a/TFT/src/User/os_timer.c b/TFT/src/User/os_timer.c index c09816a9e9..ab3867eacc 100644 --- a/TFT/src/User/os_timer.c +++ b/TFT/src/User/os_timer.c @@ -1,7 +1,10 @@ #include "os_timer.h" #include "includes.h" -volatile uint32_t os_counter = 0; +volatile struct +{ uint32_t milliseconds; + uint16_t sec_countdown; +} os_counter = {0, 1000}; void OS_TimerInitMs(void) { @@ -35,91 +38,48 @@ void OS_TimerInitMs(void) #ifdef GD32F2XX void TIMER6_IRQHandler(void) { - if ((TIMER_INTF(TIMER6) & 0x01) != 0) - { // update interrupt flag - TIMER_INTF(TIMER6) &= (uint16_t)~(1<<0); // clear interrupt flag - - os_counter++; + if ((TIMER_INTF(TIMER6) & TIMER_INTF_UPIF) != 0) + { + os_counter.milliseconds++; + os_counter.sec_countdown--; - if (os_counter % 1000 == 0) + if (os_counter.sec_countdown == 0) { - AVG_KPIS(); // debug monitoring KPI + os_counter.sec_countdown = 1000; // 1000 ms is 1 second + updatePrintTime(); // leave it here, do not move it out - updatePrintTime(); + AVG_KPIS(); // debug monitoring KPI } - loopTouchScreen(); + checkTouchScreen(); // leave it here, do not move it out + + TIMER_INTF(TIMER6) &= TIMER_INTF_UPIF; // clear interrupt flag } } #else void TIM7_IRQHandler(void) { - if ((TIM7->SR & 0x01) != 0) - { // update interrupt flag - TIM7->SR &= (uint16_t)~(1<<0); // clear interrupt flag - - os_counter++; + if ((TIM7->SR & TIM_SR_UIF) != 0) + { + os_counter.milliseconds++; + os_counter.sec_countdown--; - if (os_counter % 1000 == 0) + if (os_counter.sec_countdown == 0) { - AVG_KPIS(); // debug monitoring KPI + os_counter.sec_countdown = 1000; // 1000 ms is 1 second + updatePrintTime(); // leave it here, do not move it out - updatePrintTime(); + AVG_KPIS(); // debug monitoring KPI } - loopTouchScreen(); + checkTouchScreen(); // leave it here, do not move it out + + TIM7->SR &= ~TIM_SR_UIF; // clear interrupt flag } } #endif -// 1ms uint32_t OS_GetTimeMs(void) { - return os_counter; -} - -/* - * task: task structure to be filled - * time_ms: - */ -void OS_TaskInit(OS_TASK *task_t, uint32_t time_ms, FP_TASK function, void *para) -{ - task_t->time_ms = time_ms; - task_t->task = function; - task_t->para = para; -} - -void OS_TaskLoop(OS_TASK *task_t) -{ - if (task_t->is_exist == 0) - return; - - if (OS_GetTimeMs() < task_t->next_time) - return; - - if (task_t->is_repeat == 0) - { - task_t->is_exist = 0; - } - else - { - task_t->next_time = OS_GetTimeMs() + task_t->time_ms; - } - - (*task_t->task)(task_t->para); -} - -void OS_TaskEnable(OS_TASK *task_t, uint8_t is_exec, uint8_t is_repeat) -{ - task_t->is_exist =1; - task_t->is_repeat = is_repeat; - task_t->next_time = OS_GetTimeMs() + task_t->time_ms; - - if (is_exec) - (*task_t->task)(task_t->para); -} - -void OS_TaskDisable(OS_TASK *task_t) -{ - task_t->is_exist = 0; + return os_counter.milliseconds; } diff --git a/TFT/src/User/os_timer.h b/TFT/src/User/os_timer.h index 8d87dce2d0..b881cf0ae5 100644 --- a/TFT/src/User/os_timer.h +++ b/TFT/src/User/os_timer.h @@ -9,24 +9,9 @@ extern "C" { typedef void (*FP_TASK)(void *); -typedef struct -{ - uint32_t time_ms; - uint32_t next_time; - FP_TASK task; - void *para; - uint8_t is_exist; - uint8_t is_repeat; -} OS_TASK; - void OS_TimerInitMs(void); uint32_t OS_GetTimeMs(void); -void OS_TaskInit(OS_TASK *task, uint32_t time_ms, FP_TASK function, void *para); -void OS_TaskLoop(OS_TASK *task); -void OS_TaskEnable(OS_TASK *task, uint8_t is_exec, uint8_t is_repeat); -void OS_TaskDisable(OS_TASK *task); - #ifdef __cplusplus } #endif