|
1 | 1 |
|
2 | 2 | # Simpletimer
|
| 3 | + |
3 | 4 | a timer based on micros that will make your coding a lot easier
|
4 | 5 |
|
5 |
| -timer() |
| 6 | +timer() |
6 | 7 | as simple as that you can perform action every amount of time you feed to function for example
|
7 | 8 |
|
8 |
| - Simpletimer timer1; |
| 9 | +``` C++ |
| 10 | + Simpletimer timer1{}; |
9 | 11 | timer1.timer(1000)
|
10 | 12 | {
|
11 | 13 | //entry every 1000ms
|
12 | 14 | }
|
13 |
| - i use it as a tool in my projects so i don't need to repeat myself creating timers |
14 |
| -from Version 2.0 up you can register callbacks now only 'void functions(void)' |
| 15 | +``` |
| 16 | +
|
| 17 | +from Version 2.0 up, you can register callbacks now only 'void functions(void)' |
15 | 18 |
|
16 | 19 | # callback example
|
17 |
| - #include "Simpletimer.h" |
18 |
| - // callback function cant take anything and return anything |
19 |
| - Simpletimer timer1; |
20 |
| - void callback1(){ |
21 |
| - Serial.println("entry every 1 sec"); |
22 |
| - } |
23 | 20 |
|
24 |
| - void setup(){ |
25 |
| - Serial.begin(9600); |
26 |
| - timer1.register_callback(callback1); |
27 |
| - } |
28 |
| - void loop(){ |
29 |
| - timer1.call_callback(1000); |
| 21 | +``` C++ |
| 22 | +
|
| 23 | +#include "Simpletimer.h" |
| 24 | +
|
| 25 | +Simpletimer timer1{}; |
| 26 | +
|
| 27 | +// callback function cant take anything and return anything |
| 28 | +void callback1() { |
| 29 | + Serial.println("entry every 1 sec"); |
| 30 | +} |
| 31 | +
|
| 32 | +void setup() { |
| 33 | + Serial.begin(9600); |
| 34 | + timer1.register_callback(callback1); |
| 35 | +} |
| 36 | +
|
| 37 | +void loop() { |
| 38 | + timer1.run(1000); |
| 39 | +} |
| 40 | + |
| 41 | +``` |
30 | 42 |
|
31 |
| - } |
32 | 43 | version 2.1
|
| 44 | + |
33 | 45 | # multiple callbacks example
|
34 |
| - #include "Simpletimer.h" |
35 |
| - // callback function cant take anything and return anything |
36 |
| - #define ledpin 8 |
37 |
| - |
38 |
| - Simpletimer multicb; |
39 |
| - |
40 |
| - void callback1(); |
41 |
| - void callback2(); |
42 |
| - void callback3(); |
43 |
| - void blink(); |
44 |
| - void callback1(){ |
45 |
| - Serial.println(F("entry every 1 sec")); |
46 |
| - } |
47 |
| - void callback2(){ |
48 |
| - Serial.println(F("entry every 0.5 sec")); |
49 |
| - } |
50 |
| - void callback3(){ |
51 |
| - Serial.println(F("entry every 0.2 sec")); |
52 |
| - } |
53 |
| - volatile bool state=false; |
54 |
| - void blink(){ |
55 |
| - state=!state; |
56 |
| - } |
57 |
| - void setup(){ |
58 |
| - pinMode(ledpin,OUTPUT); |
59 |
| - size_t number_of_callbacks=4; |
60 |
| - Simpletimer::callback all_callbacks[4]={callback1,callback2,callback3,blink}; |
61 |
| - unsigned long timers[4]={1000,500,200,199}; |
62 | 46 |
|
63 |
| - Serial.begin(115200); |
64 |
| - multicb.register_multiple_callbacks(all_callbacks,timers,number_of_callbacks); |
65 |
| - Serial.println(F("program started")); |
| 47 | +``` C++ |
66 | 48 |
|
67 |
| - } |
68 |
| - void loop(){ |
| 49 | +#include <Simpletimer.h> |
| 50 | +// callback function cant take anything and return anything |
| 51 | +#define ledpin LED_BUILTIN |
69 | 52 |
|
| 53 | +Simpletimer multicb{}; |
| 54 | +bool state = false; |
70 | 55 |
|
| 56 | +void callback1(); |
| 57 | +void callback2(); |
| 58 | +void callback3(); |
| 59 | +void blink(); |
71 | 60 |
|
72 |
| - multicb.run(); |
73 |
| - digitalWrite(ledpin,state); |
| 61 | +void callback1() { |
| 62 | + Serial.println(F("entry every 1 sec")); |
| 63 | +} |
74 | 64 |
|
| 65 | +void callback2() { |
| 66 | + Serial.println(F("entry every 0.5 sec")); |
| 67 | +} |
75 | 68 |
|
76 |
| - } |
77 |
| -## perform tasks only once in loop |
78 |
| - #include <Simpletimer.h> |
79 |
| - // callback function cant take anything and return anything |
80 |
| - |
81 |
| - Simpletimer::RunOnce Once; |
82 |
| - int i=1; |
83 |
| - void callback1(){ |
84 |
| - Serial.println(F("entry once")); |
85 |
| - i++; |
86 |
| - } |
| 69 | +void callback3() { |
| 70 | + Serial.println(F("entry every 0.2 sec")); |
| 71 | +} |
87 | 72 |
|
88 |
| - void callback2(){ |
89 |
| - Serial.println(F("other method")); |
90 |
| - i++; |
91 |
| - Serial.println("number of tasks performed : "+String(i)); |
92 |
| - } |
| 73 | +void blink() { |
| 74 | + state = !state; |
| 75 | +} |
93 | 76 |
|
94 |
| - void setup(){ |
| 77 | +static const unsigned int number_of_callbacks = 4; |
| 78 | +static Simpletimer::callback all_callbacks[number_of_callbacks] |
| 79 | +{ |
| 80 | + callback1, //-- 1 |
| 81 | + callback2, //-- 2 |
| 82 | + callback3, //-- 3 |
| 83 | + blink //-- 4 |
| 84 | +}; |
| 85 | +static unsigned long timers[number_of_callbacks] |
| 86 | +{ |
| 87 | + 1000, //callback1 -- 1 |
| 88 | + 500, //callback2 -- 2 |
| 89 | + 200, //callback3 -- 3 |
| 90 | + 2000 // blink -- 4 |
| 91 | +}; |
95 | 92 |
|
96 |
| - Serial.begin(115200); |
| 93 | +void setup() { |
| 94 | + pinMode(ledpin, OUTPUT); |
97 | 95 |
|
| 96 | + Serial.begin(115200); |
98 | 97 |
|
| 98 | + multicb.register_multiple_callbacks(all_callbacks, timers, number_of_callbacks); |
99 | 99 |
|
100 |
| - } |
101 |
| - void loop(){ |
102 |
| - //static Simpletimer::RunOnce DoSomething([&](){ here goes your code that need to run once}); |
103 |
| - // perform task once in loop |
104 |
| - static Simpletimer::RunOnce DoSomething; |
105 |
| - DoSomething.Run([&](){callback1(); Serial.println(F("you can place here anything "));}); |
106 |
| - // reset previous task in anotherone and then it can perform second time |
107 |
| - static Simpletimer::RunOnce reset_previous_task; |
108 |
| - reset_previous_task.Run([&](){DoSomething.Reset(); Serial.println("task 1 RESET being called now");}); |
109 |
| - //another way to perform task once you can always reset calling Once.Reset(); |
110 |
| - Once.Run(callback2); |
| 100 | + Serial.println(F("program started")); |
| 101 | +} |
| 102 | +void loop() { |
| 103 | + multicb.run(); |
| 104 | + digitalWrite(ledpin, state); |
| 105 | +} |
111 | 106 |
|
| 107 | +``` |
112 | 108 |
|
| 109 | +## perform tasks only once in loop |
113 | 110 |
|
| 111 | +``` C++ |
114 | 112 |
|
| 113 | +#include "Simpletimer.h" |
| 114 | +// callback function cant take anything and return anything |
115 | 115 |
|
| 116 | +Simpletimer::RunOnce Once{}; |
116 | 117 |
|
| 118 | +int i = 1; |
117 | 119 |
|
118 |
| - } |
| 120 | +void callback1() { |
| 121 | + Serial.println(F("entry once")); |
| 122 | + i++; |
| 123 | +} |
| 124 | + |
| 125 | +void callback2() { |
| 126 | + Serial.println(F("other method")); |
| 127 | + i++; |
| 128 | + Serial.println("number of tasks performed : " + String(i)); |
| 129 | +} |
| 130 | + |
| 131 | +void setup() { |
| 132 | + Serial.begin(115200); |
| 133 | +} |
| 134 | + |
| 135 | +void loop() { |
| 136 | + //static Simpletimer::RunOnce DoSomething([&](){ here goes your code that need to run once}); |
| 137 | + // perform task once in loop |
| 138 | + static Simpletimer::RunOnce DoSomething; |
| 139 | + DoSomething.Run([&]() {callback1(); Serial.println(F("you can place here anything "));}); |
| 140 | + // reset previous task in anotherone and then it can perform second time |
| 141 | + static Simpletimer::RunOnce reset_previous_task; |
| 142 | + reset_previous_task.Run([&]() {DoSomething.Reset(); Serial.println("task 1 RESET being called now");}); |
| 143 | + //another way to perform task once you can always reset calling Once.Reset(); |
| 144 | + Once.Run(callback2); |
| 145 | +} |
119 | 146 |
|
| 147 | +``` |
0 commit comments