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 10, 2023
1 parent cd69550 commit 537ccee
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 288 deletions.
13 changes: 5 additions & 8 deletions TFT/src/User/API/Printing.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,14 @@ uint32_t getPrintExpectedTime(void)
return infoPrinting.expectedTime;
}

void updatePrintTime(uint32_t osTime)
void updatePrintTime(void) // Handle in interrupt
{
if (osTime % 1000 == 0)
if (infoPrinting.printing && !infoPrinting.paused)
{
if (infoPrinting.printing && !infoPrinting.paused)
{
infoPrinting.elapsedTime++;
infoPrinting.elapsedTime++;

if (infoPrinting.remainingTime > 0 && !heatHasWaiting())
infoPrinting.remainingTime--;
}
if (!heatHasWaiting() && infoPrinting.remainingTime > 0)
infoPrinting.remainingTime--;
}
}

Expand Down
2 changes: 1 addition & 1 deletion TFT/src/User/API/Printing.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void resumeAndContinue(void);
void setPrintExpectedTime(uint32_t expectedTime);
uint32_t getPrintExpectedTime(void);

void updatePrintTime(uint32_t osTime);
void updatePrintTime(void);
uint32_t getPrintTime(void);

void setPrintRemainingTime(int32_t remainingTime); // used for M73 Rxx and M117 Time Left xx
Expand Down
218 changes: 24 additions & 194 deletions TFT/src/User/API/UI/TouchProcess.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#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;
bool touchSound = true;
Expand Down Expand Up @@ -145,22 +147,28 @@ uint16_t Key_value(uint8_t total_rect, const GUI_RECT *menuRect)
void loopTouchScreen(void) // Handle in interrupt
{
static uint8_t touch;
if (!XPT2046_Read_Pen())
{
if (touch >= 20) // 20ms
{
touchScreenIsPress = true;
}
else
{
touch++;
}
}
else
{
touchScreenIsPress = false;
touch = 0;
}

// left here for better understanding the code that replaces it

// if (!XPT2046_Read_Pen())
// {
// if (touch >= TOUCH_DEBOUNCE_MS)
// {
// touchScreenIsPress = true;
// }
// else
// {
// touch++;
// }
// }
// else
// {
// touchScreenIsPress = false;
// touch = 0;
// }

touch = !XPT2046_Read_Pen() * (touch + !touchScreenIsPress);
touchScreenIsPress = !(TOUCH_DEBOUNCE_MS - touch);
}

uint8_t isPress(void)
Expand Down Expand Up @@ -207,181 +215,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;
}
97 changes: 27 additions & 70 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 one_sec_millis;
} os_counter = {0, 0};

void OS_TimerInitMs(void)
{
Expand Down Expand Up @@ -35,91 +38,45 @@ 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++;

updatePrintTime(os_counter);

loopTouchScreen();
if ((TIMER_INTF(TIMER6) & TIMER_INTF_UPIF) != 0)
{
os_counter.milliseconds++;
os_counter.one_sec_millis++;

if (os_counter == (uint32_t)(~0))
if (os_counter.one_sec_millis == 1000)
{
os_counter = 0;
os_counter.one_sec_millis = 0;
updatePrintTime(); // leave it here, do not move it out
}

loopTouchScreen(); // 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++;

updatePrintTime(os_counter);

loopTouchScreen();
if ((TIM7->SR & TIM_SR_UIF) != 0)
{
os_counter.milliseconds++;
os_counter.one_sec_millis++;

if (os_counter == (uint32_t)(~0))
if (os_counter.one_sec_millis == 1000)
{
os_counter = 0;
os_counter.one_sec_millis = 0;
updatePrintTime(); // leave it here, do not move it out
}

loopTouchScreen(); // 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, uint32_t time_ms, FP_TASK function, void *para)
{
task->time_ms = time_ms;
task->task = function;
task->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 537ccee

Please sign in to comment.