yy1717
2022-12-08 f7a18ec4494b9c5c9ef3fd440bbf68ffc6425e18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Created by YY on 2021/3/16.
//
 
#ifndef NDKADV_APPTIMER_H
#define NDKADV_APPTIMER_H
 
#include <cstdint>
#include <functional>
#include <thread>
#include <chrono>
#include <atomic>
#include <mutex>
#include <condition_variable>
#include <string>
#include <ctime>
 
#define D_SEC(n)        ((n)*1000UL)
#define D_MIN(n)        ((n)*1000UL*60UL)
#define D_HOUR(n)        ((n)*1000UL*60UL*60UL)
 
typedef struct {
    int var1;
    int var2;
    void * var_ptr;
    int var_length;
} apptimer_var_t;
 
class CTimer {
public:
    CTimer();
    virtual ~CTimer();
    static bool _updateStatus(CTimer *timer);
    static void _do_task_func(uint32_t msec, CTimer *timer, std::function<void(apptimer_var_t)> callback);
    bool start(uint32_t msec, std::function<void(apptimer_var_t)> callback);
    void stop();
    void copy(int value1, int value2, const void *data, int length);
    static void clear(CTimer *timer);
private:
    std::mutex cv_mtx;
    std::condition_variable cv;
//    std::thread callbackThread;
    std::thread *pthread;
    apptimer_var_t var;
public:
    std::atomic_int flag;
};
 
void AppTimer_init(void);
 
void AppTimer_add(void(*cb)(apptimer_var_t), uint32_t msec);
 
void AppTimer_add(void(*cb)(apptimer_var_t), uint32_t msec, int value1, int value2 = 0);
 
void AppTimer_add(void(*cb)(apptimer_var_t), uint32_t msec, void *data, int lenght);
 
void AppTimer_delete(void (*cb) (apptimer_var_t));
 
uint32_t AppTimer_GetTickCount(void);
 
uint32_t AppTimer_GetGmtTickCount(void);
 
std::string FormatTime(const char *fmt);
 
std::time_t MktimeString(const char *str, const char *fmt);
 
time_t TimeStamp(int year, int mon, int day, int hour, int min, int sec);
 
#endif //NDKADV_APPTIMER_H