Git Product home page Git Product logo

multitimer's Introduction

MultiTimer

Introduction

MultiTimer is a software timer extension module that allows for the expansion of timer tasks as needed, replacing the traditional flag-checking method. It offers a more elegant and convenient way to manage the timing sequences of a program.

How to Use

  1. Configure the system time base interface and install the timer driver:
uint64_t PlatformTicksGetFunc(void)
{
    /* Platform-specific implementation */
}

multiTimerInstall(PlatformTicksGetFunc);
  1. Instantiate a timer object:
MultiTimer timer1;
  1. Set the timing, timeout callback function, user data pointer, and start the timer:
int multiTimerStart(MultiTimer* timer, uint64_t timing, MultiTimerCallback_t callback, void* userData);
  1. Call the timer background processing function in the main loop:
int main(int argc, char *argv[])
{
    ...
    while (1) {
        ...
        multiTimerYield();
    }
}

Limitations

  1. The clock frequency of the timer directly affects its accuracy. It is recommended to use ticks with higher precision such as 1ms, 5ms, or 10ms.

  2. The callback function of the timer should not perform time-consuming operations, as this may occupy too much time, causing other timers to not timeout properly.

  3. Since the timer's callback function is executed within multiTimerYield, care should be taken not to use too much stack space to avoid stack overflow.

Example

The test_linux.c file serves as a demo for Linux platforms, showcasing how to use MultiTimer for creating and managing multiple timers with different intervals and behaviors.

#include "MultiTimer.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>

// Platform-specific function to get current ticks (milliseconds)
uint64_t getPlatformTicks() {
    struct timeval now;
    gettimeofday(&now, NULL);
    return now.tv_sec * 1000LL + now.tv_usec / 1000;
}

// Callback functions for the timers
void timerCallback1(MultiTimer* timer, void* userData) {
    printf("Timer 1 fired at %lu ms\n", getPlatformTicks());
    multiTimerStart(timer, 500, timerCallback1, NULL); // Restart timer
}

void timerCallback2(MultiTimer* timer, void* userData) {
    printf("Timer 2 fired at %lu ms\n", getPlatformTicks());
    multiTimerStart(timer, 1000, timerCallback2, NULL); // Restart timer
}

void timerCallback3(MultiTimer* timer, void* userData) {
    printf("Timer 3 (one-shot) fired at %lu ms\n", getPlatformTicks());
}

void timerCallback4(MultiTimer* timer, void* userData) {
    printf("Timer 4 is stopping Timer 1 at %lu ms\n", getPlatformTicks());
    multiTimerStop((MultiTimer*)userData);
}

int main() {
    multiTimerInstall(getPlatformTicks);

    MultiTimer timer1, timer2, timer3, timer4;

    multiTimerStart(&timer1, 500, timerCallback1, NULL); // 500 ms repeating
    multiTimerStart(&timer2, 1000, timerCallback2, NULL); // 1000 ms repeating
    multiTimerStart(&timer3, 2000, timerCallback3, NULL); // 2000 ms one-shot
    multiTimerStart(&timer4, 3000, timerCallback4, &timer1); // 3000 ms, stops timer1

    // Main loop to simulate time passage and process timers
    while (1) {
        multiTimerYield();
        usleep(1000); // Sleep for 1 ms
    }

    return 0;
}

multitimer's People

Contributors

0x1abin avatar crazyokd avatar kdurant avatar mculover666 avatar recan-li avatar recanlirtm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

multitimer's Issues

PlatformTicksGetFunc() 系统时间溢出怎么处理

用的8位机不支持uint64_t,只支持uint32_t,用的ms定时器累加计时tick
uint32_t tick;
void Timer0_IRQHandler(void) interrupt TMR0_VECTOR
{
tick++;
TH0 =(65536-4000)>>8 ;
TL0 = 65536-4000;
}
请问tick溢出时,PlatformTicksGetFunc中需要做何种处理,
还是说我只需要将tick置0

超时溢出有问题

99
_timer_ticks 是一个无符号数, 假如是一个无符号8位,当_timer_ticks为254,我超时为2m, target->timeout此时为0吧,这时候判断感觉出现问题了,会立马导致超时事件发生

提点需求,希望能完善该项目

  1. 支持其他线程在我这个定时器里创建任务
  2. 支持定时任务的循环执行,每隔一个timeout就执行一次
  3. 支持在定时器任务回调里创建新的定时器任务

如果作者允许的话,我可以为该工程实现以上需求,谢谢!

为什么重复执行的定时器不在MultiTimerYield执行重新启动,在回调函数容易漏掉

为什么重复执行的定时器不在MultiTimerYield执行重新启动,在回调函数容易漏掉
增加排序功能很好,但是去掉了重复执行功能啊,单片机又很多都是要重复执行的定时器
增加可重复执行功能
struct MultiTimerHandle {
MultiTimer* next;
uint64_t deadline;
MultiTimerCallback_t callback;
void* userData;
uint64_t interval; //记录间隔
uint8_t repeat; //重复执行标记
};
//根据repeat标志重新启动定时器
if (entry->repeat) {
MultiTimerStart(entry, entry->interval, entry->callback, entry->userData, entry->repeat)
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.