-
Notifications
You must be signed in to change notification settings - Fork 196
/
Thread.h
89 lines (64 loc) · 1.92 KB
/
Thread.h
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
Thread.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;
For instructions, go to https://github.com/ivanseidel/ArduinoThread
Created by Ivan Seidel Gomes, March, 2013.
Released into the public domain.
*/
#ifndef Thread_h
#define Thread_h
#if defined(ARDUINO) && ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include <inttypes.h>
/*
Uncomment this line to enable ThreadName Strings.
It might be usefull if you are loging thread with Serial,
or displaying a list of threads...
*/
// #define USE_THREAD_NAMES 1
class Thread{
protected:
// Desired interval between runs
unsigned long interval;
// Last runned time in Ms
unsigned long last_run;
// Scheduled run in Ms (MUST BE CACHED)
unsigned long _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
*/
void runned(unsigned long time);
// Default is to mark it runned "now"
void runned() { runned(millis()); }
// Callback for run() if not implemented
void (*_onRun)(void);
public:
// If the current Thread is enabled or not
bool enabled;
// ID of the Thread (initialized from memory adr.)
int ThreadID;
#ifdef USE_THREAD_NAMES
// Thread Name (used for better UI).
String ThreadName;
#endif
Thread(void (*callback)(void) = NULL, unsigned long _interval = 0);
// Set the desired interval for calls, and update _cached_next_run
virtual void setInterval(unsigned long _interval);
// Return if the Thread should be runned or not
virtual bool shouldRun(unsigned long time);
// Default is to check whether it should run "now"
bool shouldRun() { return shouldRun(millis()); }
// Callback set
void onRun(void (*callback)(void));
// Runs Thread
virtual void run();
};
#endif