Skip to content

Commit

Permalink
Relax OS_Time IRQHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
kisslorand committed Sep 26, 2023
1 parent 7d638de commit cdc59b5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 288 deletions.
2 changes: 1 addition & 1 deletion TFT/src/User/API/Printing.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
209 changes: 10 additions & 199 deletions TFT/src/User/API/UI/TouchProcess.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
}
10 changes: 2 additions & 8 deletions TFT/src/User/API/UI/TouchProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
92 changes: 27 additions & 65 deletions TFT/src/User/os_timer.c
Original file line number Diff line number Diff line change
@@ -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)
{
Expand Down Expand Up @@ -35,89 +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)
{
updatePrintTime();
os_counter.sec_countdown = 1000;
updatePrintTime(); // leave it here, do not move it out

AVG_SCAN_RATE(); // 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)
{
updatePrintTime();
os_counter.sec_countdown = 1000;
updatePrintTime(); // leave it here, do not move it out

AVG_SCAN_RATE(); // 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;
}
Loading

0 comments on commit cdc59b5

Please sign in to comment.