forked from ivanseidel/ArduinoThread
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadLite.h
More file actions
64 lines (47 loc) · 1.55 KB
/
ThreadLite.h
File metadata and controls
64 lines (47 loc) · 1.55 KB
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
/*
ThreadLite.h - An runnable object
Thread is responsable for holding the "action" for something,
also, it responds if it "should" or "should not" run, based on
the current time;
ThreadLite is based on the class Thread, but takes less memory
and maximum period is only 32,767 seconds.
WARNING! It is not recommended to use if you do not have a memory deficit.
Copyright © 2013 Ivan Seidel Gomes.
Copyright © 2018 Stanislav Hnatiuk.
Released into the public domain.
*/
#ifndef THREADLITE_H
#define THREADLITE_H
#if defined(ARDUINO) && ARDUINO >= 100
#include <Arduino.h>
#endif
#define TIME_INT uint16_t
#define TIME_OVERFLOW 0x8000 // uint16_t = 0x8000 or uint32_t = 0x80000000
class ThreadLite {
protected:
// Desired interval between runs
TIME_INT interval;
// Last runned time in Ms
TIME_INT last_run;
// Scheduled run in Ms (MUST BE CACHED)
TIME_INT _cached_next_run;
/*
IMPORTANT! Run after all calls to run()
Updates last_run and cache next run.
NOTE: This MUST be called if extending
this class and implementing run() method
*/
// Default is to mark it runned "now"
void runned();
// Callback for run() if not implemented
void (*_onRun)(void);
public:
ThreadLite(void (*callback)(void), TIME_INT _interval);
// Set the desired interval for calls, and update _cached_next_run
void setInterval(TIME_INT _interval);
// Default is to check whether it should run "now"
bool shouldRun();
// Runs ThreadLite
void run();
};
#endif // THREADLITE_H